-
-
Notifications
You must be signed in to change notification settings - Fork 670
/
CallableThisArrayToAnonymousFunctionRector.php
161 lines (133 loc) · 3.93 KB
/
CallableThisArrayToAnonymousFunctionRector.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php declare(strict_types=1);
namespace Rector\CodeQuality\Rector\Array_;
use PhpParser\Node;
use Rector\NodeContainer\ParsedNodesByType;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* @see https://www.php.net/manual/en/language.types.callable.php#117260
* @see https://3v4l.org/MsMbQ
* @see https://3v4l.org/KM1Ji
*/
final class CallableThisArrayToAnonymousFunctionRector extends AbstractRector
{
/**
* @var ParsedNodesByType
*/
private $parsedNodesByType;
public function __construct(ParsedNodesByType $parsedNodesByType)
{
$this->parsedNodesByType = $parsedNodesByType;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Convert [$this, "method"] to proper anonymous function', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$values = [1, 5, 3];
usort($values, [$this, 'compareSize']);
return $values;
}
private function compareSize($first, $second)
{
return $first <=> $second;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$values = [1, 5, 3];
usort($values, function ($first, $second) {
return $this->compareSize($first, $second);
});
return $values;
}
private function compareSize($first, $second)
{
return $first <=> $second;
}
}
CODE_SAMPLE
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Node\Expr\Array_::class];
}
/**
* @param Node\Expr\Array_ $node
*/
public function refactor(Node $node): ?Node
{
if (count($node->items) !== 2) {
return null;
}
// is callable
$objectVariable = $node->items[0]->value;
$classMethod = $this->matchCallableMethod($objectVariable, $node->items[1]->value);
if ($classMethod === null) {
return null;
}
$anonymousFunction = new Node\Expr\Closure();
$anonymousFunction->params = $classMethod->params;
$innerMethodCall = new Node\Expr\MethodCall($objectVariable, $classMethod->name);
$innerMethodCall->args = $this->convertParamsToArgs($classMethod->params);
if ($classMethod->returnType) {
$anonymousFunction->returnType = $classMethod->returnType;
}
$anonymousFunction->stmts[] = new Node\Stmt\Return_($innerMethodCall);
if ($objectVariable instanceof Node\Expr\Variable) {
if (! $this->isName($objectVariable, 'this')) {
$anonymousFunction->uses[] = new Node\Expr\ClosureUse($objectVariable);
}
}
return $anonymousFunction;
}
/**
* @param Node\Param[] $params
* @return Node\Arg[]
*/
private function convertParamsToArgs(array $params): array
{
$args = [];
foreach ($params as $key => $param) {
$args[$key] = new Node\Arg($param->var);
}
return $args;
}
private function matchCallableMethod(Node\Expr $objectExpr, Node\Expr $methodExpr): ?Node\Stmt\ClassMethod
{
$methodName = $this->getValue($methodExpr);
foreach ($this->getTypes($objectExpr) as $type) {
$class = $this->parsedNodesByType->findClass($type);
if ($class === null) {
continue;
}
$classMethod = $class->getMethod($methodName);
if ($classMethod === null) {
continue;
}
if ($this->isName($objectExpr, 'this')) {
return $classMethod;
}
if ($classMethod->isPublic()) {
return $classMethod;
}
return null;
}
return null;
}
}