1: | <?php declare(strict_types = 1); |
2: | |
3: | namespace PHPStan\Node; |
4: | |
5: | use PhpParser\Node; |
6: | use PhpParser\Node\Expr\Array_; |
7: | use PhpParser\Node\Expr\PropertyFetch; |
8: | use PhpParser\Node\Identifier; |
9: | use PhpParser\Node\Name; |
10: | use PhpParser\Node\Stmt\Class_; |
11: | use PhpParser\Node\Stmt\ClassLike; |
12: | use PhpParser\NodeAbstract; |
13: | use PHPStan\Analyser\Scope; |
14: | use PHPStan\Node\Expr\PropertyInitializationExpr; |
15: | use PHPStan\Node\Method\MethodCall; |
16: | use PHPStan\Node\Property\PropertyAssign; |
17: | use PHPStan\Node\Property\PropertyRead; |
18: | use PHPStan\Node\Property\PropertyWrite; |
19: | use PHPStan\Reflection\ClassReflection; |
20: | use PHPStan\Reflection\MethodReflection; |
21: | use PHPStan\Rules\Properties\ReadWritePropertiesExtensionProvider; |
22: | use PHPStan\TrinaryLogic; |
23: | use PHPStan\Type\NeverType; |
24: | use PHPStan\Type\TypeUtils; |
25: | use function array_diff_key; |
26: | use function array_key_exists; |
27: | use function array_keys; |
28: | use function in_array; |
29: | use function strtolower; |
30: | |
31: | |
32: | |
33: | |
34: | final class ClassPropertiesNode extends NodeAbstract implements VirtualNode |
35: | { |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | |
43: | |
44: | public function __construct( |
45: | private ClassLike $class, |
46: | private ReadWritePropertiesExtensionProvider $readWritePropertiesExtensionProvider, |
47: | private array $properties, |
48: | private array $propertyUsages, |
49: | private array $methodCalls, |
50: | private array $returnStatementNodes, |
51: | private array $propertyAssigns, |
52: | private ClassReflection $classReflection, |
53: | ) |
54: | { |
55: | parent::__construct($class->getAttributes()); |
56: | } |
57: | |
58: | public function getClass(): ClassLike |
59: | { |
60: | return $this->class; |
61: | } |
62: | |
63: | |
64: | |
65: | |
66: | public function getProperties(): array |
67: | { |
68: | return $this->properties; |
69: | } |
70: | |
71: | |
72: | |
73: | |
74: | public function getPropertyUsages(): array |
75: | { |
76: | return $this->propertyUsages; |
77: | } |
78: | |
79: | public function getType(): string |
80: | { |
81: | return 'PHPStan_Node_ClassPropertiesNode'; |
82: | } |
83: | |
84: | |
85: | |
86: | |
87: | public function getSubNodeNames(): array |
88: | { |
89: | return []; |
90: | } |
91: | |
92: | public function getClassReflection(): ClassReflection |
93: | { |
94: | return $this->classReflection; |
95: | } |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | public function getUninitializedProperties( |
102: | Scope $scope, |
103: | array $constructors, |
104: | ): array |
105: | { |
106: | if (!$this->getClass() instanceof Class_) { |
107: | return [[], [], []]; |
108: | } |
109: | $classReflection = $this->getClassReflection(); |
110: | |
111: | $uninitializedProperties = []; |
112: | $originalProperties = []; |
113: | $initialInitializedProperties = []; |
114: | $initializedProperties = []; |
115: | $extensions = $this->readWritePropertiesExtensionProvider->getExtensions(); |
116: | $initializedViaExtension = []; |
117: | foreach ($this->getProperties() as $property) { |
118: | if ($property->isStatic()) { |
119: | continue; |
120: | } |
121: | if ($property->getNativeType() === null) { |
122: | continue; |
123: | } |
124: | if ($property->getDefault() !== null) { |
125: | continue; |
126: | } |
127: | $originalProperties[$property->getName()] = $property; |
128: | $is = TrinaryLogic::createFromBoolean($property->isPromoted() && !$property->isPromotedFromTrait()); |
129: | if (!$is->yes() && $classReflection->hasNativeProperty($property->getName())) { |
130: | $propertyReflection = $classReflection->getNativeProperty($property->getName()); |
131: | |
132: | foreach ($extensions as $extension) { |
133: | if (!$extension->isInitialized($propertyReflection, $property->getName())) { |
134: | continue; |
135: | } |
136: | $is = TrinaryLogic::createYes(); |
137: | $initializedViaExtension[$property->getName()] = true; |
138: | break; |
139: | } |
140: | } |
141: | $initialInitializedProperties[$property->getName()] = $is; |
142: | foreach ($constructors as $constructor) { |
143: | $initializedProperties[$constructor][$property->getName()] = $is; |
144: | } |
145: | if ($is->yes()) { |
146: | continue; |
147: | } |
148: | $uninitializedProperties[$property->getName()] = $property; |
149: | } |
150: | |
151: | if ($constructors === []) { |
152: | return [$uninitializedProperties, [], []]; |
153: | } |
154: | |
155: | $initializedInConstructor = []; |
156: | if ($classReflection->hasConstructor()) { |
157: | $initializedInConstructor = array_diff_key($uninitializedProperties, $this->collectUninitializedProperties([$classReflection->getConstructor()->getName()], $uninitializedProperties)); |
158: | } |
159: | |
160: | $methodsCalledFromConstructor = $this->getMethodsCalledFromConstructor($classReflection, $initialInitializedProperties, $initializedProperties, $constructors, $initializedInConstructor); |
161: | $prematureAccess = []; |
162: | $additionalAssigns = []; |
163: | |
164: | foreach ($this->getPropertyUsages() as $usage) { |
165: | $fetch = $usage->getFetch(); |
166: | if (!$fetch instanceof PropertyFetch) { |
167: | continue; |
168: | } |
169: | $usageScope = $usage->getScope(); |
170: | if ($usageScope->getFunction() === null) { |
171: | continue; |
172: | } |
173: | $function = $usageScope->getFunction(); |
174: | if (!$function instanceof MethodReflection) { |
175: | continue; |
176: | } |
177: | if ($function->getDeclaringClass()->getName() !== $classReflection->getName()) { |
178: | continue; |
179: | } |
180: | if (!array_key_exists($function->getName(), $methodsCalledFromConstructor)) { |
181: | continue; |
182: | } |
183: | |
184: | $initializedPropertiesMap = $methodsCalledFromConstructor[$function->getName()]; |
185: | |
186: | if (!$fetch->name instanceof Identifier) { |
187: | continue; |
188: | } |
189: | $propertyName = $fetch->name->toString(); |
190: | $fetchedOnType = $usageScope->getType($fetch->var); |
191: | if (TypeUtils::findThisType($fetchedOnType) === null) { |
192: | continue; |
193: | } |
194: | |
195: | $propertyReflection = $usageScope->getPropertyReflection($fetchedOnType, $propertyName); |
196: | if ($propertyReflection === null) { |
197: | continue; |
198: | } |
199: | if ($propertyReflection->getDeclaringClass()->getName() !== $classReflection->getName()) { |
200: | continue; |
201: | } |
202: | |
203: | if ($usage instanceof PropertyWrite) { |
204: | if (array_key_exists($propertyName, $initializedPropertiesMap)) { |
205: | $hasInitialization = $initializedPropertiesMap[$propertyName]->or($usageScope->hasExpressionType(new PropertyInitializationExpr($propertyName))); |
206: | if ( |
207: | !$hasInitialization->no() |
208: | && !$usage->isPromotedPropertyWrite() |
209: | && !array_key_exists($propertyName, $initializedViaExtension) |
210: | ) { |
211: | $additionalAssigns[] = [ |
212: | $propertyName, |
213: | $fetch->getStartLine(), |
214: | $originalProperties[$propertyName], |
215: | ]; |
216: | } |
217: | } |
218: | } elseif (array_key_exists($propertyName, $initializedPropertiesMap)) { |
219: | if ( |
220: | strtolower($function->getName()) !== '__construct' |
221: | && array_key_exists($propertyName, $initializedInConstructor) |
222: | && in_array($function->getName(), $constructors, true) |
223: | ) { |
224: | continue; |
225: | } |
226: | $hasInitialization = $initializedPropertiesMap[$propertyName]->or($usageScope->hasExpressionType(new PropertyInitializationExpr($propertyName))); |
227: | if (!$hasInitialization->yes() && $usageScope->isInAnonymousFunction() && $usageScope->getParentScope() !== null) { |
228: | $hasInitialization = $hasInitialization->or($usageScope->getParentScope()->hasExpressionType(new PropertyInitializationExpr($propertyName))); |
229: | } |
230: | if (!$hasInitialization->yes()) { |
231: | $prematureAccess[] = [ |
232: | $propertyName, |
233: | $fetch->getStartLine(), |
234: | $originalProperties[$propertyName], |
235: | $usageScope->getFile(), |
236: | $usageScope->getFileDescription(), |
237: | ]; |
238: | } |
239: | } |
240: | } |
241: | |
242: | return [ |
243: | $this->collectUninitializedProperties(array_keys($methodsCalledFromConstructor), $uninitializedProperties), |
244: | $prematureAccess, |
245: | $additionalAssigns, |
246: | ]; |
247: | } |
248: | |
249: | |
250: | |
251: | |
252: | |
253: | |
254: | private function collectUninitializedProperties(array $constructors, array $uninitializedProperties): array |
255: | { |
256: | foreach ($constructors as $constructor) { |
257: | $lowerConstructorName = strtolower($constructor); |
258: | if (!array_key_exists($lowerConstructorName, $this->returnStatementNodes)) { |
259: | continue; |
260: | } |
261: | |
262: | $returnStatementsNode = $this->returnStatementNodes[$lowerConstructorName]; |
263: | $methodScope = null; |
264: | foreach ($returnStatementsNode->getExecutionEnds() as $executionEnd) { |
265: | $statementResult = $executionEnd->getStatementResult(); |
266: | $endNode = $executionEnd->getNode(); |
267: | if ($statementResult->isAlwaysTerminating()) { |
268: | if ($endNode instanceof Node\Stmt\Expression) { |
269: | $exprType = $statementResult->getScope()->getType($endNode->expr); |
270: | if ($exprType instanceof NeverType && $exprType->isExplicit()) { |
271: | continue; |
272: | } |
273: | } |
274: | } |
275: | if ($methodScope === null) { |
276: | $methodScope = $statementResult->getScope(); |
277: | continue; |
278: | } |
279: | |
280: | $methodScope = $methodScope->mergeWith($statementResult->getScope()); |
281: | } |
282: | |
283: | foreach ($returnStatementsNode->getReturnStatements() as $returnStatement) { |
284: | if ($methodScope === null) { |
285: | $methodScope = $returnStatement->getScope(); |
286: | continue; |
287: | } |
288: | $methodScope = $methodScope->mergeWith($returnStatement->getScope()); |
289: | } |
290: | |
291: | if ($methodScope === null) { |
292: | continue; |
293: | } |
294: | |
295: | foreach (array_keys($uninitializedProperties) as $propertyName) { |
296: | if (!$methodScope->hasExpressionType(new PropertyInitializationExpr($propertyName))->yes()) { |
297: | continue; |
298: | } |
299: | |
300: | unset($uninitializedProperties[$propertyName]); |
301: | } |
302: | } |
303: | |
304: | return $uninitializedProperties; |
305: | } |
306: | |
307: | |
308: | |
309: | |
310: | |
311: | |
312: | |
313: | |
314: | |
315: | private function getMethodsCalledFromConstructor( |
316: | ClassReflection $classReflection, |
317: | array $initialInitializedProperties, |
318: | array $initializedProperties, |
319: | array $methods, |
320: | array $initializedInConstructorProperties, |
321: | ): array |
322: | { |
323: | $originalMap = $initializedProperties; |
324: | $originalMethods = $methods; |
325: | |
326: | foreach ($this->methodCalls as $methodCall) { |
327: | $methodCallNode = $methodCall->getNode(); |
328: | if ($methodCallNode instanceof Array_) { |
329: | continue; |
330: | } |
331: | if (!$methodCallNode->name instanceof Identifier) { |
332: | continue; |
333: | } |
334: | $callScope = $methodCall->getScope(); |
335: | if ($methodCallNode instanceof Node\Expr\MethodCall) { |
336: | $calledOnType = $callScope->getType($methodCallNode->var); |
337: | } else { |
338: | if (!$methodCallNode->class instanceof Name) { |
339: | continue; |
340: | } |
341: | |
342: | $calledOnType = $callScope->resolveTypeByName($methodCallNode->class); |
343: | } |
344: | |
345: | if (TypeUtils::findThisType($calledOnType) === null) { |
346: | continue; |
347: | } |
348: | |
349: | $inMethod = $callScope->getFunction(); |
350: | if (!$inMethod instanceof MethodReflection) { |
351: | continue; |
352: | } |
353: | if (!in_array($inMethod->getName(), $methods, true)) { |
354: | continue; |
355: | } |
356: | |
357: | if ($inMethod->getName() !== '__construct') { |
358: | foreach ($initializedInConstructorProperties as $propertyName => $propertyNode) { |
359: | $initializedProperties[$inMethod->getName()][$propertyName] = TrinaryLogic::createYes(); |
360: | } |
361: | } |
362: | |
363: | $methodName = $methodCallNode->name->toString(); |
364: | if (array_key_exists($methodName, $initializedProperties)) { |
365: | foreach ($this->getInitializedProperties($callScope, $initializedProperties[$inMethod->getName()] ?? $initialInitializedProperties) as $propertyName => $isInitialized) { |
366: | $initializedProperties[$methodName][$propertyName] = $initializedProperties[$methodName][$propertyName]->and($isInitialized); |
367: | } |
368: | continue; |
369: | } |
370: | $methodReflection = $callScope->getMethodReflection($calledOnType, $methodName); |
371: | if ($methodReflection === null) { |
372: | continue; |
373: | } |
374: | if ($methodReflection->getDeclaringClass()->getName() !== $classReflection->getName()) { |
375: | continue; |
376: | } |
377: | $initializedProperties[$methodName] = $this->getInitializedProperties($callScope, $initializedProperties[$inMethod->getName()] ?? $initialInitializedProperties); |
378: | $methods[] = $methodName; |
379: | } |
380: | |
381: | if ($originalMap === $initializedProperties && $originalMethods === $methods) { |
382: | return $initializedProperties; |
383: | } |
384: | |
385: | return $this->getMethodsCalledFromConstructor($classReflection, $initialInitializedProperties, $initializedProperties, $methods, $initializedInConstructorProperties); |
386: | } |
387: | |
388: | |
389: | |
390: | |
391: | |
392: | private function getInitializedProperties(Scope $scope, array $initialInitializedProperties): array |
393: | { |
394: | foreach ($initialInitializedProperties as $propertyName => $isInitialized) { |
395: | $initialInitializedProperties[$propertyName] = $isInitialized->or($scope->hasExpressionType(new PropertyInitializationExpr($propertyName))); |
396: | } |
397: | |
398: | return $initialInitializedProperties; |
399: | } |
400: | |
401: | |
402: | |
403: | |
404: | public function getPropertyAssigns(): array |
405: | { |
406: | return $this->propertyAssigns; |
407: | } |
408: | |
409: | } |
410: | |