forked from palantirnet/drupal-rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssertLegacyTraitRector.php
280 lines (257 loc) · 9.21 KB
/
AssertLegacyTraitRector.php
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
declare(strict_types=1);
namespace DrupalRector\Drupal9\Rector\Deprecation;
use DrupalRector\Drupal9\Rector\ValueObject\AssertLegacyTraitConfiguration;
use DrupalRector\Services\AddCommentService;
use DrupalRector\Utility\GetDeclaringSourceTrait;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\VariadicPlaceholder;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
class AssertLegacyTraitRector extends AbstractRector implements ConfigurableRectorInterface
{
use GetDeclaringSourceTrait;
/**
* @var AssertLegacyTraitConfiguration[]
*/
private array $assertLegacyTraitMethods;
/**
* @var AddCommentService
*/
private AddCommentService $commentService;
public function __construct(AddCommentService $commentService)
{
$this->commentService = $commentService;
}
public function configure(array $configuration): void
{
foreach ($configuration as $value) {
if (!($value instanceof AssertLegacyTraitConfiguration)) {
throw new \InvalidArgumentException(sprintf('Each configuration item must be an instance of "%s"', AssertLegacyTraitConfiguration::class));
}
}
$this->assertLegacyTraitMethods = $configuration;
}
public function getNodeTypes(): array
{
return [
Node\Stmt\Expression::class,
];
}
/**
* @param string $method
* @param array<Arg|VariadicPlaceholder> $args
*
* @return Node\Expr\MethodCall
*/
protected function createAssertSessionMethodCall(string $method, array $args): Node\Expr\MethodCall
{
$assertSessionNode = $this->nodeFactory->createLocalMethodCall('assertSession');
return $this->nodeFactory->createMethodCall($assertSessionNode, $method, $args);
}
public function refactor(Node $node): ?Node
{
assert($node instanceof Node\Stmt\Expression);
if ($node->expr instanceof Node\Expr\Assign && $node->expr->expr instanceof Node\Expr\MethodCall) {
$expr = $node->expr->expr;
} elseif ($node->expr instanceof Node\Expr\MethodCall) {
$expr = $node->expr;
} else {
return null;
}
foreach ($this->assertLegacyTraitMethods as $configuration) {
if ($this->getName($expr->name) !== $configuration->getDeprecatedMethodName()) {
continue;
}
if ($this->getDeclaringSource($expr) !== $configuration->getDeclaringSource()) {
continue;
}
if ($configuration->getComment() !== '') {
$this->commentService->addDrupalRectorComment($node, $configuration->getComment());
}
$args = $expr->args;
if ($configuration->isProcessFirstArgumentOnly()) {
$args = [$expr->args[0]];
}
if ($configuration->getPrependArgument() !== null) {
array_unshift($args, $this->nodeFactory->createArg('X-Drupal-Cache-Tags'));
}
if ($configuration->isAssertSessionMethod()) {
$newExpr = $this->createAssertSessionMethodCall($configuration->getMethodName(), $args);
} else {
$newExpr = $this->nodeFactory->createLocalMethodCall($configuration->getMethodName(), $args);
}
if ($node->expr instanceof Node\Expr\MethodCall) {
$node->expr = $newExpr;
} elseif ($node->expr instanceof Node\Expr\Assign && $node->expr->expr instanceof Node\Expr\MethodCall) {
$node->expr->expr = $newExpr;
}
return $node;
}
return null;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Fixes deprecated AssertLegacyTrait::METHOD() calls', [
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
$this->assertLinkByHref('user/1/translations');
CODE_BEFORE
,
<<<'CODE_AFTER'
$this->assertSession()->linkByHrefExists('user/1/translations');
CODE_AFTER
,
[
new AssertLegacyTraitConfiguration('assertLinkByHref', 'linkByHrefExists'),
]
),
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
$this->assertLink('Anonymous comment title');
CODE_BEFORE
,
<<<'CODE_AFTER'
$this->assertSession()->linkExists('Anonymous comment title');
CODE_AFTER
,
[
new AssertLegacyTraitConfiguration('assertLink', 'linkExists'),
]
),
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
$this->assertNoEscaped('<div class="escaped">');
CODE_BEFORE
,
<<<'CODE_AFTER'
$this->assertSession()->assertNoEscaped('<div class="escaped">');
CODE_AFTER
,
[
new AssertLegacyTraitConfiguration('assertNoEscaped', 'assertNoEscaped'),
]
),
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
$this->assertNoFieldChecked('edit-settings-view-mode', 'default');
CODE_BEFORE
,
<<<'CODE_AFTER'
$this->assertSession()->checkboxNotChecked('edit-settings-view-mode', 'default');
CODE_AFTER
,
[
new AssertLegacyTraitConfiguration('assertNoFieldChecked', 'checkboxNotChecked'),
]
),
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
$this->assertNoField('files[upload]', 'Found file upload field.');
CODE_BEFORE
,
<<<'CODE_AFTER'
$this->assertSession()->fieldNotExists('files[upload]', 'Found file upload field.');
CODE_AFTER
,
[
new AssertLegacyTraitConfiguration('assertNoField', 'fieldNotExists', 'Change assertion to buttonExists() if checking for a button.'),
]
),
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
$this->assertNoLinkByHref('user/2/translations');
CODE_BEFORE
,
<<<'CODE_AFTER'
$this->assertSession()->linkByHrefNotExists('user/2/translations');
CODE_AFTER
,
[
new AssertLegacyTraitConfiguration('assertNoLinkByHref', 'linkByHrefNotExists'),
]
),
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
$this->assertNoLink('Anonymous comment title');
CODE_BEFORE
,
<<<'CODE_AFTER'
$this->assertSession()->linkNotExists('Anonymous comment title');
CODE_AFTER
,
[
new AssertLegacyTraitConfiguration('assertNoLink', 'linkNotExists'),
]
),
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
$this->assertNoOption('edit-settings-view-mode', 'default');
CODE_BEFORE
,
<<<'CODE_AFTER'
$this->assertSession()->optionNotExists('edit-settings-view-mode', 'default');
CODE_AFTER
,
[
new AssertLegacyTraitConfiguration('assertNoOption', 'optionNotExists'),
]
),
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
$this->assertNoPattern('|<h4[^>]*></h4>|', 'No empty H4 element found.');
CODE_BEFORE
,
<<<'CODE_AFTER'
$this->assertSession()->responseNotMatches('|<h4[^>]*></h4>|', 'No empty H4 element found.');
CODE_AFTER
,
[
new AssertLegacyTraitConfiguration('assertNoPattern', 'responseNotMatches'),
]
),
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
$this->assertPattern('|<h4[^>]*></h4>|', 'No empty H4 element found.');
CODE_BEFORE
,
<<<'CODE_AFTER'
$this->assertSession()->responseMatches('|<h4[^>]*></h4>|', 'No empty H4 element found.');
CODE_AFTER
,
[
new AssertLegacyTraitConfiguration('assertPattern', 'responseMatches'),
]
),
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
$this->assertNoRaw('bartik/logo.svg');
CODE_BEFORE
,
<<<'CODE_AFTER'
$this->assertSession()->responseNotContains('bartik/logo.svg');
CODE_AFTER
,
[
new AssertLegacyTraitConfiguration('assertNoRaw', 'responseNotContains', '', true, true),
]
),
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
$this->assertRaw('bartik/logo.svg');
CODE_BEFORE
,
<<<'CODE_AFTER'
$this->assertSession()->responseContains('bartik/logo.svg');
CODE_AFTER
,
[
new AssertLegacyTraitConfiguration('assertRaw', 'responseNotContains', '', true, true),
]
),
]);
}
}