|
4 | 4 |
|
5 | 5 | namespace Rector\Php84\Rector\Class_; |
6 | 6 |
|
7 | | -use Nette\Utils\Strings; |
8 | 7 | use PhpParser\Node; |
9 | | -use PhpParser\Node\Arg; |
10 | | -use PhpParser\Node\Attribute; |
11 | | -use PhpParser\Node\AttributeGroup; |
12 | | -use PhpParser\Node\Identifier; |
13 | | -use PhpParser\Node\Name\FullyQualified; |
14 | | -use PhpParser\Node\Scalar\String_; |
15 | 8 | use PhpParser\Node\Stmt\ClassConst; |
16 | 9 | use PhpParser\Node\Stmt\ClassMethod; |
17 | 10 | use PhpParser\Node\Stmt\Function_; |
18 | | -use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode; |
19 | | -use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; |
20 | | -use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; |
21 | | -use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
22 | | -use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover; |
23 | | -use Rector\Comments\NodeDocBlock\DocBlockUpdater; |
24 | | -use Rector\NodeTypeResolver\Node\AttributeKey; |
25 | | -use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory; |
| 11 | +use Rector\PhpAttribute\DeprecatedAnnotationToDeprecatedAttributeConverter; |
26 | 12 | use Rector\Rector\AbstractRector; |
27 | 13 | use Rector\ValueObject\PhpVersionFeature; |
28 | 14 | use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
|
34 | 20 | */ |
35 | 21 | final class DeprecatedAnnotationToDeprecatedAttributeRector extends AbstractRector implements MinPhpVersionInterface |
36 | 22 | { |
37 | | - /** |
38 | | - * @see https://regex101.com/r/qNytVk/1 |
39 | | - * @var string |
40 | | - */ |
41 | | - private const VERSION_MATCH_REGEX = '/^(?:(\d+\.\d+\.\d+)\s+)?(.*)$/'; |
42 | | - |
43 | | - /** |
44 | | - * @see https://regex101.com/r/SVDPOB/1 |
45 | | - * @var string |
46 | | - */ |
47 | | - private const START_STAR_SPACED_REGEX = '#^ *\*#ms'; |
48 | | - |
49 | 23 | public function __construct( |
50 | | - private readonly PhpDocTagRemover $phpDocTagRemover, |
51 | | - private readonly PhpAttributeGroupFactory $phpAttributeGroupFactory, |
52 | | - private readonly DocBlockUpdater $docBlockUpdater, |
53 | | - private readonly PhpDocInfoFactory $phpDocInfoFactory, |
| 24 | + private readonly DeprecatedAnnotationToDeprecatedAttributeConverter $converter, |
54 | 25 | ) { |
55 | 26 | } |
56 | 27 |
|
@@ -104,88 +75,11 @@ public function getNodeTypes(): array |
104 | 75 | */ |
105 | 76 | public function refactor(Node $node): ?Node |
106 | 77 | { |
107 | | - $hasChanged = false; |
108 | | - $phpDocInfo = $this->phpDocInfoFactory->createFromNode($node); |
109 | | - if ($phpDocInfo instanceof PhpDocInfo) { |
110 | | - $deprecatedAttributeGroup = $this->handleDeprecated($phpDocInfo); |
111 | | - if ($deprecatedAttributeGroup instanceof AttributeGroup) { |
112 | | - $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); |
113 | | - $node->attrGroups = array_merge($node->attrGroups, [$deprecatedAttributeGroup]); |
114 | | - $this->removeDeprecatedAnnotations($phpDocInfo); |
115 | | - $hasChanged = true; |
116 | | - } |
117 | | - } |
118 | | - |
119 | | - return $hasChanged ? $node : null; |
| 78 | + return $this->converter->convert($node); |
120 | 79 | } |
121 | 80 |
|
122 | 81 | public function provideMinPhpVersion(): int |
123 | 82 | { |
124 | 83 | return PhpVersionFeature::DEPRECATED_ATTRIBUTE; |
125 | 84 | } |
126 | | - |
127 | | - private function handleDeprecated(PhpDocInfo $phpDocInfo): ?AttributeGroup |
128 | | - { |
129 | | - $attributeGroup = null; |
130 | | - $desiredTagValueNodes = $phpDocInfo->getTagsByName('deprecated'); |
131 | | - foreach ($desiredTagValueNodes as $desiredTagValueNode) { |
132 | | - if (! $desiredTagValueNode->value instanceof DeprecatedTagValueNode) { |
133 | | - continue; |
134 | | - } |
135 | | - |
136 | | - $attributeGroup = $this->createAttributeGroup($desiredTagValueNode->value->description); |
137 | | - $this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode); |
138 | | - |
139 | | - break; |
140 | | - } |
141 | | - |
142 | | - return $attributeGroup; |
143 | | - } |
144 | | - |
145 | | - private function createAttributeGroup(string $annotationValue): AttributeGroup |
146 | | - { |
147 | | - $matches = Strings::match($annotationValue, self::VERSION_MATCH_REGEX); |
148 | | - |
149 | | - if ($matches === null) { |
150 | | - $annotationValue = Strings::replace($annotationValue, self::START_STAR_SPACED_REGEX, ''); |
151 | | - |
152 | | - return new AttributeGroup([ |
153 | | - new Attribute( |
154 | | - new FullyQualified('Deprecated'), |
155 | | - [new Arg( |
156 | | - value: new String_($annotationValue, [ |
157 | | - AttributeKey::KIND => String_::KIND_NOWDOC, |
158 | | - AttributeKey::DOC_LABEL => 'TXT', |
159 | | - ]), |
160 | | - name: new Identifier('message') |
161 | | - )] |
162 | | - ), |
163 | | - ]); |
164 | | - } |
165 | | - |
166 | | - $since = $matches[1] ?? null; |
167 | | - $message = $matches[2] ?? null; |
168 | | - |
169 | | - return $this->phpAttributeGroupFactory->createFromClassWithItems('Deprecated', array_filter([ |
170 | | - 'message' => $message, |
171 | | - 'since' => $since, |
172 | | - ])); |
173 | | - } |
174 | | - |
175 | | - private function removeDeprecatedAnnotations(PhpDocInfo $phpDocInfo): bool |
176 | | - { |
177 | | - $hasChanged = false; |
178 | | - |
179 | | - $desiredTagValueNodes = $phpDocInfo->getTagsByName('deprecated'); |
180 | | - foreach ($desiredTagValueNodes as $desiredTagValueNode) { |
181 | | - if (! $desiredTagValueNode->value instanceof GenericTagValueNode) { |
182 | | - continue; |
183 | | - } |
184 | | - |
185 | | - $this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode); |
186 | | - $hasChanged = true; |
187 | | - } |
188 | | - |
189 | | - return $hasChanged; |
190 | | - } |
191 | 85 | } |
0 commit comments