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->isAbstract()) { |
122: | continue; |
123: | } |
124: | if ($property->getNativeType() === null) { |
125: | continue; |
126: | } |
127: | if ($property->getDefault() !== null) { |
128: | continue; |
129: | } |
130: | $originalProperties[$property->getName()] = $property; |
131: | $is = TrinaryLogic::createFromBoolean($property->isPromoted() && !$property->isPromotedFromTrait()); |
132: | if (!$is->yes() && $classReflection->hasNativeProperty($property->getName())) { |
133: | $propertyReflection = $classReflection->getNativeProperty($property->getName()); |
134: | if ($propertyReflection->isVirtual()->yes()) { |
135: | continue; |
136: | } |
137: | |
138: | foreach ($extensions as $extension) { |
139: | if (!$extension->isInitialized($propertyReflection, $property->getName())) { |
140: | continue; |
141: | } |
142: | $is = TrinaryLogic::createYes(); |
143: | $initializedViaExtension[$property->getName()] = true; |
144: | break; |
145: | } |
146: | } |
147: | $initialInitializedProperties[$property->getName()] = $is; |
148: | foreach ($constructors as $constructor) { |
149: | $initializedProperties[$constructor][$property->getName()] = $is; |
150: | } |
151: | if ($is->yes()) { |
152: | continue; |
153: | } |
154: | $uninitializedProperties[$property->getName()] = $property; |
155: | } |
156: | |
157: | if ($constructors === []) { |
158: | return [$uninitializedProperties, [], []]; |
159: | } |
160: | |
161: | $initializedInConstructor = []; |
162: | if ($classReflection->hasConstructor()) { |
163: | $initializedInConstructor = array_diff_key($uninitializedProperties, $this->collectUninitializedProperties([$classReflection->getConstructor()->getName()], $uninitializedProperties)); |
164: | } |
165: | |
166: | $methodsCalledFromConstructor = $this->getMethodsCalledFromConstructor($classReflection, $initialInitializedProperties, $initializedProperties, $constructors, $initializedInConstructor); |
167: | $prematureAccess = []; |
168: | $additionalAssigns = []; |
169: | |
170: | foreach ($this->getPropertyUsages() as $usage) { |
171: | $fetch = $usage->getFetch(); |
172: | if (!$fetch instanceof PropertyFetch) { |
173: | continue; |
174: | } |
175: | $usageScope = $usage->getScope(); |
176: | if ($usageScope->getFunction() === null) { |
177: | continue; |
178: | } |
179: | $function = $usageScope->getFunction(); |
180: | if (!$function instanceof MethodReflection) { |
181: | continue; |
182: | } |
183: | if ($function->getDeclaringClass()->getName() !== $classReflection->getName()) { |
184: | continue; |
185: | } |
186: | if (!array_key_exists($function->getName(), $methodsCalledFromConstructor)) { |
187: | continue; |
188: | } |
189: | |
190: | $initializedPropertiesMap = $methodsCalledFromConstructor[$function->getName()]; |
191: | |
192: | if (!$fetch->name instanceof Identifier) { |
193: | continue; |
194: | } |
195: | $propertyName = $fetch->name->toString(); |
196: | $fetchedOnType = $usageScope->getType($fetch->var); |
197: | if (TypeUtils::findThisType($fetchedOnType) === null) { |
198: | continue; |
199: | } |
200: | |
201: | $propertyReflection = $usageScope->getPropertyReflection($fetchedOnType, $propertyName); |
202: | if ($propertyReflection === null) { |
203: | continue; |
204: | } |
205: | if ($propertyReflection->getDeclaringClass()->getName() !== $classReflection->getName()) { |
206: | continue; |
207: | } |
208: | |
209: | if ($usage instanceof PropertyWrite) { |
210: | if (array_key_exists($propertyName, $initializedPropertiesMap)) { |
211: | $hasInitialization = $initializedPropertiesMap[$propertyName]->or($usageScope->hasExpressionType(new PropertyInitializationExpr($propertyName))); |
212: | if ( |
213: | !$hasInitialization->no() |
214: | && !$usage->isPromotedPropertyWrite() |
215: | && !array_key_exists($propertyName, $initializedViaExtension) |
216: | ) { |
217: | $additionalAssigns[] = [ |
218: | $propertyName, |
219: | $fetch->getStartLine(), |
220: | $originalProperties[$propertyName], |
221: | ]; |
222: | } |
223: | } |
224: | } elseif (array_key_exists($propertyName, $initializedPropertiesMap)) { |
225: | if ( |
226: | strtolower($function->getName()) !== '__construct' |
227: | && array_key_exists($propertyName, $initializedInConstructor) |
228: | && in_array($function->getName(), $constructors, true) |
229: | ) { |
230: | continue; |
231: | } |
232: | $hasInitialization = $initializedPropertiesMap[$propertyName]->or($usageScope->hasExpressionType(new PropertyInitializationExpr($propertyName))); |
233: | if (!$hasInitialization->yes() && $usageScope->isInAnonymousFunction() && $usageScope->getParentScope() !== null) { |
234: | $hasInitialization = $hasInitialization->or($usageScope->getParentScope()->hasExpressionType(new PropertyInitializationExpr($propertyName))); |
235: | } |
236: | if (!$hasInitialization->yes()) { |
237: | $prematureAccess[] = [ |
238: | $propertyName, |
239: | $fetch->getStartLine(), |
240: | $originalProperties[$propertyName], |
241: | $usageScope->getFile(), |
242: | $usageScope->getFileDescription(), |
243: | ]; |
244: | } |
245: | } |
246: | } |
247: | |
248: | return [ |
249: | $this->collectUninitializedProperties(array_keys($methodsCalledFromConstructor), $uninitializedProperties), |
250: | $prematureAccess, |
251: | $additionalAssigns, |
252: | ]; |
253: | } |
254: | |
255: | |
256: | |
257: | |
258: | |
259: | |
260: | private function collectUninitializedProperties(array $constructors, array $uninitializedProperties): array |
261: | { |
262: | foreach ($constructors as $constructor) { |
263: | $lowerConstructorName = strtolower($constructor); |
264: | if (!array_key_exists($lowerConstructorName, $this->returnStatementNodes)) { |
265: | continue; |
266: | } |
267: | |
268: | $returnStatementsNode = $this->returnStatementNodes[$lowerConstructorName]; |
269: | $methodScope = null; |
270: | foreach ($returnStatementsNode->getExecutionEnds() as $executionEnd) { |
271: | $statementResult = $executionEnd->getStatementResult(); |
272: | $endNode = $executionEnd->getNode(); |
273: | if ($statementResult->isAlwaysTerminating()) { |
274: | if ($endNode instanceof Node\Stmt\Expression) { |
275: | $exprType = $statementResult->getScope()->getType($endNode->expr); |
276: | if ($exprType instanceof NeverType && $exprType->isExplicit()) { |
277: | continue; |
278: | } |
279: | } |
280: | } |
281: | if ($methodScope === null) { |
282: | $methodScope = $statementResult->getScope(); |
283: | continue; |
284: | } |
285: | |
286: | $methodScope = $methodScope->mergeWith($statementResult->getScope()); |
287: | } |
288: | |
289: | foreach ($returnStatementsNode->getReturnStatements() as $returnStatement) { |
290: | if ($methodScope === null) { |
291: | $methodScope = $returnStatement->getScope(); |
292: | continue; |
293: | } |
294: | $methodScope = $methodScope->mergeWith($returnStatement->getScope()); |
295: | } |
296: | |
297: | if ($methodScope === null) { |
298: | continue; |
299: | } |
300: | |
301: | foreach (array_keys($uninitializedProperties) as $propertyName) { |
302: | if (!$methodScope->hasExpressionType(new PropertyInitializationExpr($propertyName))->yes()) { |
303: | continue; |
304: | } |
305: | |
306: | unset($uninitializedProperties[$propertyName]); |
307: | } |
308: | } |
309: | |
310: | return $uninitializedProperties; |
311: | } |
312: | |
313: | |
314: | |
315: | |
316: | |
317: | |
318: | |
319: | |
320: | |
321: | private function getMethodsCalledFromConstructor( |
322: | ClassReflection $classReflection, |
323: | array $initialInitializedProperties, |
324: | array $initializedProperties, |
325: | array $methods, |
326: | array $initializedInConstructorProperties, |
327: | ): array |
328: | { |
329: | $originalMap = $initializedProperties; |
330: | $originalMethods = $methods; |
331: | |
332: | foreach ($this->methodCalls as $methodCall) { |
333: | $methodCallNode = $methodCall->getNode(); |
334: | if ($methodCallNode instanceof Array_) { |
335: | continue; |
336: | } |
337: | if (!$methodCallNode->name instanceof Identifier) { |
338: | continue; |
339: | } |
340: | $callScope = $methodCall->getScope(); |
341: | if ($methodCallNode instanceof Node\Expr\MethodCall) { |
342: | $calledOnType = $callScope->getType($methodCallNode->var); |
343: | } else { |
344: | if (!$methodCallNode->class instanceof Name) { |
345: | continue; |
346: | } |
347: | |
348: | $calledOnType = $callScope->resolveTypeByName($methodCallNode->class); |
349: | } |
350: | |
351: | if (TypeUtils::findThisType($calledOnType) === null) { |
352: | continue; |
353: | } |
354: | |
355: | $inMethod = $callScope->getFunction(); |
356: | if (!$inMethod instanceof MethodReflection) { |
357: | continue; |
358: | } |
359: | if (!in_array($inMethod->getName(), $methods, true)) { |
360: | continue; |
361: | } |
362: | |
363: | if ($inMethod->getName() !== '__construct') { |
364: | foreach ($initializedInConstructorProperties as $propertyName => $propertyNode) { |
365: | $initializedProperties[$inMethod->getName()][$propertyName] = TrinaryLogic::createYes(); |
366: | } |
367: | } |
368: | |
369: | $methodName = $methodCallNode->name->toString(); |
370: | if (array_key_exists($methodName, $initializedProperties)) { |
371: | foreach ($this->getInitializedProperties($callScope, $initializedProperties[$inMethod->getName()] ?? $initialInitializedProperties) as $propertyName => $isInitialized) { |
372: | $initializedProperties[$methodName][$propertyName] = $initializedProperties[$methodName][$propertyName]->and($isInitialized); |
373: | } |
374: | continue; |
375: | } |
376: | $methodReflection = $callScope->getMethodReflection($calledOnType, $methodName); |
377: | if ($methodReflection === null) { |
378: | continue; |
379: | } |
380: | if ($methodReflection->getDeclaringClass()->getName() !== $classReflection->getName()) { |
381: | continue; |
382: | } |
383: | $initializedProperties[$methodName] = $this->getInitializedProperties($callScope, $initializedProperties[$inMethod->getName()] ?? $initialInitializedProperties); |
384: | $methods[] = $methodName; |
385: | } |
386: | |
387: | if ($originalMap === $initializedProperties && $originalMethods === $methods) { |
388: | return $initializedProperties; |
389: | } |
390: | |
391: | return $this->getMethodsCalledFromConstructor($classReflection, $initialInitializedProperties, $initializedProperties, $methods, $initializedInConstructorProperties); |
392: | } |
393: | |
394: | |
395: | |
396: | |
397: | |
398: | private function getInitializedProperties(Scope $scope, array $initialInitializedProperties): array |
399: | { |
400: | foreach ($initialInitializedProperties as $propertyName => $isInitialized) { |
401: | $initialInitializedProperties[$propertyName] = $isInitialized->or($scope->hasExpressionType(new PropertyInitializationExpr($propertyName))); |
402: | } |
403: | |
404: | return $initialInitializedProperties; |
405: | } |
406: | |
407: | |
408: | |
409: | |
410: | public function getPropertyAssigns(): array |
411: | { |
412: | return $this->propertyAssigns; |
413: | } |
414: | |
415: | } |
416: | |