Skip to content

Commit

Permalink
[Php80] Fix Non namespaced class with annotation on AnnotationToAttri…
Browse files Browse the repository at this point in the history
…buteRector (#268)

Co-authored-by: Stephan Vierkant <[email protected]>
  • Loading branch information
samsonasik and stephanvierkant authored Jun 22, 2021
1 parent 0c035c3 commit b7359ed
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* @ApiPlatform\Core\Annotation\ApiResource(
* routePrefix="/demo/"
* )
*/
final class NonNamespacedClassWithAnnotation
{
}
?>
-----
<?php

#[\ApiPlatform\Core\Annotation\ApiResource(routePrefix: '/demo/')]
final class NonNamespacedClassWithAnnotation
{
}
?>
15 changes: 14 additions & 1 deletion rules/Php80/Rector/Class_/AnnotationToAttributeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b7359ed

Please sign in to comment.