-
-
Notifications
You must be signed in to change notification settings - Fork 699
/
Copy pathReplaceArgumentDefaultValueRector.php
108 lines (106 loc) · 4 KB
/
ReplaceArgumentDefaultValueRector.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
<?php
declare (strict_types=1);
namespace Rector\Arguments\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Arguments\ArgumentDefaultValueReplacer;
use Rector\Arguments\ValueObject\ReplaceArgumentDefaultValue;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix202501\Webmozart\Assert\Assert;
/**
* @api used in rector-symfony
* @see \Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\ReplaceArgumentDefaultValueRectorTest
*/
final class ReplaceArgumentDefaultValueRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @readonly
*/
private ArgumentDefaultValueReplacer $argumentDefaultValueReplacer;
/**
* @var ReplaceArgumentDefaultValue[]
*/
private array $replaceArgumentDefaultValues = [];
public function __construct(ArgumentDefaultValueReplacer $argumentDefaultValueReplacer)
{
$this->argumentDefaultValueReplacer = $argumentDefaultValueReplacer;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Replaces defined map of arguments in defined methods and their calls.', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
$someObject = new SomeClass;
$someObject->someMethod(SomeClass::OLD_CONSTANT);
CODE_SAMPLE
, <<<'CODE_SAMPLE'
$someObject = new SomeClass;
$someObject->someMethod(false);
CODE_SAMPLE
, [new ReplaceArgumentDefaultValue('SomeClass', 'someMethod', 0, 'SomeClass::OLD_CONSTANT', \false)])]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [MethodCall::class, StaticCall::class, ClassMethod::class, New_::class];
}
/**
* @param MethodCall|StaticCall|ClassMethod|New_ $node
* @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\New_|null
*/
public function refactor(Node $node)
{
$hasChanged = \false;
if ($node instanceof New_) {
return $this->refactorNew($node);
}
$nodeName = $this->getName($node->name);
if ($nodeName === null) {
return null;
}
foreach ($this->replaceArgumentDefaultValues as $replaceArgumentDefaultValue) {
if (!$this->nodeNameResolver->isStringName($nodeName, $replaceArgumentDefaultValue->getMethod())) {
continue;
}
if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($node, $replaceArgumentDefaultValue->getObjectType())) {
continue;
}
$replacedNode = $this->argumentDefaultValueReplacer->processReplaces($node, $replaceArgumentDefaultValue);
if ($replacedNode instanceof Node) {
$hasChanged = \true;
}
}
if ($hasChanged) {
return $node;
}
return null;
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration) : void
{
Assert::allIsAOf($configuration, ReplaceArgumentDefaultValue::class);
$this->replaceArgumentDefaultValues = $configuration;
}
private function refactorNew(New_ $new) : ?New_
{
foreach ($this->replaceArgumentDefaultValues as $replaceArgumentDefaultValue) {
if ($replaceArgumentDefaultValue->getMethod() !== MethodName::CONSTRUCT) {
continue;
}
if (!$this->isObjectType($new, $replaceArgumentDefaultValue->getObjectType())) {
continue;
}
return $this->argumentDefaultValueReplacer->processReplaces($new, $replaceArgumentDefaultValue);
}
return null;
}
}