From 7afec9a02c39848257341c4d2d08e1ddf52bc58b Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 13 Feb 2024 05:42:42 +0100 Subject: [PATCH] Add unique PHP 8.1 constraints (#5612) * add unique PHP 8.1 constraints * [ci-review] Rector Rectify * Fix * Fix * ensure trim space * Fix --------- Co-authored-by: GitHub Action Co-authored-by: Abdul Malik Ikhsan --- .../FixturePhp81/unique_constraints.php.inc | 26 ++++++++++++++ .../config/nested_attributes_php81.php | 6 ++++ .../Class_/AnnotationToAttributeRector.php | 4 ++- .../DoctrineAnnotationDecorator.php | 34 ++++++++++++++----- 4 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/FixturePhp81/unique_constraints.php.inc diff --git a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/FixturePhp81/unique_constraints.php.inc b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/FixturePhp81/unique_constraints.php.inc new file mode 100644 index 00000000000..1c50ede6ebf --- /dev/null +++ b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/FixturePhp81/unique_constraints.php.inc @@ -0,0 +1,26 @@ + +----- + diff --git a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/config/nested_attributes_php81.php b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/config/nested_attributes_php81.php index 4abe1af7c71..aabe01c478d 100644 --- a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/config/nested_attributes_php81.php +++ b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/config/nested_attributes_php81.php @@ -1,6 +1,8 @@ useImportsResolver->resolveBareUses(); - // 1. bare tags without annotation class, e.g. "@inject" + // 1. bare tags without annotation class, e.g. "@require" $genericAttributeGroups = $this->processGenericTags($phpDocInfo); // 2. Doctrine annotation classes @@ -233,6 +233,7 @@ private function processDoctrineAnnotationClasses(PhpDocInfo $phpDocInfo, array } $doctrineTagValueNode = $phpDocChildNode->value; + $annotationToAttribute = $this->matchAnnotationToAttribute($doctrineTagValueNode); if (! $annotationToAttribute instanceof AnnotationToAttribute) { continue; @@ -242,6 +243,7 @@ private function processDoctrineAnnotationClasses(PhpDocInfo $phpDocInfo, array $doctrineTagValueNode, $annotationToAttribute, ); + $doctrineTagValueNodes[] = $doctrineTagValueNode; } diff --git a/src/BetterPhpDocParser/PhpDocParser/DoctrineAnnotationDecorator.php b/src/BetterPhpDocParser/PhpDocParser/DoctrineAnnotationDecorator.php index 884c7c8ddae..ae9a9cebf23 100644 --- a/src/BetterPhpDocParser/PhpDocParser/DoctrineAnnotationDecorator.php +++ b/src/BetterPhpDocParser/PhpDocParser/DoctrineAnnotationDecorator.php @@ -172,10 +172,12 @@ private function processTextSpacelessInTextNode( if (! str_starts_with($otherText, '@\\') && trim($otherText) !== '') { $phpDocNode->children[$key] = new PhpDocTextNode($otherText); array_splice($phpDocNode->children, $key + 1, 0, $spacelessPhpDocTagNodes); - } else { - unset($phpDocNode->children[$key]); - array_splice($phpDocNode->children, $key, 0, $spacelessPhpDocTagNodes); + + return; } + + unset($phpDocNode->children[$key]); + array_splice($phpDocNode->children, $key, 0, $spacelessPhpDocTagNodes); } private function transformGenericTagValueNodesToDoctrineAnnotationTagValueNodes( @@ -448,15 +450,14 @@ private function resolveFqnAnnotationSpacelessPhpDocTagNode( $nestedAnnotationOpen = explode('(', (string) $fullyQualifiedAnnotationClass); $fullyQualifiedAnnotationClass = $nestedAnnotationOpen[0]; - $annotationContent = $match['annotation_content'] ?? null; - $tagName = '@\\' . $fullyQualifiedAnnotationClass; $formerStartEnd = $phpDocTextNode->getAttribute(PhpDocAttributeKey::START_AND_END); - if (isset($nestedAnnotationOpen[1])) { - $annotationContent = '("' . trim($nestedAnnotationOpen[1], '"\'') . '")'; - } + $annotationContent = $this->resolveAnnotationContent( + $match['annotation_content'] ?? '', + $nestedAnnotationOpen + ); $spacelessPhpDocTagNodes[] = $this->createDoctrineSpacelessPhpDocTagNode( $annotationContent, @@ -469,4 +470,21 @@ private function resolveFqnAnnotationSpacelessPhpDocTagNode( return $spacelessPhpDocTagNodes; } + + /** + * @param string[]|null[] $nestedAnnotationOpen + */ + private function resolveAnnotationContent(string $annotationContent, array $nestedAnnotationOpen): string + { + if (! isset($nestedAnnotationOpen[1])) { + return $annotationContent; + } + + $trimmedNestedAnnotationOpen = trim($nestedAnnotationOpen[1]); + if (str_ends_with($trimmedNestedAnnotationOpen, '{')) { + return $annotationContent; + } + + return '("' . trim($trimmedNestedAnnotationOpen, '"\'') . '")'; + } }