Skip to content

Commit cb015c6

Browse files
committed
allow comments when creating properties
1 parent d0c4228 commit cb015c6

File tree

2 files changed

+13
-51
lines changed

2 files changed

+13
-51
lines changed

src/Util/ClassSourceManipulator.php

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ final class ClassSourceManipulator
6565
public function __construct(
6666
string $sourceCode,
6767
bool $overwrite = false,
68-
bool $useAnnotations = true,
68+
bool $useAnnotations = false,
6969
bool $fluentMutators = true,
70-
bool $useAttributesForDoctrineMapping = false
70+
bool $useAttributesForDoctrineMapping = true
7171
) {
7272
$this->overwrite = $overwrite;
7373
$this->useAnnotations = $useAnnotations;
@@ -124,7 +124,7 @@ public function addEntityField(string $propertyName, array $columnOptions, array
124124
$defaultValue = new Node\Expr\Array_([], ['kind' => Node\Expr\Array_::KIND_SHORT]);
125125
}
126126

127-
$this->addProperty(name: $propertyName, annotationLines: $comments, defaultValue: $defaultValue, attributes: $attributes);
127+
$this->addProperty(name: $propertyName, defaultValue: $defaultValue, attributes: $attributes, comments: $comments);
128128

129129
$this->addGetter(
130130
$propertyName,
@@ -144,27 +144,13 @@ public function addEmbeddedEntity(string $propertyName, string $className): void
144144
{
145145
$typeHint = $this->addUseStatementIfNecessary($className);
146146

147-
// $annotations = [];
148-
// $attributes = [];
149-
//
150-
// if (!$this->useAttributesForDoctrineMapping) {
151-
// $annotations = [
152-
// $this->buildAnnotationLine(
153-
// '@ORM\\Embedded',
154-
// [
155-
// 'class' => new ClassNameValue($className, $typeHint),
156-
// ]
157-
// ),
158-
// ];
159-
// } else {
160147
$attributes = [
161148
$this->buildAttributeNode(
162149
Embedded::class,
163150
['class' => new ClassNameValue($className, $typeHint)],
164151
'ORM'
165152
),
166153
];
167-
// }
168154

169155
$this->addProperty(name: $propertyName, attributes: $attributes);
170156

@@ -355,8 +341,12 @@ public function createMethodLevelBlankLine()
355341
/**
356342
* @param array<Node\Attribute|Node\AttributeGroup> $attributes
357343
*/
358-
public function addProperty(string $name, array $annotationLines = [], $defaultValue = null, array $attributes = []): void
344+
public function addProperty(string $name, array $annotationLines = [], $defaultValue = null, array $attributes = [], array $comments = []): void
359345
{
346+
if (!empty($annotationLines)) {
347+
@trigger_deprecation('symfony/maker-bundle', '0.0.0', 'CSM:addProperty(annotationLines:) is going to be removed. Triggered from addProperty...');
348+
}
349+
360350
if ($this->propertyExists($name)) {
361351
// we never overwrite properties
362352
return;
@@ -368,8 +358,10 @@ public function addProperty(string $name, array $annotationLines = [], $defaultV
368358
foreach ($attributes as $attribute) {
369359
$newPropertyBuilder->addAttribute($attribute);
370360
}
371-
} elseif ($annotationLines && $this->useAnnotations) {
372-
$newPropertyBuilder->setDocComment($this->createDocBlock($annotationLines));
361+
}
362+
363+
if ($comments) {
364+
$newPropertyBuilder->setDocComment($this->createDocBlock($comments));
373365
}
374366

375367
if (null !== $defaultValue) {
@@ -559,34 +551,16 @@ private function addSingularRelation(BaseRelation $relation): void
559551
$annotationOptions['cascade'] = ['persist', 'remove'];
560552
}
561553

562-
// $annotations = [];
563-
// $attributes = [];
564-
565-
// if (!$this->useAttributesForDoctrineMapping) {
566-
// $annotations = [
567-
// $this->buildAnnotationLine(
568-
// $relation instanceof RelationManyToOne ? '@ORM\\ManyToOne' : '@ORM\\OneToOne',
569-
// $annotationOptions
570-
// ),
571-
// ];
572-
// } else {
573554
$attributes = [
574555
$this->buildAttributeNode(
575556
$relation instanceof RelationManyToOne ? ManyToOne::class : OneToOne::class,
576557
$annotationOptions,
577558
'ORM'
578559
),
579560
];
580-
// }
581561

582562
if (!$relation->isNullable() && $relation->isOwning()) {
583-
// if (!$this->useAttributesForDoctrineMapping) {
584-
// $annotations[] = $this->buildAnnotationLine('@ORM\\JoinColumn', [
585-
// 'nullable' => false,
586-
// ]);
587-
// } else {
588563
$attributes[] = $this->buildAttributeNode(JoinColumn::class, ['nullable' => false], 'ORM');
589-
// }
590564
}
591565

592566
$this->addProperty(name: $relation->getPropertyName(), attributes: $attributes);
@@ -654,25 +628,13 @@ private function addCollectionRelation(BaseCollectionRelation $relation): void
654628
$annotationOptions['orphanRemoval'] = true;
655629
}
656630

657-
// $annotations = [];
658-
// $attributes = [];
659-
//
660-
// if (!$this->useAttributesForDoctrineMapping) {
661-
// $annotations = [
662-
// $this->buildAnnotationLine(
663-
// $relation instanceof RelationManyToMany ? '@ORM\\ManyToMany' : '@ORM\\OneToMany',
664-
// $annotationOptions
665-
// ),
666-
// ];
667-
// } else {
668631
$attributes = [
669632
$this->buildAttributeNode(
670633
$relation instanceof RelationManyToMany ? ManyToMany::class : OneToMany::class,
671634
$annotationOptions,
672635
'ORM'
673636
),
674637
];
675-
// }
676638

677639
$this->addProperty(name: $relation->getPropertyName(), attributes: $attributes);
678640

tests/Util/ClassSourceManipulatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testAddProperty(string $sourceFilename, $propertyName, array $co
3535
$manipulator = new ClassSourceManipulator($source);
3636
$method = (new \ReflectionObject($manipulator))->getMethod('addProperty');
3737
$method->setAccessible(true);
38-
$method->invoke($manipulator, $propertyName, $commentLines);
38+
$method->invoke($manipulator, name: $propertyName, comments: $commentLines);
3939

4040
$this->assertSame($expectedSource, $manipulator->getSourceCode());
4141
}

0 commit comments

Comments
 (0)