Skip to content

Commit 929b460

Browse files
committed
wip - relations should use constructor arguments
1 parent e90e624 commit 929b460

File tree

4 files changed

+103
-87
lines changed

4 files changed

+103
-87
lines changed

src/Doctrine/BaseRelation.php

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,30 @@
1616
*/
1717
abstract class BaseRelation
1818
{
19-
private $propertyName;
20-
private $targetClassName;
21-
private $targetPropertyName;
2219
private $customReturnType;
23-
private $isSelfReferencing = false;
24-
private $mapInverseRelation = true;
25-
private $avoidSetter = false;
20+
21+
public function __construct(
22+
private ?string $propertyName = null,
23+
private ?string $targetClassName = null,
24+
private ?string $targetPropertyName = null,
25+
private bool $isSelfReferencing = false,
26+
private bool $mapInverseRelation = true,
27+
private bool $avoidSetter = false,
28+
private bool $isCustomReturnTypeNullable = false,
29+
) {
30+
// @TODO - triggers are tmp, do not merge w/ them in place.
31+
if (null === $this->propertyName) {
32+
trigger_deprecation('symfony/maker-bundle', '0.0.0', 'No null propertyName');
33+
}
34+
35+
if (null === $this->targetClassName) {
36+
trigger_deprecation('symfony/maker-bundle', '0.0.0', 'No null targetClassName');
37+
}
38+
39+
if (null === $this->targetPropertyName) {
40+
trigger_deprecation('symfony/maker-bundle', '0.0.0', 'No null targetClassName');
41+
}
42+
}
2643

2744
abstract public function isOwning(): bool;
2845

@@ -33,6 +50,7 @@ public function getPropertyName(): string
3350

3451
public function setPropertyName(string $propertyName): self
3552
{
53+
trigger_deprecation('symfony/maker-bundle', '0.0.0', 'Set propertyname via constructor');
3654
$this->propertyName = $propertyName;
3755

3856
return $this;
@@ -45,6 +63,7 @@ public function getTargetClassName(): string
4563

4664
public function setTargetClassName(string $targetClassName): self
4765
{
66+
trigger_deprecation('symfony/maker-bundle', '0.0.0', 'Set targetname via constructor');
4867
$this->targetClassName = $targetClassName;
4968

5069
return $this;
@@ -57,6 +76,7 @@ public function getTargetPropertyName(): ?string
5776

5877
public function setTargetPropertyName(?string $targetPropertyName): self
5978
{
79+
trigger_deprecation('symfony/maker-bundle', '0.0.0', 'Set targetPropertyName via constructor');
6080
$this->targetPropertyName = $targetPropertyName;
6181

6282
return $this;
@@ -67,13 +87,6 @@ public function isSelfReferencing(): bool
6787
return $this->isSelfReferencing;
6888
}
6989

70-
public function setIsSelfReferencing(bool $isSelfReferencing): self
71-
{
72-
$this->isSelfReferencing = $isSelfReferencing;
73-
74-
return $this;
75-
}
76-
7790
public function getMapInverseRelation(): bool
7891
{
7992
return $this->mapInverseRelation;
@@ -91,13 +104,6 @@ public function shouldAvoidSetter(): bool
91104
return $this->avoidSetter;
92105
}
93106

94-
public function avoidSetter(bool $avoidSetter = true): self
95-
{
96-
$this->avoidSetter = $avoidSetter;
97-
98-
return $this;
99-
}
100-
101107
public function getCustomReturnType(): ?string
102108
{
103109
return $this->customReturnType;
@@ -108,10 +114,9 @@ public function isCustomReturnTypeNullable(): bool
108114
return $this->isCustomReturnTypeNullable;
109115
}
110116

111-
public function setCustomReturnType(string $customReturnType, bool $isNullable)
117+
public function setCustomReturnType(string $customReturnType): self
112118
{
113119
$this->customReturnType = $customReturnType;
114-
$this->isCustomReturnTypeNullable = $isNullable;
115120

116121
return $this;
117122
}

src/Doctrine/EntityRegenerator.php

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -122,47 +122,51 @@ public function regenerateEntities(string $classOrNamespace): void
122122

123123
switch ($mapping['type']) {
124124
case ClassMetadata::MANY_TO_ONE:
125-
$relation = (new RelationManyToOne())
126-
->setPropertyName($mapping['fieldName'])
125+
$relation = (new RelationManyToOne(
126+
propertyName: $mapping['fieldName'],
127+
targetClassName: $mapping['targetEntity'],
128+
targetPropertyName:$mapping['inversedBy'],
129+
mapInverseRelation: null !== $mapping['inversedBy'],
130+
))
127131
->setIsNullable($getIsNullable($mapping))
128-
->setTargetClassName($mapping['targetEntity'])
129-
->setTargetPropertyName($mapping['inversedBy'])
130-
->setMapInverseRelation(null !== $mapping['inversedBy'])
131132
;
132133

133134
$manipulator->addManyToOneRelation($relation);
134135

135136
break;
136137
case ClassMetadata::ONE_TO_MANY:
137-
$relation = (new RelationOneToMany())
138-
->setPropertyName($mapping['fieldName'])
139-
->setTargetClassName($mapping['targetEntity'])
140-
->setTargetPropertyName($mapping['mappedBy'])
138+
$relation = (new RelationOneToMany(
139+
propertyName: $mapping['fieldName'],
140+
targetClassName: $mapping['targetEntity'],
141+
targetPropertyName:$mapping['mappedBy'],
142+
))
141143
->setOrphanRemoval($mapping['orphanRemoval'])
142144
;
143145

144146
$manipulator->addOneToManyRelation($relation);
145147

146148
break;
147149
case ClassMetadata::MANY_TO_MANY:
148-
$relation = (new RelationManyToMany())
149-
->setPropertyName($mapping['fieldName'])
150-
->setTargetClassName($mapping['targetEntity'])
151-
->setTargetPropertyName($mapping['mappedBy'])
150+
$relation = (new RelationManyToMany(
151+
propertyName: $mapping['fieldName'],
152+
targetClassName: $mapping['targetEntity'],
153+
targetPropertyName: $mapping['mappedBy'],
154+
mapInverseRelation: $mapping['isOwningSide'] ? (null !== $mapping['inversedBy']) : true,
155+
))
152156
->setIsOwning($mapping['isOwningSide'])
153-
->setMapInverseRelation($mapping['isOwningSide'] ? (null !== $mapping['inversedBy']) : true)
154157
;
155158

156159
$manipulator->addManyToManyRelation($relation);
157160

158161
break;
159162
case ClassMetadata::ONE_TO_ONE:
160-
$relation = (new RelationOneToOne())
161-
->setPropertyName($mapping['fieldName'])
162-
->setTargetClassName($mapping['targetEntity'])
163-
->setTargetPropertyName($mapping['isOwningSide'] ? $mapping['inversedBy'] : $mapping['mappedBy'])
163+
$relation = (new RelationOneToOne(
164+
propertyName: $mapping['fieldName'],
165+
targetClassName: $mapping['targetEntity'],
166+
targetPropertyName: $mapping['isOwningSide'] ? $mapping['inversedBy'] : $mapping['mappedBy'],
167+
mapInverseRelation: $mapping['isOwningSide'] ? (null !== $mapping['inversedBy']) : true,
168+
))
164169
->setIsOwning($mapping['isOwningSide'])
165-
->setMapInverseRelation($mapping['isOwningSide'] ? (null !== $mapping['inversedBy']) : true)
166170
->setIsNullable($getIsNullable($mapping))
167171
;
168172

src/Doctrine/EntityRelation.php

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -81,55 +81,60 @@ public static function getValidRelationTypes(): array
8181
public function getOwningRelation(): RelationManyToMany|RelationOneToOne|RelationManyToOne
8282
{
8383
return match ($this->getType()) {
84-
self::MANY_TO_ONE => (new RelationManyToOne())
85-
->setPropertyName($this->owningProperty)
86-
->setTargetClassName($this->inverseClass)
87-
->setTargetPropertyName($this->inverseProperty)
84+
self::MANY_TO_ONE => (new RelationManyToOne(
85+
propertyName: $this->owningProperty,
86+
targetClassName: $this->inverseClass,
87+
targetPropertyName: $this->inverseProperty,
88+
isSelfReferencing: $this->isSelfReferencing,
89+
mapInverseRelation: $this->mapInverseRelation,
90+
))
91+
->setIsNullable($this->isNullable),
92+
self::MANY_TO_MANY => (new RelationManyToMany(
93+
propertyName: $this->owningProperty,
94+
targetClassName: $this->inverseClass,
95+
targetPropertyName: $this->inverseProperty,
96+
isSelfReferencing: $this->isSelfReferencing,
97+
mapInverseRelation: $this->mapInverseRelation,
98+
))
99+
->setIsOwning(true),
100+
self::ONE_TO_ONE => (new RelationOneToOne(
101+
propertyName: $this->owningProperty,
102+
targetClassName: $this->inverseClass,
103+
targetPropertyName: $this->inverseProperty,
104+
isSelfReferencing: $this->isSelfReferencing,
105+
mapInverseRelation: $this->mapInverseRelation,
106+
))
88107
->setIsNullable($this->isNullable)
89-
->setIsSelfReferencing($this->isSelfReferencing)
90-
->setMapInverseRelation($this->mapInverseRelation),
91-
self::MANY_TO_MANY => (new RelationManyToMany())
92-
->setPropertyName($this->owningProperty)
93-
->setTargetClassName($this->inverseClass)
94-
->setTargetPropertyName($this->inverseProperty)
95-
->setIsOwning(true)->setMapInverseRelation(
96-
$this->mapInverseRelation
97-
)
98-
->setIsSelfReferencing($this->isSelfReferencing),
99-
self::ONE_TO_ONE => (new RelationOneToOne())
100-
->setPropertyName($this->owningProperty)
101-
->setTargetClassName($this->inverseClass)
102-
->setTargetPropertyName($this->inverseProperty)
103-
->setIsNullable($this->isNullable)
104-
->setIsOwning(true)
105-
->setIsSelfReferencing($this->isSelfReferencing)
106-
->setMapInverseRelation($this->mapInverseRelation),
108+
->setIsOwning(true),
107109
default => throw new \InvalidArgumentException('Invalid type'),
108110
};
109111
}
110112

111113
public function getInverseRelation(): RelationManyToMany|RelationOneToOne|RelationOneToMany
112114
{
113115
return match ($this->getType()) {
114-
self::MANY_TO_ONE => (new RelationOneToMany())
115-
->setPropertyName($this->inverseProperty)
116-
->setTargetClassName($this->owningClass)
117-
->setTargetPropertyName($this->owningProperty)
118-
->setOrphanRemoval($this->orphanRemoval)
119-
->setIsSelfReferencing($this->isSelfReferencing),
120-
self::MANY_TO_MANY => (new RelationManyToMany())
121-
->setPropertyName($this->inverseProperty)
122-
->setTargetClassName($this->owningClass)
123-
->setTargetPropertyName($this->owningProperty)
124-
->setIsOwning(false)
125-
->setIsSelfReferencing($this->isSelfReferencing),
126-
self::ONE_TO_ONE => (new RelationOneToOne())
127-
->setPropertyName($this->inverseProperty)
128-
->setTargetClassName($this->owningClass)
129-
->setTargetPropertyName($this->owningProperty)
116+
self::MANY_TO_ONE => (new RelationOneToMany(
117+
propertyName: $this->inverseProperty,
118+
targetClassName: $this->owningClass,
119+
targetPropertyName: $this->owningProperty,
120+
isSelfReferencing: $this->isSelfReferencing
121+
))
122+
->setOrphanRemoval($this->orphanRemoval),
123+
self::MANY_TO_MANY => (new RelationManyToMany(
124+
propertyName: $this->inverseProperty,
125+
targetClassName: $this->owningClass,
126+
targetPropertyName: $this->owningProperty,
127+
isSelfReferencing: $this->isSelfReferencing
128+
))
129+
->setIsOwning(false),
130+
self::ONE_TO_ONE => (new RelationOneToOne(
131+
propertyName: $this->inverseProperty,
132+
targetClassName: $this->owningClass,
133+
targetPropertyName: $this->owningProperty,
134+
isSelfReferencing: $this->isSelfReferencing
135+
))
130136
->setIsNullable($this->isNullable)
131-
->setIsOwning(false)
132-
->setIsSelfReferencing($this->isSelfReferencing),
137+
->setIsOwning(false),
133138
default => throw new \InvalidArgumentException('Invalid type'),
134139
};
135140
}

src/Maker/MakeResetPassword.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -427,12 +427,14 @@ private function generateRequestEntity(Generator $generator, ClassNameDetails $r
427427
CODE
428428
);
429429

430-
$manipulator->addManyToOneRelation((new RelationManyToOne())
431-
->setPropertyName('user')
432-
->setTargetClassName($this->userClass)
433-
->setMapInverseRelation(false)
434-
->setCustomReturnType('object', false)
435-
->avoidSetter()
430+
$manipulator->addManyToOneRelation((new RelationManyToOne(
431+
propertyName: 'user',
432+
targetClassName: $this->userClass,
433+
mapInverseRelation: false,
434+
avoidSetter: true,
435+
isCustomReturnTypeNullable: false
436+
))
437+
->setCustomReturnType('object')
436438
);
437439

438440
$this->fileManager->dumpFile($requestEntityPath, $manipulator->getSourceCode());

0 commit comments

Comments
 (0)