-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathLocalPropertyAnalyzer.php
More file actions
208 lines (169 loc) · 7.29 KB
/
LocalPropertyAnalyzer.php
File metadata and controls
208 lines (169 loc) · 7.29 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
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
declare(strict_types=1);
namespace Rector\CodeQuality\NodeAnalyzer;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\NodeVisitor;
use PHPStan\Analyser\Scope;
use PHPStan\Type\MixedType;
use Rector\CodeQuality\TypeResolver\ArrayDimFetchTypeResolver;
use Rector\CodeQuality\ValueObject\DefinedPropertyWithType;
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
final readonly class LocalPropertyAnalyzer
{
private const string LARAVEL_COLLECTION_CLASS = 'Illuminate\Support\Collection';
public function __construct(
private SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
private NodeNameResolver $nodeNameResolver,
private ArrayDimFetchTypeResolver $arrayDimFetchTypeResolver,
private NodeTypeResolver $nodeTypeResolver,
private PropertyFetchAnalyzer $propertyFetchAnalyzer,
private TypeFactory $typeFactory,
) {
}
/**
* @return DefinedPropertyWithType[]
*/
public function resolveFetchedPropertiesToTypesFromClass(Class_ $class): array
{
$definedPropertiesWithTypes = [];
foreach ($class->getMethods() as $classMethod) {
$methodName = $this->nodeNameResolver->getName($classMethod);
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
$classMethod->getStmts(),
function (Node $node) use (&$definedPropertiesWithTypes, $methodName): ?int {
if ($this->shouldSkip($node)) {
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
if ($node instanceof Assign && ($node->var instanceof PropertyFetch || $node->var instanceof ArrayDimFetch)) {
$propertyFetch = $node->var;
$propertyName = $this->resolvePropertyName(
$propertyFetch instanceof ArrayDimFetch ? $propertyFetch->var : $propertyFetch
);
if ($propertyName === null) {
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
if ($propertyFetch instanceof ArrayDimFetch) {
$propertyType = $this->arrayDimFetchTypeResolver->resolve($propertyFetch, $node);
$definedPropertiesWithTypes[] = new DefinedPropertyWithType(
$propertyName,
$propertyType,
$methodName
);
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
$propertyType = $this->nodeTypeResolver->getType($node->expr);
$definedPropertiesWithTypes[] = new DefinedPropertyWithType(
$propertyName,
$propertyType,
$methodName
);
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
$propertyName = $this->resolvePropertyName($node);
if ($propertyName === null) {
return null;
}
$definedPropertiesWithTypes[] = new DefinedPropertyWithType(
$propertyName,
new MixedType(),
$methodName
);
return null;
}
);
}
return $this->normalizeToSingleType($definedPropertiesWithTypes);
}
private function shouldSkip(Node $node): bool
{
// skip anonymous classes and inner function
if ($node instanceof Class_ || $node instanceof Function_) {
return true;
}
// skip closure call
if ($node instanceof MethodCall && $node->var instanceof Closure) {
return true;
}
if ($node instanceof StaticCall) {
return $this->nodeNameResolver->isName($node->class, self::LARAVEL_COLLECTION_CLASS);
}
return false;
}
private function resolvePropertyName(Node $node): string|null
{
if (! $node instanceof PropertyFetch) {
return null;
}
if (! $this->propertyFetchAnalyzer->isLocalPropertyFetch($node)) {
return null;
}
if ($this->shouldSkipPropertyFetch($node)) {
return null;
}
return $this->nodeNameResolver->getName($node->name);
}
private function shouldSkipPropertyFetch(PropertyFetch $propertyFetch): bool
{
if ($this->isPartOfClosureBind($propertyFetch)) {
return true;
}
return ! $propertyFetch->name instanceof Identifier;
}
/**
* @param DefinedPropertyWithType[] $definedPropertiesWithTypes
* @return DefinedPropertyWithType[]
*/
private function normalizeToSingleType(array $definedPropertiesWithTypes): array
{
$definedPropertiesWithTypesByPropertyName = [];
foreach ($definedPropertiesWithTypes as $definedPropertyWithType) {
$definedPropertiesWithTypesByPropertyName[$definedPropertyWithType->getName()][] = $definedPropertyWithType;
}
$normalizedDefinedPropertiesWithTypes = [];
foreach ($definedPropertiesWithTypesByPropertyName as $propertyName => $definedPropertiesWithTypes) {
if (count($definedPropertiesWithTypes) === 1) {
$normalizedDefinedPropertiesWithTypes[] = $definedPropertiesWithTypes[0];
continue;
}
$propertyTypes = [];
foreach ($definedPropertiesWithTypes as $definedPropertyWithType) {
/** @var DefinedPropertyWithType $definedPropertyWithType */
$propertyTypes[] = $definedPropertyWithType->getType();
}
$normalizePropertyType = $this->typeFactory->createMixedPassedOrUnionType($propertyTypes);
$normalizedDefinedPropertiesWithTypes[] = new DefinedPropertyWithType(
$propertyName,
$normalizePropertyType,
// skip as multiple places can define the same property
null
);
}
return $normalizedDefinedPropertiesWithTypes;
}
/**
* Local property is actually not local one, but belongs to passed object
* See https://ocramius.github.io/blog/accessing-private-php-class-members-without-reflection/
*/
private function isPartOfClosureBind(PropertyFetch $propertyFetch): bool
{
$scope = $propertyFetch->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return false;
}
return $scope->isInClosureBind();
}
}