-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathDowngradeAttributeToAnnotationRector.php
More file actions
237 lines (199 loc) · 7.52 KB
/
DowngradeAttributeToAnnotationRector.php
File metadata and controls
237 lines (199 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp80\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\Scope;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\DowngradePhp80\ValueObject\DowngradeAttributeToAnnotation;
use Rector\NodeFactory\DoctrineAnnotationFactory;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
/**
* @changelog https://php.watch/articles/php-attributes#syntax
*
* @see \Rector\Tests\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector\DowngradeAttributeToAnnotationRectorTest
*/
final class DowngradeAttributeToAnnotationRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var string[]
*/
private const SKIPPED_ATTRIBUTES = ['Attribute', 'ReturnTypeWillChange', 'AllowDynamicProperties', 'Override'];
/**
* @var DowngradeAttributeToAnnotation[]
*/
private array $attributesToAnnotations = [];
private bool $isDowngraded = false;
public function __construct(
private readonly DoctrineAnnotationFactory $doctrineAnnotationFactory,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly PhpDocInfoFactory $phpDocInfoFactory
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Refactor PHP attribute markers to annotations notation', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
use Symfony\Component\Routing\Annotation\Route;
class SymfonyRoute
{
#[Route(path: '/path', name: 'action')]
public function action()
{
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Symfony\Component\Routing\Annotation\Route;
class SymfonyRoute
{
/**
* @Route("/path", name="action")
*/
public function action()
{
}
}
CODE_SAMPLE
,
[new DowngradeAttributeToAnnotation('Symfony\Component\Routing\Annotation\Route')]
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class, ClassMethod::class, Property::class, Interface_::class, Param::class, Function_::class];
}
/**
* @param Class_|ClassMethod|Property|Interface_|Param|Function_ $node
*/
public function refactor(Node $node): ?Node
{
if ($node->attrGroups === []) {
return null;
}
$this->isDowngraded = false;
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$oldTokens = $this->file->getOldTokens();
foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $key => $attribute) {
if ($this->shouldSkipAttribute($attribute)) {
if (isset($oldTokens[$attrGroup->getEndTokenPos() + 1]) && ! str_contains(
(string) $oldTokens[$attrGroup->getEndTokenPos() + 1],
"\n"
)) {
// add new line
$oldTokens[$attrGroup->getEndTokenPos() + 1]->text = "\n" . $this->resolveIndentation(
$node,
$attrGroup,
$attribute
) . $oldTokens[$attrGroup->getEndTokenPos() + 1]->text;
$this->isDowngraded = true;
}
continue;
}
$attributeToAnnotation = $this->matchAttributeToAnnotation($attribute, $this->attributesToAnnotations);
if (! $attributeToAnnotation instanceof DowngradeAttributeToAnnotation) {
// clear the attribute to avoid inlining to a comment that will ignore the rest of the line
unset($attrGroup->attrs[$key]);
continue;
}
unset($attrGroup->attrs[$key]);
$this->isDowngraded = true;
if (! \str_contains($attributeToAnnotation->getTag(), '\\')) {
$phpDocInfo->addPhpDocTagNode(
new PhpDocTagNode('@' . $attributeToAnnotation->getTag(), new GenericTagValueNode(''))
);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
continue;
}
$doctrineAnnotation = $this->doctrineAnnotationFactory->createFromAttribute(
$attribute,
$attributeToAnnotation->getTag()
);
$phpDocInfo->addTagValueNode($doctrineAnnotation);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
}
}
// cleanup empty attr groups
$this->cleanupEmptyAttrGroups($node);
if (! $this->isDowngraded) {
return null;
}
return $node;
}
/**
* @param DowngradeAttributeToAnnotation[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allIsAOf($configuration, DowngradeAttributeToAnnotation::class);
$this->attributesToAnnotations = $configuration;
}
private function resolveIndentation(Node $node, AttributeGroup $attributeGroup, Attribute $attribute): string
{
$scope = $node->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return '';
}
if (
($node->getStartTokenPos() === strlen($attribute->name->toString()) - 2)
|| ($node instanceof Class_ && $scope->isInFirstLevelStatement())
) {
return '';
}
$indent = $attributeGroup->getEndTokenPos() - $node->getStartTokenPos() + 2;
return $indent > 0 ? str_repeat(' ', $indent) : '';
}
private function cleanupEmptyAttrGroups(
ClassMethod | Property | Class_ | Interface_ | Param | Function_ $node
): void {
foreach ($node->attrGroups as $key => $attrGroup) {
if ($attrGroup->attrs !== []) {
continue;
}
unset($node->attrGroups[$key]);
$this->isDowngraded = true;
}
}
/**
* @param DowngradeAttributeToAnnotation[] $attributesToAnnotations
*/
private function matchAttributeToAnnotation(
Attribute $attribute,
array $attributesToAnnotations
): ?DowngradeAttributeToAnnotation {
foreach ($attributesToAnnotations as $attributeToAnnotation) {
if (! $this->isName($attribute->name, $attributeToAnnotation->getAttributeClass())) {
continue;
}
return $attributeToAnnotation;
}
return null;
}
private function shouldSkipAttribute(Attribute $attribute): bool
{
$attributeName = $attribute->name->toString();
return in_array($attributeName, self::SKIPPED_ATTRIBUTES, true);
}
}