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: * @api
34: */
35: final class ClassPropertiesNode extends NodeAbstract implements VirtualNode
36: {
37:
38: /**
39: * @param ClassPropertyNode[] $properties
40: * @param array<int, PropertyRead|PropertyWrite> $propertyUsages
41: * @param array<int, MethodCall> $methodCalls
42: * @param array<string, MethodReturnStatementsNode> $returnStatementNodes
43: * @param list<PropertyAssign> $propertyAssigns
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: * @return ClassPropertyNode[]
66: */
67: public function getProperties(): array
68: {
69: return $this->properties;
70: }
71:
72: /**
73: * @return array<int, PropertyRead|PropertyWrite>
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: * @return string[]
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: * @param string[] $constructors
102: * @return array{array<string, ClassPropertyNode>, array<array{string, int, ClassPropertyNode, string, string}>, array<array{string, int, ClassPropertyNode}>}
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, $constructors);
170: $prematureAccess = [];
171: $additionalAssigns = [];
172: foreach ($this->getPropertyUsages() as $usage) {
173: $fetch = $usage->getFetch();
174: if (!$fetch instanceof PropertyFetch) {
175: continue;
176: }
177: $usageScope = $usage->getScope();
178: if ($usageScope->getFunction() === null) {
179: continue;
180: }
181: $function = $usageScope->getFunction();
182: if (!$function instanceof MethodReflection) {
183: continue;
184: }
185: if ($function->getDeclaringClass()->getName() !== $classReflection->getName()) {
186: continue;
187: }
188: if (!array_key_exists($function->getName(), $methodsCalledFromConstructor)) {
189: continue;
190: }
191:
192: $initializedPropertiesMap = $methodsCalledFromConstructor[$function->getName()];
193:
194: if (!$fetch->name instanceof Identifier) {
195: continue;
196: }
197: $propertyName = $fetch->name->toString();
198: $fetchedOnType = $usageScope->getType($fetch->var);
199: if (TypeUtils::findThisType($fetchedOnType) === null) {
200: continue;
201: }
202:
203: $propertyReflection = $usageScope->getInstancePropertyReflection($fetchedOnType, $propertyName);
204: if ($propertyReflection === null) {
205: continue;
206: }
207: if ($propertyReflection->getDeclaringClass()->getName() !== $classReflection->getName()) {
208: continue;
209: }
210:
211: if ($usage instanceof PropertyWrite) {
212: if (array_key_exists($propertyName, $initializedPropertiesMap)) {
213: $hasInitialization = $initializedPropertiesMap[$propertyName];
214: if (in_array($function->getName(), $constructors, true)) {
215: $hasInitialization = $hasInitialization->or($usageScope->hasExpressionType(new PropertyInitializationExpr($propertyName)));
216: }
217: if (
218: !$hasInitialization->no()
219: && !$usage->isPromotedPropertyWrite()
220: && !array_key_exists($propertyName, $initializedViaExtension)
221: ) {
222: $additionalAssigns[] = [
223: $propertyName,
224: $fetch->getStartLine(),
225: $originalProperties[$propertyName],
226: ];
227: }
228: }
229: } elseif (array_key_exists($propertyName, $initializedPropertiesMap)) {
230: if (
231: strtolower($function->getName()) !== '__construct'
232: && array_key_exists($propertyName, $initializedInConstructor)
233: && in_array($function->getName(), $constructors, true)
234: ) {
235: continue;
236: }
237: $hasInitialization = $initializedPropertiesMap[$propertyName]->or($usageScope->hasExpressionType(new PropertyInitializationExpr($propertyName)));
238: if (!$hasInitialization->yes() && $usageScope->isInAnonymousFunction() && $usageScope->getParentScope() !== null) {
239: $hasInitialization = $hasInitialization->or($usageScope->getParentScope()->hasExpressionType(new PropertyInitializationExpr($propertyName)));
240: }
241: if (!$hasInitialization->yes()) {
242: $prematureAccess[] = [
243: $propertyName,
244: $fetch->getStartLine(),
245: $originalProperties[$propertyName],
246: $usageScope->getFile(),
247: $usageScope->getFileDescription(),
248: ];
249: }
250: }
251: }
252:
253: return [
254: $this->collectUninitializedProperties(array_keys($methodsCalledFromConstructor), $uninitializedProperties),
255: $prematureAccess,
256: $additionalAssigns,
257: ];
258: }
259:
260: /**
261: * @param list<string> $constructors
262: * @param array<string, ClassPropertyNode> $uninitializedProperties
263: * @return array<string, ClassPropertyNode>
264: */
265: private function collectUninitializedProperties(array $constructors, array $uninitializedProperties): array
266: {
267: foreach ($constructors as $constructor) {
268: $lowerConstructorName = strtolower($constructor);
269: if (!array_key_exists($lowerConstructorName, $this->returnStatementNodes)) {
270: continue;
271: }
272:
273: $returnStatementsNode = $this->returnStatementNodes[$lowerConstructorName];
274: $methodScope = null;
275: foreach ($returnStatementsNode->getExecutionEnds() as $executionEnd) {
276: $statementResult = $executionEnd->getStatementResult();
277: $endNode = $executionEnd->getNode();
278: if ($statementResult->isAlwaysTerminating()) {
279: if ($endNode instanceof Node\Stmt\Expression) {
280: $exprType = $statementResult->getScope()->getType($endNode->expr);
281: if ($exprType instanceof NeverType && $exprType->isExplicit()) {
282: continue;
283: }
284: }
285: }
286: if ($methodScope === null) {
287: $methodScope = $statementResult->getScope();
288: continue;
289: }
290:
291: $methodScope = $methodScope->mergeWith($statementResult->getScope());
292: }
293:
294: foreach ($returnStatementsNode->getReturnStatements() as $returnStatement) {
295: if ($methodScope === null) {
296: $methodScope = $returnStatement->getScope();
297: continue;
298: }
299: $methodScope = $methodScope->mergeWith($returnStatement->getScope());
300: }
301:
302: if ($methodScope === null) {
303: continue;
304: }
305:
306: foreach (array_keys($uninitializedProperties) as $propertyName) {
307: if (!$methodScope->hasExpressionType(new PropertyInitializationExpr($propertyName))->yes()) {
308: continue;
309: }
310:
311: unset($uninitializedProperties[$propertyName]);
312: }
313: }
314:
315: return $uninitializedProperties;
316: }
317:
318: /**
319: * @param string[] $methods
320: * @param array<string, TrinaryLogic> $initialInitializedProperties
321: * @param array<string, array<string, TrinaryLogic>> $initializedProperties
322: * @param array<string, ClassPropertyNode> $initializedInConstructorProperties
323: * @param string[] $originalConstructors
324: *
325: * @return array<string, array<string, TrinaryLogic>>
326: */
327: private function getMethodsCalledFromConstructor(
328: ClassReflection $classReflection,
329: array $initialInitializedProperties,
330: array $initializedProperties,
331: array $methods,
332: array $initializedInConstructorProperties,
333: array $originalConstructors,
334: ): array
335: {
336: $originalMap = $initializedProperties;
337: $originalMethods = $methods;
338:
339: foreach ($this->methodCalls as $methodCall) {
340: $methodCallNode = $methodCall->getNode();
341: if ($methodCallNode instanceof Array_) {
342: continue;
343: }
344: if (!$methodCallNode->name instanceof Identifier) {
345: continue;
346: }
347: $callScope = $methodCall->getScope();
348: if ($methodCallNode instanceof Node\Expr\MethodCall) {
349: $calledOnType = $callScope->getType($methodCallNode->var);
350: } else {
351: if (!$methodCallNode->class instanceof Name) {
352: continue;
353: }
354:
355: $calledOnType = $callScope->resolveTypeByName($methodCallNode->class);
356: }
357:
358: if (TypeUtils::findThisType($calledOnType) === null) {
359: continue;
360: }
361:
362: $inMethod = $callScope->getFunction();
363: if (!$inMethod instanceof MethodReflection) {
364: continue;
365: }
366: if (!in_array($inMethod->getName(), $methods, true)) {
367: continue;
368: }
369:
370: if ($inMethod->getName() !== '__construct' && in_array($inMethod->getName(), $originalConstructors, true)) {
371: foreach (array_keys($initializedInConstructorProperties) as $propertyName) {
372: $initializedProperties[$inMethod->getName()][$propertyName] = TrinaryLogic::createYes();
373: }
374: }
375:
376: $methodName = $methodCallNode->name->toString();
377: if (array_key_exists($methodName, $initializedProperties)) {
378: foreach ($this->getInitializedProperties($callScope, $initializedProperties[$inMethod->getName()] ?? $initialInitializedProperties) as $propertyName => $isInitialized) {
379: $initializedProperties[$methodName][$propertyName] = $initializedProperties[$methodName][$propertyName]->and($isInitialized);
380: }
381: continue;
382: }
383: $methodReflection = $callScope->getMethodReflection($calledOnType, $methodName);
384: if ($methodReflection === null) {
385: continue;
386: }
387: if ($methodReflection->getDeclaringClass()->getName() !== $classReflection->getName()) {
388: continue;
389: }
390: $initializedProperties[$methodName] = $this->getInitializedProperties($callScope, $initializedProperties[$inMethod->getName()] ?? $initialInitializedProperties);
391: $methods[] = $methodName;
392: }
393:
394: if ($originalMap === $initializedProperties && $originalMethods === $methods) {
395: return $initializedProperties;
396: }
397:
398: return $this->getMethodsCalledFromConstructor($classReflection, $initialInitializedProperties, $initializedProperties, $methods, $initializedInConstructorProperties, $originalConstructors);
399: }
400:
401: /**
402: * @param array<string, TrinaryLogic> $initialInitializedProperties
403: * @return array<string, TrinaryLogic>
404: */
405: private function getInitializedProperties(Scope $scope, array $initialInitializedProperties): array
406: {
407: foreach ($initialInitializedProperties as $propertyName => $isInitialized) {
408: $initialInitializedProperties[$propertyName] = $isInitialized->or($scope->hasExpressionType(new PropertyInitializationExpr($propertyName)));
409: }
410:
411: return $initialInitializedProperties;
412: }
413:
414: /**
415: * @return list<PropertyAssign>
416: */
417: public function getPropertyAssigns(): array
418: {
419: return $this->propertyAssigns;
420: }
421:
422: }
423: