forked from rectorphp/rector-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplaceSensioRouteAnnotationWithSymfonyRector.php
More file actions
168 lines (141 loc) · 4.88 KB
/
ReplaceSensioRouteAnnotationWithSymfonyRector.php
File metadata and controls
168 lines (141 loc) · 4.88 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
<?php
declare(strict_types=1);
namespace Rector\Symfony\Symfony34\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode;
use Rector\BetterPhpDocParser\PhpDoc\StringNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Configuration\RenamedClassesDataCollector;
use Rector\Rector\AbstractRector;
use Rector\Symfony\Enum\SymfonyAnnotation;
use Rector\Symfony\PhpDocNode\SymfonyRouteTagValueNodeFactory;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://medium.com/@nebkam/symfony-deprecated-route-and-method-annotations-4d5e1d34556a
* @changelog https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html#method-annotation
*
* @see \Rector\Symfony\Tests\Symfony34\Rector\ClassMethod\ReplaceSensioRouteAnnotationWithSymfonyRector\ReplaceSensioRouteAnnotationWithSymfonyRectorTest
*/
final class ReplaceSensioRouteAnnotationWithSymfonyRector extends AbstractRector
{
/**
* @var string
*/
private const SENSIO_ROUTE_NAME = 'Sensio\Bundle\FrameworkExtraBundle\Configuration\Route';
public function __construct(
private readonly SymfonyRouteTagValueNodeFactory $symfonyRouteTagValueNodeFactory,
private readonly PhpDocTagRemover $phpDocTagRemover,
private readonly RenamedClassesDataCollector $renamedClassesDataCollector,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace Sensio @Route annotation with Symfony one',
[
new CodeSample(
<<<'CODE_SAMPLE'
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
final class SomeClass
{
/**
* @Route()
*/
public function run()
{
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Symfony\Component\Routing\Annotation\Route;
final class SomeClass
{
/**
* @Route()
*/
public function run()
{
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassMethod::class, Class_::class];
}
/**
* @param ClassMethod|Class_ $node
*/
public function refactor(Node $node): ?Node
{
// early return in case of non public method
if ($node instanceof ClassMethod && ! $node->isPublic()) {
return null;
}
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
if (! $phpDocInfo instanceof PhpDocInfo) {
return null;
}
$sensioDoctrineAnnotationTagValueNodes = $phpDocInfo->findByAnnotationClass(self::SENSIO_ROUTE_NAME);
// nothing to find
if ($sensioDoctrineAnnotationTagValueNodes === []) {
return null;
}
foreach ($sensioDoctrineAnnotationTagValueNodes as $sensioDoctrineAnnotationTagValueNode) {
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $sensioDoctrineAnnotationTagValueNode);
// unset service, that is deprecated
$sensioDoctrineAnnotationTagValueNode->removeValue('service');
$values = $sensioDoctrineAnnotationTagValueNode->getValues();
$symfonyRouteTagValueNode = $this->symfonyRouteTagValueNodeFactory->createFromItems($values);
// avoid adding this one
if ($node instanceof Class_ && $this->isEmptySensioRoute($values)) {
continue;
}
$phpDocInfo->addTagValueNode($symfonyRouteTagValueNode);
}
$this->renamedClassesDataCollector->addOldToNewClasses([
self::SENSIO_ROUTE_NAME => SymfonyAnnotation::ROUTE,
]);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
return $node;
}
/**
* @param mixed[] $values
*/
private function isEmptySensioRoute(array $values): bool
{
if ($values === []) {
return true;
}
if (count($values) !== 1) {
return false;
}
$singleValue = $values[0];
if (! $singleValue instanceof ArrayItemNode) {
return false;
}
if ($singleValue->key !== null) {
return false;
}
$stringNode = $singleValue->value;
if (! $stringNode instanceof StringNode) {
return false;
}
return $singleValue->value->value === '/';
}
}