|
| 1 | +<?php |
| 2 | + |
| 3 | +declare (strict_types=1); |
| 4 | +namespace Rector\Php84\Rector\Class_; |
| 5 | + |
| 6 | +use RectorPrefix202505\Nette\Utils\Strings; |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\AttributeGroup; |
| 9 | +use PhpParser\Node\Stmt\ClassConst; |
| 10 | +use PhpParser\Node\Stmt\ClassMethod; |
| 11 | +use PhpParser\Node\Stmt\Function_; |
| 12 | +use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode; |
| 13 | +use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; |
| 14 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; |
| 15 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
| 16 | +use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover; |
| 17 | +use Rector\Comments\NodeDocBlock\DocBlockUpdater; |
| 18 | +use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory; |
| 19 | +use Rector\Rector\AbstractRector; |
| 20 | +use Rector\ValueObject\PhpVersionFeature; |
| 21 | +use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
| 22 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 23 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 24 | +/** |
| 25 | + * @see \Rector\Tests\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector\DeprecatedAnnotationToDeprecatedAttributeRectorTest |
| 26 | + */ |
| 27 | +final class DeprecatedAnnotationToDeprecatedAttributeRector extends AbstractRector implements MinPhpVersionInterface |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @readonly |
| 31 | + */ |
| 32 | + private PhpDocTagRemover $phpDocTagRemover; |
| 33 | + /** |
| 34 | + * @readonly |
| 35 | + */ |
| 36 | + private PhpAttributeGroupFactory $phpAttributeGroupFactory; |
| 37 | + /** |
| 38 | + * @readonly |
| 39 | + */ |
| 40 | + private DocBlockUpdater $docBlockUpdater; |
| 41 | + /** |
| 42 | + * @readonly |
| 43 | + */ |
| 44 | + private PhpDocInfoFactory $phpDocInfoFactory; |
| 45 | + /** |
| 46 | + * @see https://regex101.com/r/qNytVk/1 |
| 47 | + * @var string |
| 48 | + */ |
| 49 | + private const VERSION_MATCH_REGEX = '/^(?:(\\d+\\.\\d+\\.\\d+)\\s+)?(.*)$/'; |
| 50 | + public function __construct(PhpDocTagRemover $phpDocTagRemover, PhpAttributeGroupFactory $phpAttributeGroupFactory, DocBlockUpdater $docBlockUpdater, PhpDocInfoFactory $phpDocInfoFactory) |
| 51 | + { |
| 52 | + $this->phpDocTagRemover = $phpDocTagRemover; |
| 53 | + $this->phpAttributeGroupFactory = $phpAttributeGroupFactory; |
| 54 | + $this->docBlockUpdater = $docBlockUpdater; |
| 55 | + $this->phpDocInfoFactory = $phpDocInfoFactory; |
| 56 | + } |
| 57 | + public function getRuleDefinition() : RuleDefinition |
| 58 | + { |
| 59 | + return new RuleDefinition('Change @deprecated annotation to Deprecated attribute', [new CodeSample(<<<'CODE_SAMPLE' |
| 60 | +/** |
| 61 | + * @deprecated 1.0.0 Use SomeOtherClass instead |
| 62 | + */ |
| 63 | +class SomeClass |
| 64 | +{ |
| 65 | +} |
| 66 | +CODE_SAMPLE |
| 67 | +, <<<'CODE_SAMPLE' |
| 68 | +#[\Deprecated(message: 'Use SomeOtherClass instead', since: '1.0.0')] |
| 69 | +class SomeClass |
| 70 | +{ |
| 71 | +} |
| 72 | +CODE_SAMPLE |
| 73 | +), new CodeSample(<<<'CODE_SAMPLE' |
| 74 | +/** |
| 75 | + * @deprecated 1.0.0 Use SomeOtherFunction instead |
| 76 | + */ |
| 77 | +function someFunction() |
| 78 | +{ |
| 79 | +} |
| 80 | +CODE_SAMPLE |
| 81 | +, <<<'CODE_SAMPLE' |
| 82 | +#[\Deprecated(message: 'Use SomeOtherFunction instead', since: '1.0.0')] |
| 83 | +function someFunction() |
| 84 | +{ |
| 85 | +} |
| 86 | +CODE_SAMPLE |
| 87 | +)]); |
| 88 | + } |
| 89 | + public function getNodeTypes() : array |
| 90 | + { |
| 91 | + return [Function_::class, ClassMethod::class, ClassConst::class]; |
| 92 | + } |
| 93 | + /** |
| 94 | + * @param ClassConst|Function_|ClassMethod $node |
| 95 | + */ |
| 96 | + public function refactor(Node $node) : ?Node |
| 97 | + { |
| 98 | + $hasChanged = \false; |
| 99 | + $phpDocInfo = $this->phpDocInfoFactory->createFromNode($node); |
| 100 | + if ($phpDocInfo instanceof PhpDocInfo) { |
| 101 | + $deprecatedAttributeGroup = $this->handleDeprecated($phpDocInfo); |
| 102 | + if ($deprecatedAttributeGroup instanceof AttributeGroup) { |
| 103 | + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); |
| 104 | + $node->attrGroups = \array_merge($node->attrGroups, [$deprecatedAttributeGroup]); |
| 105 | + $this->removeDeprecatedAnnotations($phpDocInfo); |
| 106 | + $hasChanged = \true; |
| 107 | + } |
| 108 | + } |
| 109 | + return $hasChanged ? $node : null; |
| 110 | + } |
| 111 | + public function provideMinPhpVersion() : int |
| 112 | + { |
| 113 | + return PhpVersionFeature::DEPRECATED_ATTRIBUTE; |
| 114 | + } |
| 115 | + private function handleDeprecated(PhpDocInfo $phpDocInfo) : ?AttributeGroup |
| 116 | + { |
| 117 | + $attributeGroup = null; |
| 118 | + $desiredTagValueNodes = $phpDocInfo->getTagsByName('deprecated'); |
| 119 | + foreach ($desiredTagValueNodes as $desiredTagValueNode) { |
| 120 | + if (!$desiredTagValueNode->value instanceof DeprecatedTagValueNode) { |
| 121 | + continue; |
| 122 | + } |
| 123 | + $attributeGroup = $this->createAttributeGroup($desiredTagValueNode->value->description); |
| 124 | + $this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode); |
| 125 | + break; |
| 126 | + } |
| 127 | + return $attributeGroup; |
| 128 | + } |
| 129 | + private function createAttributeGroup(string $annotationValue) : AttributeGroup |
| 130 | + { |
| 131 | + $matches = Strings::match($annotationValue, self::VERSION_MATCH_REGEX); |
| 132 | + $since = $matches[1] ?? null; |
| 133 | + $message = $matches[2] ?? null; |
| 134 | + return $this->phpAttributeGroupFactory->createFromClassWithItems('Deprecated', \array_filter(['message' => $message, 'since' => $since])); |
| 135 | + } |
| 136 | + private function removeDeprecatedAnnotations(PhpDocInfo $phpDocInfo) : bool |
| 137 | + { |
| 138 | + $hasChanged = \false; |
| 139 | + $desiredTagValueNodes = $phpDocInfo->getTagsByName('deprecated'); |
| 140 | + foreach ($desiredTagValueNodes as $desiredTagValueNode) { |
| 141 | + if (!$desiredTagValueNode->value instanceof GenericTagValueNode) { |
| 142 | + continue; |
| 143 | + } |
| 144 | + $this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode); |
| 145 | + $hasChanged = \true; |
| 146 | + } |
| 147 | + return $hasChanged; |
| 148 | + } |
| 149 | +} |
0 commit comments