Skip to content

Commit c8ea64d

Browse files
committed
move property attribute logic to one place
1 parent 6f21375 commit c8ea64d

File tree

2 files changed

+26
-94
lines changed

2 files changed

+26
-94
lines changed

src/Maker/MakeRegistrationForm.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -391,18 +391,9 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
391391
);
392392
$userManipulator->setIo($io);
393393

394-
// $annotation = [];
395-
// $attrNode = [];
396-
//
397-
// if ($usesAttributes) {
398-
// $attrNode = [$userManipulator->buildAttributeNode('ORM\Column', ['type' => 'boolean'])];
399-
// } else {
400-
// $annotation = ['@ORM\Column(type="boolean")'];
401-
// }
402-
403394
$userManipulator->addProperty(
404395
'isVerified',
405-
['@ORM\Column(type="boolean")'],
396+
['@ORM\Column(type="boolean")'], // @legacy - Remove when annotation support is dropped.
406397
false,
407398
[$userManipulator->buildAttributeNode('ORM\Column', ['type' => 'boolean'])]
408399
);

src/Util/ClassSourceManipulator.php

Lines changed: 25 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,19 @@ public function addEntityField(string $propertyName, array $columnOptions, array
9090
$typeHint = $this->getEntityTypeHint($columnOptions['type']);
9191
$nullable = $columnOptions['nullable'] ?? false;
9292
$isId = (bool) ($columnOptions['id'] ?? false);
93-
$attributes = [];
94-
95-
if ($this->useAttributesForDoctrineMapping) {
96-
$attributes[] = $this->buildAttributeNode('ORM\Column', $columnOptions);
97-
} else {
98-
$comments[] = $this->buildAnnotationLine('@ORM\Column', $columnOptions);
99-
}
93+
$comments[] = $this->buildAnnotationLine('@ORM\Column', $columnOptions);
10094

10195
$defaultValue = null;
10296
if ('array' === $typeHint) {
10397
$defaultValue = new Node\Expr\Array_([], ['kind' => Node\Expr\Array_::KIND_SHORT]);
10498
}
10599

106-
$this->addProperty($propertyName, $comments, $defaultValue, $attributes);
100+
$this->addProperty(
101+
$propertyName,
102+
$comments,
103+
$defaultValue,
104+
[$this->buildAttributeNode('ORM\Column', $columnOptions)]
105+
);
107106

108107
$this->addGetter(
109108
$propertyName,
@@ -123,30 +122,12 @@ public function addEmbeddedEntity(string $propertyName, string $className): void
123122
{
124123
$typeHint = $this->addUseStatementIfNecessary($className);
125124

126-
$annotations = [];
127-
$attributes = [];
128-
129-
if (!$this->useAttributesForDoctrineMapping) {
130-
$annotations = [
131-
$this->buildAnnotationLine(
132-
'@ORM\\Embedded',
133-
[
134-
'class' => new ClassNameValue($className, $typeHint),
135-
]
136-
),
137-
];
138-
} else {
139-
$attributes = [
140-
$this->buildAttributeNode(
141-
'ORM\\Embedded',
142-
[
143-
'class' => new ClassNameValue($className, $typeHint),
144-
]
145-
),
146-
];
147-
}
148-
149-
$this->addProperty($propertyName, $annotations, null, $attributes);
125+
$this->addProperty(
126+
$propertyName,
127+
[$this->buildAnnotationLine('@ORM\\Embedded', ['class' => new ClassNameValue($className, $typeHint)])],
128+
null,
129+
[$this->buildAttributeNode('ORM\\Embedded', ['class' => new ClassNameValue($className, $typeHint)])]
130+
);
150131

151132
// logic to avoid re-adding the same ArrayCollection line
152133
$addEmbedded = true;
@@ -345,15 +326,12 @@ public function addProperty(string $name, array $annotationLines = [], $defaultV
345326

346327
$newPropertyBuilder = (new Builder\Property($name))->makePrivate();
347328

348-
// if ($annotationLines && $this->useAnnotations) {
349-
// $newPropertyBuilder->setDocComment($this->createDocBlock($annotationLines));
350-
// }
351-
352-
if ($this->useAttributesForDoctrineMapping) {
329+
if ($attributes && $this->useAttributesForDoctrineMapping) {
353330
foreach ($attributes as $attribute) {
354331
$newPropertyBuilder->addAttribute($attribute);
355332
}
356333
} elseif ($annotationLines && $this->useAnnotations) {
334+
// @legacy - Remove when annotation support is dropped.
357335
$newPropertyBuilder->setDocComment($this->createDocBlock($annotationLines));
358336
}
359337

@@ -526,35 +504,12 @@ private function addSingularRelation(BaseRelation $relation): void
526504
$annotationOptions['cascade'] = ['persist', 'remove'];
527505
}
528506

529-
$annotations = [];
530-
$attributes = [];
531-
532-
if (!$this->useAttributesForDoctrineMapping) {
533-
$annotations = [
534-
$this->buildAnnotationLine(
535-
$relation instanceof RelationManyToOne ? '@ORM\\ManyToOne' : '@ORM\\OneToOne',
536-
$annotationOptions
537-
),
538-
];
539-
} else {
540-
$attributes = [
541-
$this->buildAttributeNode(
542-
$relation instanceof RelationManyToOne ? 'ORM\\ManyToOne' : 'ORM\\OneToOne',
543-
$annotationOptions
544-
),
545-
];
546-
}
507+
$annotations = [$this->buildAnnotationLine($relation instanceof RelationManyToOne ? '@ORM\\ManyToOne' : '@ORM\\OneToOne', $annotationOptions)];
508+
$attributes = [$this->buildAttributeNode($relation instanceof RelationManyToOne ? 'ORM\\ManyToOne' : 'ORM\\OneToOne', $annotationOptions)];
547509

548510
if (!$relation->isNullable() && $relation->isOwning()) {
549-
if (!$this->useAttributesForDoctrineMapping) {
550-
$annotations[] = $this->buildAnnotationLine('@ORM\\JoinColumn', [
551-
'nullable' => false,
552-
]);
553-
} else {
554-
$attributes[] = $this->buildAttributeNode('ORM\\JoinColumn', [
555-
'nullable' => false,
556-
]);
557-
}
511+
$annotations[] = $this->buildAnnotationLine('@ORM\\JoinColumn', ['nullable' => false]);
512+
$attributes[] = $this->buildAttributeNode('ORM\\JoinColumn', ['nullable' => false]);
558513
}
559514

560515
$this->addProperty($relation->getPropertyName(), $annotations, null, $attributes);
@@ -622,26 +577,12 @@ private function addCollectionRelation(BaseCollectionRelation $relation): void
622577
$annotationOptions['orphanRemoval'] = true;
623578
}
624579

625-
$annotations = [];
626-
$attributes = [];
627-
628-
if (!$this->useAttributesForDoctrineMapping) {
629-
$annotations = [
630-
$this->buildAnnotationLine(
631-
$relation instanceof RelationManyToMany ? '@ORM\\ManyToMany' : '@ORM\\OneToMany',
632-
$annotationOptions
633-
),
634-
];
635-
} else {
636-
$attributes = [
637-
$this->buildAttributeNode(
638-
$relation instanceof RelationManyToMany ? 'ORM\\ManyToMany' : 'ORM\\OneToMany',
639-
$annotationOptions
640-
),
641-
];
642-
}
643-
644-
$this->addProperty($relation->getPropertyName(), $annotations, null, $attributes);
580+
$this->addProperty(
581+
$relation->getPropertyName(),
582+
[$this->buildAnnotationLine($relation instanceof RelationManyToMany ? '@ORM\\ManyToMany' : '@ORM\\OneToMany', $annotationOptions)],
583+
null,
584+
[$this->buildAttributeNode($relation instanceof RelationManyToMany ? 'ORM\\ManyToMany' : 'ORM\\OneToMany', $annotationOptions)]
585+
);
645586

646587
// logic to avoid re-adding the same ArrayCollection line
647588
$addArrayCollection = true;

0 commit comments

Comments
 (0)