From b7359eda29da4377d8ee842c0964f981fbd42abd Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 22 Jun 2021 23:01:49 +0700 Subject: [PATCH] [Php80] Fix Non namespaced class with annotation on AnnotationToAttributeRector (#268) Co-authored-by: Stephan Vierkant --- ...n_namespaced_class_with_annotation.php.inc | 19 +++++++++++++++++++ .../Class_/AnnotationToAttributeRector.php | 15 ++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/non_namespaced_class_with_annotation.php.inc diff --git a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/non_namespaced_class_with_annotation.php.inc b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/non_namespaced_class_with_annotation.php.inc new file mode 100644 index 000000000000..df9007a49356 --- /dev/null +++ b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/non_namespaced_class_with_annotation.php.inc @@ -0,0 +1,19 @@ + +----- + diff --git a/rules/Php80/Rector/Class_/AnnotationToAttributeRector.php b/rules/Php80/Rector/Class_/AnnotationToAttributeRector.php index 698bce80feaa..1f308112994d 100644 --- a/rules/Php80/Rector/Class_/AnnotationToAttributeRector.php +++ b/rules/Php80/Rector/Class_/AnnotationToAttributeRector.php @@ -11,6 +11,7 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Property; +use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode; use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode; @@ -147,7 +148,7 @@ private function processApplyAttrGroups(array $tags, PhpDocInfo $phpDocInfo, Nod foreach ($tags as $tag) { foreach ($this->annotationsToAttributes as $annotationToAttribute) { $annotationToAttributeTag = $annotationToAttribute->getTag(); - if ($phpDocInfo->hasByName($annotationToAttributeTag)) { + if ($this->isFoundGenericTag($phpDocInfo, $tag->value, $annotationToAttributeTag)) { // 1. remove php-doc tag $this->phpDocTagRemover->removeByName($phpDocInfo, $annotationToAttributeTag); @@ -182,6 +183,18 @@ private function processApplyAttrGroups(array $tags, PhpDocInfo $phpDocInfo, Nod return $hasNewAttrGroups; } + private function isFoundGenericTag( + PhpDocInfo $phpDocInfo, + PhpDocTagValueNode $phpDocTagValueNode, + string $annotationToAttributeTag + ): bool + { + if (! $phpDocInfo->hasByName($annotationToAttributeTag)) { + return false; + } + return $phpDocTagValueNode instanceof GenericTagValueNode; + } + private function shouldSkip( PhpDocTagValueNode $phpDocTagValueNode, PhpDocInfo $phpDocInfo,