-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathRemoveAssignmentFromVoidMethodRector.php
More file actions
113 lines (100 loc) · 3.2 KB
/
RemoveAssignmentFromVoidMethodRector.php
File metadata and controls
113 lines (100 loc) · 3.2 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
<?php
declare(strict_types=1);
namespace Cake\Upgrade\Rector\Cake6;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\Expression;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* Removes variable assignments from method calls that now return void.
*
* @see https://github.com/cakephp/cakephp/pull/19220
* @see https://github.com/cakephp/cakephp/pull/19243
*/
final class RemoveAssignmentFromVoidMethodRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var array<\Cake\Upgrade\Rector\Cake6\VoidMethod>
*/
private array $voidMethods = [];
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove variable assignment from void method calls',
[
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
$result = $request->allowMethod(['POST']);
$result = Configure::load('app');
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$request->allowMethod(['POST']);
Configure::load('app');
CODE_SAMPLE
,
[
new VoidMethod('Cake\Http\ServerRequest', 'allowMethod'),
new VoidMethod('Cake\Core\Configure', 'load'),
],
),
],
);
}
/**
* @return array<class-string<\PhpParser\Node>>
*/
public function getNodeTypes(): array
{
return [Expression::class];
}
/**
* @param \PhpParser\Node\Stmt\Expression $node
*/
public function refactor(Node $node): ?Node
{
if (!$node->expr instanceof Assign) {
return null;
}
$assign = $node->expr;
// Handle instance method calls
if ($assign->expr instanceof MethodCall) {
$methodCall = $assign->expr;
foreach ($this->voidMethods as $voidMethod) {
if (!$this->isObjectType($methodCall->var, $voidMethod->getObjectType())) {
continue;
}
if (!$this->isName($methodCall->name, $voidMethod->getMethod())) {
continue;
}
return new Expression($methodCall);
}
}
// Handle static method calls
if ($assign->expr instanceof StaticCall) {
$staticCall = $assign->expr;
foreach ($this->voidMethods as $voidMethod) {
if (!$this->isName($staticCall->class, $voidMethod->getClass())) {
continue;
}
if (!$this->isName($staticCall->name, $voidMethod->getMethod())) {
continue;
}
return new Expression($staticCall);
}
}
return null;
}
/**
* @param list<mixed> $configuration
*/
public function configure(array $configuration): void
{
$this->voidMethods = $configuration;
}
}