-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathEventSubscriberInterfaceToAttributeRector.php
More file actions
230 lines (195 loc) · 6.17 KB
/
EventSubscriberInterfaceToAttributeRector.php
File metadata and controls
230 lines (195 loc) · 6.17 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
<?php
declare(strict_types=1);
namespace Rector\Doctrine\Bundle210\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Doctrine\Enum\DoctrineClass;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see https://github.com/doctrine/DoctrineBundle/pull/1592
*
* @see \Rector\Doctrine\Tests\Bundle210\Rector\Class_\EventSubscriberInterfaceToAttributeRector\EventSubscriberInterfaceToAttributeRectorTest
*/
final class EventSubscriberInterfaceToAttributeRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ReflectionProvider $reflectionProvider,
) {
}
public function provideMinPhpVersion(): int
{
return PhpVersionFeature::ATTRIBUTES;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace EventSubscriberInterface with #[AsDoctrineListener] attribute',
[
new CodeSample(
<<<'CODE_SAMPLE'
use Doctrine\ORM\Event\PrePersistEventArgs;
use Doctrine\ORM\Event\PostUpdateEventArgs;
use Doctrine\Common\EventSubscriberInterface;
use Doctrine\ORM\Events;
class MyEventSubscriber implements EventSubscriberInterface
{
public function getSubscribedEvents()
{
return array(
Events::postUpdate,
Events::prePersist,
);
}
public function postUpdate(PostUpdateEventArgs $args)
{
// ...
}
public function prePersist(PrePersistEventArgs $args)
{
// ...
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\ORM\Event\PrePersistEventArgs;
use Doctrine\ORM\Event\PostUpdateEventArgs;
use Doctrine\ORM\Events;
#[AsDoctrineListener(event: Events::postUpdate)]
#[AsDoctrineListener(event: Events::prePersist)]
class MyEventSubscriber
{
public function postUpdate(PostUpdateEventArgs $args)
{
// ...
}
public function prePersist(PrePersistEventArgs $args)
{
// ...
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->reflectionProvider->hasClass(DoctrineClass::AS_DOCTRINE_LISTENER_ATTRIBUTE)) {
return null;
}
if (! $this->hasImplements($node, DoctrineClass::EVENT_SUBSCRIBER)
&& ! $this->hasImplements($node, DoctrineClass::EVENT_SUBSCRIBER_INTERFACE)) {
return null;
}
foreach ($node->stmts as $key => $classStmt) {
if (! $classStmt instanceof ClassMethod) {
continue;
}
if (! $this->isName($classStmt, 'getSubscribedEvents')) {
continue;
}
$getSubscribedEventsClassMethod = $classStmt;
if ($getSubscribedEventsClassMethod->stmts === []) {
continue;
}
// $firstStmt = $getSubscribedEventsClassMethod->stmts[0];
// if ($firstStmt instanceof Return_ && $firstStmt->expr instanceof Array_
// ) {
$this->refactorSubscriberArrayToClassAttributes($node, $getSubscribedEventsClassMethod);
// }
$this->removeImplements(
$node,
[DoctrineClass::EVENT_SUBSCRIBER, DoctrineClass::EVENT_SUBSCRIBER_INTERFACE]
);
// remove method
unset($node->stmts[$key]);
return $node;
}
return null;
}
private function refactorSubscriberArrayToClassAttributes(
Class_ $class,
ClassMethod $getSubscribedEventsClassMethod
): void {
foreach ((array) $getSubscribedEventsClassMethod->stmts as $stmt) {
if (! $stmt instanceof Return_) {
continue;
}
if (! $stmt->expr instanceof Array_) {
continue;
}
$arguments = $this->extractArrayItemsToExprs($stmt->expr);
$this->addClassAttribute($class, $arguments);
}
}
/**
* @return array<Expr>
*/
private function extractArrayItemsToExprs(Array_ $array): array
{
$arguments = [];
foreach ($array->items as $item) {
$arguments[] = $item->value;
}
return $arguments;
}
/**
* @param array<Expr> $arguments
*/
private function addClassAttribute(Class_ $class, array $arguments): void
{
foreach ($arguments as $argument) {
$class->attrGroups[] = new AttributeGroup([new Attribute(
new FullyQualified(DoctrineClass::AS_DOCTRINE_LISTENER_ATTRIBUTE),
[new Arg($argument, false, false, [], new Identifier('event'))]
)]);
}
}
private function hasImplements(Class_ $class, string $interfaceFQN): bool
{
foreach ($class->implements as $implement) {
if ($this->isName($implement, $interfaceFQN)) {
return true;
}
}
return false;
}
/**
* @param array<string> $interfaceFQNS
*/
private function removeImplements(Class_ $class, array $interfaceFQNS): void
{
foreach ($class->implements as $key => $implement) {
if (! $this->isNames($implement, $interfaceFQNS)) {
continue;
}
unset($class->implements[$key]);
}
}
}