forked from rectorphp/rector-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageSubscriberInterfaceToAttributeRector.php
More file actions
239 lines (205 loc) · 6.81 KB
/
MessageSubscriberInterfaceToAttributeRector.php
File metadata and controls
239 lines (205 loc) · 6.81 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
238
239
<?php
declare(strict_types=1);
namespace Rector\Symfony\Symfony62\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\Symfony\Helper\MessengerHelper;
use Rector\Symfony\NodeAnalyzer\ClassAnalyzer;
use Rector\Symfony\NodeManipulator\ClassManipulator;
use Rector\ValueObject\MethodName;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see Rector\Symfony\Tests\Symfony62\Rector\Class_\MessageSubscriberInterfaceToAttributeRector\MessageSubscriberInterfaceToAttributeRectorTest
*/
final class MessageSubscriberInterfaceToAttributeRector extends AbstractRector implements MinPhpVersionInterface
{
private Class_ $subscriberClass;
private string $newInvokeMethodName;
public function __construct(
private readonly MessengerHelper $messengerHelper,
private readonly ClassManipulator $classManipulator,
private readonly ClassAnalyzer $classAnalyzer,
private readonly ValueResolver $valueResolver,
) {
}
public function provideMinPhpVersion(): int
{
return PhpVersionFeature::ATTRIBUTES;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace MessageSubscriberInterface with AsMessageHandler attribute(s)',
[
new CodeSample(
<<<'CODE_SAMPLE'
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
class SmsNotificationHandler implements MessageSubscriberInterface
{
public function __invoke(SmsNotification $message)
{
// ...
}
public function handleOtherSmsNotification(OtherSmsNotification $message)
{
// ...
}
public static function getHandledMessages(): iterable
{
// handle this message on __invoke
yield SmsNotification::class;
// also handle this message on handleOtherSmsNotification
yield OtherSmsNotification::class => [
'method' => 'handleOtherSmsNotification',
'priority' => 0,
'bus' => 'messenger.bus.default',
];
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
class SmsNotificationHandler
{
#[AsMessageHandler]
public function handleSmsNotification(SmsNotification $message)
{
// ...
}
#[AsMessageHandler(priority: 0, bus: 'messenger.bus.default']
public function handleOtherSmsNotification(OtherSmsNotification $message)
{
// ...
}
}
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->classAnalyzer->hasImplements($node, MessengerHelper::MESSAGE_SUBSCRIBER_INTERFACE)) {
return null;
}
$this->subscriberClass = $node;
$getHandledMessagesClassMethod = $node->getMethod('getHandledMessages');
if (! $getHandledMessagesClassMethod instanceof ClassMethod) {
return null;
}
$stmts = (array) $getHandledMessagesClassMethod->stmts;
if ($stmts === []) {
return null;
}
if (
($stmts[0] instanceof Expression) &&
$stmts[0]->expr instanceof Yield_
) {
$this->handleYields($stmts);
}
$this->classManipulator->removeImplements($node, [MessengerHelper::MESSAGE_SUBSCRIBER_INTERFACE]);
unset($node->stmts[$getHandledMessagesClassMethod->getAttribute(AttributeKey::STMT_KEY)]);
return $node;
}
/**
* @param array<int, Node\Stmt> $expressions
*/
private function handleYields(array $expressions): void
{
foreach ($expressions as $expression) {
if (! $expression instanceof Expression ||
! $expression->expr instanceof Yield_
) {
continue;
}
$method = MethodName::INVOKE;
$arguments = [];
if ($expression->expr->key instanceof ClassConstFetch) {
$array = $expression->expr->value;
if (! $array instanceof Array_) {
continue;
}
$arguments = $this->parseArguments($array, $method);
$this->addAttribute($method, $arguments);
continue;
}
$value = $expression->expr->value;
if (
(! $value instanceof ClassConstFetch) ||
! $value->class instanceof Name
) {
continue;
}
$classParts = $value->class->getParts();
$this->newInvokeMethodName = 'handle' . end($classParts);
$this->addAttribute($method, $arguments);
}
}
/**
* @return array<string, mixed>
*/
private function parseArguments(Array_ $array, string &$method): array
{
foreach ($array->items as $item) {
if (
! $item instanceof ArrayItem ||
! $item->key instanceof Expr ||
! $item->value instanceof Expr
) {
continue;
}
$key = (string) $this->valueResolver->getValue($item->key);
$value = $this->valueResolver->getValue($item->value);
if ($key === 'method') {
$method = $value;
continue;
}
$arguments[$key] = $value;
}
return $arguments ?? [];
}
/**
* @param array<string, mixed> $arguments
*/
private function addAttribute(string $classMethodName, array $arguments): void
{
$classMethod = $this->subscriberClass->getMethod($classMethodName);
if (! $classMethod instanceof ClassMethod) {
return;
}
if ($classMethodName === MethodName::INVOKE) {
$this->renameInvoke($classMethod);
}
$this->messengerHelper->addAttribute($classMethod, $arguments);
}
private function renameInvoke(ClassMethod $classMethod): void
{
$classMethod->name = new Identifier($this->newInvokeMethodName);
}
}