-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PHP 8.0] Extend DoctrineAnnotationClassToAttributeRector rule to cov…
…er short target annotations (#1260)
- Loading branch information
1 parent
660adeb
commit d2f7c2e
Showing
7 changed files
with
119 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...Php80/Rector/Class_/DoctrineAnnotationClassToAttributeRector/Fixture/short_target.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\DoctrineAnnotationClassToAttributeRector\Fixture; | ||
|
||
/** | ||
* @Annotation | ||
* @Target("METHOD") | ||
*/ | ||
final class ShortTarget | ||
{ | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\DoctrineAnnotationClassToAttributeRector\Fixture; | ||
|
||
#[\Attribute(\Attribute::TARGET_METHOD)] | ||
final class ShortTarget | ||
{ | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php80\NodeAnalyzer; | ||
|
||
use PhpParser\Node\Expr\ClassConstFetch; | ||
use Rector\Core\PhpParser\Node\NodeFactory; | ||
|
||
final class AnnotationTargetResolver | ||
{ | ||
/** | ||
* @see https://github.com/doctrine/annotations/blob/e6e7b7d5b45a2f2abc5460cc6396480b2b1d321f/lib/Doctrine/Common/Annotations/Annotation/Target.php#L24-L29 | ||
* @var array<string, string> | ||
*/ | ||
private const TARGET_TO_CONSTANT_MAP = [ | ||
'METHOD' => 'TARGET_METHOD', | ||
'PROPERTY' => 'TARGET_PROPERTY', | ||
'CLASS' => 'TARGET_CLASS', | ||
'FUNCTION' => 'TARGET_FUNCTION', | ||
'ALL' => 'TARGET_ALL', | ||
// special case | ||
'ANNOTATION' => 'TARGET_CLASS', | ||
]; | ||
|
||
public function __construct( | ||
private NodeFactory $nodeFactory, | ||
) { | ||
} | ||
|
||
/** | ||
* @param array<int|string, mixed> $targetValues | ||
* @return ClassConstFetch[] | ||
*/ | ||
public function resolveFlagClassConstFetches(array $targetValues): array | ||
{ | ||
$classConstFetches = []; | ||
|
||
foreach (self::TARGET_TO_CONSTANT_MAP as $target => $constant) { | ||
if (! in_array($target, $targetValues, true)) { | ||
continue; | ||
} | ||
|
||
$classConstFetches[] = $this->nodeFactory->createClassConstFetch('Attribute', $constant); | ||
} | ||
|
||
return $classConstFetches; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters