-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRemoveIntermediaryMethodRector.php
137 lines (120 loc) · 4.33 KB
/
RemoveIntermediaryMethodRector.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
<?php
declare(strict_types=1);
namespace Rector\CakePHP\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use Rector\CakePHP\ValueObject\RemoveIntermediaryMethod;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Defluent\NodeAnalyzer\FluentChainMethodCallNodeAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
/**
* @see https://book.cakephp.org/3.0/en/appendices/3-4-migration-guide.html#deprecated-combined-get-set-methods
* @see https://github.com/cakephp/cakephp/commit/326292688c5e6d08945a3cafa4b6ffb33e714eea#diff-e7c0f0d636ca50a0350e9be316d8b0f9
*
* @see \Rector\CakePHP\Tests\Rector\MethodCall\ModalToGetSetRector\ModalToGetSetRectorTest
*/
final class RemoveIntermediaryMethodRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var string
*/
final public const REMOVE_INTERMEDIARY_METHOD = 'remove_intermediary_method';
/**
* @var RemoveIntermediaryMethod[]
*/
private array $removeIntermediaryMethod = [];
public function __construct(
private readonly FluentChainMethodCallNodeAnalyzer $fluentChainMethodCallNodeAnalyzer
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Removes an intermediary method call for when a higher level API is added.',
[
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
$users = $this->getTableLocator()->get('Users');
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$users = $this->fetchTable('Users');
CODE_SAMPLE
,
[
self::REMOVE_INTERMEDIARY_METHOD => [
new RemoveIntermediaryMethod('getTableLocator', 'get', 'fetchTable'),
],
]
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}
/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
$removeIntermediaryMethod = $this->matchTypeAndMethodName($node);
if (! $removeIntermediaryMethod instanceof RemoveIntermediaryMethod) {
return null;
}
/** @var MethodCall $var */
$var = $node->var;
$target = $var->var;
return new MethodCall($target, $removeIntermediaryMethod->getFinalMethod(), $node->args);
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
$removeIntermediaryMethods = $configuration[self::REMOVE_INTERMEDIARY_METHOD] ?? $configuration;
Assert::isArray($removeIntermediaryMethods);
Assert::allIsAOf($removeIntermediaryMethods, RemoveIntermediaryMethod::class);
$this->removeIntermediaryMethod = $removeIntermediaryMethods;
}
private function matchTypeAndMethodName(MethodCall $methodCall): ?RemoveIntermediaryMethod
{
$rootMethodCall = $this->fluentChainMethodCallNodeAnalyzer->resolveRootMethodCall($methodCall);
if (! $rootMethodCall instanceof MethodCall) {
return null;
}
if (! $rootMethodCall->var instanceof Variable) {
return null;
}
if (! $this->nodeNameResolver->isName($rootMethodCall->var, 'this')) {
return null;
}
/** @var MethodCall $var */
$var = $methodCall->var;
if (
(! $methodCall->name instanceof Identifier) ||
(! $var->name instanceof Identifier)
) {
return null;
}
foreach ($this->removeIntermediaryMethod as $singleRemoveIntermediaryMethod) {
if (! $this->isName($methodCall->name, $singleRemoveIntermediaryMethod->getSecondMethod())) {
continue;
}
if (! $this->isName($var->name, $singleRemoveIntermediaryMethod->getFirstMethod())) {
continue;
}
return $singleRemoveIntermediaryMethod;
}
return null;
}
}