-
Notifications
You must be signed in to change notification settings - Fork 72
/
MethodToMethodWithCheckRector.php
214 lines (186 loc) · 7.4 KB
/
MethodToMethodWithCheckRector.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
<?php
declare(strict_types=1);
namespace DrupalRector\Rector\Deprecation;
use DrupalRector\Rector\ValueObject\MethodToMethodWithCheckConfiguration;
use DrupalRector\Services\AddCommentService;
use PhpParser\Node;
use PHPStan\Type\ObjectType;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* Replaces deprecated method calls with a new method.
*
* What is covered:
* - Changes the name of the method.
*
* Improvement opportunities:
* - Checks the variable has a certain class.
*/
class MethodToMethodWithCheckRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var MethodToMethodWithCheckConfiguration[]
*/
private array $configuration;
/**
* @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 MethodToMethodWithCheckConfiguration)) {
throw new \InvalidArgumentException(sprintf('Each configuration item must be an instance of "%s"', MethodToMethodWithCheckConfiguration::class));
}
}
$this->configuration = $configuration;
}
/**
* {@inheritdoc}
*/
public function getNodeTypes(): array
{
return [
Node\Stmt\Expression::class,
Node\Expr\MethodCall::class,
];
}
/**
* {@inheritdoc}
*/
public function refactor(Node $node): ?Node
{
assert($node instanceof Node\Stmt\Expression || $node instanceof Node\Expr\MethodCall);
if (!$node instanceof Node\Expr\MethodCall && !$node->expr instanceof Node\Expr\MethodCall && !($node->expr instanceof Node\Expr\Assign && $node->expr->expr instanceof Node\Expr\MethodCall)) {
return null;
}
foreach ($this->configuration as $configuration) {
if ($node instanceof Node\Expr\MethodCall && $this->getName($node->name) !== $configuration->getDeprecatedMethodName()) {
continue;
}
if ($node instanceof Node\Stmt\Expression && $node->expr instanceof Node\Expr\MethodCall && $this->getName($node->expr->name) !== $configuration->getDeprecatedMethodName()) {
continue;
}
if ($node instanceof Node\Stmt\Expression && $node->expr instanceof Node\Expr\Assign && $node->expr->expr instanceof Node\Expr\MethodCall && $this->getName($node->expr->expr->name) !== $configuration->getDeprecatedMethodName()) {
continue;
}
if ($node instanceof Node\Expr\MethodCall) {
$methodNode = $this->refactorNode($node, null, $configuration);
if (is_null($methodNode)) {
continue;
}
return $methodNode;
}
if ($node->expr instanceof Node\Expr\MethodCall) {
$methodNode = $this->refactorNode($node->expr, $node, $configuration);
if (is_null($methodNode)) {
continue;
}
$node->expr = $methodNode;
} elseif ($node->expr instanceof Node\Expr\Assign && $node->expr->expr instanceof Node\Expr\MethodCall) {
$methodNode = $this->refactorNode($node->expr->expr, $node, $configuration);
if (is_null($methodNode)) {
continue;
}
$node->expr->expr = $methodNode;
}
return $node;
}
return null;
}
public function refactorNode(Node\Expr\MethodCall $node, ?Node\Stmt\Expression $statement, MethodToMethodWithCheckConfiguration $configuration): ?Node\Expr\MethodCall
{
assert($node instanceof Node\Expr\MethodCall);
$callerType = $this->nodeTypeResolver->getType($node->var);
$expectedType = new ObjectType($configuration->getClassName());
$isSuperOf = $expectedType->isSuperTypeOf($callerType);
if ($isSuperOf->yes()) {
$node->name = new Node\Identifier($configuration->getMethodName());
return $node;
}
if ($isSuperOf->maybe()) {
if ($node->var instanceof Node\Expr\Variable) {
$node_var = $node->var->name;
$node_var = "$$node_var";
} elseif ($node->var instanceof Node\Expr\MethodCall) {
$node_var = $node->var->name;
$node_var = "$node_var()";
} else {
throw new ShouldNotHappenException('Unexpected node type: '.get_class($node->var));
}
$className = $configuration->getClassName();
if (!is_null($statement)) {
$this->commentService->addDrupalRectorComment(
$statement,
"Please confirm that `$node_var` is an instance of `$className`. Only the method name and not the class name was checked for this replacement, so this may be a false positive."
);
}
$node->name = new Node\Identifier($configuration->getMethodName());
return $node;
}
return null;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Fixes deprecated MetadataBag::clearCsrfTokenSeed() calls, used in Drupal 8 and 9 deprecations', [
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
$metadata_bag = new \Drupal\Core\Session\MetadataBag(new \Drupal\Core\Site\Settings([]));
$metadata_bag->clearCsrfTokenSeed();
CODE_BEFORE
,
<<<'CODE_AFTER'
$metadata_bag = new \Drupal\Core\Session\MetadataBag(new \Drupal\Core\Site\Settings([]));
$metadata_bag->stampNew();
CODE_AFTER
,
[
new MethodToMethodWithCheckConfiguration(
'Drupal\Core\Session\MetadataBag',
'clearCsrfTokenSeed',
'stampNew'
),
]
),
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
$url = $entity->urlInfo();
CODE_BEFORE
,
<<<'CODE_AFTER'
$url = $entity->toUrl();
CODE_AFTER
,
[
new MethodToMethodWithCheckConfiguration('Drupal\Core\Entity\EntityInterface', 'urlInfo', 'toUrl'),
]
),
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
/* @var \Drupal\node\Entity\Node $node */
$node = \Drupal::entityTypeManager()->getStorage('node')->load(123);
$entity_type = $node->getEntityType();
$entity_type->getLowercaseLabel();
CODE_BEFORE
,
<<<'CODE_AFTER'
/* @var \Drupal\node\Entity\Node $node */
$node = \Drupal::entityTypeManager()->getStorage('node')->load(123);
$entity_type = $node->getEntityType();
$entity_type->getSingularLabel();
CODE_AFTER
,
[
new MethodToMethodWithCheckConfiguration('Drupal\Core\Entity\EntityTypeInterface', 'getLowercaseLabel', 'getSingularLabel'),
]
),
]);
}
}