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