-
-
Notifications
You must be signed in to change notification settings - Fork 680
/
ArgumentRemoverRector.php
147 lines (145 loc) · 5.02 KB
/
ArgumentRemoverRector.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
<?php
declare (strict_types=1);
namespace Rector\Removing\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\VariadicPlaceholder;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\Removing\ValueObject\ArgumentRemover;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix202409\Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Removing\Rector\ClassMethod\ArgumentRemoverRector\ArgumentRemoverRectorTest
*/
final class ArgumentRemoverRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @readonly
* @var \Rector\PhpParser\Node\Value\ValueResolver
*/
private $valueResolver;
/**
* @var ArgumentRemover[]
*/
private $removedArguments = [];
/**
* @var bool
*/
private $hasChanged = \false;
public function __construct(ValueResolver $valueResolver)
{
$this->valueResolver = $valueResolver;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Removes defined arguments in defined methods and their calls.', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
$someObject = new SomeClass;
$someObject->someMethod(true);
CODE_SAMPLE
, <<<'CODE_SAMPLE'
$someObject = new SomeClass;
$someObject->someMethod();
CODE_SAMPLE
, [new ArgumentRemover('ExampleClass', 'someMethod', 0, [\true])])]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [MethodCall::class, StaticCall::class, ClassMethod::class];
}
/**
* @param MethodCall|StaticCall|ClassMethod $node
* @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Stmt\ClassMethod|null
*/
public function refactor(Node $node)
{
$this->hasChanged = \false;
foreach ($this->removedArguments as $removedArgument) {
if (!$this->isName($node->name, $removedArgument->getMethod())) {
continue;
}
if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($node, $removedArgument->getObjectType())) {
continue;
}
$this->processPosition($node, $removedArgument);
}
if ($this->hasChanged) {
return $node;
}
return null;
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration) : void
{
Assert::allIsAOf($configuration, ArgumentRemover::class);
$this->removedArguments = $configuration;
}
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall $node
*/
private function processPosition($node, ArgumentRemover $argumentRemover) : void
{
if ($argumentRemover->getValue() === null) {
if ($node instanceof MethodCall || $node instanceof StaticCall) {
unset($node->args[$argumentRemover->getPosition()]);
} else {
unset($node->params[$argumentRemover->getPosition()]);
}
return;
}
$match = $argumentRemover->getValue();
if (isset($match['name'])) {
$this->removeByName($node, $argumentRemover->getPosition(), $match['name']);
return;
}
// only argument specific value can be removed
if ($node instanceof ClassMethod) {
return;
}
if (!isset($node->args[$argumentRemover->getPosition()])) {
return;
}
if ($this->isArgumentValueMatch($node->args[$argumentRemover->getPosition()], $match)) {
$this->hasChanged = \true;
unset($node->args[$argumentRemover->getPosition()]);
}
}
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall $node
*/
private function removeByName($node, int $position, string $name) : void
{
if ($node instanceof MethodCall || $node instanceof StaticCall) {
if (isset($node->args[$position]) && $this->isName($node->args[$position], $name)) {
unset($node->args[$position]);
}
return;
}
if (!(isset($node->params[$position]) && $this->isName($node->params[$position], $name))) {
return;
}
unset($node->params[$position]);
}
/**
* @param mixed[] $values
* @param \PhpParser\Node\Arg|\PhpParser\Node\VariadicPlaceholder $arg
*/
private function isArgumentValueMatch($arg, array $values) : bool
{
if (!$arg instanceof Arg) {
return \false;
}
$nodeValue = $this->valueResolver->getValue($arg->value);
return \in_array($nodeValue, $values, \true);
}
}