Skip to content

Commit

Permalink
[PHP 8.0] Attributes - add support for single target (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 13, 2021
1 parent 3157491 commit 5e57bb6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Rector\Tests\Php80\Rector\Class_\DoctrineAnnotationClassToAttributeRector\Fixture;

use Doctrine\Common\Annotations\Annotation\Target;

/**
* @Annotation
* @Target("METHOD")
*/
final class TargetMethodOnly
{
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\Class_\DoctrineAnnotationClassToAttributeRector\Fixture;

use Doctrine\Common\Annotations\Annotation\Target;

#[\Attribute(\Attribute::TARGET_METHOD)]
final class TargetMethodOnly
{
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public function refactor(Node $node): ?Node
$requiredDoctrineAnnotationTagValueNode = $propertyPhpDocInfo->getByAnnotationClass(
'Doctrine\Common\Annotations\Annotation\Required'
);

if (! $requiredDoctrineAnnotationTagValueNode instanceof DoctrineAnnotationTagValueNode) {
continue;
}
Expand Down Expand Up @@ -207,12 +208,14 @@ private function decorateTarget(PhpDocInfo $phpDocInfo, AttributeGroup $attribut
}

$targets = $targetDoctrineAnnotationTagValueNode->getSilentValue();
if (! $targets instanceof CurlyListNode) {
if ($targets instanceof CurlyListNode) {
$targetValues = $targets->getValuesWithExplicitSilentAndWithoutQuotes();
} elseif (is_string($targets)) {
$targetValues = [$targets];
} else {
return;
}

$targetValues = $targets->getValuesWithExplicitSilentAndWithoutQuotes();

$flags = $this->resolveFlags($targetValues);
$flagCollection = $this->attributeFlagFactory->createFlagCollection($flags);
if ($flagCollection === null) {
Expand Down

0 comments on commit 5e57bb6

Please sign in to comment.