From 89af6578903b86267acff17d5d577795dd912d89 Mon Sep 17 00:00:00 2001 From: Jeroen Thora Date: Thu, 21 Jul 2022 17:14:56 +0200 Subject: [PATCH] Attempt to fix incorrect doctrine table attribute values on php 8.1 --- .../NodeFactory/PhpAttributeGroupFactory.php | 13 +++++-------- .../PhpAttribute/UnwrapableAnnotationAnalyzer.php | 10 ---------- ...e_and_nested_index_and_uniqueconstraints.php.inc | 4 +++- .../Rector/Class_/AnnotationToAttributeRector.php | 8 ++++++++ 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/packages/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php b/packages/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php index 78bebdb6cd8..09c1d14c672 100644 --- a/packages/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php +++ b/packages/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php @@ -28,21 +28,18 @@ final class PhpAttributeGroupFactory /** * @var array> */ - private array $unwrappedAnnotations = []; + private array $unwrappedAnnotations = [ + 'Doctrine\ORM\Mapping\Table' => ['indexes', 'uniqueConstraints'], + 'Doctrine\ORM\Mapping\Entity' => ['uniqueConstraints'], + ]; public function __construct( private readonly AnnotationToAttributeMapper $annotationToAttributeMapper, private readonly AttributeNameFactory $attributeNameFactory, private readonly NamedArgsFactory $namedArgsFactory, private readonly ExprParameterReflectionTypeCorrector $exprParameterReflectionTypeCorrector, - private readonly AttributeArrayNameInliner $attributeArrayNameInliner, - PhpVersionProvider $phpVersionProvider + private readonly AttributeArrayNameInliner $attributeArrayNameInliner ) { - // nested indexes supported only since PHP 8.1 - if (! $phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::NEW_INITIALIZERS)) { - $this->unwrappedAnnotations['Doctrine\ORM\Mapping\Table'] = ['indexes', 'uniqueConstraints']; - $this->unwrappedAnnotations['Doctrine\ORM\Mapping\Entity'][] = 'uniqueConstraints'; - } } public function createFromSimpleTag(AnnotationToAttribute $annotationToAttribute): AttributeGroup diff --git a/packages/PhpAttribute/UnwrapableAnnotationAnalyzer.php b/packages/PhpAttribute/UnwrapableAnnotationAnalyzer.php index 813e1d5af97..97f63ccc890 100644 --- a/packages/PhpAttribute/UnwrapableAnnotationAnalyzer.php +++ b/packages/PhpAttribute/UnwrapableAnnotationAnalyzer.php @@ -29,11 +29,6 @@ final class UnwrapableAnnotationAnalyzer */ private array $annotationsToAttributes = []; - public function __construct( - private readonly PhpVersionProvider $phpVersionProvider - ) { - } - /** * @param AnnotationToAttribute[] $annotationsToAttributes */ @@ -47,11 +42,6 @@ public function configure(array $annotationsToAttributes): void */ public function areUnwrappable(array $doctrineAnnotationTagValueNodes): bool { - // the new in initilazers is handled directly - if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::NEW_INITIALIZERS)) { - return false; - } - foreach ($doctrineAnnotationTagValueNodes as $doctrineAnnotationTagValueNode) { $annotationClassName = $doctrineAnnotationTagValueNode->identifierTypeNode->getAttribute( PhpDocAttributeKey::RESOLVED_CLASS diff --git a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/FixturePhp81/table_and_nested_index_and_uniqueconstraints.php.inc b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/FixturePhp81/table_and_nested_index_and_uniqueconstraints.php.inc index a0f54caf642..4b7f769a2dc 100644 --- a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/FixturePhp81/table_and_nested_index_and_uniqueconstraints.php.inc +++ b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/FixturePhp81/table_and_nested_index_and_uniqueconstraints.php.inc @@ -23,7 +23,9 @@ namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\F use Doctrine\ORM\Mapping as ORM; -#[ORM\Table(indexes: [new ORM\Index(name: 'search_idx', columns: ['name', 'c'])], uniqueConstraints: [new ORM\UniqueConstraint(name: 'name_idx', columns: ['name'])])] +#[ORM\Table] +#[ORM\Index(name: 'search_idx', columns: ['name', 'c'])] +#[ORM\UniqueConstraint(name: 'name_idx', columns: ['name'])] class TableAndNestedIndex { } diff --git a/rules/Php80/Rector/Class_/AnnotationToAttributeRector.php b/rules/Php80/Rector/Class_/AnnotationToAttributeRector.php index 05f38e83bb8..4f46170b8b5 100644 --- a/rules/Php80/Rector/Class_/AnnotationToAttributeRector.php +++ b/rules/Php80/Rector/Class_/AnnotationToAttributeRector.php @@ -250,6 +250,14 @@ private function processDoctrineAnnotationClasses(PhpDocInfo $phpDocInfo, array $shouldInlinedNested = true; } + // Inline nested annotations when they can/need to be unwrapped (doctrine @Table(index: [@Index(...)]) + if ($nestedDoctrineAnnotationTagValueNodes !== [] + && $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::NEW_INITIALIZERS) + && $this->unwrapableAnnotationAnalyzer->areUnwrappable($nestedDoctrineAnnotationTagValueNodes) + ) { + $shouldInlinedNested = true; + } + if (! $this->removableAnnotationAnalyzer->isRemovable($doctrineTagValueNode)) { $doctrineTagAndAnnotationToAttributes[] = new DoctrineTagAndAnnotationToAttribute( $doctrineTagValueNode,