forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToStringToMethodCallRector.php
117 lines (99 loc) · 3.07 KB
/
ToStringToMethodCallRector.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
<?php
declare(strict_types=1);
namespace Rector\Transform\Rector\String_;
use PhpParser\Node;
use PhpParser\Node\Expr\Cast\String_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PHPStan\Type\ObjectType;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Transform\Rector\String_\ToStringToMethodCallRector\ToStringToMethodCallRectorTest
*/
final class ToStringToMethodCallRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @api
* @var string
*/
public const METHOD_NAMES_BY_TYPE = 'method_names_by_type';
/**
* @var array<string, string>
*/
private $methodNamesByType = [];
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Turns defined code uses of "__toString()" method to specific method calls.', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
$someValue = new SomeObject;
$result = (string) $someValue;
$result = $someValue->__toString();
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$someValue = new SomeObject;
$result = $someValue->getPath();
$result = $someValue->getPath();
CODE_SAMPLE
,
[
self::METHOD_NAMES_BY_TYPE => [
'SomeObject' => 'getPath',
],
]
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [String_::class, MethodCall::class];
}
/**
* @param String_|MethodCall $node
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof String_) {
return $this->processStringNode($node);
}
return $this->processMethodCall($node);
}
/**
* @param array<string, array<string, string>> $configuration
*/
public function configure(array $configuration): void
{
$this->methodNamesByType = $configuration[self::METHOD_NAMES_BY_TYPE] ?? [];
}
private function processStringNode(String_ $string): ?Node
{
foreach ($this->methodNamesByType as $type => $methodName) {
if (! $this->isObjectType($string->expr, new ObjectType($type))) {
continue;
}
return $this->nodeFactory->createMethodCall($string->expr, $methodName);
}
return null;
}
private function processMethodCall(MethodCall $methodCall): ?Node
{
foreach ($this->methodNamesByType as $type => $methodName) {
if (! $this->isObjectType($methodCall->var, new ObjectType($type))) {
continue;
}
if (! $this->isName($methodCall->name, '__toString')) {
continue;
}
$methodCall->name = new Identifier($methodName);
return $methodCall;
}
return null;
}
}