Repository navigation
Expand file tree
/
Copy pathMethodCallToFuncCallRector.php
More file actions
105 lines (91 loc) · 2.83 KB
/
Copy pathMethodCallToFuncCallRector.php
File metadata and controls
105 lines (91 loc) · 2.83 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
<?php
declare(strict_types=1);
namespace Rector\Transform\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\ObjectType;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\ConfiguredCodeSample;
use Rector\RuleDoc\RuleDefinition;
use Rector\Transform\ValueObject\MethodCallToFuncCall;
use Webmozart\Assert\Assert;
/**
* @note used extensively https://github.com/search?q=MethodCallToFuncCallRector%3A%3Aclass&type=code
* @see \Rector\Tests\Transform\Rector\MethodCall\MethodCallToFuncCallRector\MethodCallToFuncCallRectorTest
*/
final class MethodCallToFuncCallRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var array<string, MethodCallToFuncCall[]>
*/
private array $methodCallsToFuncCallsByMethod = [];
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change method call to function call', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public function show()
{
return $this->render('some_template');
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
public function show()
{
return view('some_template');
}
}
CODE_SAMPLE
,
[new MethodCallToFuncCall('SomeClass', 'render', 'view')]
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}
/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
if ($node->isFirstClassCallable()) {
return null;
}
$methodName = $this->getName($node->name);
if ($methodName === null) {
return null;
}
foreach ($this->methodCallsToFuncCallsByMethod[strtolower($methodName)] ?? [] as $methodCallToFuncCall) {
if (! $this->isObjectType($node->var, new ObjectType($methodCallToFuncCall->getObjectType()))) {
continue;
}
return new FuncCall(new FullyQualified($methodCallToFuncCall->getFunctionName()), $node->getArgs());
}
return null;
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allIsInstanceOf($configuration, MethodCallToFuncCall::class);
$this->methodCallsToFuncCallsByMethod = [];
foreach ($configuration as $methodCallToFuncCall) {
$this->methodCallsToFuncCallsByMethod[strtolower($methodCallToFuncCall->getMethodName())][] = $methodCallToFuncCall;
}
}
}