Skip to content

Commit a522c11

Browse files
committed
better way to determine an attribute prefix
1 parent dc72ab9 commit a522c11

File tree

3 files changed

+23
-43
lines changed

3 files changed

+23
-43
lines changed

src/Maker/MakeRegistrationForm.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,10 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
372372
$userManipulator->setIo($io);
373373

374374
if ($this->doctrineHelper->isDoctrineSupportingAttributes()) {
375-
$userManipulator->addAttributeToClass(new ClassNameDetails(UniqueEntity::class, '\\', null), ['fields' => [$usernameField], 'message' => sprintf('There is already an account with this %s', $usernameField)]);
375+
$userManipulator->addAttributeToClass(
376+
UniqueEntity::class,
377+
['fields' => [$usernameField], 'message' => sprintf('There is already an account with this %s', $usernameField)]
378+
);
376379
} else {
377380
$userManipulator->addAnnotationToClass(
378381
UniqueEntity::class,
@@ -400,7 +403,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
400403
'isVerified',
401404
['@ORM\Column(type="boolean")'],
402405
false,
403-
[$userManipulator->buildAttributeNode(new ClassNameDetails(Column::class, '\\', null, 'ORM'), ['type' => 'boolean'])]
406+
[$userManipulator->buildAttributeNode(Column::class, ['type' => 'boolean'], 'ORM')]
404407
);
405408
$userManipulator->addAccessorMethod('isVerified', 'isVerified', 'bool', false);
406409
$userManipulator->addSetter('isVerified', 'bool', false);

src/Util/ClassNameDetails.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@ final class ClassNameDetails
1818
private $fullClassName;
1919
private $namespacePrefix;
2020
private $suffix;
21-
private $substitutePrefix;
2221

23-
public function __construct(string $fullClassName, string $namespacePrefix, string $suffix = null, string $substitutePrefix = null)
22+
public function __construct(string $fullClassName, string $namespacePrefix, string $suffix = null)
2423
{
2524
$this->fullClassName = $fullClassName;
2625
$this->namespacePrefix = trim($namespacePrefix, '\\');
2726
$this->suffix = $suffix;
28-
$this->substitutePrefix = $substitutePrefix;
2927
}
3028

3129
public function getFullName(): string
@@ -54,20 +52,4 @@ public function getRelativeNameWithoutSuffix(): string
5452
{
5553
return Str::removeSuffix($this->getRelativeName(), $this->suffix);
5654
}
57-
58-
/**
59-
* Returns the Short Name of the class with the substitute prefix.
60-
*
61-
* E.g. \Doctrine\ORM\Mapping\Column::class and the substitutePrefix = ORM,
62-
* the ORM\Column will be returned.
63-
*/
64-
public function getShortNameWithSubstitutePrefix(): string
65-
{
66-
return sprintf('%s\\%s', $this->substitutePrefix, $this->getShortName());
67-
}
68-
69-
public function hasSubstitutePrefix(): bool
70-
{
71-
return null !== $this->substitutePrefix;
72-
}
7355
}

src/Util/ClassSourceManipulator.php

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function addEntityField(string $propertyName, array $columnOptions, array
100100
$attributes = [];
101101

102102
if ($this->useAttributesForDoctrineMapping) {
103-
$attributes[] = $this->buildAttributeNode(new ClassNameDetails(Column::class, '\\', null, 'ORM'), $columnOptions);
103+
$attributes[] = $this->buildAttributeNode(Column::class, $columnOptions, 'ORM');
104104
} else {
105105
$comments[] = $this->buildAnnotationLine('@ORM\Column', $columnOptions);
106106
}
@@ -145,10 +145,9 @@ public function addEmbeddedEntity(string $propertyName, string $className): void
145145
} else {
146146
$attributes = [
147147
$this->buildAttributeNode(
148-
new ClassNameDetails(Embedded::class, '\\', null, 'ORM'),
149-
[
150-
'class' => new ClassNameValue($className, $typeHint),
151-
]
148+
Embedded::class,
149+
['class' => new ClassNameValue($className, $typeHint)],
150+
'ORM'
152151
),
153152
];
154153
}
@@ -368,9 +367,9 @@ public function addProperty(string $name, array $annotationLines = [], $defaultV
368367
$this->addNodeAfterProperties($newPropertyNode);
369368
}
370369

371-
public function addAttributeToClass(ClassNameDetails $attributeClass, array $options): void
370+
public function addAttributeToClass(string $attributeClass, array $options): void
372371
{
373-
$this->addUseStatementIfNecessary($attributeClass->getFullName());
372+
$this->addUseStatementIfNecessary($attributeClass);
374373

375374
$classNode = $this->getClassNode();
376375

@@ -551,12 +550,11 @@ private function addSingularRelation(BaseRelation $relation): void
551550
),
552551
];
553552
} else {
554-
$relationClass = $relation instanceof RelationManyToOne ? ManyToOne::class : OneToOne::class;
555-
556553
$attributes = [
557554
$this->buildAttributeNode(
558-
new ClassNameDetails($relationClass, '\\', null, 'ORM'),
559-
$annotationOptions
555+
$relation instanceof RelationManyToOne ? ManyToOne::class : OneToOne::class,
556+
$annotationOptions,
557+
'ORM'
560558
),
561559
];
562560
}
@@ -567,9 +565,7 @@ private function addSingularRelation(BaseRelation $relation): void
567565
'nullable' => false,
568566
]);
569567
} else {
570-
$attributes[] = $this->buildAttributeNode(new ClassNameDetails(JoinColumn::class, '\\', null, 'ORM'), [
571-
'nullable' => false,
572-
]);
568+
$attributes[] = $this->buildAttributeNode(JoinColumn::class, ['nullable' => false], 'ORM');
573569
}
574570
}
575571

@@ -649,12 +645,11 @@ private function addCollectionRelation(BaseCollectionRelation $relation): void
649645
),
650646
];
651647
} else {
652-
$relationClassName = $relation instanceof RelationManyToMany ? ManyToMany::class : OneToMany::class;
653-
654648
$attributes = [
655649
$this->buildAttributeNode(
656-
new ClassNameDetails($relationClassName, '\\', null, 'ORM'),
657-
$annotationOptions
650+
$relation instanceof RelationManyToMany ? ManyToMany::class : OneToMany::class,
651+
$annotationOptions,
652+
'ORM'
658653
),
659654
];
660655
}
@@ -928,21 +923,21 @@ public function addUseStatementIfNecessary(string $class): string
928923
/**
929924
* Builds a PHPParser attribute node.
930925
*
931-
* @param ClassNameDetails $attributeClass The attribute class which should be used for the attribute. If the class has a substitute
926+
* @param string $attributeClass The attribute class which should be used for the attribute. If the class has a substitute
932927
* prefix, the node is built using the prefix. E.g. #[ORM\Column()] Otherwise, the
933928
* short name is used. E.g. #[Column()]
934929
* @param array $options the named arguments for the attribute ($key = argument name, $value = argument value)
935930
*/
936-
public function buildAttributeNode(ClassNameDetails $attributeClass, array $options): Node\Attribute
931+
public function buildAttributeNode(string $attributeClass, array $options, string $attributePrefix = null): Node\Attribute
937932
{
938-
$options = $this->sortOptionsByClassConstructorParameters($options, $attributeClass->getFullName());
933+
$options = $this->sortOptionsByClassConstructorParameters($options, $attributeClass);
939934

940935
$context = $this;
941936
$nodeArguments = array_map(static function ($option, $value) use ($context) {
942937
return new Node\Arg($context->buildNodeExprByValue($value), false, false, [], new Node\Identifier($option));
943938
}, array_keys($options), array_values($options));
944939

945-
$class = $attributeClass->hasSubstitutePrefix() ? $attributeClass->getShortNameWithSubstitutePrefix() : $attributeClass->getShortName();
940+
$class = $attributePrefix ? sprintf('%s\\%s', $attributePrefix, Str::getShortClassName($attributeClass)) : Str::getShortClassName($attributeClass);
946941

947942
return new Node\Attribute(
948943
new Node\Name($class),

0 commit comments

Comments
 (0)