forked from rectorphp/rector-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayToFirstClassCallableRector.php
More file actions
195 lines (164 loc) · 5.92 KB
/
ArrayToFirstClassCallableRector.php
File metadata and controls
195 lines (164 loc) · 5.92 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
declare(strict_types=1);
namespace Rector\Php81\Rector\Array_;
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\VariadicPlaceholder;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodMatcher;
use Rector\NodeCollector\ValueObject\ArrayCallable;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\ValueObject\PhpVersion;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see RFC https://wiki.php.net/rfc/first_class_callable_syntax
* @see \Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\ArrayToFirstClassCallableRectorTest
*/
class ArrayToFirstClassCallableRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ArrayCallableMethodMatcher $arrayCallableMethodMatcher,
private readonly ReflectionProvider $reflectionProvider,
private readonly ReflectionResolver $reflectionResolver,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Upgrade array callable to first class callable', [
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run()
{
$name = [$this, 'name'];
}
public function name()
{
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run()
{
$name = $this->name(...);
}
public function name()
{
}
}
CODE_SAMPLE
,
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Array_::class];
}
/**
* @param Array_ $node
*/
public function refactor(Node $node): StaticCall|MethodCall|null
{
if ($node->getAttribute(AttributeKey::IS_INSIDE_SYMFONY_PHP_CLOSURE)) {
return null;
}
$scope = ScopeFetcher::fetch($node);
$arrayCallable = $this->arrayCallableMethodMatcher->match($node, $scope);
if (! $arrayCallable instanceof ArrayCallable) {
return null;
}
$callerExpr = $arrayCallable->getCallerExpr();
if (! $callerExpr instanceof Variable && ! $callerExpr instanceof PropertyFetch && ! $callerExpr instanceof ClassConstFetch) {
return null;
}
if ($node->getAttribute(AttributeKey::IS_CLASS_CONST_VALUE)) {
return null;
}
if ($node->getAttribute(AttributeKey::IS_DEFAULT_PROPERTY_VALUE)) {
return null;
}
if ($node->getAttribute(AttributeKey::IS_PARAM_DEFAULT)) {
return null;
}
$args = [new VariadicPlaceholder()];
if ($callerExpr instanceof ClassConstFetch) {
$type = $this->getType($callerExpr->class);
if ($type instanceof FullyQualifiedObjectType && $this->isNonStaticOtherObject(
$type,
$arrayCallable,
$scope,
)) {
return null;
}
return new StaticCall($callerExpr->class, $arrayCallable->getMethod(), $args);
}
$methodName = $arrayCallable->getMethod();
$methodCall = new MethodCall($callerExpr, $methodName, $args);
if ($this->isReferenceToNonPublicMethodOutsideOwningScope($methodCall, $methodName)) {
return null;
}
return $methodCall;
}
public function provideMinPhpVersion(): int
{
return PhpVersion::PHP_81;
}
private function isNonStaticOtherObject(
FullyQualifiedObjectType $fullyQualifiedObjectType,
ArrayCallable $arrayCallable,
Scope $scope
): bool {
$classReflection = $scope->getClassReflection();
if ($classReflection instanceof ClassReflection && $classReflection->getName() === $fullyQualifiedObjectType->getClassName()) {
return false;
}
$arrayClassReflection = $this->reflectionProvider->getClass($arrayCallable->getClass());
// we're unable to find it
if (! $arrayClassReflection->hasMethod($arrayCallable->getMethod())) {
return false;
}
$extendedMethodReflection = $arrayClassReflection->getMethod($arrayCallable->getMethod(), $scope);
if (! $extendedMethodReflection->isStatic()) {
return true;
}
return ! $extendedMethodReflection->isPublic();
}
private function isReferenceToNonPublicMethodOutsideOwningScope(MethodCall $methodCall, string $methodName): bool
{
if ($methodCall->var instanceof Variable && $methodCall->var->name === 'this') {
// If the callable is scoped to `$this` then it can be converted even if it is protected / private
return false;
}
// If the callable is scoped to another object / variable then it should only be converted if it is public
// https://github.com/rectorphp/rector/issues/8659
$classReflection = $this->reflectionResolver->resolveClassReflectionSourceObject($methodCall);
if ($classReflection instanceof ClassReflection && $classReflection->hasNativeMethod($methodName)) {
$method = $classReflection->getNativeMethod($methodName);
if (! $method->isPublic()) {
return true;
}
}
return false;
}
}