1: | <?php declare(strict_types = 1); |
2: | |
3: | namespace PHPStan\Analyser; |
4: | |
5: | use ArrayAccess; |
6: | use Closure; |
7: | use DivisionByZeroError; |
8: | use PhpParser\Comment\Doc; |
9: | use PhpParser\Modifiers; |
10: | use PhpParser\Node; |
11: | use PhpParser\Node\Arg; |
12: | use PhpParser\Node\AttributeGroup; |
13: | use PhpParser\Node\ComplexType; |
14: | use PhpParser\Node\Expr; |
15: | use PhpParser\Node\Expr\Array_; |
16: | use PhpParser\Node\Expr\ArrayDimFetch; |
17: | use PhpParser\Node\Expr\Assign; |
18: | use PhpParser\Node\Expr\AssignRef; |
19: | use PhpParser\Node\Expr\BinaryOp; |
20: | use PhpParser\Node\Expr\BinaryOp\BooleanAnd; |
21: | use PhpParser\Node\Expr\BinaryOp\BooleanOr; |
22: | use PhpParser\Node\Expr\BinaryOp\Coalesce; |
23: | use PhpParser\Node\Expr\BooleanNot; |
24: | use PhpParser\Node\Expr\CallLike; |
25: | use PhpParser\Node\Expr\Cast; |
26: | use PhpParser\Node\Expr\ConstFetch; |
27: | use PhpParser\Node\Expr\ErrorSuppress; |
28: | use PhpParser\Node\Expr\Exit_; |
29: | use PhpParser\Node\Expr\FuncCall; |
30: | use PhpParser\Node\Expr\Instanceof_; |
31: | use PhpParser\Node\Expr\List_; |
32: | use PhpParser\Node\Expr\MethodCall; |
33: | use PhpParser\Node\Expr\New_; |
34: | use PhpParser\Node\Expr\PropertyFetch; |
35: | use PhpParser\Node\Expr\StaticCall; |
36: | use PhpParser\Node\Expr\StaticPropertyFetch; |
37: | use PhpParser\Node\Expr\Ternary; |
38: | use PhpParser\Node\Expr\Variable; |
39: | use PhpParser\Node\Identifier; |
40: | use PhpParser\Node\Name; |
41: | use PhpParser\Node\Stmt\Break_; |
42: | use PhpParser\Node\Stmt\Class_; |
43: | use PhpParser\Node\Stmt\Continue_; |
44: | use PhpParser\Node\Stmt\Do_; |
45: | use PhpParser\Node\Stmt\Echo_; |
46: | use PhpParser\Node\Stmt\For_; |
47: | use PhpParser\Node\Stmt\Foreach_; |
48: | use PhpParser\Node\Stmt\If_; |
49: | use PhpParser\Node\Stmt\InlineHTML; |
50: | use PhpParser\Node\Stmt\Return_; |
51: | use PhpParser\Node\Stmt\Static_; |
52: | use PhpParser\Node\Stmt\Switch_; |
53: | use PhpParser\Node\Stmt\TryCatch; |
54: | use PhpParser\Node\Stmt\Unset_; |
55: | use PhpParser\Node\Stmt\While_; |
56: | use PhpParser\NodeFinder; |
57: | use PhpParser\NodeTraverser; |
58: | use PhpParser\NodeVisitor\CloningVisitor; |
59: | use PhpParser\NodeVisitorAbstract; |
60: | use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClass; |
61: | use PHPStan\BetterReflection\Reflection\ReflectionEnum; |
62: | use PHPStan\BetterReflection\Reflector\Reflector; |
63: | use PHPStan\BetterReflection\SourceLocator\Ast\Strategy\NodeToReflection; |
64: | use PHPStan\BetterReflection\SourceLocator\Located\LocatedSource; |
65: | use PHPStan\DependencyInjection\Reflection\ClassReflectionExtensionRegistryProvider; |
66: | use PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider; |
67: | use PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider; |
68: | use PHPStan\DependencyInjection\Type\ParameterOutTypeExtensionProvider; |
69: | use PHPStan\File\FileHelper; |
70: | use PHPStan\File\FileReader; |
71: | use PHPStan\Node\BooleanAndNode; |
72: | use PHPStan\Node\BooleanOrNode; |
73: | use PHPStan\Node\BreaklessWhileLoopNode; |
74: | use PHPStan\Node\CatchWithUnthrownExceptionNode; |
75: | use PHPStan\Node\ClassConstantsNode; |
76: | use PHPStan\Node\ClassMethodsNode; |
77: | use PHPStan\Node\ClassPropertiesNode; |
78: | use PHPStan\Node\ClassPropertyNode; |
79: | use PHPStan\Node\ClassStatementsGatherer; |
80: | use PHPStan\Node\ClosureReturnStatementsNode; |
81: | use PHPStan\Node\DoWhileLoopConditionNode; |
82: | use PHPStan\Node\ExecutionEndNode; |
83: | use PHPStan\Node\Expr\AlwaysRememberedExpr; |
84: | use PHPStan\Node\Expr\ExistingArrayDimFetch; |
85: | use PHPStan\Node\Expr\GetIterableKeyTypeExpr; |
86: | use PHPStan\Node\Expr\GetIterableValueTypeExpr; |
87: | use PHPStan\Node\Expr\GetOffsetValueTypeExpr; |
88: | use PHPStan\Node\Expr\OriginalPropertyTypeExpr; |
89: | use PHPStan\Node\Expr\PropertyInitializationExpr; |
90: | use PHPStan\Node\Expr\SetExistingOffsetValueTypeExpr; |
91: | use PHPStan\Node\Expr\SetOffsetValueTypeExpr; |
92: | use PHPStan\Node\Expr\TypeExpr; |
93: | use PHPStan\Node\Expr\UnsetOffsetExpr; |
94: | use PHPStan\Node\FinallyExitPointsNode; |
95: | use PHPStan\Node\FunctionCallableNode; |
96: | use PHPStan\Node\FunctionReturnStatementsNode; |
97: | use PHPStan\Node\InArrowFunctionNode; |
98: | use PHPStan\Node\InClassMethodNode; |
99: | use PHPStan\Node\InClassNode; |
100: | use PHPStan\Node\InClosureNode; |
101: | use PHPStan\Node\InForeachNode; |
102: | use PHPStan\Node\InFunctionNode; |
103: | use PHPStan\Node\InPropertyHookNode; |
104: | use PHPStan\Node\InstantiationCallableNode; |
105: | use PHPStan\Node\InTraitNode; |
106: | use PHPStan\Node\InvalidateExprNode; |
107: | use PHPStan\Node\LiteralArrayItem; |
108: | use PHPStan\Node\LiteralArrayNode; |
109: | use PHPStan\Node\MatchExpressionArm; |
110: | use PHPStan\Node\MatchExpressionArmBody; |
111: | use PHPStan\Node\MatchExpressionArmCondition; |
112: | use PHPStan\Node\MatchExpressionNode; |
113: | use PHPStan\Node\MethodCallableNode; |
114: | use PHPStan\Node\MethodReturnStatementsNode; |
115: | use PHPStan\Node\NoopExpressionNode; |
116: | use PHPStan\Node\PropertyAssignNode; |
117: | use PHPStan\Node\PropertyHookReturnStatementsNode; |
118: | use PHPStan\Node\PropertyHookStatementNode; |
119: | use PHPStan\Node\ReturnStatement; |
120: | use PHPStan\Node\StaticMethodCallableNode; |
121: | use PHPStan\Node\UnreachableStatementNode; |
122: | use PHPStan\Node\VariableAssignNode; |
123: | use PHPStan\Node\VarTagChangedExpressionTypeNode; |
124: | use PHPStan\Parser\ArrowFunctionArgVisitor; |
125: | use PHPStan\Parser\ClosureArgVisitor; |
126: | use PHPStan\Parser\ImmediatelyInvokedClosureVisitor; |
127: | use PHPStan\Parser\LineAttributesVisitor; |
128: | use PHPStan\Parser\Parser; |
129: | use PHPStan\Php\PhpVersion; |
130: | use PHPStan\PhpDoc\PhpDocInheritanceResolver; |
131: | use PHPStan\PhpDoc\ResolvedPhpDocBlock; |
132: | use PHPStan\PhpDoc\StubPhpDocProvider; |
133: | use PHPStan\PhpDoc\Tag\VarTag; |
134: | use PHPStan\Reflection\Assertions; |
135: | use PHPStan\Reflection\AttributeReflectionFactory; |
136: | use PHPStan\Reflection\Callables\CallableParametersAcceptor; |
137: | use PHPStan\Reflection\Callables\SimpleImpurePoint; |
138: | use PHPStan\Reflection\Callables\SimpleThrowPoint; |
139: | use PHPStan\Reflection\ClassReflection; |
140: | use PHPStan\Reflection\ExtendedMethodReflection; |
141: | use PHPStan\Reflection\ExtendedParameterReflection; |
142: | use PHPStan\Reflection\ExtendedParametersAcceptor; |
143: | use PHPStan\Reflection\FunctionReflection; |
144: | use PHPStan\Reflection\InitializerExprContext; |
145: | use PHPStan\Reflection\InitializerExprTypeResolver; |
146: | use PHPStan\Reflection\MethodReflection; |
147: | use PHPStan\Reflection\Native\NativeMethodReflection; |
148: | use PHPStan\Reflection\Native\NativeParameterReflection; |
149: | use PHPStan\Reflection\ParameterReflection; |
150: | use PHPStan\Reflection\ParametersAcceptor; |
151: | use PHPStan\Reflection\ParametersAcceptorSelector; |
152: | use PHPStan\Reflection\Php\PhpFunctionFromParserNodeReflection; |
153: | use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection; |
154: | use PHPStan\Reflection\Php\PhpMethodReflection; |
155: | use PHPStan\Reflection\Php\PhpPropertyReflection; |
156: | use PHPStan\Reflection\ReflectionProvider; |
157: | use PHPStan\Reflection\SignatureMap\SignatureMapProvider; |
158: | use PHPStan\Rules\Properties\ReadWritePropertiesExtensionProvider; |
159: | use PHPStan\ShouldNotHappenException; |
160: | use PHPStan\TrinaryLogic; |
161: | use PHPStan\Type\Accessory\AccessoryArrayListType; |
162: | use PHPStan\Type\Accessory\NonEmptyArrayType; |
163: | use PHPStan\Type\ArrayType; |
164: | use PHPStan\Type\ClosureType; |
165: | use PHPStan\Type\Constant\ConstantArrayType; |
166: | use PHPStan\Type\Constant\ConstantArrayTypeBuilder; |
167: | use PHPStan\Type\Constant\ConstantBooleanType; |
168: | use PHPStan\Type\Constant\ConstantIntegerType; |
169: | use PHPStan\Type\ErrorType; |
170: | use PHPStan\Type\FileTypeMapper; |
171: | use PHPStan\Type\GeneralizePrecision; |
172: | use PHPStan\Type\Generic\TemplateTypeHelper; |
173: | use PHPStan\Type\Generic\TemplateTypeMap; |
174: | use PHPStan\Type\Generic\TemplateTypeVariance; |
175: | use PHPStan\Type\Generic\TemplateTypeVarianceMap; |
176: | use PHPStan\Type\IntegerType; |
177: | use PHPStan\Type\IntersectionType; |
178: | use PHPStan\Type\MixedType; |
179: | use PHPStan\Type\NeverType; |
180: | use PHPStan\Type\NullType; |
181: | use PHPStan\Type\ObjectType; |
182: | use PHPStan\Type\ObjectWithoutClassType; |
183: | use PHPStan\Type\ParserNodeTypeToPHPStanType; |
184: | use PHPStan\Type\ResourceType; |
185: | use PHPStan\Type\StaticType; |
186: | use PHPStan\Type\StaticTypeFactory; |
187: | use PHPStan\Type\StringType; |
188: | use PHPStan\Type\ThisType; |
189: | use PHPStan\Type\Type; |
190: | use PHPStan\Type\TypeCombinator; |
191: | use PHPStan\Type\TypeTraverser; |
192: | use PHPStan\Type\TypeUtils; |
193: | use PHPStan\Type\UnionType; |
194: | use ReflectionProperty; |
195: | use Throwable; |
196: | use Traversable; |
197: | use TypeError; |
198: | use UnhandledMatchError; |
199: | use function array_fill_keys; |
200: | use function array_filter; |
201: | use function array_key_exists; |
202: | use function array_key_last; |
203: | use function array_keys; |
204: | use function array_map; |
205: | use function array_merge; |
206: | use function array_pop; |
207: | use function array_reverse; |
208: | use function array_slice; |
209: | use function array_values; |
210: | use function base64_decode; |
211: | use function count; |
212: | use function in_array; |
213: | use function is_array; |
214: | use function is_int; |
215: | use function is_string; |
216: | use function ksort; |
217: | use function sprintf; |
218: | use function str_starts_with; |
219: | use function strtolower; |
220: | use function trim; |
221: | use const PHP_VERSION_ID; |
222: | use const SORT_NUMERIC; |
223: | |
224: | final class NodeScopeResolver |
225: | { |
226: | |
227: | private const LOOP_SCOPE_ITERATIONS = 3; |
228: | private const GENERALIZE_AFTER_ITERATION = 1; |
229: | |
230: | |
231: | private array $analysedFiles = []; |
232: | |
233: | |
234: | private array $earlyTerminatingMethodNames; |
235: | |
236: | |
237: | private array $calledMethodStack = []; |
238: | |
239: | |
240: | private array $calledMethodResults = []; |
241: | |
242: | |
243: | |
244: | |
245: | |
246: | |
247: | public function __construct( |
248: | private readonly ReflectionProvider $reflectionProvider, |
249: | private readonly InitializerExprTypeResolver $initializerExprTypeResolver, |
250: | private readonly Reflector $reflector, |
251: | private readonly ClassReflectionExtensionRegistryProvider $classReflectionExtensionRegistryProvider, |
252: | private readonly ParameterOutTypeExtensionProvider $parameterOutTypeExtensionProvider, |
253: | private readonly Parser $parser, |
254: | private readonly FileTypeMapper $fileTypeMapper, |
255: | private readonly StubPhpDocProvider $stubPhpDocProvider, |
256: | private readonly PhpVersion $phpVersion, |
257: | private readonly SignatureMapProvider $signatureMapProvider, |
258: | private readonly AttributeReflectionFactory $attributeReflectionFactory, |
259: | private readonly PhpDocInheritanceResolver $phpDocInheritanceResolver, |
260: | private readonly FileHelper $fileHelper, |
261: | private readonly TypeSpecifier $typeSpecifier, |
262: | private readonly DynamicThrowTypeExtensionProvider $dynamicThrowTypeExtensionProvider, |
263: | private readonly ReadWritePropertiesExtensionProvider $readWritePropertiesExtensionProvider, |
264: | private readonly ParameterClosureTypeExtensionProvider $parameterClosureTypeExtensionProvider, |
265: | private readonly ScopeFactory $scopeFactory, |
266: | private readonly bool $polluteScopeWithLoopInitialAssignments, |
267: | private readonly bool $polluteScopeWithAlwaysIterableForeach, |
268: | private readonly bool $polluteScopeWithBlock, |
269: | private readonly array $earlyTerminatingMethodCalls, |
270: | private readonly array $earlyTerminatingFunctionCalls, |
271: | private readonly array $universalObjectCratesClasses, |
272: | private readonly bool $implicitThrows, |
273: | private readonly bool $treatPhpDocTypesAsCertain, |
274: | ) |
275: | { |
276: | $earlyTerminatingMethodNames = []; |
277: | foreach ($this->earlyTerminatingMethodCalls as $methodNames) { |
278: | foreach ($methodNames as $methodName) { |
279: | $earlyTerminatingMethodNames[strtolower($methodName)] = true; |
280: | } |
281: | } |
282: | $this->earlyTerminatingMethodNames = $earlyTerminatingMethodNames; |
283: | } |
284: | |
285: | |
286: | |
287: | |
288: | |
289: | public function setAnalysedFiles(array $files): void |
290: | { |
291: | $this->analysedFiles = array_fill_keys($files, true); |
292: | } |
293: | |
294: | |
295: | |
296: | |
297: | |
298: | |
299: | public function processNodes( |
300: | array $nodes, |
301: | MutatingScope $scope, |
302: | callable $nodeCallback, |
303: | ): void |
304: | { |
305: | $alreadyTerminated = false; |
306: | foreach ($nodes as $i => $node) { |
307: | if ( |
308: | !$node instanceof Node\Stmt |
309: | || ($alreadyTerminated && !($node instanceof Node\Stmt\Function_ || $node instanceof Node\Stmt\ClassLike)) |
310: | ) { |
311: | continue; |
312: | } |
313: | |
314: | $statementResult = $this->processStmtNode($node, $scope, $nodeCallback, StatementContext::createTopLevel()); |
315: | $scope = $statementResult->getScope(); |
316: | if ($alreadyTerminated || !$statementResult->isAlwaysTerminating()) { |
317: | continue; |
318: | } |
319: | |
320: | $alreadyTerminated = true; |
321: | $nextStmts = $this->getNextUnreachableStatements(array_slice($nodes, $i + 1), true); |
322: | $this->processUnreachableStatement($nextStmts, $scope, $nodeCallback); |
323: | } |
324: | } |
325: | |
326: | |
327: | |
328: | |
329: | |
330: | private function processUnreachableStatement(array $nextStmts, MutatingScope $scope, callable $nodeCallback): void |
331: | { |
332: | if ($nextStmts === []) { |
333: | return; |
334: | } |
335: | |
336: | $unreachableStatement = null; |
337: | $nextStatements = []; |
338: | |
339: | foreach ($nextStmts as $key => $nextStmt) { |
340: | if ($key === 0) { |
341: | $unreachableStatement = $nextStmt; |
342: | continue; |
343: | } |
344: | |
345: | $nextStatements[] = $nextStmt; |
346: | } |
347: | |
348: | if (!$unreachableStatement instanceof Node\Stmt) { |
349: | return; |
350: | } |
351: | |
352: | $nodeCallback(new UnreachableStatementNode($unreachableStatement, $nextStatements), $scope); |
353: | } |
354: | |
355: | |
356: | |
357: | |
358: | |
359: | |
360: | public function processStmtNodes( |
361: | Node $parentNode, |
362: | array $stmts, |
363: | MutatingScope $scope, |
364: | callable $nodeCallback, |
365: | StatementContext $context, |
366: | ): StatementResult |
367: | { |
368: | $exitPoints = []; |
369: | $throwPoints = []; |
370: | $impurePoints = []; |
371: | $alreadyTerminated = false; |
372: | $hasYield = false; |
373: | $stmtCount = count($stmts); |
374: | $shouldCheckLastStatement = $parentNode instanceof Node\Stmt\Function_ |
375: | || $parentNode instanceof Node\Stmt\ClassMethod |
376: | || $parentNode instanceof PropertyHookStatementNode |
377: | || $parentNode instanceof Expr\Closure; |
378: | foreach ($stmts as $i => $stmt) { |
379: | if ($alreadyTerminated && !($stmt instanceof Node\Stmt\Function_ || $stmt instanceof Node\Stmt\ClassLike)) { |
380: | continue; |
381: | } |
382: | |
383: | $isLast = $i === $stmtCount - 1; |
384: | $statementResult = $this->processStmtNode( |
385: | $stmt, |
386: | $scope, |
387: | $nodeCallback, |
388: | $context, |
389: | ); |
390: | $scope = $statementResult->getScope(); |
391: | $hasYield = $hasYield || $statementResult->hasYield(); |
392: | |
393: | if ($shouldCheckLastStatement && $isLast) { |
394: | |
395: | $parentNode = $parentNode; |
396: | |
397: | $endStatements = $statementResult->getEndStatements(); |
398: | if (count($endStatements) > 0) { |
399: | foreach ($endStatements as $endStatement) { |
400: | $endStatementResult = $endStatement->getResult(); |
401: | $nodeCallback(new ExecutionEndNode( |
402: | $endStatement->getStatement(), |
403: | new StatementResult( |
404: | $endStatementResult->getScope(), |
405: | $hasYield, |
406: | $endStatementResult->isAlwaysTerminating(), |
407: | $endStatementResult->getExitPoints(), |
408: | $endStatementResult->getThrowPoints(), |
409: | $endStatementResult->getImpurePoints(), |
410: | ), |
411: | $parentNode->getReturnType() !== null, |
412: | ), $endStatementResult->getScope()); |
413: | } |
414: | } else { |
415: | $nodeCallback(new ExecutionEndNode( |
416: | $stmt, |
417: | new StatementResult( |
418: | $scope, |
419: | $hasYield, |
420: | $statementResult->isAlwaysTerminating(), |
421: | $statementResult->getExitPoints(), |
422: | $statementResult->getThrowPoints(), |
423: | $statementResult->getImpurePoints(), |
424: | ), |
425: | $parentNode->getReturnType() !== null, |
426: | ), $scope); |
427: | } |
428: | } |
429: | |
430: | $exitPoints = array_merge($exitPoints, $statementResult->getExitPoints()); |
431: | $throwPoints = array_merge($throwPoints, $statementResult->getThrowPoints()); |
432: | $impurePoints = array_merge($impurePoints, $statementResult->getImpurePoints()); |
433: | |
434: | if ($alreadyTerminated || !$statementResult->isAlwaysTerminating()) { |
435: | continue; |
436: | } |
437: | |
438: | $alreadyTerminated = true; |
439: | $nextStmts = $this->getNextUnreachableStatements(array_slice($stmts, $i + 1), $parentNode instanceof Node\Stmt\Namespace_); |
440: | $this->processUnreachableStatement($nextStmts, $scope, $nodeCallback); |
441: | } |
442: | |
443: | $statementResult = new StatementResult($scope, $hasYield, $alreadyTerminated, $exitPoints, $throwPoints, $impurePoints); |
444: | if ($stmtCount === 0 && $shouldCheckLastStatement) { |
445: | |
446: | $parentNode = $parentNode; |
447: | $returnTypeNode = $parentNode->getReturnType(); |
448: | if ($parentNode instanceof Expr\Closure) { |
449: | $parentNode = new Node\Stmt\Expression($parentNode, $parentNode->getAttributes()); |
450: | } |
451: | $nodeCallback(new ExecutionEndNode( |
452: | $parentNode, |
453: | $statementResult, |
454: | $returnTypeNode !== null, |
455: | ), $scope); |
456: | } |
457: | |
458: | return $statementResult; |
459: | } |
460: | |
461: | |
462: | |
463: | |
464: | private function processStmtNode( |
465: | Node\Stmt $stmt, |
466: | MutatingScope $scope, |
467: | callable $nodeCallback, |
468: | StatementContext $context, |
469: | ): StatementResult |
470: | { |
471: | if ( |
472: | !$stmt instanceof Static_ |
473: | && !$stmt instanceof Foreach_ |
474: | && !$stmt instanceof Node\Stmt\Global_ |
475: | && !$stmt instanceof Node\Stmt\Property |
476: | && !$stmt instanceof Node\Stmt\ClassConst |
477: | && !$stmt instanceof Node\Stmt\Const_ |
478: | ) { |
479: | $scope = $this->processStmtVarAnnotation($scope, $stmt, null, $nodeCallback); |
480: | } |
481: | |
482: | if ($stmt instanceof Node\Stmt\ClassMethod) { |
483: | if (!$scope->isInClass()) { |
484: | throw new ShouldNotHappenException(); |
485: | } |
486: | if ( |
487: | $scope->isInTrait() |
488: | && $scope->getClassReflection()->hasNativeMethod($stmt->name->toString()) |
489: | ) { |
490: | $methodReflection = $scope->getClassReflection()->getNativeMethod($stmt->name->toString()); |
491: | if ($methodReflection instanceof NativeMethodReflection) { |
492: | return new StatementResult($scope, false, false, [], [], []); |
493: | } |
494: | if ($methodReflection instanceof PhpMethodReflection) { |
495: | $declaringTrait = $methodReflection->getDeclaringTrait(); |
496: | if ($declaringTrait === null || $declaringTrait->getName() !== $scope->getTraitReflection()->getName()) { |
497: | return new StatementResult($scope, false, false, [], [], []); |
498: | } |
499: | } |
500: | } |
501: | } |
502: | |
503: | $stmtScope = $scope; |
504: | if ($stmt instanceof Node\Stmt\Expression && $stmt->expr instanceof Expr\Throw_) { |
505: | $stmtScope = $this->processStmtVarAnnotation($scope, $stmt, $stmt->expr->expr, $nodeCallback); |
506: | } |
507: | if ($stmt instanceof Return_) { |
508: | $stmtScope = $this->processStmtVarAnnotation($scope, $stmt, $stmt->expr, $nodeCallback); |
509: | } |
510: | |
511: | $nodeCallback($stmt, $stmtScope); |
512: | |
513: | $overridingThrowPoints = $this->getOverridingThrowPoints($stmt, $scope); |
514: | |
515: | if ($stmt instanceof Node\Stmt\Declare_) { |
516: | $hasYield = false; |
517: | $throwPoints = []; |
518: | $impurePoints = []; |
519: | $alwaysTerminating = false; |
520: | $exitPoints = []; |
521: | foreach ($stmt->declares as $declare) { |
522: | $nodeCallback($declare, $scope); |
523: | $nodeCallback($declare->value, $scope); |
524: | if ( |
525: | $declare->key->name !== 'strict_types' |
526: | || !($declare->value instanceof Node\Scalar\Int_) |
527: | || $declare->value->value !== 1 |
528: | ) { |
529: | continue; |
530: | } |
531: | |
532: | $scope = $scope->enterDeclareStrictTypes(); |
533: | } |
534: | |
535: | if ($stmt->stmts !== null) { |
536: | $result = $this->processStmtNodes($stmt, $stmt->stmts, $scope, $nodeCallback, $context); |
537: | $scope = $result->getScope(); |
538: | $hasYield = $result->hasYield(); |
539: | $throwPoints = $result->getThrowPoints(); |
540: | $impurePoints = $result->getImpurePoints(); |
541: | $alwaysTerminating = $result->isAlwaysTerminating(); |
542: | $exitPoints = $result->getExitPoints(); |
543: | } |
544: | |
545: | return new StatementResult($scope, $hasYield, $alwaysTerminating, $exitPoints, $throwPoints, $impurePoints); |
546: | } elseif ($stmt instanceof Node\Stmt\Function_) { |
547: | $hasYield = false; |
548: | $throwPoints = []; |
549: | $impurePoints = []; |
550: | $this->processAttributeGroups($stmt, $stmt->attrGroups, $scope, $nodeCallback); |
551: | [$templateTypeMap, $phpDocParameterTypes, $phpDocImmediatelyInvokedCallableParameters, $phpDocClosureThisTypeParameters, $phpDocReturnType, $phpDocThrowType, $deprecatedDescription, $isDeprecated, $isInternal, , $isPure, $acceptsNamedArguments, , $phpDocComment, $asserts,, $phpDocParameterOutTypes] = $this->getPhpDocs($scope, $stmt); |
552: | |
553: | foreach ($stmt->params as $param) { |
554: | $this->processParamNode($stmt, $param, $scope, $nodeCallback); |
555: | } |
556: | |
557: | if ($stmt->returnType !== null) { |
558: | $nodeCallback($stmt->returnType, $scope); |
559: | } |
560: | |
561: | if (!$isDeprecated) { |
562: | [$isDeprecated, $deprecatedDescription] = $this->getDeprecatedAttribute($scope, $stmt); |
563: | } |
564: | |
565: | $functionScope = $scope->enterFunction( |
566: | $stmt, |
567: | $templateTypeMap, |
568: | $phpDocParameterTypes, |
569: | $phpDocReturnType, |
570: | $phpDocThrowType, |
571: | $deprecatedDescription, |
572: | $isDeprecated, |
573: | $isInternal, |
574: | $isPure, |
575: | $acceptsNamedArguments, |
576: | $asserts, |
577: | $phpDocComment, |
578: | $phpDocParameterOutTypes, |
579: | $phpDocImmediatelyInvokedCallableParameters, |
580: | $phpDocClosureThisTypeParameters, |
581: | ); |
582: | $functionReflection = $functionScope->getFunction(); |
583: | if (!$functionReflection instanceof PhpFunctionFromParserNodeReflection) { |
584: | throw new ShouldNotHappenException(); |
585: | } |
586: | |
587: | $nodeCallback(new InFunctionNode($functionReflection, $stmt), $functionScope); |
588: | |
589: | $gatheredReturnStatements = []; |
590: | $gatheredYieldStatements = []; |
591: | $executionEnds = []; |
592: | $functionImpurePoints = []; |
593: | $statementResult = $this->processStmtNodes($stmt, $stmt->stmts, $functionScope, static function (Node $node, Scope $scope) use ($nodeCallback, $functionScope, &$gatheredReturnStatements, &$gatheredYieldStatements, &$executionEnds, &$functionImpurePoints): void { |
594: | $nodeCallback($node, $scope); |
595: | if ($scope->getFunction() !== $functionScope->getFunction()) { |
596: | return; |
597: | } |
598: | if ($scope->isInAnonymousFunction()) { |
599: | return; |
600: | } |
601: | if ($node instanceof PropertyAssignNode) { |
602: | $functionImpurePoints[] = new ImpurePoint( |
603: | $scope, |
604: | $node, |
605: | 'propertyAssign', |
606: | 'property assignment', |
607: | true, |
608: | ); |
609: | return; |
610: | } |
611: | if ($node instanceof ExecutionEndNode) { |
612: | $executionEnds[] = $node; |
613: | return; |
614: | } |
615: | if ($node instanceof Expr\Yield_ || $node instanceof Expr\YieldFrom) { |
616: | $gatheredYieldStatements[] = $node; |
617: | } |
618: | if (!$node instanceof Return_) { |
619: | return; |
620: | } |
621: | |
622: | $gatheredReturnStatements[] = new ReturnStatement($scope, $node); |
623: | }, StatementContext::createTopLevel()); |
624: | |
625: | $nodeCallback(new FunctionReturnStatementsNode( |
626: | $stmt, |
627: | $gatheredReturnStatements, |
628: | $gatheredYieldStatements, |
629: | $statementResult, |
630: | $executionEnds, |
631: | array_merge($statementResult->getImpurePoints(), $functionImpurePoints), |
632: | $functionReflection, |
633: | ), $functionScope); |
634: | } elseif ($stmt instanceof Node\Stmt\ClassMethod) { |
635: | $hasYield = false; |
636: | $throwPoints = []; |
637: | $impurePoints = []; |
638: | $this->processAttributeGroups($stmt, $stmt->attrGroups, $scope, $nodeCallback); |
639: | [$templateTypeMap, $phpDocParameterTypes, $phpDocImmediatelyInvokedCallableParameters, $phpDocClosureThisTypeParameters, $phpDocReturnType, $phpDocThrowType, $deprecatedDescription, $isDeprecated, $isInternal, $isFinal, $isPure, $acceptsNamedArguments, , $phpDocComment, $asserts, $selfOutType, $phpDocParameterOutTypes] = $this->getPhpDocs($scope, $stmt); |
640: | |
641: | foreach ($stmt->params as $param) { |
642: | $this->processParamNode($stmt, $param, $scope, $nodeCallback); |
643: | } |
644: | |
645: | if ($stmt->returnType !== null) { |
646: | $nodeCallback($stmt->returnType, $scope); |
647: | } |
648: | |
649: | if (!$isDeprecated) { |
650: | [$isDeprecated, $deprecatedDescription] = $this->getDeprecatedAttribute($scope, $stmt); |
651: | } |
652: | |
653: | $isFromTrait = $stmt->getAttribute('originalTraitMethodName') === '__construct'; |
654: | $isConstructor = $isFromTrait || $stmt->name->toLowerString() === '__construct'; |
655: | |
656: | $methodScope = $scope->enterClassMethod( |
657: | $stmt, |
658: | $templateTypeMap, |
659: | $phpDocParameterTypes, |
660: | $phpDocReturnType, |
661: | $phpDocThrowType, |
662: | $deprecatedDescription, |
663: | $isDeprecated, |
664: | $isInternal, |
665: | $isFinal, |
666: | $isPure, |
667: | $acceptsNamedArguments, |
668: | $asserts, |
669: | $selfOutType, |
670: | $phpDocComment, |
671: | $phpDocParameterOutTypes, |
672: | $phpDocImmediatelyInvokedCallableParameters, |
673: | $phpDocClosureThisTypeParameters, |
674: | $isConstructor, |
675: | ); |
676: | |
677: | if (!$scope->isInClass()) { |
678: | throw new ShouldNotHappenException(); |
679: | } |
680: | |
681: | $classReflection = $scope->getClassReflection(); |
682: | |
683: | if ($isConstructor) { |
684: | foreach ($stmt->params as $param) { |
685: | if ($param->flags === 0 && $param->hooks === []) { |
686: | continue; |
687: | } |
688: | |
689: | if (!$param->var instanceof Variable || !is_string($param->var->name)) { |
690: | throw new ShouldNotHappenException(); |
691: | } |
692: | $phpDoc = null; |
693: | if ($param->getDocComment() !== null) { |
694: | $phpDoc = $param->getDocComment()->getText(); |
695: | } |
696: | $nodeCallback(new ClassPropertyNode( |
697: | $param->var->name, |
698: | $param->flags, |
699: | $param->type !== null ? ParserNodeTypeToPHPStanType::resolve($param->type, $classReflection) : null, |
700: | null, |
701: | $phpDoc, |
702: | $phpDocParameterTypes[$param->var->name] ?? null, |
703: | true, |
704: | $isFromTrait, |
705: | $param, |
706: | false, |
707: | $scope->isInTrait(), |
708: | $classReflection->isReadOnly(), |
709: | false, |
710: | $classReflection, |
711: | ), $methodScope); |
712: | $this->processPropertyHooks( |
713: | $stmt, |
714: | $param->type, |
715: | $phpDocParameterTypes[$param->var->name] ?? null, |
716: | $param->var->name, |
717: | $param->hooks, |
718: | $scope, |
719: | $nodeCallback, |
720: | ); |
721: | $methodScope = $methodScope->assignExpression(new PropertyInitializationExpr($param->var->name), new MixedType(), new MixedType()); |
722: | } |
723: | } |
724: | |
725: | if ($stmt->getAttribute('virtual', false) === false) { |
726: | $methodReflection = $methodScope->getFunction(); |
727: | if (!$methodReflection instanceof PhpMethodFromParserNodeReflection) { |
728: | throw new ShouldNotHappenException(); |
729: | } |
730: | $nodeCallback(new InClassMethodNode($classReflection, $methodReflection, $stmt), $methodScope); |
731: | } |
732: | |
733: | if ($stmt->stmts !== null) { |
734: | $gatheredReturnStatements = []; |
735: | $gatheredYieldStatements = []; |
736: | $executionEnds = []; |
737: | $methodImpurePoints = []; |
738: | $statementResult = $this->processStmtNodes($stmt, $stmt->stmts, $methodScope, static function (Node $node, Scope $scope) use ($nodeCallback, $methodScope, &$gatheredReturnStatements, &$gatheredYieldStatements, &$executionEnds, &$methodImpurePoints): void { |
739: | $nodeCallback($node, $scope); |
740: | if ($scope->getFunction() !== $methodScope->getFunction()) { |
741: | return; |
742: | } |
743: | if ($scope->isInAnonymousFunction()) { |
744: | return; |
745: | } |
746: | if ($node instanceof PropertyAssignNode) { |
747: | if ( |
748: | $node->getPropertyFetch() instanceof Expr\PropertyFetch |
749: | && $scope->getFunction() instanceof PhpMethodFromParserNodeReflection |
750: | && $scope->getFunction()->getDeclaringClass()->hasConstructor() |
751: | && $scope->getFunction()->getDeclaringClass()->getConstructor()->getName() === $scope->getFunction()->getName() |
752: | && TypeUtils::findThisType($scope->getType($node->getPropertyFetch()->var)) !== null |
753: | ) { |
754: | return; |
755: | } |
756: | $methodImpurePoints[] = new ImpurePoint( |
757: | $scope, |
758: | $node, |
759: | 'propertyAssign', |
760: | 'property assignment', |
761: | true, |
762: | ); |
763: | return; |
764: | } |
765: | if ($node instanceof ExecutionEndNode) { |
766: | $executionEnds[] = $node; |
767: | return; |
768: | } |
769: | if ($node instanceof Expr\Yield_ || $node instanceof Expr\YieldFrom) { |
770: | $gatheredYieldStatements[] = $node; |
771: | } |
772: | if (!$node instanceof Return_) { |
773: | return; |
774: | } |
775: | |
776: | $gatheredReturnStatements[] = new ReturnStatement($scope, $node); |
777: | }, StatementContext::createTopLevel()); |
778: | |
779: | $methodReflection = $methodScope->getFunction(); |
780: | if (!$methodReflection instanceof PhpMethodFromParserNodeReflection) { |
781: | throw new ShouldNotHappenException(); |
782: | } |
783: | |
784: | $nodeCallback(new MethodReturnStatementsNode( |
785: | $stmt, |
786: | $gatheredReturnStatements, |
787: | $gatheredYieldStatements, |
788: | $statementResult, |
789: | $executionEnds, |
790: | array_merge($statementResult->getImpurePoints(), $methodImpurePoints), |
791: | $classReflection, |
792: | $methodReflection, |
793: | ), $methodScope); |
794: | } |
795: | } elseif ($stmt instanceof Echo_) { |
796: | $hasYield = false; |
797: | $throwPoints = []; |
798: | foreach ($stmt->exprs as $echoExpr) { |
799: | $result = $this->processExprNode($stmt, $echoExpr, $scope, $nodeCallback, ExpressionContext::createDeep()); |
800: | $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); |
801: | $scope = $result->getScope(); |
802: | $hasYield = $hasYield || $result->hasYield(); |
803: | } |
804: | |
805: | $throwPoints = $overridingThrowPoints ?? $throwPoints; |
806: | $impurePoints = [ |
807: | new ImpurePoint($scope, $stmt, 'echo', 'echo', true), |
808: | ]; |
809: | } elseif ($stmt instanceof Return_) { |
810: | if ($stmt->expr !== null) { |
811: | $result = $this->processExprNode($stmt, $stmt->expr, $scope, $nodeCallback, ExpressionContext::createDeep()); |
812: | $throwPoints = $result->getThrowPoints(); |
813: | $impurePoints = $result->getImpurePoints(); |
814: | $scope = $result->getScope(); |
815: | $hasYield = $result->hasYield(); |
816: | } else { |
817: | $hasYield = false; |
818: | $throwPoints = []; |
819: | $impurePoints = []; |
820: | } |
821: | |
822: | return new StatementResult($scope, $hasYield, true, [ |
823: | new StatementExitPoint($stmt, $scope), |
824: | ], $overridingThrowPoints ?? $throwPoints, $impurePoints); |
825: | } elseif ($stmt instanceof Continue_ || $stmt instanceof Break_) { |
826: | if ($stmt->num !== null) { |
827: | $result = $this->processExprNode($stmt, $stmt->num, $scope, $nodeCallback, ExpressionContext::createDeep()); |
828: | $scope = $result->getScope(); |
829: | $hasYield = $result->hasYield(); |
830: | $throwPoints = $result->getThrowPoints(); |
831: | $impurePoints = $result->getImpurePoints(); |
832: | } else { |
833: | $hasYield = false; |
834: | $throwPoints = []; |
835: | $impurePoints = []; |
836: | } |
837: | |
838: | return new StatementResult($scope, $hasYield, true, [ |
839: | new StatementExitPoint($stmt, $scope), |
840: | ], $overridingThrowPoints ?? $throwPoints, $impurePoints); |
841: | } elseif ($stmt instanceof Node\Stmt\Expression) { |
842: | if ($stmt->expr instanceof Expr\Throw_) { |
843: | $scope = $stmtScope; |
844: | } |
845: | $earlyTerminationExpr = $this->findEarlyTerminatingExpr($stmt->expr, $scope); |
846: | $hasAssign = false; |
847: | $currentScope = $scope; |
848: | $result = $this->processExprNode($stmt, $stmt->expr, $scope, static function (Node $node, Scope $scope) use ($nodeCallback, $currentScope, &$hasAssign): void { |
849: | $nodeCallback($node, $scope); |
850: | if ($scope->getAnonymousFunctionReflection() !== $currentScope->getAnonymousFunctionReflection()) { |
851: | return; |
852: | } |
853: | if ($scope->getFunction() !== $currentScope->getFunction()) { |
854: | return; |
855: | } |
856: | if (!$node instanceof VariableAssignNode && !$node instanceof PropertyAssignNode) { |
857: | return; |
858: | } |
859: | |
860: | $hasAssign = true; |
861: | }, ExpressionContext::createTopLevel()); |
862: | $throwPoints = array_filter($result->getThrowPoints(), static fn ($throwPoint) => $throwPoint->isExplicit()); |
863: | if ( |
864: | count($result->getImpurePoints()) === 0 |
865: | && count($throwPoints) === 0 |
866: | && !$stmt->expr instanceof Expr\PostInc |
867: | && !$stmt->expr instanceof Expr\PreInc |
868: | && !$stmt->expr instanceof Expr\PostDec |
869: | && !$stmt->expr instanceof Expr\PreDec |
870: | ) { |
871: | $nodeCallback(new NoopExpressionNode($stmt->expr, $hasAssign), $scope); |
872: | } |
873: | $scope = $result->getScope(); |
874: | $scope = $scope->filterBySpecifiedTypes($this->typeSpecifier->specifyTypesInCondition( |
875: | $scope, |
876: | $stmt->expr, |
877: | TypeSpecifierContext::createNull(), |
878: | )); |
879: | $hasYield = $result->hasYield(); |
880: | $throwPoints = $result->getThrowPoints(); |
881: | $impurePoints = $result->getImpurePoints(); |
882: | if ($earlyTerminationExpr !== null) { |
883: | return new StatementResult($scope, $hasYield, true, [ |
884: | new StatementExitPoint($stmt, $scope), |
885: | ], $overridingThrowPoints ?? $throwPoints, $impurePoints); |
886: | } |
887: | return new StatementResult($scope, $hasYield, false, [], $overridingThrowPoints ?? $throwPoints, $impurePoints); |
888: | } elseif ($stmt instanceof Node\Stmt\Namespace_) { |
889: | if ($stmt->name !== null) { |
890: | $scope = $scope->enterNamespace($stmt->name->toString()); |
891: | } |
892: | |
893: | $scope = $this->processStmtNodes($stmt, $stmt->stmts, $scope, $nodeCallback, $context)->getScope(); |
894: | $hasYield = false; |
895: | $throwPoints = []; |
896: | $impurePoints = []; |
897: | } elseif ($stmt instanceof Node\Stmt\Trait_) { |
898: | return new StatementResult($scope, false, false, [], [], []); |
899: | } elseif ($stmt instanceof Node\Stmt\ClassLike) { |
900: | if (!$context->isTopLevel()) { |
901: | return new StatementResult($scope, false, false, [], [], []); |
902: | } |
903: | $hasYield = false; |
904: | $throwPoints = []; |
905: | $impurePoints = []; |
906: | if (isset($stmt->namespacedName)) { |
907: | $classReflection = $this->getCurrentClassReflection($stmt, $stmt->namespacedName->toString(), $scope); |
908: | $classScope = $scope->enterClass($classReflection); |
909: | $nodeCallback(new InClassNode($stmt, $classReflection), $classScope); |
910: | } elseif ($stmt instanceof Class_) { |
911: | if ($stmt->name === null) { |
912: | throw new ShouldNotHappenException(); |
913: | } |
914: | if (!$stmt->isAnonymous()) { |
915: | $classReflection = $this->reflectionProvider->getClass($stmt->name->toString()); |
916: | } else { |
917: | $classReflection = $this->reflectionProvider->getAnonymousClassReflection($stmt, $scope); |
918: | } |
919: | $classScope = $scope->enterClass($classReflection); |
920: | $nodeCallback(new InClassNode($stmt, $classReflection), $classScope); |
921: | } else { |
922: | throw new ShouldNotHappenException(); |
923: | } |
924: | |
925: | $classStatementsGatherer = new ClassStatementsGatherer($classReflection, $nodeCallback); |
926: | $this->processAttributeGroups($stmt, $stmt->attrGroups, $classScope, $classStatementsGatherer); |
927: | |
928: | $this->processStmtNodes($stmt, $stmt->stmts, $classScope, $classStatementsGatherer, $context); |
929: | $nodeCallback(new ClassPropertiesNode($stmt, $this->readWritePropertiesExtensionProvider, $classStatementsGatherer->getProperties(), $classStatementsGatherer->getPropertyUsages(), $classStatementsGatherer->getMethodCalls(), $classStatementsGatherer->getReturnStatementsNodes(), $classStatementsGatherer->getPropertyAssigns(), $classReflection), $classScope); |
930: | $nodeCallback(new ClassMethodsNode($stmt, $classStatementsGatherer->getMethods(), $classStatementsGatherer->getMethodCalls(), $classReflection), $classScope); |
931: | $nodeCallback(new ClassConstantsNode($stmt, $classStatementsGatherer->getConstants(), $classStatementsGatherer->getConstantFetches(), $classReflection), $classScope); |
932: | $classReflection->evictPrivateSymbols(); |
933: | $this->calledMethodResults = []; |
934: | } elseif ($stmt instanceof Node\Stmt\Property) { |
935: | $hasYield = false; |
936: | $throwPoints = []; |
937: | $impurePoints = []; |
938: | $this->processAttributeGroups($stmt, $stmt->attrGroups, $scope, $nodeCallback); |
939: | |
940: | $nativePropertyType = $stmt->type !== null ? ParserNodeTypeToPHPStanType::resolve($stmt->type, $scope->getClassReflection()) : null; |
941: | |
942: | [,,,,,,,,,,,,$isReadOnly, $docComment, ,,,$varTags, $isAllowedPrivateMutation] = $this->getPhpDocs($scope, $stmt); |
943: | $phpDocType = null; |
944: | if (isset($varTags[0]) && count($varTags) === 1) { |
945: | $phpDocType = $varTags[0]->getType(); |
946: | } |
947: | |
948: | foreach ($stmt->props as $prop) { |
949: | $nodeCallback($prop, $scope); |
950: | if ($prop->default !== null) { |
951: | $this->processExprNode($stmt, $prop->default, $scope, $nodeCallback, ExpressionContext::createDeep()); |
952: | } |
953: | |
954: | if (!$scope->isInClass()) { |
955: | throw new ShouldNotHappenException(); |
956: | } |
957: | $propertyName = $prop->name->toString(); |
958: | |
959: | if ($phpDocType === null) { |
960: | if (isset($varTags[$propertyName])) { |
961: | $phpDocType = $varTags[$propertyName]->getType(); |
962: | } |
963: | } |
964: | |
965: | $propStmt = clone $stmt; |
966: | $propStmt->setAttributes($prop->getAttributes()); |
967: | $nodeCallback( |
968: | new ClassPropertyNode( |
969: | $propertyName, |
970: | $stmt->flags, |
971: | $nativePropertyType, |
972: | $prop->default, |
973: | $docComment, |
974: | $phpDocType, |
975: | false, |
976: | false, |
977: | $propStmt, |
978: | $isReadOnly, |
979: | $scope->isInTrait(), |
980: | $scope->getClassReflection()->isReadOnly(), |
981: | $isAllowedPrivateMutation, |
982: | $scope->getClassReflection(), |
983: | ), |
984: | $scope, |
985: | ); |
986: | } |
987: | |
988: | if (count($stmt->hooks) > 0) { |
989: | if (!isset($propertyName)) { |
990: | throw new ShouldNotHappenException('Property name should be known when analysing hooks.'); |
991: | } |
992: | $this->processPropertyHooks( |
993: | $stmt, |
994: | $stmt->type, |
995: | $phpDocType, |
996: | $propertyName, |
997: | $stmt->hooks, |
998: | $scope, |
999: | $nodeCallback, |
1000: | ); |
1001: | } |
1002: | |
1003: | if ($stmt->type !== null) { |
1004: | $nodeCallback($stmt->type, $scope); |
1005: | } |
1006: | } elseif ($stmt instanceof If_) { |
1007: | $conditionType = ($this->treatPhpDocTypesAsCertain ? $scope->getType($stmt->cond) : $scope->getNativeType($stmt->cond))->toBoolean(); |
1008: | $ifAlwaysTrue = $conditionType->isTrue()->yes(); |
1009: | $condResult = $this->processExprNode($stmt, $stmt->cond, $scope, $nodeCallback, ExpressionContext::createDeep()); |
1010: | $exitPoints = []; |
1011: | $throwPoints = $overridingThrowPoints ?? $condResult->getThrowPoints(); |
1012: | $impurePoints = $condResult->getImpurePoints(); |
1013: | $endStatements = []; |
1014: | $finalScope = null; |
1015: | $alwaysTerminating = true; |
1016: | $hasYield = $condResult->hasYield(); |
1017: | |
1018: | $branchScopeStatementResult = $this->processStmtNodes($stmt, $stmt->stmts, $condResult->getTruthyScope(), $nodeCallback, $context); |
1019: | |
1020: | if (!$conditionType instanceof ConstantBooleanType || $conditionType->getValue()) { |
1021: | $exitPoints = $branchScopeStatementResult->getExitPoints(); |
1022: | $throwPoints = array_merge($throwPoints, $branchScopeStatementResult->getThrowPoints()); |
1023: | $impurePoints = array_merge($impurePoints, $branchScopeStatementResult->getImpurePoints()); |
1024: | $branchScope = $branchScopeStatementResult->getScope(); |
1025: | $finalScope = $branchScopeStatementResult->isAlwaysTerminating() ? null : $branchScope; |
1026: | $alwaysTerminating = $branchScopeStatementResult->isAlwaysTerminating(); |
1027: | if (count($branchScopeStatementResult->getEndStatements()) > 0) { |
1028: | $endStatements = array_merge($endStatements, $branchScopeStatementResult->getEndStatements()); |
1029: | } elseif (count($stmt->stmts) > 0) { |
1030: | $endStatements[] = new EndStatementResult($stmt->stmts[count($stmt->stmts) - 1], $branchScopeStatementResult); |
1031: | } else { |
1032: | $endStatements[] = new EndStatementResult($stmt, $branchScopeStatementResult); |
1033: | } |
1034: | $hasYield = $branchScopeStatementResult->hasYield() || $hasYield; |
1035: | } |
1036: | |
1037: | $scope = $condResult->getFalseyScope(); |
1038: | $lastElseIfConditionIsTrue = false; |
1039: | |
1040: | $condScope = $scope; |
1041: | foreach ($stmt->elseifs as $elseif) { |
1042: | $nodeCallback($elseif, $scope); |
1043: | $elseIfConditionType = ($this->treatPhpDocTypesAsCertain ? $condScope->getType($elseif->cond) : $scope->getNativeType($elseif->cond))->toBoolean(); |
1044: | $condResult = $this->processExprNode($stmt, $elseif->cond, $condScope, $nodeCallback, ExpressionContext::createDeep()); |
1045: | $throwPoints = array_merge($throwPoints, $condResult->getThrowPoints()); |
1046: | $impurePoints = array_merge($impurePoints, $condResult->getImpurePoints()); |
1047: | $condScope = $condResult->getScope(); |
1048: | $branchScopeStatementResult = $this->processStmtNodes($elseif, $elseif->stmts, $condResult->getTruthyScope(), $nodeCallback, $context); |
1049: | |
1050: | if ( |
1051: | !$ifAlwaysTrue |
1052: | && ( |
1053: | !$lastElseIfConditionIsTrue |
1054: | && ( |
1055: | !$elseIfConditionType instanceof ConstantBooleanType |
1056: | || $elseIfConditionType->getValue() |
1057: | ) |
1058: | ) |
1059: | ) { |
1060: | $exitPoints = array_merge($exitPoints, $branchScopeStatementResult->getExitPoints()); |
1061: | $throwPoints = array_merge($throwPoints, $branchScopeStatementResult->getThrowPoints()); |
1062: | $impurePoints = array_merge($impurePoints, $branchScopeStatementResult->getImpurePoints()); |
1063: | $branchScope = $branchScopeStatementResult->getScope(); |
1064: | $finalScope = $branchScopeStatementResult->isAlwaysTerminating() ? $finalScope : $branchScope->mergeWith($finalScope); |
1065: | $alwaysTerminating = $alwaysTerminating && $branchScopeStatementResult->isAlwaysTerminating(); |
1066: | if (count($branchScopeStatementResult->getEndStatements()) > 0) { |
1067: | $endStatements = array_merge($endStatements, $branchScopeStatementResult->getEndStatements()); |
1068: | } elseif (count($elseif->stmts) > 0) { |
1069: | $endStatements[] = new EndStatementResult($elseif->stmts[count($elseif->stmts) - 1], $branchScopeStatementResult); |
1070: | } else { |
1071: | $endStatements[] = new EndStatementResult($elseif, $branchScopeStatementResult); |
1072: | } |
1073: | $hasYield = $hasYield || $branchScopeStatementResult->hasYield(); |
1074: | } |
1075: | |
1076: | if ( |
1077: | $elseIfConditionType->isTrue()->yes() |
1078: | ) { |
1079: | $lastElseIfConditionIsTrue = true; |
1080: | } |
1081: | |
1082: | $condScope = $condScope->filterByFalseyValue($elseif->cond); |
1083: | $scope = $condScope; |
1084: | } |
1085: | |
1086: | if ($stmt->else === null) { |
1087: | if (!$ifAlwaysTrue && !$lastElseIfConditionIsTrue) { |
1088: | $finalScope = $scope->mergeWith($finalScope); |
1089: | $alwaysTerminating = false; |
1090: | } |
1091: | } else { |
1092: | $nodeCallback($stmt->else, $scope); |
1093: | $branchScopeStatementResult = $this->processStmtNodes($stmt->else, $stmt->else->stmts, $scope, $nodeCallback, $context); |
1094: | |
1095: | if (!$ifAlwaysTrue && !$lastElseIfConditionIsTrue) { |
1096: | $exitPoints = array_merge($exitPoints, $branchScopeStatementResult->getExitPoints()); |
1097: | $throwPoints = array_merge($throwPoints, $branchScopeStatementResult->getThrowPoints()); |
1098: | $impurePoints = array_merge($impurePoints, $branchScopeStatementResult->getImpurePoints()); |
1099: | $branchScope = $branchScopeStatementResult->getScope(); |
1100: | $finalScope = $branchScopeStatementResult->isAlwaysTerminating() ? $finalScope : $branchScope->mergeWith($finalScope); |
1101: | $alwaysTerminating = $alwaysTerminating && $branchScopeStatementResult->isAlwaysTerminating(); |
1102: | if (count($branchScopeStatementResult->getEndStatements()) > 0) { |
1103: | $endStatements = array_merge($endStatements, $branchScopeStatementResult->getEndStatements()); |
1104: | } elseif (count($stmt->else->stmts) > 0) { |
1105: | $endStatements[] = new EndStatementResult($stmt->else->stmts[count($stmt->else->stmts) - 1], $branchScopeStatementResult); |
1106: | } else { |
1107: | $endStatements[] = new EndStatementResult($stmt->else, $branchScopeStatementResult); |
1108: | } |
1109: | $hasYield = $hasYield || $branchScopeStatementResult->hasYield(); |
1110: | } |
1111: | } |
1112: | |
1113: | if ($finalScope === null) { |
1114: | $finalScope = $scope; |
1115: | } |
1116: | |
1117: | if ($stmt->else === null && !$ifAlwaysTrue && !$lastElseIfConditionIsTrue) { |
1118: | $endStatements[] = new EndStatementResult($stmt, new StatementResult($finalScope, $hasYield, $alwaysTerminating, $exitPoints, $throwPoints, $impurePoints)); |
1119: | } |
1120: | |
1121: | return new StatementResult($finalScope, $hasYield, $alwaysTerminating, $exitPoints, $throwPoints, $impurePoints, $endStatements); |
1122: | } elseif ($stmt instanceof Node\Stmt\TraitUse) { |
1123: | $hasYield = false; |
1124: | $throwPoints = []; |
1125: | $impurePoints = []; |
1126: | $this->processTraitUse($stmt, $scope, $nodeCallback); |
1127: | } elseif ($stmt instanceof Foreach_) { |
1128: | $condResult = $this->processExprNode($stmt, $stmt->expr, $scope, $nodeCallback, ExpressionContext::createDeep()); |
1129: | $throwPoints = $overridingThrowPoints ?? $condResult->getThrowPoints(); |
1130: | $impurePoints = $condResult->getImpurePoints(); |
1131: | $scope = $condResult->getScope(); |
1132: | $arrayComparisonExpr = new BinaryOp\NotIdentical( |
1133: | $stmt->expr, |
1134: | new Array_([]), |
1135: | ); |
1136: | if ($stmt->expr instanceof Variable && is_string($stmt->expr->name)) { |
1137: | $scope = $this->processVarAnnotation($scope, [$stmt->expr->name], $stmt); |
1138: | } |
1139: | $nodeCallback(new InForeachNode($stmt), $scope); |
1140: | $originalScope = $scope; |
1141: | $bodyScope = $scope; |
1142: | |
1143: | if ($context->isTopLevel()) { |
1144: | $originalScope = $this->polluteScopeWithAlwaysIterableForeach ? $scope->filterByTruthyValue($arrayComparisonExpr) : $scope; |
1145: | $bodyScope = $this->enterForeach($originalScope, $originalScope, $stmt); |
1146: | $count = 0; |
1147: | do { |
1148: | $prevScope = $bodyScope; |
1149: | $bodyScope = $bodyScope->mergeWith($this->polluteScopeWithAlwaysIterableForeach ? $scope->filterByTruthyValue($arrayComparisonExpr) : $scope); |
1150: | $bodyScope = $this->enterForeach($bodyScope, $originalScope, $stmt); |
1151: | $bodyScopeResult = $this->processStmtNodes($stmt, $stmt->stmts, $bodyScope, static function (): void { |
1152: | }, $context->enterDeep())->filterOutLoopExitPoints(); |
1153: | $bodyScope = $bodyScopeResult->getScope(); |
1154: | foreach ($bodyScopeResult->getExitPointsByType(Continue_::class) as $continueExitPoint) { |
1155: | $bodyScope = $bodyScope->mergeWith($continueExitPoint->getScope()); |
1156: | } |
1157: | if ($bodyScope->equals($prevScope)) { |
1158: | break; |
1159: | } |
1160: | |
1161: | if ($count >= self::GENERALIZE_AFTER_ITERATION) { |
1162: | $bodyScope = $prevScope->generalizeWith($bodyScope); |
1163: | } |
1164: | $count++; |
1165: | } while ($count < self::LOOP_SCOPE_ITERATIONS); |
1166: | } |
1167: | |
1168: | $bodyScope = $bodyScope->mergeWith($this->polluteScopeWithAlwaysIterableForeach ? $scope->filterByTruthyValue($arrayComparisonExpr) : $scope); |
1169: | $bodyScope = $this->enterForeach($bodyScope, $originalScope, $stmt); |
1170: | $finalScopeResult = $this->processStmtNodes($stmt, $stmt->stmts, $bodyScope, $nodeCallback, $context)->filterOutLoopExitPoints(); |
1171: | $finalScope = $finalScopeResult->getScope(); |
1172: | foreach ($finalScopeResult->getExitPointsByType(Continue_::class) as $continueExitPoint) { |
1173: | $finalScope = $continueExitPoint->getScope()->mergeWith($finalScope); |
1174: | } |
1175: | foreach ($finalScopeResult->getExitPointsByType(Break_::class) as $breakExitPoint) { |
1176: | $finalScope = $breakExitPoint->getScope()->mergeWith($finalScope); |
1177: | } |
1178: | |
1179: | $exprType = $scope->getType($stmt->expr); |
1180: | $isIterableAtLeastOnce = $exprType->isIterableAtLeastOnce(); |
1181: | if ($exprType->isIterable()->no() || $isIterableAtLeastOnce->maybe()) { |
1182: | $finalScope = $finalScope->mergeWith($scope->filterByTruthyValue(new BooleanOr( |
1183: | new BinaryOp\Identical( |
1184: | $stmt->expr, |
1185: | new Array_([]), |
1186: | ), |
1187: | new FuncCall(new Name\FullyQualified('is_object'), [ |
1188: | new Arg($stmt->expr), |
1189: | ]), |
1190: | ))); |
1191: | } elseif ($isIterableAtLeastOnce->no() || $finalScopeResult->isAlwaysTerminating()) { |
1192: | $finalScope = $scope; |
1193: | } elseif (!$this->polluteScopeWithAlwaysIterableForeach) { |
1194: | $finalScope = $scope->processAlwaysIterableForeachScopeWithoutPollute($finalScope); |
1195: | |
1196: | } |
1197: | |
1198: | if (!$isIterableAtLeastOnce->no()) { |
1199: | $throwPoints = array_merge($throwPoints, $finalScopeResult->getThrowPoints()); |
1200: | $impurePoints = array_merge($impurePoints, $finalScopeResult->getImpurePoints()); |
1201: | } |
1202: | if (!(new ObjectType(Traversable::class))->isSuperTypeOf($scope->getType($stmt->expr))->no()) { |
1203: | $throwPoints[] = ThrowPoint::createImplicit($scope, $stmt->expr); |
1204: | } |
1205: | |
1206: | return new StatementResult( |
1207: | $finalScope, |
1208: | $finalScopeResult->hasYield() || $condResult->hasYield(), |
1209: | $isIterableAtLeastOnce->yes() && $finalScopeResult->isAlwaysTerminating(), |
1210: | $finalScopeResult->getExitPointsForOuterLoop(), |
1211: | $throwPoints, |
1212: | $impurePoints, |
1213: | ); |
1214: | } elseif ($stmt instanceof While_) { |
1215: | $condResult = $this->processExprNode($stmt, $stmt->cond, $scope, static function (): void { |
1216: | }, ExpressionContext::createDeep()); |
1217: | $bodyScope = $condResult->getTruthyScope(); |
1218: | |
1219: | if ($context->isTopLevel()) { |
1220: | $count = 0; |
1221: | do { |
1222: | $prevScope = $bodyScope; |
1223: | $bodyScope = $bodyScope->mergeWith($scope); |
1224: | $bodyScope = $this->processExprNode($stmt, $stmt->cond, $bodyScope, static function (): void { |
1225: | }, ExpressionContext::createDeep())->getTruthyScope(); |
1226: | $bodyScopeResult = $this->processStmtNodes($stmt, $stmt->stmts, $bodyScope, static function (): void { |
1227: | }, $context->enterDeep())->filterOutLoopExitPoints(); |
1228: | $bodyScope = $bodyScopeResult->getScope(); |
1229: | foreach ($bodyScopeResult->getExitPointsByType(Continue_::class) as $continueExitPoint) { |
1230: | $bodyScope = $bodyScope->mergeWith($continueExitPoint->getScope()); |
1231: | } |
1232: | if ($bodyScope->equals($prevScope)) { |
1233: | break; |
1234: | } |
1235: | |
1236: | if ($count >= self::GENERALIZE_AFTER_ITERATION) { |
1237: | $bodyScope = $prevScope->generalizeWith($bodyScope); |
1238: | } |
1239: | $count++; |
1240: | } while ($count < self::LOOP_SCOPE_ITERATIONS); |
1241: | } |
1242: | |
1243: | $bodyScope = $bodyScope->mergeWith($scope); |
1244: | $bodyScopeMaybeRan = $bodyScope; |
1245: | $bodyScope = $this->processExprNode($stmt, $stmt->cond, $bodyScope, $nodeCallback, ExpressionContext::createDeep())->getTruthyScope(); |
1246: | $finalScopeResult = $this->processStmtNodes($stmt, $stmt->stmts, $bodyScope, $nodeCallback, $context)->filterOutLoopExitPoints(); |
1247: | $finalScope = $finalScopeResult->getScope()->filterByFalseyValue($stmt->cond); |
1248: | |
1249: | $condBooleanType = ($this->treatPhpDocTypesAsCertain ? $bodyScopeMaybeRan->getType($stmt->cond) : $bodyScopeMaybeRan->getNativeType($stmt->cond))->toBoolean(); |
1250: | $alwaysIterates = $condBooleanType->isTrue()->yes() && $context->isTopLevel(); |
1251: | $neverIterates = $condBooleanType->isFalse()->yes() && $context->isTopLevel(); |
1252: | if (!$alwaysIterates) { |
1253: | foreach ($finalScopeResult->getExitPointsByType(Continue_::class) as $continueExitPoint) { |
1254: | $finalScope = $finalScope->mergeWith($continueExitPoint->getScope()); |
1255: | } |
1256: | } |
1257: | |
1258: | $breakExitPoints = $finalScopeResult->getExitPointsByType(Break_::class); |
1259: | foreach ($breakExitPoints as $breakExitPoint) { |
1260: | $finalScope = $finalScope->mergeWith($breakExitPoint->getScope()); |
1261: | } |
1262: | |
1263: | $beforeCondBooleanType = ($this->treatPhpDocTypesAsCertain ? $scope->getType($stmt->cond) : $scope->getNativeType($stmt->cond))->toBoolean(); |
1264: | $isIterableAtLeastOnce = $beforeCondBooleanType->isTrue()->yes(); |
1265: | $nodeCallback(new BreaklessWhileLoopNode($stmt, $finalScopeResult->getExitPoints()), $bodyScopeMaybeRan); |
1266: | |
1267: | if ($alwaysIterates) { |
1268: | $isAlwaysTerminating = count($finalScopeResult->getExitPointsByType(Break_::class)) === 0; |
1269: | } elseif ($isIterableAtLeastOnce) { |
1270: | $isAlwaysTerminating = $finalScopeResult->isAlwaysTerminating(); |
1271: | } else { |
1272: | $isAlwaysTerminating = false; |
1273: | } |
1274: | $condScope = $condResult->getFalseyScope(); |
1275: | if (!$isIterableAtLeastOnce) { |
1276: | if (!$this->polluteScopeWithLoopInitialAssignments) { |
1277: | $condScope = $condScope->mergeWith($scope); |
1278: | } |
1279: | $finalScope = $finalScope->mergeWith($condScope); |
1280: | } |
1281: | |
1282: | $throwPoints = $overridingThrowPoints ?? $condResult->getThrowPoints(); |
1283: | $impurePoints = $condResult->getImpurePoints(); |
1284: | if (!$neverIterates) { |
1285: | $throwPoints = array_merge($throwPoints, $finalScopeResult->getThrowPoints()); |
1286: | $impurePoints = array_merge($impurePoints, $finalScopeResult->getImpurePoints()); |
1287: | } |
1288: | |
1289: | return new StatementResult( |
1290: | $finalScope, |
1291: | $finalScopeResult->hasYield() || $condResult->hasYield(), |
1292: | $isAlwaysTerminating, |
1293: | $finalScopeResult->getExitPointsForOuterLoop(), |
1294: | $throwPoints, |
1295: | $impurePoints, |
1296: | ); |
1297: | } elseif ($stmt instanceof Do_) { |
1298: | $finalScope = null; |
1299: | $bodyScope = $scope; |
1300: | $count = 0; |
1301: | $hasYield = false; |
1302: | $throwPoints = []; |
1303: | $impurePoints = []; |
1304: | |
1305: | if ($context->isTopLevel()) { |
1306: | do { |
1307: | $prevScope = $bodyScope; |
1308: | $bodyScope = $bodyScope->mergeWith($scope); |
1309: | $bodyScopeResult = $this->processStmtNodes($stmt, $stmt->stmts, $bodyScope, static function (): void { |
1310: | }, $context->enterDeep())->filterOutLoopExitPoints(); |
1311: | $alwaysTerminating = $bodyScopeResult->isAlwaysTerminating(); |
1312: | $bodyScope = $bodyScopeResult->getScope(); |
1313: | foreach ($bodyScopeResult->getExitPointsByType(Continue_::class) as $continueExitPoint) { |
1314: | $bodyScope = $bodyScope->mergeWith($continueExitPoint->getScope()); |
1315: | } |
1316: | $finalScope = $alwaysTerminating ? $finalScope : $bodyScope->mergeWith($finalScope); |
1317: | foreach ($bodyScopeResult->getExitPointsByType(Break_::class) as $breakExitPoint) { |
1318: | $finalScope = $breakExitPoint->getScope()->mergeWith($finalScope); |
1319: | } |
1320: | $bodyScope = $this->processExprNode($stmt, $stmt->cond, $bodyScope, static function (): void { |
1321: | }, ExpressionContext::createDeep())->getTruthyScope(); |
1322: | if ($bodyScope->equals($prevScope)) { |
1323: | break; |
1324: | } |
1325: | |
1326: | if ($count >= self::GENERALIZE_AFTER_ITERATION) { |
1327: | $bodyScope = $prevScope->generalizeWith($bodyScope); |
1328: | } |
1329: | $count++; |
1330: | } while ($count < self::LOOP_SCOPE_ITERATIONS); |
1331: | |
1332: | $bodyScope = $bodyScope->mergeWith($scope); |
1333: | } |
1334: | |
1335: | $bodyScopeResult = $this->processStmtNodes($stmt, $stmt->stmts, $bodyScope, $nodeCallback, $context)->filterOutLoopExitPoints(); |
1336: | $bodyScope = $bodyScopeResult->getScope(); |
1337: | foreach ($bodyScopeResult->getExitPointsByType(Continue_::class) as $continueExitPoint) { |
1338: | $bodyScope = $bodyScope->mergeWith($continueExitPoint->getScope()); |
1339: | } |
1340: | $condBooleanType = ($this->treatPhpDocTypesAsCertain ? $bodyScope->getType($stmt->cond) : $bodyScope->getNativeType($stmt->cond))->toBoolean(); |
1341: | $alwaysIterates = $condBooleanType->isTrue()->yes() && $context->isTopLevel(); |
1342: | |
1343: | $nodeCallback(new DoWhileLoopConditionNode($stmt->cond, $bodyScopeResult->getExitPoints()), $bodyScope); |
1344: | |
1345: | if ($alwaysIterates) { |
1346: | $alwaysTerminating = count($bodyScopeResult->getExitPointsByType(Break_::class)) === 0; |
1347: | } else { |
1348: | $alwaysTerminating = $bodyScopeResult->isAlwaysTerminating(); |
1349: | } |
1350: | $finalScope = $alwaysTerminating ? $finalScope : $bodyScope->mergeWith($finalScope); |
1351: | if ($finalScope === null) { |
1352: | $finalScope = $scope; |
1353: | } |
1354: | if (!$alwaysTerminating) { |
1355: | $condResult = $this->processExprNode($stmt, $stmt->cond, $bodyScope, $nodeCallback, ExpressionContext::createDeep()); |
1356: | $hasYield = $condResult->hasYield(); |
1357: | $throwPoints = $condResult->getThrowPoints(); |
1358: | $impurePoints = $condResult->getImpurePoints(); |
1359: | $finalScope = $condResult->getFalseyScope(); |
1360: | } else { |
1361: | $this->processExprNode($stmt, $stmt->cond, $bodyScope, $nodeCallback, ExpressionContext::createDeep()); |
1362: | } |
1363: | foreach ($bodyScopeResult->getExitPointsByType(Break_::class) as $breakExitPoint) { |
1364: | $finalScope = $breakExitPoint->getScope()->mergeWith($finalScope); |
1365: | } |
1366: | |
1367: | return new StatementResult( |
1368: | $finalScope, |
1369: | $bodyScopeResult->hasYield() || $hasYield, |
1370: | $alwaysTerminating, |
1371: | $bodyScopeResult->getExitPointsForOuterLoop(), |
1372: | array_merge($throwPoints, $bodyScopeResult->getThrowPoints()), |
1373: | array_merge($impurePoints, $bodyScopeResult->getImpurePoints()), |
1374: | ); |
1375: | } elseif ($stmt instanceof For_) { |
1376: | $initScope = $scope; |
1377: | $hasYield = false; |
1378: | $throwPoints = []; |
1379: | $impurePoints = []; |
1380: | foreach ($stmt->init as $initExpr) { |
1381: | $initResult = $this->processExprNode($stmt, $initExpr, $initScope, $nodeCallback, ExpressionContext::createTopLevel()); |
1382: | $initScope = $initResult->getScope(); |
1383: | $hasYield = $hasYield || $initResult->hasYield(); |
1384: | $throwPoints = array_merge($throwPoints, $initResult->getThrowPoints()); |
1385: | $impurePoints = array_merge($impurePoints, $initResult->getImpurePoints()); |
1386: | } |
1387: | |
1388: | $bodyScope = $initScope; |
1389: | $isIterableAtLeastOnce = TrinaryLogic::createYes(); |
1390: | $lastCondExpr = $stmt->cond[count($stmt->cond) - 1] ?? null; |
1391: | foreach ($stmt->cond as $condExpr) { |
1392: | $condResult = $this->processExprNode($stmt, $condExpr, $bodyScope, static function (): void { |
1393: | }, ExpressionContext::createDeep()); |
1394: | $initScope = $condResult->getScope(); |
1395: | $condResultScope = $condResult->getScope(); |
1396: | |
1397: | if ($condExpr === $lastCondExpr) { |
1398: | $condTruthiness = ($this->treatPhpDocTypesAsCertain ? $condResultScope->getType($condExpr) : $condResultScope->getNativeType($condExpr))->toBoolean(); |
1399: | $isIterableAtLeastOnce = $isIterableAtLeastOnce->and($condTruthiness->isTrue()); |
1400: | } |
1401: | |
1402: | $hasYield = $hasYield || $condResult->hasYield(); |
1403: | $throwPoints = array_merge($throwPoints, $condResult->getThrowPoints()); |
1404: | $impurePoints = array_merge($impurePoints, $condResult->getImpurePoints()); |
1405: | $bodyScope = $condResult->getTruthyScope(); |
1406: | } |
1407: | |
1408: | if ($context->isTopLevel()) { |
1409: | $count = 0; |
1410: | do { |
1411: | $prevScope = $bodyScope; |
1412: | $bodyScope = $bodyScope->mergeWith($initScope); |
1413: | if ($lastCondExpr !== null) { |
1414: | $bodyScope = $this->processExprNode($stmt, $lastCondExpr, $bodyScope, static function (): void { |
1415: | }, ExpressionContext::createDeep())->getTruthyScope(); |
1416: | } |
1417: | $bodyScopeResult = $this->processStmtNodes($stmt, $stmt->stmts, $bodyScope, static function (): void { |
1418: | }, $context->enterDeep())->filterOutLoopExitPoints(); |
1419: | $bodyScope = $bodyScopeResult->getScope(); |
1420: | foreach ($bodyScopeResult->getExitPointsByType(Continue_::class) as $continueExitPoint) { |
1421: | $bodyScope = $bodyScope->mergeWith($continueExitPoint->getScope()); |
1422: | } |
1423: | foreach ($stmt->loop as $loopExpr) { |
1424: | $exprResult = $this->processExprNode($stmt, $loopExpr, $bodyScope, static function (): void { |
1425: | }, ExpressionContext::createTopLevel()); |
1426: | $bodyScope = $exprResult->getScope(); |
1427: | $hasYield = $hasYield || $exprResult->hasYield(); |
1428: | $throwPoints = array_merge($throwPoints, $exprResult->getThrowPoints()); |
1429: | $impurePoints = array_merge($impurePoints, $exprResult->getImpurePoints()); |
1430: | } |
1431: | |
1432: | if ($bodyScope->equals($prevScope)) { |
1433: | break; |
1434: | } |
1435: | |
1436: | if ($count >= self::GENERALIZE_AFTER_ITERATION) { |
1437: | $bodyScope = $prevScope->generalizeWith($bodyScope); |
1438: | } |
1439: | $count++; |
1440: | } while ($count < self::LOOP_SCOPE_ITERATIONS); |
1441: | } |
1442: | |
1443: | $bodyScope = $bodyScope->mergeWith($initScope); |
1444: | |
1445: | $alwaysIterates = TrinaryLogic::createFromBoolean($context->isTopLevel()); |
1446: | if ($lastCondExpr !== null) { |
1447: | $alwaysIterates = $alwaysIterates->and($bodyScope->getType($lastCondExpr)->toBoolean()->isTrue()); |
1448: | $bodyScope = $this->processExprNode($stmt, $lastCondExpr, $bodyScope, $nodeCallback, ExpressionContext::createDeep())->getTruthyScope(); |
1449: | } |
1450: | |
1451: | $finalScopeResult = $this->processStmtNodes($stmt, $stmt->stmts, $bodyScope, $nodeCallback, $context)->filterOutLoopExitPoints(); |
1452: | $finalScope = $finalScopeResult->getScope(); |
1453: | foreach ($finalScopeResult->getExitPointsByType(Continue_::class) as $continueExitPoint) { |
1454: | $finalScope = $continueExitPoint->getScope()->mergeWith($finalScope); |
1455: | } |
1456: | |
1457: | $loopScope = $finalScope; |
1458: | foreach ($stmt->loop as $loopExpr) { |
1459: | $loopScope = $this->processExprNode($stmt, $loopExpr, $loopScope, $nodeCallback, ExpressionContext::createTopLevel())->getScope(); |
1460: | } |
1461: | $finalScope = $finalScope->generalizeWith($loopScope); |
1462: | |
1463: | if ($lastCondExpr !== null) { |
1464: | $finalScope = $finalScope->filterByFalseyValue($lastCondExpr); |
1465: | } |
1466: | |
1467: | foreach ($finalScopeResult->getExitPointsByType(Break_::class) as $breakExitPoint) { |
1468: | $finalScope = $breakExitPoint->getScope()->mergeWith($finalScope); |
1469: | } |
1470: | |
1471: | if ($isIterableAtLeastOnce->no() || $finalScopeResult->isAlwaysTerminating()) { |
1472: | if ($this->polluteScopeWithLoopInitialAssignments) { |
1473: | $finalScope = $initScope; |
1474: | } else { |
1475: | $finalScope = $scope; |
1476: | } |
1477: | |
1478: | } elseif ($isIterableAtLeastOnce->maybe()) { |
1479: | if ($this->polluteScopeWithLoopInitialAssignments) { |
1480: | $finalScope = $finalScope->mergeWith($initScope); |
1481: | } else { |
1482: | $finalScope = $finalScope->mergeWith($scope); |
1483: | } |
1484: | } else { |
1485: | if (!$this->polluteScopeWithLoopInitialAssignments) { |
1486: | $finalScope = $finalScope->mergeWith($scope); |
1487: | } |
1488: | } |
1489: | |
1490: | if ($alwaysIterates->yes()) { |
1491: | $isAlwaysTerminating = count($finalScopeResult->getExitPointsByType(Break_::class)) === 0; |
1492: | } elseif ($isIterableAtLeastOnce->yes()) { |
1493: | $isAlwaysTerminating = $finalScopeResult->isAlwaysTerminating(); |
1494: | } else { |
1495: | $isAlwaysTerminating = false; |
1496: | } |
1497: | |
1498: | return new StatementResult( |
1499: | $finalScope, |
1500: | $finalScopeResult->hasYield() || $hasYield, |
1501: | $isAlwaysTerminating, |
1502: | $finalScopeResult->getExitPointsForOuterLoop(), |
1503: | array_merge($throwPoints, $finalScopeResult->getThrowPoints()), |
1504: | array_merge($impurePoints, $finalScopeResult->getImpurePoints()), |
1505: | ); |
1506: | } elseif ($stmt instanceof Switch_) { |
1507: | $condResult = $this->processExprNode($stmt, $stmt->cond, $scope, $nodeCallback, ExpressionContext::createDeep()); |
1508: | $scope = $condResult->getScope(); |
1509: | $scopeForBranches = $scope; |
1510: | $finalScope = null; |
1511: | $prevScope = null; |
1512: | $hasDefaultCase = false; |
1513: | $alwaysTerminating = true; |
1514: | $hasYield = $condResult->hasYield(); |
1515: | $exitPointsForOuterLoop = []; |
1516: | $throwPoints = $condResult->getThrowPoints(); |
1517: | $impurePoints = $condResult->getImpurePoints(); |
1518: | foreach ($stmt->cases as $caseNode) { |
1519: | if ($caseNode->cond !== null) { |
1520: | $condExpr = new BinaryOp\Equal($stmt->cond, $caseNode->cond); |
1521: | $caseResult = $this->processExprNode($stmt, $caseNode->cond, $scopeForBranches, $nodeCallback, ExpressionContext::createDeep()); |
1522: | $scopeForBranches = $caseResult->getScope(); |
1523: | $hasYield = $hasYield || $caseResult->hasYield(); |
1524: | $throwPoints = array_merge($throwPoints, $caseResult->getThrowPoints()); |
1525: | $impurePoints = array_merge($impurePoints, $caseResult->getImpurePoints()); |
1526: | $branchScope = $caseResult->getTruthyScope()->filterByTruthyValue($condExpr); |
1527: | } else { |
1528: | $hasDefaultCase = true; |
1529: | $branchScope = $scopeForBranches; |
1530: | } |
1531: | |
1532: | $branchScope = $branchScope->mergeWith($prevScope); |
1533: | $branchScopeResult = $this->processStmtNodes($caseNode, $caseNode->stmts, $branchScope, $nodeCallback, $context); |
1534: | $branchScope = $branchScopeResult->getScope(); |
1535: | $branchFinalScopeResult = $branchScopeResult->filterOutLoopExitPoints(); |
1536: | $hasYield = $hasYield || $branchFinalScopeResult->hasYield(); |
1537: | foreach ($branchScopeResult->getExitPointsByType(Break_::class) as $breakExitPoint) { |
1538: | $alwaysTerminating = false; |
1539: | $finalScope = $breakExitPoint->getScope()->mergeWith($finalScope); |
1540: | } |
1541: | foreach ($branchScopeResult->getExitPointsByType(Continue_::class) as $continueExitPoint) { |
1542: | $finalScope = $continueExitPoint->getScope()->mergeWith($finalScope); |
1543: | } |
1544: | $exitPointsForOuterLoop = array_merge($exitPointsForOuterLoop, $branchFinalScopeResult->getExitPointsForOuterLoop()); |
1545: | $throwPoints = array_merge($throwPoints, $branchFinalScopeResult->getThrowPoints()); |
1546: | $impurePoints = array_merge($impurePoints, $branchFinalScopeResult->getImpurePoints()); |
1547: | if ($branchScopeResult->isAlwaysTerminating()) { |
1548: | $alwaysTerminating = $alwaysTerminating && $branchFinalScopeResult->isAlwaysTerminating(); |
1549: | $prevScope = null; |
1550: | if (isset($condExpr)) { |
1551: | $scopeForBranches = $scopeForBranches->filterByFalseyValue($condExpr); |
1552: | } |
1553: | if (!$branchFinalScopeResult->isAlwaysTerminating()) { |
1554: | $finalScope = $branchScope->mergeWith($finalScope); |
1555: | } |
1556: | } else { |
1557: | $prevScope = $branchScope; |
1558: | } |
1559: | } |
1560: | |
1561: | $exhaustive = $scopeForBranches->getType($stmt->cond) instanceof NeverType; |
1562: | |
1563: | if (!$hasDefaultCase && !$exhaustive) { |
1564: | $alwaysTerminating = false; |
1565: | } |
1566: | |
1567: | if ($prevScope !== null && isset($branchFinalScopeResult)) { |
1568: | $finalScope = $prevScope->mergeWith($finalScope); |
1569: | $alwaysTerminating = $alwaysTerminating && $branchFinalScopeResult->isAlwaysTerminating(); |
1570: | } |
1571: | |
1572: | if ((!$hasDefaultCase && !$exhaustive) || $finalScope === null) { |
1573: | $finalScope = $scope->mergeWith($finalScope); |
1574: | } |
1575: | |
1576: | return new StatementResult($finalScope, $hasYield, $alwaysTerminating, $exitPointsForOuterLoop, $throwPoints, $impurePoints); |
1577: | } elseif ($stmt instanceof TryCatch) { |
1578: | $branchScopeResult = $this->processStmtNodes($stmt, $stmt->stmts, $scope, $nodeCallback, $context); |
1579: | $branchScope = $branchScopeResult->getScope(); |
1580: | $finalScope = $branchScopeResult->isAlwaysTerminating() ? null : $branchScope; |
1581: | |
1582: | $exitPoints = []; |
1583: | $finallyExitPoints = []; |
1584: | $alwaysTerminating = $branchScopeResult->isAlwaysTerminating(); |
1585: | $hasYield = $branchScopeResult->hasYield(); |
1586: | |
1587: | if ($stmt->finally !== null) { |
1588: | $finallyScope = $branchScope; |
1589: | } else { |
1590: | $finallyScope = null; |
1591: | } |
1592: | foreach ($branchScopeResult->getExitPoints() as $exitPoint) { |
1593: | $finallyExitPoints[] = $exitPoint; |
1594: | if ($exitPoint->getStatement() instanceof Node\Stmt\Expression && $exitPoint->getStatement()->expr instanceof Expr\Throw_) { |
1595: | continue; |
1596: | } |
1597: | if ($finallyScope !== null) { |
1598: | $finallyScope = $finallyScope->mergeWith($exitPoint->getScope()); |
1599: | } |
1600: | $exitPoints[] = $exitPoint; |
1601: | } |
1602: | |
1603: | $throwPoints = $branchScopeResult->getThrowPoints(); |
1604: | $impurePoints = $branchScopeResult->getImpurePoints(); |
1605: | $throwPointsForLater = []; |
1606: | $pastCatchTypes = new NeverType(); |
1607: | |
1608: | foreach ($stmt->catches as $catchNode) { |
1609: | $nodeCallback($catchNode, $scope); |
1610: | |
1611: | $originalCatchTypes = array_map(static fn (Name $name): Type => new ObjectType($name->toString()), $catchNode->types); |
1612: | $catchTypes = array_map(static fn (Type $type): Type => TypeCombinator::remove($type, $pastCatchTypes), $originalCatchTypes); |
1613: | |
1614: | $originalCatchType = TypeCombinator::union(...$originalCatchTypes); |
1615: | $catchType = TypeCombinator::union(...$catchTypes); |
1616: | $pastCatchTypes = TypeCombinator::union($pastCatchTypes, $originalCatchType); |
1617: | |
1618: | $matchingThrowPoints = []; |
1619: | $matchingCatchTypes = array_fill_keys(array_keys($originalCatchTypes), false); |
1620: | |
1621: | |
1622: | foreach ($originalCatchTypes as $catchTypeIndex => $catchTypeItem) { |
1623: | if (!$catchTypeItem->isSuperTypeOf(new ObjectType(Throwable::class))->yes()) { |
1624: | continue; |
1625: | } |
1626: | |
1627: | foreach ($throwPoints as $throwPointIndex => $throwPoint) { |
1628: | $matchingThrowPoints[$throwPointIndex] = $throwPoint; |
1629: | $matchingCatchTypes[$catchTypeIndex] = true; |
1630: | } |
1631: | } |
1632: | |
1633: | |
1634: | $onlyExplicitIsThrow = true; |
1635: | if (count($matchingThrowPoints) === 0) { |
1636: | foreach ($throwPoints as $throwPointIndex => $throwPoint) { |
1637: | foreach ($catchTypes as $catchTypeIndex => $catchTypeItem) { |
1638: | if ($catchTypeItem->isSuperTypeOf($throwPoint->getType())->no()) { |
1639: | continue; |
1640: | } |
1641: | |
1642: | $matchingCatchTypes[$catchTypeIndex] = true; |
1643: | if (!$throwPoint->isExplicit()) { |
1644: | continue; |
1645: | } |
1646: | $throwNode = $throwPoint->getNode(); |
1647: | if ( |
1648: | !$throwNode instanceof Expr\Throw_ |
1649: | && !($throwNode instanceof Node\Stmt\Expression && $throwNode->expr instanceof Expr\Throw_) |
1650: | ) { |
1651: | $onlyExplicitIsThrow = false; |
1652: | } |
1653: | $matchingThrowPoints[$throwPointIndex] = $throwPoint; |
1654: | } |
1655: | } |
1656: | } |
1657: | |
1658: | |
1659: | if (count($matchingThrowPoints) === 0 || $onlyExplicitIsThrow) { |
1660: | foreach ($throwPoints as $throwPointIndex => $throwPoint) { |
1661: | if ($throwPoint->isExplicit()) { |
1662: | continue; |
1663: | } |
1664: | |
1665: | foreach ($catchTypes as $catchTypeIndex => $catchTypeItem) { |
1666: | if ($catchTypeItem->isSuperTypeOf($throwPoint->getType())->no()) { |
1667: | continue; |
1668: | } |
1669: | |
1670: | $matchingThrowPoints[$throwPointIndex] = $throwPoint; |
1671: | } |
1672: | } |
1673: | } |
1674: | |
1675: | |
1676: | if (count($matchingThrowPoints) === 0) { |
1677: | if ($originalCatchType->isSuperTypeOf(new ObjectType(Throwable::class))->yes()) { |
1678: | foreach ($branchScopeResult->getThrowPoints() as $originalThrowPoint) { |
1679: | if (!$originalThrowPoint->canContainAnyThrowable()) { |
1680: | continue; |
1681: | } |
1682: | |
1683: | $matchingThrowPoints[] = $originalThrowPoint; |
1684: | $matchingCatchTypes = array_fill_keys(array_keys($originalCatchTypes), true); |
1685: | } |
1686: | } |
1687: | } |
1688: | |
1689: | |
1690: | foreach ($matchingCatchTypes as $catchTypeIndex => $matched) { |
1691: | if ($matched) { |
1692: | continue; |
1693: | } |
1694: | $nodeCallback(new CatchWithUnthrownExceptionNode($catchNode, $catchTypes[$catchTypeIndex], $originalCatchTypes[$catchTypeIndex]), $scope); |
1695: | } |
1696: | |
1697: | if (count($matchingThrowPoints) === 0) { |
1698: | continue; |
1699: | } |
1700: | |
1701: | |
1702: | $newThrowPoints = []; |
1703: | foreach ($throwPoints as $throwPoint) { |
1704: | $newThrowPoint = $throwPoint->subtractCatchType($originalCatchType); |
1705: | |
1706: | if ($newThrowPoint->getType() instanceof NeverType) { |
1707: | continue; |
1708: | } |
1709: | |
1710: | $newThrowPoints[] = $newThrowPoint; |
1711: | } |
1712: | $throwPoints = $newThrowPoints; |
1713: | |
1714: | $catchScope = null; |
1715: | foreach ($matchingThrowPoints as $matchingThrowPoint) { |
1716: | if ($catchScope === null) { |
1717: | $catchScope = $matchingThrowPoint->getScope(); |
1718: | } else { |
1719: | $catchScope = $catchScope->mergeWith($matchingThrowPoint->getScope()); |
1720: | } |
1721: | } |
1722: | |
1723: | $variableName = null; |
1724: | if ($catchNode->var !== null) { |
1725: | if (!is_string($catchNode->var->name)) { |
1726: | throw new ShouldNotHappenException(); |
1727: | } |
1728: | |
1729: | $variableName = $catchNode->var->name; |
1730: | } |
1731: | |
1732: | $catchScopeResult = $this->processStmtNodes($catchNode, $catchNode->stmts, $catchScope->enterCatchType($catchType, $variableName), $nodeCallback, $context); |
1733: | $catchScopeForFinally = $catchScopeResult->getScope(); |
1734: | |
1735: | $finalScope = $catchScopeResult->isAlwaysTerminating() ? $finalScope : $catchScopeResult->getScope()->mergeWith($finalScope); |
1736: | $alwaysTerminating = $alwaysTerminating && $catchScopeResult->isAlwaysTerminating(); |
1737: | $hasYield = $hasYield || $catchScopeResult->hasYield(); |
1738: | $catchThrowPoints = $catchScopeResult->getThrowPoints(); |
1739: | $impurePoints = array_merge($impurePoints, $catchScopeResult->getImpurePoints()); |
1740: | $throwPointsForLater = array_merge($throwPointsForLater, $catchThrowPoints); |
1741: | |
1742: | if ($finallyScope !== null) { |
1743: | $finallyScope = $finallyScope->mergeWith($catchScopeForFinally); |
1744: | } |
1745: | foreach ($catchScopeResult->getExitPoints() as $exitPoint) { |
1746: | $finallyExitPoints[] = $exitPoint; |
1747: | if ($exitPoint->getStatement() instanceof Node\Stmt\Expression && $exitPoint->getStatement()->expr instanceof Expr\Throw_) { |
1748: | continue; |
1749: | } |
1750: | if ($finallyScope !== null) { |
1751: | $finallyScope = $finallyScope->mergeWith($exitPoint->getScope()); |
1752: | } |
1753: | $exitPoints[] = $exitPoint; |
1754: | } |
1755: | |
1756: | foreach ($catchThrowPoints as $catchThrowPoint) { |
1757: | if ($finallyScope === null) { |
1758: | continue; |
1759: | } |
1760: | $finallyScope = $finallyScope->mergeWith($catchThrowPoint->getScope()); |
1761: | } |
1762: | } |
1763: | |
1764: | if ($finalScope === null) { |
1765: | $finalScope = $scope; |
1766: | } |
1767: | |
1768: | foreach ($throwPoints as $throwPoint) { |
1769: | if ($finallyScope === null) { |
1770: | continue; |
1771: | } |
1772: | $finallyScope = $finallyScope->mergeWith($throwPoint->getScope()); |
1773: | } |
1774: | |
1775: | if ($finallyScope !== null) { |
1776: | $originalFinallyScope = $finallyScope; |
1777: | $finallyResult = $this->processStmtNodes($stmt->finally, $stmt->finally->stmts, $finallyScope, $nodeCallback, $context); |
1778: | $alwaysTerminating = $alwaysTerminating || $finallyResult->isAlwaysTerminating(); |
1779: | $hasYield = $hasYield || $finallyResult->hasYield(); |
1780: | $throwPointsForLater = array_merge($throwPointsForLater, $finallyResult->getThrowPoints()); |
1781: | $impurePoints = array_merge($impurePoints, $finallyResult->getImpurePoints()); |
1782: | $finallyScope = $finallyResult->getScope(); |
1783: | $finalScope = $finallyResult->isAlwaysTerminating() ? $finalScope : $finalScope->processFinallyScope($finallyScope, $originalFinallyScope); |
1784: | if (count($finallyResult->getExitPoints()) > 0) { |
1785: | $nodeCallback(new FinallyExitPointsNode( |
1786: | $finallyResult->getExitPoints(), |
1787: | $finallyExitPoints, |
1788: | ), $scope); |
1789: | } |
1790: | $exitPoints = array_merge($exitPoints, $finallyResult->getExitPoints()); |
1791: | } |
1792: | |
1793: | return new StatementResult($finalScope, $hasYield, $alwaysTerminating, $exitPoints, array_merge($throwPoints, $throwPointsForLater), $impurePoints); |
1794: | } elseif ($stmt instanceof Unset_) { |
1795: | $hasYield = false; |
1796: | $throwPoints = []; |
1797: | $impurePoints = []; |
1798: | foreach ($stmt->vars as $var) { |
1799: | $scope = $this->lookForSetAllowedUndefinedExpressions($scope, $var); |
1800: | $exprResult = $this->processExprNode($stmt, $var, $scope, $nodeCallback, ExpressionContext::createDeep()); |
1801: | $scope = $exprResult->getScope(); |
1802: | $scope = $this->lookForUnsetAllowedUndefinedExpressions($scope, $var); |
1803: | $hasYield = $hasYield || $exprResult->hasYield(); |
1804: | $throwPoints = array_merge($throwPoints, $exprResult->getThrowPoints()); |
1805: | $impurePoints = array_merge($impurePoints, $exprResult->getImpurePoints()); |
1806: | if ($var instanceof ArrayDimFetch && $var->dim !== null) { |
1807: | $cloningTraverser = new NodeTraverser(); |
1808: | $cloningTraverser->addVisitor(new CloningVisitor()); |
1809: | |
1810: | |
1811: | [$clonedVar] = $cloningTraverser->traverse([$var->var]); |
1812: | |
1813: | $traverser = new NodeTraverser(); |
1814: | $traverser->addVisitor(new class () extends NodeVisitorAbstract { |
1815: | |
1816: | public function leaveNode(Node $node): ?ExistingArrayDimFetch |
1817: | { |
1818: | if (!$node instanceof ArrayDimFetch || $node->dim === null) { |
1819: | return null; |
1820: | } |
1821: | |
1822: | return new ExistingArrayDimFetch($node->var, $node->dim); |
1823: | } |
1824: | |
1825: | }); |
1826: | |
1827: | |
1828: | [$clonedVar] = $traverser->traverse([$clonedVar]); |
1829: | $scope = $this->processAssignVar( |
1830: | $scope, |
1831: | $stmt, |
1832: | $clonedVar, |
1833: | new UnsetOffsetExpr($var->var, $var->dim), |
1834: | static function (Node $node, Scope $scope) use ($nodeCallback): void { |
1835: | if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) { |
1836: | return; |
1837: | } |
1838: | |
1839: | $nodeCallback($node, $scope); |
1840: | }, |
1841: | ExpressionContext::createDeep(), |
1842: | static fn (MutatingScope $scope): ExpressionResult => new ExpressionResult($scope, false, [], []), |
1843: | false, |
1844: | )->getScope(); |
1845: | } elseif ($var instanceof PropertyFetch) { |
1846: | $scope = $scope->invalidateExpression($var); |
1847: | $impurePoints[] = new ImpurePoint( |
1848: | $scope, |
1849: | $var, |
1850: | 'propertyUnset', |
1851: | 'property unset', |
1852: | true, |
1853: | ); |
1854: | } else { |
1855: | $scope = $scope->invalidateExpression($var); |
1856: | } |
1857: | |
1858: | } |
1859: | } elseif ($stmt instanceof Node\Stmt\Use_) { |
1860: | $hasYield = false; |
1861: | $throwPoints = []; |
1862: | $impurePoints = []; |
1863: | foreach ($stmt->uses as $use) { |
1864: | $nodeCallback($use, $scope); |
1865: | } |
1866: | } elseif ($stmt instanceof Node\Stmt\Global_) { |
1867: | $hasYield = false; |
1868: | $throwPoints = []; |
1869: | $impurePoints = [ |
1870: | new ImpurePoint( |
1871: | $scope, |
1872: | $stmt, |
1873: | 'global', |
1874: | 'global variable', |
1875: | true, |
1876: | ), |
1877: | ]; |
1878: | $vars = []; |
1879: | foreach ($stmt->vars as $var) { |
1880: | if (!$var instanceof Variable) { |
1881: | throw new ShouldNotHappenException(); |
1882: | } |
1883: | $scope = $this->lookForSetAllowedUndefinedExpressions($scope, $var); |
1884: | $varResult = $this->processExprNode($stmt, $var, $scope, $nodeCallback, ExpressionContext::createDeep()); |
1885: | $impurePoints = array_merge($impurePoints, $varResult->getImpurePoints()); |
1886: | $scope = $this->lookForUnsetAllowedUndefinedExpressions($scope, $var); |
1887: | |
1888: | if (!is_string($var->name)) { |
1889: | continue; |
1890: | } |
1891: | |
1892: | $scope = $scope->assignVariable($var->name, new MixedType(), new MixedType(), TrinaryLogic::createYes()); |
1893: | $vars[] = $var->name; |
1894: | } |
1895: | $scope = $this->processVarAnnotation($scope, $vars, $stmt); |
1896: | } elseif ($stmt instanceof Static_) { |
1897: | $hasYield = false; |
1898: | $throwPoints = []; |
1899: | $impurePoints = [ |
1900: | new ImpurePoint( |
1901: | $scope, |
1902: | $stmt, |
1903: | 'static', |
1904: | 'static variable', |
1905: | true, |
1906: | ), |
1907: | ]; |
1908: | |
1909: | $vars = []; |
1910: | foreach ($stmt->vars as $var) { |
1911: | if (!is_string($var->var->name)) { |
1912: | throw new ShouldNotHappenException(); |
1913: | } |
1914: | |
1915: | if ($var->default !== null) { |
1916: | $defaultExprResult = $this->processExprNode($stmt, $var->default, $scope, $nodeCallback, ExpressionContext::createDeep()); |
1917: | $impurePoints = array_merge($impurePoints, $defaultExprResult->getImpurePoints()); |
1918: | } |
1919: | |
1920: | $scope = $scope->enterExpressionAssign($var->var); |
1921: | $varResult = $this->processExprNode($stmt, $var->var, $scope, $nodeCallback, ExpressionContext::createDeep()); |
1922: | $impurePoints = array_merge($impurePoints, $varResult->getImpurePoints()); |
1923: | $scope = $scope->exitExpressionAssign($var->var); |
1924: | |
1925: | $scope = $scope->assignVariable($var->var->name, new MixedType(), new MixedType(), TrinaryLogic::createYes()); |
1926: | $vars[] = $var->var->name; |
1927: | } |
1928: | |
1929: | $scope = $this->processVarAnnotation($scope, $vars, $stmt); |
1930: | } elseif ($stmt instanceof Node\Stmt\Const_) { |
1931: | $hasYield = false; |
1932: | $throwPoints = []; |
1933: | $impurePoints = []; |
1934: | foreach ($stmt->consts as $const) { |
1935: | $nodeCallback($const, $scope); |
1936: | $constResult = $this->processExprNode($stmt, $const->value, $scope, $nodeCallback, ExpressionContext::createDeep()); |
1937: | $impurePoints = array_merge($impurePoints, $constResult->getImpurePoints()); |
1938: | if ($const->namespacedName !== null) { |
1939: | $constantName = new Name\FullyQualified($const->namespacedName->toString()); |
1940: | } else { |
1941: | $constantName = new Name\FullyQualified($const->name->toString()); |
1942: | } |
1943: | $scope = $scope->assignExpression(new ConstFetch($constantName), $scope->getType($const->value), $scope->getNativeType($const->value)); |
1944: | } |
1945: | } elseif ($stmt instanceof Node\Stmt\ClassConst) { |
1946: | $hasYield = false; |
1947: | $throwPoints = []; |
1948: | $impurePoints = []; |
1949: | $this->processAttributeGroups($stmt, $stmt->attrGroups, $scope, $nodeCallback); |
1950: | foreach ($stmt->consts as $const) { |
1951: | $nodeCallback($const, $scope); |
1952: | $constResult = $this->processExprNode($stmt, $const->value, $scope, $nodeCallback, ExpressionContext::createDeep()); |
1953: | $impurePoints = array_merge($impurePoints, $constResult->getImpurePoints()); |
1954: | if ($scope->getClassReflection() === null) { |
1955: | throw new ShouldNotHappenException(); |
1956: | } |
1957: | $scope = $scope->assignExpression( |
1958: | new Expr\ClassConstFetch(new Name\FullyQualified($scope->getClassReflection()->getName()), $const->name), |
1959: | $scope->getType($const->value), |
1960: | $scope->getNativeType($const->value), |
1961: | ); |
1962: | } |
1963: | } elseif ($stmt instanceof Node\Stmt\EnumCase) { |
1964: | $hasYield = false; |
1965: | $throwPoints = []; |
1966: | $this->processAttributeGroups($stmt, $stmt->attrGroups, $scope, $nodeCallback); |
1967: | $impurePoints = []; |
1968: | if ($stmt->expr !== null) { |
1969: | $exprResult = $this->processExprNode($stmt, $stmt->expr, $scope, $nodeCallback, ExpressionContext::createDeep()); |
1970: | $impurePoints = $exprResult->getImpurePoints(); |
1971: | } |
1972: | } elseif ($stmt instanceof InlineHTML) { |
1973: | $hasYield = false; |
1974: | $throwPoints = []; |
1975: | $impurePoints = [ |
1976: | new ImpurePoint($scope, $stmt, 'betweenPhpTags', 'output between PHP opening and closing tags', true), |
1977: | ]; |
1978: | } elseif ($stmt instanceof Node\Stmt\Block) { |
1979: | $result = $this->processStmtNodes($stmt, $stmt->stmts, $scope, $nodeCallback, $context); |
1980: | if ($this->polluteScopeWithBlock) { |
1981: | return $result; |
1982: | } |
1983: | |
1984: | return new StatementResult( |
1985: | $scope->mergeWith($result->getScope()), |
1986: | $result->hasYield(), |
1987: | $result->isAlwaysTerminating(), |
1988: | $result->getExitPoints(), |
1989: | $result->getThrowPoints(), |
1990: | $result->getImpurePoints(), |
1991: | $result->getEndStatements(), |
1992: | ); |
1993: | } elseif ($stmt instanceof Node\Stmt\Nop) { |
1994: | $hasYield = false; |
1995: | $throwPoints = $overridingThrowPoints ?? []; |
1996: | $impurePoints = []; |
1997: | } elseif ($stmt instanceof Node\Stmt\GroupUse) { |
1998: | $hasYield = false; |
1999: | $throwPoints = []; |
2000: | foreach ($stmt->uses as $use) { |
2001: | $nodeCallback($use, $scope); |
2002: | } |
2003: | $impurePoints = []; |
2004: | } else { |
2005: | $hasYield = false; |
2006: | $throwPoints = $overridingThrowPoints ?? []; |
2007: | $impurePoints = []; |
2008: | } |
2009: | |
2010: | return new StatementResult($scope, $hasYield, false, [], $throwPoints, $impurePoints); |
2011: | } |
2012: | |
2013: | |
2014: | |
2015: | |
2016: | private function getDeprecatedAttribute(Scope $scope, Node\Stmt\Function_|Node\Stmt\ClassMethod|Node\PropertyHook $stmt): array |
2017: | { |
2018: | $initializerExprContext = InitializerExprContext::fromStubParameter( |
2019: | $scope->isInClass() ? $scope->getClassReflection()->getName() : null, |
2020: | $scope->getFile(), |
2021: | $stmt, |
2022: | ); |
2023: | $isDeprecated = false; |
2024: | $deprecatedDescription = null; |
2025: | $deprecatedDescriptionType = null; |
2026: | foreach ($stmt->attrGroups as $attrGroup) { |
2027: | foreach ($attrGroup->attrs as $attr) { |
2028: | if ($attr->name->toString() !== 'Deprecated') { |
2029: | continue; |
2030: | } |
2031: | $isDeprecated = true; |
2032: | $arguments = $attr->args; |
2033: | foreach ($arguments as $i => $arg) { |
2034: | $argName = $arg->name; |
2035: | if ($argName === null) { |
2036: | if ($i !== 0) { |
2037: | continue; |
2038: | } |
2039: | |
2040: | $deprecatedDescriptionType = $this->initializerExprTypeResolver->getType($arg->value, $initializerExprContext); |
2041: | break; |
2042: | } |
2043: | |
2044: | if ($argName->toString() !== 'message') { |
2045: | continue; |
2046: | } |
2047: | |
2048: | $deprecatedDescriptionType = $this->initializerExprTypeResolver->getType($arg->value, $initializerExprContext); |
2049: | break; |
2050: | } |
2051: | } |
2052: | } |
2053: | |
2054: | if ($deprecatedDescriptionType !== null) { |
2055: | $constantStrings = $deprecatedDescriptionType->getConstantStrings(); |
2056: | if (count($constantStrings) === 1) { |
2057: | $deprecatedDescription = $constantStrings[0]->getValue(); |
2058: | } |
2059: | } |
2060: | |
2061: | return [$isDeprecated, $deprecatedDescription]; |
2062: | } |
2063: | |
2064: | |
2065: | |
2066: | |
2067: | private function getOverridingThrowPoints(Node\Stmt $statement, MutatingScope $scope): ?array |
2068: | { |
2069: | foreach ($statement->getComments() as $comment) { |
2070: | if (!$comment instanceof Doc) { |
2071: | continue; |
2072: | } |
2073: | |
2074: | $function = $scope->getFunction(); |
2075: | $resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc( |
2076: | $scope->getFile(), |
2077: | $scope->isInClass() ? $scope->getClassReflection()->getName() : null, |
2078: | $scope->isInTrait() ? $scope->getTraitReflection()->getName() : null, |
2079: | $function !== null ? $function->getName() : null, |
2080: | $comment->getText(), |
2081: | ); |
2082: | |
2083: | $throwsTag = $resolvedPhpDoc->getThrowsTag(); |
2084: | if ($throwsTag !== null) { |
2085: | $throwsType = $throwsTag->getType(); |
2086: | if ($throwsType->isVoid()->yes()) { |
2087: | return []; |
2088: | } |
2089: | |
2090: | return [ThrowPoint::createExplicit($scope, $throwsType, $statement, false)]; |
2091: | } |
2092: | } |
2093: | |
2094: | return null; |
2095: | } |
2096: | |
2097: | private function getCurrentClassReflection(Node\Stmt\ClassLike $stmt, string $className, Scope $scope): ClassReflection |
2098: | { |
2099: | if (!$this->reflectionProvider->hasClass($className)) { |
2100: | return $this->createAstClassReflection($stmt, $className, $scope); |
2101: | } |
2102: | |
2103: | $defaultClassReflection = $this->reflectionProvider->getClass($className); |
2104: | if ($defaultClassReflection->getFileName() !== $scope->getFile()) { |
2105: | return $this->createAstClassReflection($stmt, $className, $scope); |
2106: | } |
2107: | |
2108: | $startLine = $defaultClassReflection->getNativeReflection()->getStartLine(); |
2109: | if ($startLine !== $stmt->getStartLine()) { |
2110: | return $this->createAstClassReflection($stmt, $className, $scope); |
2111: | } |
2112: | |
2113: | return $defaultClassReflection; |
2114: | } |
2115: | |
2116: | private function createAstClassReflection(Node\Stmt\ClassLike $stmt, string $className, Scope $scope): ClassReflection |
2117: | { |
2118: | $nodeToReflection = new NodeToReflection(); |
2119: | $betterReflectionClass = $nodeToReflection->__invoke( |
2120: | $this->reflector, |
2121: | $stmt, |
2122: | new LocatedSource(FileReader::read($scope->getFile()), $className, $scope->getFile()), |
2123: | $scope->getNamespace() !== null ? new Node\Stmt\Namespace_(new Name($scope->getNamespace())) : null, |
2124: | ); |
2125: | if (!$betterReflectionClass instanceof \PHPStan\BetterReflection\Reflection\ReflectionClass) { |
2126: | throw new ShouldNotHappenException(); |
2127: | } |
2128: | |
2129: | $enumAdapter = base64_decode('UEhQU3RhblxCZXR0ZXJSZWZsZWN0aW9uXFJlZmxlY3Rpb25cQWRhcHRlclxSZWZsZWN0aW9uRW51bQ==', true); |
2130: | |
2131: | return new ClassReflection( |
2132: | $this->reflectionProvider, |
2133: | $this->initializerExprTypeResolver, |
2134: | $this->fileTypeMapper, |
2135: | $this->stubPhpDocProvider, |
2136: | $this->phpDocInheritanceResolver, |
2137: | $this->phpVersion, |
2138: | $this->signatureMapProvider, |
2139: | $this->attributeReflectionFactory, |
2140: | $this->classReflectionExtensionRegistryProvider->getRegistry()->getPropertiesClassReflectionExtensions(), |
2141: | $this->classReflectionExtensionRegistryProvider->getRegistry()->getMethodsClassReflectionExtensions(), |
2142: | $this->classReflectionExtensionRegistryProvider->getRegistry()->getAllowedSubTypesClassReflectionExtensions(), |
2143: | $this->classReflectionExtensionRegistryProvider->getRegistry()->getRequireExtendsPropertyClassReflectionExtension(), |
2144: | $this->classReflectionExtensionRegistryProvider->getRegistry()->getRequireExtendsMethodsClassReflectionExtension(), |
2145: | $betterReflectionClass->getName(), |
2146: | $betterReflectionClass instanceof ReflectionEnum && PHP_VERSION_ID >= 80000 ? new $enumAdapter($betterReflectionClass) : new ReflectionClass($betterReflectionClass), |
2147: | null, |
2148: | null, |
2149: | null, |
2150: | $this->universalObjectCratesClasses, |
2151: | sprintf('%s:%d', $scope->getFile(), $stmt->getStartLine()), |
2152: | ); |
2153: | } |
2154: | |
2155: | private function lookForSetAllowedUndefinedExpressions(MutatingScope $scope, Expr $expr): MutatingScope |
2156: | { |
2157: | return $this->lookForExpressionCallback($scope, $expr, static fn (MutatingScope $scope, Expr $expr): MutatingScope => $scope->setAllowedUndefinedExpression($expr)); |
2158: | } |
2159: | |
2160: | private function lookForUnsetAllowedUndefinedExpressions(MutatingScope $scope, Expr $expr): MutatingScope |
2161: | { |
2162: | return $this->lookForExpressionCallback($scope, $expr, static fn (MutatingScope $scope, Expr $expr): MutatingScope => $scope->unsetAllowedUndefinedExpression($expr)); |
2163: | } |
2164: | |
2165: | |
2166: | |
2167: | |
2168: | private function lookForExpressionCallback(MutatingScope $scope, Expr $expr, Closure $callback): MutatingScope |
2169: | { |
2170: | if (!$expr instanceof ArrayDimFetch || $expr->dim !== null) { |
2171: | $scope = $callback($scope, $expr); |
2172: | } |
2173: | |
2174: | if ($expr instanceof ArrayDimFetch) { |
2175: | $scope = $this->lookForExpressionCallback($scope, $expr->var, $callback); |
2176: | } elseif ($expr instanceof PropertyFetch || $expr instanceof Expr\NullsafePropertyFetch) { |
2177: | $scope = $this->lookForExpressionCallback($scope, $expr->var, $callback); |
2178: | } elseif ($expr instanceof StaticPropertyFetch && $expr->class instanceof Expr) { |
2179: | $scope = $this->lookForExpressionCallback($scope, $expr->class, $callback); |
2180: | } elseif ($expr instanceof List_) { |
2181: | foreach ($expr->items as $item) { |
2182: | if ($item === null) { |
2183: | continue; |
2184: | } |
2185: | |
2186: | $scope = $this->lookForExpressionCallback($scope, $item->value, $callback); |
2187: | } |
2188: | } |
2189: | |
2190: | return $scope; |
2191: | } |
2192: | |
2193: | private function ensureShallowNonNullability(MutatingScope $scope, Scope $originalScope, Expr $exprToSpecify): EnsuredNonNullabilityResult |
2194: | { |
2195: | $exprType = $scope->getType($exprToSpecify); |
2196: | $isNull = $exprType->isNull(); |
2197: | if ($isNull->yes()) { |
2198: | return new EnsuredNonNullabilityResult($scope, []); |
2199: | } |
2200: | |
2201: | |
2202: | $certainty = TrinaryLogic::createYes(); |
2203: | $hasExpressionType = $originalScope->hasExpressionType($exprToSpecify); |
2204: | if (!$hasExpressionType->no()) { |
2205: | $certainty = $hasExpressionType; |
2206: | } |
2207: | |
2208: | $exprTypeWithoutNull = TypeCombinator::removeNull($exprType); |
2209: | if ($exprType->equals($exprTypeWithoutNull)) { |
2210: | $originalExprType = $originalScope->getType($exprToSpecify); |
2211: | if (!$originalExprType->equals($exprTypeWithoutNull)) { |
2212: | $originalNativeType = $originalScope->getNativeType($exprToSpecify); |
2213: | |
2214: | return new EnsuredNonNullabilityResult($scope, [ |
2215: | new EnsuredNonNullabilityResultExpression($exprToSpecify, $originalExprType, $originalNativeType, $certainty), |
2216: | ]); |
2217: | } |
2218: | return new EnsuredNonNullabilityResult($scope, []); |
2219: | } |
2220: | |
2221: | $nativeType = $scope->getNativeType($exprToSpecify); |
2222: | $scope = $scope->specifyExpressionType( |
2223: | $exprToSpecify, |
2224: | $exprTypeWithoutNull, |
2225: | TypeCombinator::removeNull($nativeType), |
2226: | TrinaryLogic::createYes(), |
2227: | ); |
2228: | |
2229: | return new EnsuredNonNullabilityResult( |
2230: | $scope, |
2231: | [ |
2232: | new EnsuredNonNullabilityResultExpression($exprToSpecify, $exprType, $nativeType, $certainty), |
2233: | ], |
2234: | ); |
2235: | } |
2236: | |
2237: | private function ensureNonNullability(MutatingScope $scope, Expr $expr): EnsuredNonNullabilityResult |
2238: | { |
2239: | $specifiedExpressions = []; |
2240: | $originalScope = $scope; |
2241: | $scope = $this->lookForExpressionCallback($scope, $expr, function ($scope, $expr) use (&$specifiedExpressions, $originalScope) { |
2242: | $result = $this->ensureShallowNonNullability($scope, $originalScope, $expr); |
2243: | foreach ($result->getSpecifiedExpressions() as $specifiedExpression) { |
2244: | $specifiedExpressions[] = $specifiedExpression; |
2245: | } |
2246: | return $result->getScope(); |
2247: | }); |
2248: | |
2249: | return new EnsuredNonNullabilityResult($scope, $specifiedExpressions); |
2250: | } |
2251: | |
2252: | |
2253: | |
2254: | |
2255: | private function revertNonNullability(MutatingScope $scope, array $specifiedExpressions): MutatingScope |
2256: | { |
2257: | foreach ($specifiedExpressions as $specifiedExpressionResult) { |
2258: | $scope = $scope->specifyExpressionType( |
2259: | $specifiedExpressionResult->getExpression(), |
2260: | $specifiedExpressionResult->getOriginalType(), |
2261: | $specifiedExpressionResult->getOriginalNativeType(), |
2262: | $specifiedExpressionResult->getCertainty(), |
2263: | ); |
2264: | } |
2265: | |
2266: | return $scope; |
2267: | } |
2268: | |
2269: | private function findEarlyTerminatingExpr(Expr $expr, Scope $scope): ?Expr |
2270: | { |
2271: | if (($expr instanceof MethodCall || $expr instanceof Expr\StaticCall) && $expr->name instanceof Node\Identifier) { |
2272: | if (array_key_exists($expr->name->toLowerString(), $this->earlyTerminatingMethodNames)) { |
2273: | if ($expr instanceof MethodCall) { |
2274: | $methodCalledOnType = $scope->getType($expr->var); |
2275: | } else { |
2276: | if ($expr->class instanceof Name) { |
2277: | $methodCalledOnType = $scope->resolveTypeByName($expr->class); |
2278: | } else { |
2279: | $methodCalledOnType = $scope->getType($expr->class); |
2280: | } |
2281: | } |
2282: | |
2283: | foreach ($methodCalledOnType->getObjectClassNames() as $referencedClass) { |
2284: | if (!$this->reflectionProvider->hasClass($referencedClass)) { |
2285: | continue; |
2286: | } |
2287: | |
2288: | $classReflection = $this->reflectionProvider->getClass($referencedClass); |
2289: | foreach (array_merge([$referencedClass], $classReflection->getParentClassesNames(), $classReflection->getNativeReflection()->getInterfaceNames()) as $className) { |
2290: | if (!isset($this->earlyTerminatingMethodCalls[$className])) { |
2291: | continue; |
2292: | } |
2293: | |
2294: | if (in_array((string) $expr->name, $this->earlyTerminatingMethodCalls[$className], true)) { |
2295: | return $expr; |
2296: | } |
2297: | } |
2298: | } |
2299: | } |
2300: | } |
2301: | |
2302: | if ($expr instanceof FuncCall && $expr->name instanceof Name) { |
2303: | if (in_array((string) $expr->name, $this->earlyTerminatingFunctionCalls, true)) { |
2304: | return $expr; |
2305: | } |
2306: | } |
2307: | |
2308: | if ($expr instanceof Expr\Exit_ || $expr instanceof Expr\Throw_) { |
2309: | return $expr; |
2310: | } |
2311: | |
2312: | $exprType = $scope->getType($expr); |
2313: | if ($exprType instanceof NeverType && $exprType->isExplicit()) { |
2314: | return $expr; |
2315: | } |
2316: | |
2317: | return null; |
2318: | } |
2319: | |
2320: | |
2321: | |
2322: | |
2323: | public function processExprNode(Node\Stmt $stmt, Expr $expr, MutatingScope $scope, callable $nodeCallback, ExpressionContext $context): ExpressionResult |
2324: | { |
2325: | if ($expr instanceof Expr\CallLike && $expr->isFirstClassCallable()) { |
2326: | if ($expr instanceof FuncCall) { |
2327: | $newExpr = new FunctionCallableNode($expr->name, $expr); |
2328: | } elseif ($expr instanceof MethodCall) { |
2329: | $newExpr = new MethodCallableNode($expr->var, $expr->name, $expr); |
2330: | } elseif ($expr instanceof StaticCall) { |
2331: | $newExpr = new StaticMethodCallableNode($expr->class, $expr->name, $expr); |
2332: | } elseif ($expr instanceof New_ && !$expr->class instanceof Class_) { |
2333: | $newExpr = new InstantiationCallableNode($expr->class, $expr); |
2334: | } else { |
2335: | throw new ShouldNotHappenException(); |
2336: | } |
2337: | |
2338: | return $this->processExprNode($stmt, $newExpr, $scope, $nodeCallback, $context); |
2339: | } |
2340: | |
2341: | $this->callNodeCallbackWithExpression($nodeCallback, $expr, $scope, $context); |
2342: | |
2343: | if ($expr instanceof Variable) { |
2344: | $hasYield = false; |
2345: | $throwPoints = []; |
2346: | $impurePoints = []; |
2347: | if ($expr->name instanceof Expr) { |
2348: | return $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep()); |
2349: | } elseif (in_array($expr->name, Scope::SUPERGLOBAL_VARIABLES, true)) { |
2350: | $impurePoints[] = new ImpurePoint($scope, $expr, 'superglobal', 'access to superglobal variable', true); |
2351: | } |
2352: | } elseif ($expr instanceof Assign || $expr instanceof AssignRef) { |
2353: | $result = $this->processAssignVar( |
2354: | $scope, |
2355: | $stmt, |
2356: | $expr->var, |
2357: | $expr->expr, |
2358: | $nodeCallback, |
2359: | $context, |
2360: | function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context): ExpressionResult { |
2361: | $impurePoints = []; |
2362: | if ($expr instanceof AssignRef) { |
2363: | $referencedExpr = $expr->expr; |
2364: | while ($referencedExpr instanceof ArrayDimFetch) { |
2365: | $referencedExpr = $referencedExpr->var; |
2366: | } |
2367: | |
2368: | if ($referencedExpr instanceof PropertyFetch || $referencedExpr instanceof StaticPropertyFetch) { |
2369: | $impurePoints[] = new ImpurePoint( |
2370: | $scope, |
2371: | $expr, |
2372: | 'propertyAssignByRef', |
2373: | 'property assignment by reference', |
2374: | false, |
2375: | ); |
2376: | } |
2377: | |
2378: | $scope = $scope->enterExpressionAssign($expr->expr); |
2379: | } |
2380: | |
2381: | if ($expr->var instanceof Variable && is_string($expr->var->name)) { |
2382: | $context = $context->enterRightSideAssign( |
2383: | $expr->var->name, |
2384: | $scope->getType($expr->expr), |
2385: | $scope->getNativeType($expr->expr), |
2386: | ); |
2387: | } |
2388: | |
2389: | $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); |
2390: | $hasYield = $result->hasYield(); |
2391: | $throwPoints = $result->getThrowPoints(); |
2392: | $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); |
2393: | $scope = $result->getScope(); |
2394: | |
2395: | if ($expr instanceof AssignRef) { |
2396: | $scope = $scope->exitExpressionAssign($expr->expr); |
2397: | } |
2398: | |
2399: | return new ExpressionResult($scope, $hasYield, $throwPoints, $impurePoints); |
2400: | }, |
2401: | true, |
2402: | ); |
2403: | $scope = $result->getScope(); |
2404: | $hasYield = $result->hasYield(); |
2405: | $throwPoints = $result->getThrowPoints(); |
2406: | $impurePoints = $result->getImpurePoints(); |
2407: | $vars = $this->getAssignedVariables($expr->var); |
2408: | if (count($vars) > 0) { |
2409: | $varChangedScope = false; |
2410: | $scope = $this->processVarAnnotation($scope, $vars, $stmt, $varChangedScope); |
2411: | if (!$varChangedScope) { |
2412: | $scope = $this->processStmtVarAnnotation($scope, $stmt, null, $nodeCallback); |
2413: | } |
2414: | } |
2415: | } elseif ($expr instanceof Expr\AssignOp) { |
2416: | $result = $this->processAssignVar( |
2417: | $scope, |
2418: | $stmt, |
2419: | $expr->var, |
2420: | $expr, |
2421: | $nodeCallback, |
2422: | $context, |
2423: | function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context): ExpressionResult { |
2424: | $originalScope = $scope; |
2425: | if ($expr instanceof Expr\AssignOp\Coalesce) { |
2426: | $scope = $scope->filterByFalseyValue( |
2427: | new BinaryOp\NotIdentical($expr->var, new ConstFetch(new Name('null'))), |
2428: | ); |
2429: | } |
2430: | |
2431: | $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); |
2432: | if ($expr instanceof Expr\AssignOp\Coalesce) { |
2433: | return new ExpressionResult( |
2434: | $result->getScope()->mergeWith($originalScope), |
2435: | $result->hasYield(), |
2436: | $result->getThrowPoints(), |
2437: | $result->getImpurePoints(), |
2438: | ); |
2439: | } |
2440: | |
2441: | return $result; |
2442: | }, |
2443: | $expr instanceof Expr\AssignOp\Coalesce, |
2444: | ); |
2445: | $scope = $result->getScope(); |
2446: | $hasYield = $result->hasYield(); |
2447: | $throwPoints = $result->getThrowPoints(); |
2448: | $impurePoints = $result->getImpurePoints(); |
2449: | if ( |
2450: | ($expr instanceof Expr\AssignOp\Div || $expr instanceof Expr\AssignOp\Mod) && |
2451: | !$scope->getType($expr->expr)->toNumber()->isSuperTypeOf(new ConstantIntegerType(0))->no() |
2452: | ) { |
2453: | $throwPoints[] = ThrowPoint::createExplicit($scope, new ObjectType(DivisionByZeroError::class), $expr, false); |
2454: | } |
2455: | } elseif ($expr instanceof FuncCall) { |
2456: | $parametersAcceptor = null; |
2457: | $functionReflection = null; |
2458: | $throwPoints = []; |
2459: | $impurePoints = []; |
2460: | if ($expr->name instanceof Expr) { |
2461: | $nameType = $scope->getType($expr->name); |
2462: | if (!$nameType->isCallable()->no()) { |
2463: | $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( |
2464: | $scope, |
2465: | $expr->getArgs(), |
2466: | $nameType->getCallableParametersAcceptors($scope), |
2467: | null, |
2468: | ); |
2469: | } |
2470: | |
2471: | $nameResult = $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep()); |
2472: | $scope = $nameResult->getScope(); |
2473: | $throwPoints = $nameResult->getThrowPoints(); |
2474: | $impurePoints = $nameResult->getImpurePoints(); |
2475: | if ( |
2476: | $nameType->isObject()->yes() |
2477: | && $nameType->isCallable()->yes() |
2478: | && (new ObjectType(Closure::class))->isSuperTypeOf($nameType)->no() |
2479: | ) { |
2480: | $invokeResult = $this->processExprNode( |
2481: | $stmt, |
2482: | new MethodCall($expr->name, '__invoke', $expr->getArgs(), $expr->getAttributes()), |
2483: | $scope, |
2484: | static function (): void { |
2485: | }, |
2486: | $context->enterDeep(), |
2487: | ); |
2488: | $throwPoints = array_merge($throwPoints, $invokeResult->getThrowPoints()); |
2489: | $impurePoints = array_merge($impurePoints, $invokeResult->getImpurePoints()); |
2490: | } elseif ($parametersAcceptor instanceof CallableParametersAcceptor) { |
2491: | $callableThrowPoints = array_map(static fn (SimpleThrowPoint $throwPoint) => $throwPoint->isExplicit() ? ThrowPoint::createExplicit($scope, $throwPoint->getType(), $expr, $throwPoint->canContainAnyThrowable()) : ThrowPoint::createImplicit($scope, $expr), $parametersAcceptor->getThrowPoints()); |
2492: | if (!$this->implicitThrows) { |
2493: | $callableThrowPoints = array_values(array_filter($callableThrowPoints, static fn (ThrowPoint $throwPoint) => $throwPoint->isExplicit())); |
2494: | } |
2495: | $throwPoints = array_merge($throwPoints, $callableThrowPoints); |
2496: | $impurePoints = array_merge($impurePoints, array_map(static fn (SimpleImpurePoint $impurePoint) => new ImpurePoint($scope, $expr, $impurePoint->getIdentifier(), $impurePoint->getDescription(), $impurePoint->isCertain()), $parametersAcceptor->getImpurePoints())); |
2497: | |
2498: | $scope = $this->processImmediatelyCalledCallable($scope, $parametersAcceptor->getInvalidateExpressions(), $parametersAcceptor->getUsedVariables()); |
2499: | } |
2500: | } elseif ($this->reflectionProvider->hasFunction($expr->name, $scope)) { |
2501: | $functionReflection = $this->reflectionProvider->getFunction($expr->name, $scope); |
2502: | $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( |
2503: | $scope, |
2504: | $expr->getArgs(), |
2505: | $functionReflection->getVariants(), |
2506: | $functionReflection->getNamedArgumentsVariants(), |
2507: | ); |
2508: | $impurePoint = SimpleImpurePoint::createFromVariant($functionReflection, $parametersAcceptor); |
2509: | if ($impurePoint !== null) { |
2510: | $impurePoints[] = new ImpurePoint($scope, $expr, $impurePoint->getIdentifier(), $impurePoint->getDescription(), $impurePoint->isCertain()); |
2511: | } |
2512: | } else { |
2513: | $impurePoints[] = new ImpurePoint( |
2514: | $scope, |
2515: | $expr, |
2516: | 'functionCall', |
2517: | 'call to unknown function', |
2518: | false, |
2519: | ); |
2520: | } |
2521: | |
2522: | if ($parametersAcceptor !== null) { |
2523: | $expr = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $expr) ?? $expr; |
2524: | } |
2525: | $result = $this->processArgs($stmt, $functionReflection, null, $parametersAcceptor, $expr, $scope, $nodeCallback, $context); |
2526: | $scope = $result->getScope(); |
2527: | $hasYield = $result->hasYield(); |
2528: | $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); |
2529: | $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); |
2530: | |
2531: | if ($functionReflection !== null) { |
2532: | $functionThrowPoint = $this->getFunctionThrowPoint($functionReflection, $parametersAcceptor, $expr, $scope); |
2533: | if ($functionThrowPoint !== null) { |
2534: | $throwPoints[] = $functionThrowPoint; |
2535: | } |
2536: | } else { |
2537: | $throwPoints[] = ThrowPoint::createImplicit($scope, $expr); |
2538: | } |
2539: | |
2540: | if ( |
2541: | $functionReflection !== null |
2542: | && in_array($functionReflection->getName(), ['json_encode', 'json_decode'], true) |
2543: | ) { |
2544: | $scope = $scope->invalidateExpression(new FuncCall(new Name('json_last_error'), [])) |
2545: | ->invalidateExpression(new FuncCall(new Name\FullyQualified('json_last_error'), [])) |
2546: | ->invalidateExpression(new FuncCall(new Name('json_last_error_msg'), [])) |
2547: | ->invalidateExpression(new FuncCall(new Name\FullyQualified('json_last_error_msg'), [])); |
2548: | } |
2549: | |
2550: | if ( |
2551: | $functionReflection !== null |
2552: | && $functionReflection->getName() === 'file_put_contents' |
2553: | && count($expr->getArgs()) > 0 |
2554: | ) { |
2555: | $scope = $scope->invalidateExpression(new FuncCall(new Name('file_get_contents'), [$expr->getArgs()[0]])) |
2556: | ->invalidateExpression(new FuncCall(new Name\FullyQualified('file_get_contents'), [$expr->getArgs()[0]])); |
2557: | } |
2558: | |
2559: | if ( |
2560: | $functionReflection !== null |
2561: | && in_array($functionReflection->getName(), ['array_pop', 'array_shift'], true) |
2562: | && count($expr->getArgs()) >= 1 |
2563: | ) { |
2564: | $arrayArg = $expr->getArgs()[0]->value; |
2565: | |
2566: | $arrayArgType = $scope->getType($arrayArg); |
2567: | $arrayArgNativeType = $scope->getNativeType($arrayArg); |
2568: | |
2569: | $isArrayPop = $functionReflection->getName() === 'array_pop'; |
2570: | $scope = $scope->invalidateExpression($arrayArg)->assignExpression( |
2571: | $arrayArg, |
2572: | $isArrayPop ? $arrayArgType->popArray() : $arrayArgType->shiftArray(), |
2573: | $isArrayPop ? $arrayArgNativeType->popArray() : $arrayArgNativeType->shiftArray(), |
2574: | ); |
2575: | } |
2576: | |
2577: | if ( |
2578: | $functionReflection !== null |
2579: | && in_array($functionReflection->getName(), ['array_push', 'array_unshift'], true) |
2580: | && count($expr->getArgs()) >= 2 |
2581: | ) { |
2582: | $arrayType = $this->getArrayFunctionAppendingType($functionReflection, $scope, $expr); |
2583: | $arrayNativeType = $this->getArrayFunctionAppendingType($functionReflection, $scope->doNotTreatPhpDocTypesAsCertain(), $expr); |
2584: | |
2585: | $arrayArg = $expr->getArgs()[0]->value; |
2586: | $scope = $scope->invalidateExpression($arrayArg)->assignExpression($arrayArg, $arrayType, $arrayNativeType); |
2587: | } |
2588: | |
2589: | if ( |
2590: | $functionReflection !== null |
2591: | && in_array($functionReflection->getName(), ['fopen', 'file_get_contents'], true) |
2592: | ) { |
2593: | $scope = $scope->assignVariable('http_response_header', TypeCombinator::intersect(new ArrayType(new IntegerType(), new StringType()), new AccessoryArrayListType()), new ArrayType(new IntegerType(), new StringType()), TrinaryLogic::createYes()); |
2594: | } |
2595: | |
2596: | if ( |
2597: | $functionReflection !== null |
2598: | && $functionReflection->getName() === 'shuffle' |
2599: | ) { |
2600: | $arrayArg = $expr->getArgs()[0]->value; |
2601: | $scope = $scope->assignExpression( |
2602: | $arrayArg, |
2603: | $scope->getType($arrayArg)->shuffleArray(), |
2604: | $scope->getNativeType($arrayArg)->shuffleArray(), |
2605: | ); |
2606: | } |
2607: | |
2608: | if ( |
2609: | $functionReflection !== null |
2610: | && $functionReflection->getName() === 'array_splice' |
2611: | && count($expr->getArgs()) >= 1 |
2612: | ) { |
2613: | $arrayArg = $expr->getArgs()[0]->value; |
2614: | $arrayArgType = $scope->getType($arrayArg); |
2615: | $valueType = $arrayArgType->getIterableValueType(); |
2616: | if (count($expr->getArgs()) >= 4) { |
2617: | $replacementType = $scope->getType($expr->getArgs()[3]->value)->toArray(); |
2618: | $valueType = TypeCombinator::union($valueType, $replacementType->getIterableValueType()); |
2619: | } |
2620: | $scope = $scope->invalidateExpression($arrayArg)->assignExpression( |
2621: | $arrayArg, |
2622: | new ArrayType($arrayArgType->getIterableKeyType(), $valueType), |
2623: | new ArrayType($arrayArgType->getIterableKeyType(), $valueType), |
2624: | ); |
2625: | } |
2626: | |
2627: | if ( |
2628: | $functionReflection !== null |
2629: | && in_array($functionReflection->getName(), ['sort', 'rsort', 'usort'], true) |
2630: | && count($expr->getArgs()) >= 1 |
2631: | ) { |
2632: | $arrayArg = $expr->getArgs()[0]->value; |
2633: | $scope = $scope->assignExpression( |
2634: | $arrayArg, |
2635: | $this->getArraySortPreserveListFunctionType($scope->getType($arrayArg)), |
2636: | $this->getArraySortPreserveListFunctionType($scope->getNativeType($arrayArg)), |
2637: | ); |
2638: | } |
2639: | |
2640: | if ( |
2641: | $functionReflection !== null |
2642: | && in_array($functionReflection->getName(), ['natcasesort', 'natsort', 'arsort', 'asort', 'ksort', 'krsort', 'uasort', 'uksort'], true) |
2643: | && count($expr->getArgs()) >= 1 |
2644: | ) { |
2645: | $arrayArg = $expr->getArgs()[0]->value; |
2646: | $scope = $scope->assignExpression( |
2647: | $arrayArg, |
2648: | $this->getArraySortDoNotPreserveListFunctionType($scope->getType($arrayArg)), |
2649: | $this->getArraySortDoNotPreserveListFunctionType($scope->getNativeType($arrayArg)), |
2650: | ); |
2651: | } |
2652: | |
2653: | if ( |
2654: | $functionReflection !== null |
2655: | && $functionReflection->getName() === 'extract' |
2656: | ) { |
2657: | $extractedArg = $expr->getArgs()[0]->value; |
2658: | $extractedType = $scope->getType($extractedArg); |
2659: | $constantArrays = $extractedType->getConstantArrays(); |
2660: | if (count($constantArrays) > 0) { |
2661: | $properties = []; |
2662: | $optionalProperties = []; |
2663: | $refCount = []; |
2664: | foreach ($constantArrays as $constantArray) { |
2665: | foreach ($constantArray->getKeyTypes() as $i => $keyType) { |
2666: | if ($keyType->isString()->no()) { |
2667: | |
2668: | continue; |
2669: | } |
2670: | $key = (string) $keyType->getValue(); |
2671: | $valueType = $constantArray->getValueTypes()[$i]; |
2672: | $optional = $constantArray->isOptionalKey($i); |
2673: | if ($optional) { |
2674: | $optionalProperties[] = $key; |
2675: | } |
2676: | if (isset($properties[$key])) { |
2677: | $properties[$key] = TypeCombinator::union($properties[$key], $valueType); |
2678: | $refCount[$key]++; |
2679: | } else { |
2680: | $properties[$key] = $valueType; |
2681: | $refCount[$key] = 1; |
2682: | } |
2683: | } |
2684: | } |
2685: | foreach ($properties as $name => $type) { |
2686: | $optional = in_array($name, $optionalProperties, true) || $refCount[$name] < count($constantArrays); |
2687: | $scope = $scope->assignVariable($name, $type, $type, $optional ? TrinaryLogic::createMaybe() : TrinaryLogic::createYes()); |
2688: | } |
2689: | } else { |
2690: | $scope = $scope->afterExtractCall(); |
2691: | } |
2692: | } |
2693: | |
2694: | if ( |
2695: | $functionReflection !== null |
2696: | && in_array($functionReflection->getName(), ['clearstatcache', 'unlink'], true) |
2697: | ) { |
2698: | $scope = $scope->afterClearstatcacheCall(); |
2699: | } |
2700: | |
2701: | if ( |
2702: | $functionReflection !== null |
2703: | && str_starts_with($functionReflection->getName(), 'openssl') |
2704: | ) { |
2705: | $scope = $scope->afterOpenSslCall($functionReflection->getName()); |
2706: | } |
2707: | |
2708: | } elseif ($expr instanceof MethodCall) { |
2709: | $originalScope = $scope; |
2710: | if ( |
2711: | ($expr->var instanceof Expr\Closure || $expr->var instanceof Expr\ArrowFunction) |
2712: | && $expr->name instanceof Node\Identifier |
2713: | && strtolower($expr->name->name) === 'call' |
2714: | && isset($expr->getArgs()[0]) |
2715: | ) { |
2716: | $closureCallScope = $scope->enterClosureCall( |
2717: | $scope->getType($expr->getArgs()[0]->value), |
2718: | $scope->getNativeType($expr->getArgs()[0]->value), |
2719: | ); |
2720: | } |
2721: | |
2722: | $result = $this->processExprNode($stmt, $expr->var, $closureCallScope ?? $scope, $nodeCallback, $context->enterDeep()); |
2723: | $hasYield = $result->hasYield(); |
2724: | $throwPoints = $result->getThrowPoints(); |
2725: | $impurePoints = $result->getImpurePoints(); |
2726: | $scope = $result->getScope(); |
2727: | if (isset($closureCallScope)) { |
2728: | $scope = $scope->restoreOriginalScopeAfterClosureBind($originalScope); |
2729: | } |
2730: | $parametersAcceptor = null; |
2731: | $methodReflection = null; |
2732: | $calledOnType = $scope->getType($expr->var); |
2733: | if ($expr->name instanceof Expr) { |
2734: | $methodNameResult = $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep()); |
2735: | $throwPoints = array_merge($throwPoints, $methodNameResult->getThrowPoints()); |
2736: | $scope = $methodNameResult->getScope(); |
2737: | } else { |
2738: | $methodName = $expr->name->name; |
2739: | $methodReflection = $scope->getMethodReflection($calledOnType, $methodName); |
2740: | if ($methodReflection !== null) { |
2741: | $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( |
2742: | $scope, |
2743: | $expr->getArgs(), |
2744: | $methodReflection->getVariants(), |
2745: | $methodReflection->getNamedArgumentsVariants(), |
2746: | ); |
2747: | |
2748: | $methodThrowPoint = $this->getMethodThrowPoint($methodReflection, $parametersAcceptor, $expr, $scope); |
2749: | if ($methodThrowPoint !== null) { |
2750: | $throwPoints[] = $methodThrowPoint; |
2751: | } |
2752: | } |
2753: | } |
2754: | |
2755: | if ($methodReflection !== null) { |
2756: | $impurePoint = SimpleImpurePoint::createFromVariant($methodReflection, $parametersAcceptor); |
2757: | if ($impurePoint !== null) { |
2758: | $impurePoints[] = new ImpurePoint($scope, $expr, $impurePoint->getIdentifier(), $impurePoint->getDescription(), $impurePoint->isCertain()); |
2759: | } |
2760: | } else { |
2761: | $impurePoints[] = new ImpurePoint( |
2762: | $scope, |
2763: | $expr, |
2764: | 'methodCall', |
2765: | 'call to unknown method', |
2766: | false, |
2767: | ); |
2768: | } |
2769: | |
2770: | if ($parametersAcceptor !== null) { |
2771: | $expr = ArgumentsNormalizer::reorderMethodArguments($parametersAcceptor, $expr) ?? $expr; |
2772: | } |
2773: | |
2774: | $result = $this->processArgs( |
2775: | $stmt, |
2776: | $methodReflection, |
2777: | $methodReflection !== null ? $scope->getNakedMethod($calledOnType, $methodReflection->getName()) : null, |
2778: | $parametersAcceptor, |
2779: | $expr, |
2780: | $scope, |
2781: | $nodeCallback, |
2782: | $context, |
2783: | ); |
2784: | $scope = $result->getScope(); |
2785: | |
2786: | if ($methodReflection !== null) { |
2787: | $hasSideEffects = $methodReflection->hasSideEffects(); |
2788: | if ($hasSideEffects->yes() || $methodReflection->getName() === '__construct') { |
2789: | $nodeCallback(new InvalidateExprNode($expr->var), $scope); |
2790: | $scope = $scope->invalidateExpression($expr->var, true); |
2791: | } |
2792: | if ($parametersAcceptor !== null && !$methodReflection->isStatic()) { |
2793: | $selfOutType = $methodReflection->getSelfOutType(); |
2794: | if ($selfOutType !== null) { |
2795: | $scope = $scope->assignExpression( |
2796: | $expr->var, |
2797: | TemplateTypeHelper::resolveTemplateTypes( |
2798: | $selfOutType, |
2799: | $parametersAcceptor->getResolvedTemplateTypeMap(), |
2800: | $parametersAcceptor instanceof ExtendedParametersAcceptor ? $parametersAcceptor->getCallSiteVarianceMap() : TemplateTypeVarianceMap::createEmpty(), |
2801: | TemplateTypeVariance::createCovariant(), |
2802: | ), |
2803: | $scope->getNativeType($expr->var), |
2804: | ); |
2805: | } |
2806: | } |
2807: | |
2808: | if ( |
2809: | $scope->isInClass() |
2810: | && $scope->getClassReflection()->getName() === $methodReflection->getDeclaringClass()->getName() |
2811: | |
2812: | |
2813: | |
2814: | |
2815: | |
2816: | |
2817: | && TypeUtils::findThisType($calledOnType) !== null |
2818: | ) { |
2819: | $calledMethodScope = $this->processCalledMethod($methodReflection); |
2820: | if ($calledMethodScope !== null) { |
2821: | $scope = $scope->mergeInitializedProperties($calledMethodScope); |
2822: | } |
2823: | } |
2824: | } else { |
2825: | $throwPoints[] = ThrowPoint::createImplicit($scope, $expr); |
2826: | } |
2827: | $hasYield = $hasYield || $result->hasYield(); |
2828: | $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); |
2829: | $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); |
2830: | } elseif ($expr instanceof Expr\NullsafeMethodCall) { |
2831: | $nonNullabilityResult = $this->ensureShallowNonNullability($scope, $scope, $expr->var); |
2832: | $exprResult = $this->processExprNode($stmt, new MethodCall($expr->var, $expr->name, $expr->args, array_merge($expr->getAttributes(), ['virtualNullsafeMethodCall' => true])), $nonNullabilityResult->getScope(), $nodeCallback, $context); |
2833: | $scope = $this->revertNonNullability($exprResult->getScope(), $nonNullabilityResult->getSpecifiedExpressions()); |
2834: | |
2835: | return new ExpressionResult( |
2836: | $scope, |
2837: | $exprResult->hasYield(), |
2838: | $exprResult->getThrowPoints(), |
2839: | $exprResult->getImpurePoints(), |
2840: | static fn (): MutatingScope => $scope->filterByTruthyValue($expr), |
2841: | static fn (): MutatingScope => $scope->filterByFalseyValue($expr), |
2842: | ); |
2843: | } elseif ($expr instanceof StaticCall) { |
2844: | $hasYield = false; |
2845: | $throwPoints = []; |
2846: | $impurePoints = []; |
2847: | if ($expr->class instanceof Expr) { |
2848: | $objectClasses = $scope->getType($expr->class)->getObjectClassNames(); |
2849: | if (count($objectClasses) !== 1) { |
2850: | $objectClasses = $scope->getType(new New_($expr->class))->getObjectClassNames(); |
2851: | } |
2852: | if (count($objectClasses) === 1) { |
2853: | $objectExprResult = $this->processExprNode($stmt, new StaticCall(new Name($objectClasses[0]), $expr->name, []), $scope, static function (): void { |
2854: | }, $context->enterDeep()); |
2855: | $additionalThrowPoints = $objectExprResult->getThrowPoints(); |
2856: | } else { |
2857: | $additionalThrowPoints = [ThrowPoint::createImplicit($scope, $expr)]; |
2858: | } |
2859: | $classResult = $this->processExprNode($stmt, $expr->class, $scope, $nodeCallback, $context->enterDeep()); |
2860: | $hasYield = $classResult->hasYield(); |
2861: | $throwPoints = array_merge($throwPoints, $classResult->getThrowPoints()); |
2862: | $impurePoints = array_merge($impurePoints, $classResult->getImpurePoints()); |
2863: | foreach ($additionalThrowPoints as $throwPoint) { |
2864: | $throwPoints[] = $throwPoint; |
2865: | } |
2866: | $scope = $classResult->getScope(); |
2867: | } |
2868: | |
2869: | $parametersAcceptor = null; |
2870: | $methodReflection = null; |
2871: | if ($expr->name instanceof Expr) { |
2872: | $result = $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep()); |
2873: | $hasYield = $hasYield || $result->hasYield(); |
2874: | $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); |
2875: | $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); |
2876: | $scope = $result->getScope(); |
2877: | } elseif ($expr->class instanceof Name) { |
2878: | $classType = $scope->resolveTypeByName($expr->class); |
2879: | $methodName = $expr->name->name; |
2880: | if ($classType->hasMethod($methodName)->yes()) { |
2881: | $methodReflection = $classType->getMethod($methodName, $scope); |
2882: | $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( |
2883: | $scope, |
2884: | $expr->getArgs(), |
2885: | $methodReflection->getVariants(), |
2886: | $methodReflection->getNamedArgumentsVariants(), |
2887: | ); |
2888: | |
2889: | $methodThrowPoint = $this->getStaticMethodThrowPoint($methodReflection, $parametersAcceptor, $expr, $scope); |
2890: | if ($methodThrowPoint !== null) { |
2891: | $throwPoints[] = $methodThrowPoint; |
2892: | } |
2893: | |
2894: | $declaringClass = $methodReflection->getDeclaringClass(); |
2895: | if ( |
2896: | $declaringClass->getName() === 'Closure' |
2897: | && strtolower($methodName) === 'bind' |
2898: | ) { |
2899: | $thisType = null; |
2900: | $nativeThisType = null; |
2901: | if (isset($expr->getArgs()[1])) { |
2902: | $argType = $scope->getType($expr->getArgs()[1]->value); |
2903: | if ($argType->isNull()->yes()) { |
2904: | $thisType = null; |
2905: | } else { |
2906: | $thisType = $argType; |
2907: | } |
2908: | |
2909: | $nativeArgType = $scope->getNativeType($expr->getArgs()[1]->value); |
2910: | if ($nativeArgType->isNull()->yes()) { |
2911: | $nativeThisType = null; |
2912: | } else { |
2913: | $nativeThisType = $nativeArgType; |
2914: | } |
2915: | } |
2916: | $scopeClasses = ['static']; |
2917: | if (isset($expr->getArgs()[2])) { |
2918: | $argValue = $expr->getArgs()[2]->value; |
2919: | $argValueType = $scope->getType($argValue); |
2920: | |
2921: | $directClassNames = $argValueType->getObjectClassNames(); |
2922: | if (count($directClassNames) > 0) { |
2923: | $scopeClasses = $directClassNames; |
2924: | $thisTypes = []; |
2925: | foreach ($directClassNames as $directClassName) { |
2926: | $thisTypes[] = new ObjectType($directClassName); |
2927: | } |
2928: | $thisType = TypeCombinator::union(...$thisTypes); |
2929: | } else { |
2930: | $thisType = $argValueType->getClassStringObjectType(); |
2931: | $scopeClasses = $thisType->getObjectClassNames(); |
2932: | } |
2933: | } |
2934: | $closureBindScope = $scope->enterClosureBind($thisType, $nativeThisType, $scopeClasses); |
2935: | } |
2936: | } else { |
2937: | $throwPoints[] = ThrowPoint::createImplicit($scope, $expr); |
2938: | } |
2939: | } |
2940: | |
2941: | if ($methodReflection !== null) { |
2942: | $impurePoint = SimpleImpurePoint::createFromVariant($methodReflection, $parametersAcceptor); |
2943: | if ($impurePoint !== null) { |
2944: | $impurePoints[] = new ImpurePoint($scope, $expr, $impurePoint->getIdentifier(), $impurePoint->getDescription(), $impurePoint->isCertain()); |
2945: | } |
2946: | } else { |
2947: | $impurePoints[] = new ImpurePoint( |
2948: | $scope, |
2949: | $expr, |
2950: | 'methodCall', |
2951: | 'call to unknown method', |
2952: | false, |
2953: | ); |
2954: | } |
2955: | |
2956: | if ($parametersAcceptor !== null) { |
2957: | $expr = ArgumentsNormalizer::reorderStaticCallArguments($parametersAcceptor, $expr) ?? $expr; |
2958: | } |
2959: | $result = $this->processArgs($stmt, $methodReflection, null, $parametersAcceptor, $expr, $scope, $nodeCallback, $context, $closureBindScope ?? null); |
2960: | $scope = $result->getScope(); |
2961: | $scopeFunction = $scope->getFunction(); |
2962: | |
2963: | if ( |
2964: | $methodReflection !== null |
2965: | && !$methodReflection->isStatic() |
2966: | && ( |
2967: | $methodReflection->hasSideEffects()->yes() |
2968: | || $methodReflection->getName() === '__construct' |
2969: | ) |
2970: | && $scopeFunction instanceof MethodReflection |
2971: | && !$scopeFunction->isStatic() |
2972: | && $scope->isInClass() |
2973: | && ( |
2974: | $scope->getClassReflection()->getName() === $methodReflection->getDeclaringClass()->getName() |
2975: | || $scope->getClassReflection()->isSubclassOf($methodReflection->getDeclaringClass()->getName()) |
2976: | ) |
2977: | ) { |
2978: | $scope = $scope->invalidateExpression(new Variable('this'), true); |
2979: | } |
2980: | |
2981: | if ( |
2982: | $methodReflection !== null |
2983: | && !$methodReflection->isStatic() |
2984: | && $methodReflection->getName() === '__construct' |
2985: | && $scopeFunction instanceof MethodReflection |
2986: | && !$scopeFunction->isStatic() |
2987: | && $scope->isInClass() |
2988: | && $scope->getClassReflection()->isSubclassOf($methodReflection->getDeclaringClass()->getName()) |
2989: | ) { |
2990: | $thisType = $scope->getType(new Variable('this')); |
2991: | $methodClassReflection = $methodReflection->getDeclaringClass(); |
2992: | foreach ($methodClassReflection->getNativeReflection()->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED) as $property) { |
2993: | if (!$property->isPromoted() || $property->getDeclaringClass()->getName() !== $methodClassReflection->getName()) { |
2994: | continue; |
2995: | } |
2996: | |
2997: | $scope = $scope->assignInitializedProperty($thisType, $property->getName()); |
2998: | } |
2999: | } |
3000: | |
3001: | $hasYield = $hasYield || $result->hasYield(); |
3002: | $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); |
3003: | $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); |
3004: | } elseif ($expr instanceof PropertyFetch) { |
3005: | $scopeBeforeVar = $scope; |
3006: | $result = $this->processExprNode($stmt, $expr->var, $scope, $nodeCallback, $context->enterDeep()); |
3007: | $hasYield = $result->hasYield(); |
3008: | $throwPoints = $result->getThrowPoints(); |
3009: | $impurePoints = $result->getImpurePoints(); |
3010: | $scope = $result->getScope(); |
3011: | if ($expr->name instanceof Expr) { |
3012: | $result = $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep()); |
3013: | $hasYield = $hasYield || $result->hasYield(); |
3014: | $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); |
3015: | $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); |
3016: | $scope = $result->getScope(); |
3017: | if ($this->phpVersion->supportsPropertyHooks()) { |
3018: | $throwPoints[] = ThrowPoint::createImplicit($scope, $expr); |
3019: | } |
3020: | } else { |
3021: | $propertyName = $expr->name->toString(); |
3022: | $propertyHolderType = $scopeBeforeVar->getType($expr->var); |
3023: | $propertyReflection = $scopeBeforeVar->getPropertyReflection($propertyHolderType, $propertyName); |
3024: | if ($propertyReflection !== null && $this->phpVersion->supportsPropertyHooks()) { |
3025: | $propertyDeclaringClass = $propertyReflection->getDeclaringClass(); |
3026: | if ($propertyDeclaringClass->hasNativeProperty($propertyName)) { |
3027: | $nativeProperty = $propertyDeclaringClass->getNativeProperty($propertyName); |
3028: | $throwPoints = array_merge($throwPoints, $this->getPropertyReadThrowPointsFromGetHook($scopeBeforeVar, $expr, $nativeProperty)); |
3029: | } |
3030: | } |
3031: | } |
3032: | } elseif ($expr instanceof Expr\NullsafePropertyFetch) { |
3033: | $nonNullabilityResult = $this->ensureShallowNonNullability($scope, $scope, $expr->var); |
3034: | $exprResult = $this->processExprNode($stmt, new PropertyFetch($expr->var, $expr->name, array_merge($expr->getAttributes(), ['virtualNullsafePropertyFetch' => true])), $nonNullabilityResult->getScope(), $nodeCallback, $context); |
3035: | $scope = $this->revertNonNullability($exprResult->getScope(), $nonNullabilityResult->getSpecifiedExpressions()); |
3036: | |
3037: | return new ExpressionResult( |
3038: | $scope, |
3039: | $exprResult->hasYield(), |
3040: | $exprResult->getThrowPoints(), |
3041: | $exprResult->getImpurePoints(), |
3042: | static fn (): MutatingScope => $scope->filterByTruthyValue($expr), |
3043: | static fn (): MutatingScope => $scope->filterByFalseyValue($expr), |
3044: | ); |
3045: | } elseif ($expr instanceof StaticPropertyFetch) { |
3046: | $hasYield = false; |
3047: | $throwPoints = []; |
3048: | $impurePoints = []; |
3049: | if ($expr->class instanceof Expr) { |
3050: | $result = $this->processExprNode($stmt, $expr->class, $scope, $nodeCallback, $context->enterDeep()); |
3051: | $hasYield = $result->hasYield(); |
3052: | $throwPoints = $result->getThrowPoints(); |
3053: | $impurePoints = $result->getImpurePoints(); |
3054: | $scope = $result->getScope(); |
3055: | } |
3056: | if ($expr->name instanceof Expr) { |
3057: | $result = $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep()); |
3058: | $hasYield = $hasYield || $result->hasYield(); |
3059: | $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); |
3060: | $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); |
3061: | $scope = $result->getScope(); |
3062: | } |
3063: | } elseif ($expr instanceof Expr\Closure) { |
3064: | $processClosureResult = $this->processClosureNode($stmt, $expr, $scope, $nodeCallback, $context, null); |
3065: | |
3066: | return new ExpressionResult( |
3067: | $processClosureResult->getScope(), |
3068: | false, |
3069: | [], |
3070: | [], |
3071: | ); |
3072: | } elseif ($expr instanceof Expr\ArrowFunction) { |
3073: | $result = $this->processArrowFunctionNode($stmt, $expr, $scope, $nodeCallback, null); |
3074: | return new ExpressionResult( |
3075: | $result->getScope(), |
3076: | $result->hasYield(), |
3077: | [], |
3078: | [], |
3079: | ); |
3080: | } elseif ($expr instanceof ErrorSuppress) { |
3081: | $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context); |
3082: | $hasYield = $result->hasYield(); |
3083: | $throwPoints = $result->getThrowPoints(); |
3084: | $impurePoints = $result->getImpurePoints(); |
3085: | $scope = $result->getScope(); |
3086: | } elseif ($expr instanceof Exit_) { |
3087: | $hasYield = false; |
3088: | $throwPoints = []; |
3089: | $kind = $expr->getAttribute('kind', Exit_::KIND_EXIT); |
3090: | $identifier = $kind === Exit_::KIND_DIE ? 'die' : 'exit'; |
3091: | $impurePoints = [ |
3092: | new ImpurePoint($scope, $expr, $identifier, $identifier, true), |
3093: | ]; |
3094: | if ($expr->expr !== null) { |
3095: | $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); |
3096: | $hasYield = $result->hasYield(); |
3097: | $throwPoints = $result->getThrowPoints(); |
3098: | $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); |
3099: | $scope = $result->getScope(); |
3100: | } |
3101: | } elseif ($expr instanceof Node\Scalar\InterpolatedString) { |
3102: | $hasYield = false; |
3103: | $throwPoints = []; |
3104: | $impurePoints = []; |
3105: | foreach ($expr->parts as $part) { |
3106: | if (!$part instanceof Expr) { |
3107: | continue; |
3108: | } |
3109: | $result = $this->processExprNode($stmt, $part, $scope, $nodeCallback, $context->enterDeep()); |
3110: | $hasYield = $hasYield || $result->hasYield(); |
3111: | $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); |
3112: | $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); |
3113: | $scope = $result->getScope(); |
3114: | } |
3115: | } elseif ($expr instanceof ArrayDimFetch) { |
3116: | $hasYield = false; |
3117: | $throwPoints = []; |
3118: | $impurePoints = []; |
3119: | if ($expr->dim !== null) { |
3120: | $result = $this->processExprNode($stmt, $expr->dim, $scope, $nodeCallback, $context->enterDeep()); |
3121: | $hasYield = $result->hasYield(); |
3122: | $throwPoints = $result->getThrowPoints(); |
3123: | $impurePoints = $result->getImpurePoints(); |
3124: | $scope = $result->getScope(); |
3125: | } |
3126: | |
3127: | $result = $this->processExprNode($stmt, $expr->var, $scope, $nodeCallback, $context->enterDeep()); |
3128: | $hasYield = $hasYield || $result->hasYield(); |
3129: | $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); |
3130: | $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); |
3131: | $scope = $result->getScope(); |
3132: | } elseif ($expr instanceof Array_) { |
3133: | $itemNodes = []; |
3134: | $hasYield = false; |
3135: | $throwPoints = []; |
3136: | $impurePoints = []; |
3137: | foreach ($expr->items as $arrayItem) { |
3138: | $itemNodes[] = new LiteralArrayItem($scope, $arrayItem); |
3139: | $nodeCallback($arrayItem, $scope); |
3140: | if ($arrayItem->key !== null) { |
3141: | $keyResult = $this->processExprNode($stmt, $arrayItem->key, $scope, $nodeCallback, $context->enterDeep()); |
3142: | $hasYield = $hasYield || $keyResult->hasYield(); |
3143: | $throwPoints = array_merge($throwPoints, $keyResult->getThrowPoints()); |
3144: | $impurePoints = array_merge($impurePoints, $keyResult->getImpurePoints()); |
3145: | $scope = $keyResult->getScope(); |
3146: | } |
3147: | |
3148: | $valueResult = $this->processExprNode($stmt, $arrayItem->value, $scope, $nodeCallback, $context->enterDeep()); |
3149: | $hasYield = $hasYield || $valueResult->hasYield(); |
3150: | $throwPoints = array_merge($throwPoints, $valueResult->getThrowPoints()); |
3151: | $impurePoints = array_merge($impurePoints, $valueResult->getImpurePoints()); |
3152: | $scope = $valueResult->getScope(); |
3153: | } |
3154: | $nodeCallback(new LiteralArrayNode($expr, $itemNodes), $scope); |
3155: | } elseif ($expr instanceof BooleanAnd || $expr instanceof BinaryOp\LogicalAnd) { |
3156: | $leftResult = $this->processExprNode($stmt, $expr->left, $scope, $nodeCallback, $context->enterDeep()); |
3157: | $rightResult = $this->processExprNode($stmt, $expr->right, $leftResult->getTruthyScope(), $nodeCallback, $context); |
3158: | $rightExprType = $rightResult->getScope()->getType($expr->right); |
3159: | if ($rightExprType instanceof NeverType && $rightExprType->isExplicit()) { |
3160: | $leftMergedWithRightScope = $leftResult->getFalseyScope(); |
3161: | } else { |
3162: | $leftMergedWithRightScope = $leftResult->getScope()->mergeWith($rightResult->getScope()); |
3163: | } |
3164: | |
3165: | $this->callNodeCallbackWithExpression($nodeCallback, new BooleanAndNode($expr, $leftResult->getTruthyScope()), $scope, $context); |
3166: | |
3167: | return new ExpressionResult( |
3168: | $leftMergedWithRightScope, |
3169: | $leftResult->hasYield() || $rightResult->hasYield(), |
3170: | array_merge($leftResult->getThrowPoints(), $rightResult->getThrowPoints()), |
3171: | array_merge($leftResult->getImpurePoints(), $rightResult->getImpurePoints()), |
3172: | static fn (): MutatingScope => $rightResult->getScope()->filterByTruthyValue($expr), |
3173: | static fn (): MutatingScope => $leftMergedWithRightScope->filterByFalseyValue($expr), |
3174: | ); |
3175: | } elseif ($expr instanceof BooleanOr || $expr instanceof BinaryOp\LogicalOr) { |
3176: | $leftResult = $this->processExprNode($stmt, $expr->left, $scope, $nodeCallback, $context->enterDeep()); |
3177: | $rightResult = $this->processExprNode($stmt, $expr->right, $leftResult->getFalseyScope(), $nodeCallback, $context); |
3178: | $rightExprType = $rightResult->getScope()->getType($expr->right); |
3179: | if ($rightExprType instanceof NeverType && $rightExprType->isExplicit()) { |
3180: | $leftMergedWithRightScope = $leftResult->getTruthyScope(); |
3181: | } else { |
3182: | $leftMergedWithRightScope = $leftResult->getScope()->mergeWith($rightResult->getScope()); |
3183: | } |
3184: | |
3185: | $this->callNodeCallbackWithExpression($nodeCallback, new BooleanOrNode($expr, $leftResult->getFalseyScope()), $scope, $context); |
3186: | |
3187: | return new ExpressionResult( |
3188: | $leftMergedWithRightScope, |
3189: | $leftResult->hasYield() || $rightResult->hasYield(), |
3190: | array_merge($leftResult->getThrowPoints(), $rightResult->getThrowPoints()), |
3191: | array_merge($leftResult->getImpurePoints(), $rightResult->getImpurePoints()), |
3192: | static fn (): MutatingScope => $leftMergedWithRightScope->filterByTruthyValue($expr), |
3193: | static fn (): MutatingScope => $rightResult->getScope()->filterByFalseyValue($expr), |
3194: | ); |
3195: | } elseif ($expr instanceof Coalesce) { |
3196: | $nonNullabilityResult = $this->ensureNonNullability($scope, $expr->left); |
3197: | $condScope = $this->lookForSetAllowedUndefinedExpressions($nonNullabilityResult->getScope(), $expr->left); |
3198: | $condResult = $this->processExprNode($stmt, $expr->left, $condScope, $nodeCallback, $context->enterDeep()); |
3199: | $scope = $this->revertNonNullability($condResult->getScope(), $nonNullabilityResult->getSpecifiedExpressions()); |
3200: | $scope = $this->lookForUnsetAllowedUndefinedExpressions($scope, $expr->left); |
3201: | |
3202: | $rightScope = $scope->filterByFalseyValue($expr); |
3203: | $rightResult = $this->processExprNode($stmt, $expr->right, $rightScope, $nodeCallback, $context->enterDeep()); |
3204: | $rightExprType = $scope->getType($expr->right); |
3205: | if ($rightExprType instanceof NeverType && $rightExprType->isExplicit()) { |
3206: | $scope = $scope->filterByTruthyValue(new Expr\Isset_([$expr->left])); |
3207: | } else { |
3208: | $scope = $scope->filterByTruthyValue(new Expr\Isset_([$expr->left]))->mergeWith($rightResult->getScope()); |
3209: | } |
3210: | |
3211: | $hasYield = $condResult->hasYield() || $rightResult->hasYield(); |
3212: | $throwPoints = array_merge($condResult->getThrowPoints(), $rightResult->getThrowPoints()); |
3213: | $impurePoints = array_merge($condResult->getImpurePoints(), $rightResult->getImpurePoints()); |
3214: | } elseif ($expr instanceof BinaryOp) { |
3215: | $result = $this->processExprNode($stmt, $expr->left, $scope, $nodeCallback, $context->enterDeep()); |
3216: | $scope = $result->getScope(); |
3217: | $hasYield = $result->hasYield(); |
3218: | $throwPoints = $result->getThrowPoints(); |
3219: | $impurePoints = $result->getImpurePoints(); |
3220: | $result = $this->processExprNode($stmt, $expr->right, $scope, $nodeCallback, $context->enterDeep()); |
3221: | if ( |
3222: | ($expr instanceof BinaryOp\Div || $expr instanceof BinaryOp\Mod) && |
3223: | !$scope->getType($expr->right)->toNumber()->isSuperTypeOf(new ConstantIntegerType(0))->no() |
3224: | ) { |
3225: | $throwPoints[] = ThrowPoint::createExplicit($scope, new ObjectType(DivisionByZeroError::class), $expr, false); |
3226: | } |
3227: | $scope = $result->getScope(); |
3228: | $hasYield = $hasYield || $result->hasYield(); |
3229: | $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); |
3230: | $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); |
3231: | } elseif ($expr instanceof Expr\Include_) { |
3232: | $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); |
3233: | $throwPoints = $result->getThrowPoints(); |
3234: | $throwPoints[] = ThrowPoint::createImplicit($scope, $expr); |
3235: | $impurePoints = $result->getImpurePoints(); |
3236: | $impurePoints[] = new ImpurePoint( |
3237: | $scope, |
3238: | $expr, |
3239: | in_array($expr->type, [Expr\Include_::TYPE_INCLUDE, Expr\Include_::TYPE_INCLUDE_ONCE], true) ? 'include' : 'require', |
3240: | in_array($expr->type, [Expr\Include_::TYPE_INCLUDE, Expr\Include_::TYPE_INCLUDE_ONCE], true) ? 'include' : 'require', |
3241: | true, |
3242: | ); |
3243: | $hasYield = $result->hasYield(); |
3244: | $scope = $result->getScope()->afterExtractCall(); |
3245: | } elseif ($expr instanceof Expr\Print_) { |
3246: | $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); |
3247: | $throwPoints = $result->getThrowPoints(); |
3248: | $impurePoints = $result->getImpurePoints(); |
3249: | $impurePoints[] = new ImpurePoint($scope, $expr, 'print', 'print', true); |
3250: | $hasYield = $result->hasYield(); |
3251: | |
3252: | $scope = $result->getScope(); |
3253: | } elseif ($expr instanceof Cast\String_) { |
3254: | $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); |
3255: | $throwPoints = $result->getThrowPoints(); |
3256: | $impurePoints = $result->getImpurePoints(); |
3257: | $hasYield = $result->hasYield(); |
3258: | |
3259: | $exprType = $scope->getType($expr->expr); |
3260: | $toStringMethod = $scope->getMethodReflection($exprType, '__toString'); |
3261: | if ($toStringMethod !== null) { |
3262: | if (!$toStringMethod->hasSideEffects()->no()) { |
3263: | $impurePoints[] = new ImpurePoint( |
3264: | $scope, |
3265: | $expr, |
3266: | 'methodCall', |
3267: | sprintf('call to method %s::%s()', $toStringMethod->getDeclaringClass()->getDisplayName(), $toStringMethod->getName()), |
3268: | $toStringMethod->isPure()->no(), |
3269: | ); |
3270: | } |
3271: | } |
3272: | |
3273: | $scope = $result->getScope(); |
3274: | } elseif ( |
3275: | $expr instanceof Expr\BitwiseNot |
3276: | || $expr instanceof Cast |
3277: | || $expr instanceof Expr\Clone_ |
3278: | || $expr instanceof Expr\UnaryMinus |
3279: | || $expr instanceof Expr\UnaryPlus |
3280: | ) { |
3281: | $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); |
3282: | $throwPoints = $result->getThrowPoints(); |
3283: | $impurePoints = $result->getImpurePoints(); |
3284: | $hasYield = $result->hasYield(); |
3285: | |
3286: | $scope = $result->getScope(); |
3287: | } elseif ($expr instanceof Expr\Eval_) { |
3288: | $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); |
3289: | $throwPoints = $result->getThrowPoints(); |
3290: | $throwPoints[] = ThrowPoint::createImplicit($scope, $expr); |
3291: | $impurePoints = $result->getImpurePoints(); |
3292: | $impurePoints[] = new ImpurePoint($scope, $expr, 'eval', 'eval', true); |
3293: | $hasYield = $result->hasYield(); |
3294: | |
3295: | $scope = $result->getScope(); |
3296: | } elseif ($expr instanceof Expr\YieldFrom) { |
3297: | $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); |
3298: | $throwPoints = $result->getThrowPoints(); |
3299: | $throwPoints[] = ThrowPoint::createImplicit($scope, $expr); |
3300: | $impurePoints = $result->getImpurePoints(); |
3301: | $impurePoints[] = new ImpurePoint( |
3302: | $scope, |
3303: | $expr, |
3304: | 'yieldFrom', |
3305: | 'yield from', |
3306: | true, |
3307: | ); |
3308: | $hasYield = true; |
3309: | |
3310: | $scope = $result->getScope(); |
3311: | } elseif ($expr instanceof BooleanNot) { |
3312: | $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); |
3313: | $scope = $result->getScope(); |
3314: | $hasYield = $result->hasYield(); |
3315: | $throwPoints = $result->getThrowPoints(); |
3316: | $impurePoints = $result->getImpurePoints(); |
3317: | } elseif ($expr instanceof Expr\ClassConstFetch) { |
3318: | if ($expr->class instanceof Expr) { |
3319: | $result = $this->processExprNode($stmt, $expr->class, $scope, $nodeCallback, $context->enterDeep()); |
3320: | $scope = $result->getScope(); |
3321: | $hasYield = $result->hasYield(); |
3322: | $throwPoints = $result->getThrowPoints(); |
3323: | $impurePoints = $result->getImpurePoints(); |
3324: | } else { |
3325: | $hasYield = false; |
3326: | $throwPoints = []; |
3327: | $impurePoints = []; |
3328: | $nodeCallback($expr->class, $scope); |
3329: | } |
3330: | |
3331: | if ($expr->name instanceof Expr) { |
3332: | $result = $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep()); |
3333: | $scope = $result->getScope(); |
3334: | $hasYield = $hasYield || $result->hasYield(); |
3335: | $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); |
3336: | $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); |
3337: | } else { |
3338: | $nodeCallback($expr->name, $scope); |
3339: | } |
3340: | } elseif ($expr instanceof Expr\Empty_) { |
3341: | $nonNullabilityResult = $this->ensureNonNullability($scope, $expr->expr); |
3342: | $scope = $this->lookForSetAllowedUndefinedExpressions($nonNullabilityResult->getScope(), $expr->expr); |
3343: | $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); |
3344: | $scope = $result->getScope(); |
3345: | $hasYield = $result->hasYield(); |
3346: | $throwPoints = $result->getThrowPoints(); |
3347: | $impurePoints = $result->getImpurePoints(); |
3348: | $scope = $this->revertNonNullability($scope, $nonNullabilityResult->getSpecifiedExpressions()); |
3349: | $scope = $this->lookForUnsetAllowedUndefinedExpressions($scope, $expr->expr); |
3350: | } elseif ($expr instanceof Expr\Isset_) { |
3351: | $hasYield = false; |
3352: | $throwPoints = []; |
3353: | $impurePoints = []; |
3354: | $nonNullabilityResults = []; |
3355: | foreach ($expr->vars as $var) { |
3356: | $nonNullabilityResult = $this->ensureNonNullability($scope, $var); |
3357: | $scope = $this->lookForSetAllowedUndefinedExpressions($nonNullabilityResult->getScope(), $var); |
3358: | $result = $this->processExprNode($stmt, $var, $scope, $nodeCallback, $context->enterDeep()); |
3359: | $scope = $result->getScope(); |
3360: | $hasYield = $hasYield || $result->hasYield(); |
3361: | $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); |
3362: | $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); |
3363: | $nonNullabilityResults[] = $nonNullabilityResult; |
3364: | } |
3365: | foreach (array_reverse($expr->vars) as $var) { |
3366: | $scope = $this->lookForUnsetAllowedUndefinedExpressions($scope, $var); |
3367: | } |
3368: | foreach (array_reverse($nonNullabilityResults) as $nonNullabilityResult) { |
3369: | $scope = $this->revertNonNullability($scope, $nonNullabilityResult->getSpecifiedExpressions()); |
3370: | } |
3371: | } elseif ($expr instanceof Instanceof_) { |
3372: | $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); |
3373: | $scope = $result->getScope(); |
3374: | $hasYield = $result->hasYield(); |
3375: | $throwPoints = $result->getThrowPoints(); |
3376: | $impurePoints = $result->getImpurePoints(); |
3377: | if ($expr->class instanceof Expr) { |
3378: | $result = $this->processExprNode($stmt, $expr->class, $scope, $nodeCallback, $context->enterDeep()); |
3379: | $scope = $result->getScope(); |
3380: | $hasYield = $hasYield || $result->hasYield(); |
3381: | $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); |
3382: | $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); |
3383: | } |
3384: | } elseif ($expr instanceof List_) { |
3385: | |
3386: | return new ExpressionResult($scope, false, [], []); |
3387: | } elseif ($expr instanceof New_) { |
3388: | $parametersAcceptor = null; |
3389: | $constructorReflection = null; |
3390: | $hasYield = false; |
3391: | $throwPoints = []; |
3392: | $impurePoints = []; |
3393: | $className = null; |
3394: | if ($expr->class instanceof Expr || $expr->class instanceof Name) { |
3395: | if ($expr->class instanceof Expr) { |
3396: | $objectClasses = $scope->getType($expr)->getObjectClassNames(); |
3397: | if (count($objectClasses) === 1) { |
3398: | $objectExprResult = $this->processExprNode($stmt, new New_(new Name($objectClasses[0])), $scope, static function (): void { |
3399: | }, $context->enterDeep()); |
3400: | $className = $objectClasses[0]; |
3401: | $additionalThrowPoints = $objectExprResult->getThrowPoints(); |
3402: | } else { |
3403: | $additionalThrowPoints = [ThrowPoint::createImplicit($scope, $expr)]; |
3404: | } |
3405: | |
3406: | $result = $this->processExprNode($stmt, $expr->class, $scope, $nodeCallback, $context->enterDeep()); |
3407: | $scope = $result->getScope(); |
3408: | $hasYield = $result->hasYield(); |
3409: | $throwPoints = $result->getThrowPoints(); |
3410: | $impurePoints = $result->getImpurePoints(); |
3411: | foreach ($additionalThrowPoints as $throwPoint) { |
3412: | $throwPoints[] = $throwPoint; |
3413: | } |
3414: | } else { |
3415: | $className = $scope->resolveName($expr->class); |
3416: | } |
3417: | |
3418: | $classReflection = null; |
3419: | if ($className !== null && $this->reflectionProvider->hasClass($className)) { |
3420: | $classReflection = $this->reflectionProvider->getClass($className); |
3421: | if ($classReflection->hasConstructor()) { |
3422: | $constructorReflection = $classReflection->getConstructor(); |
3423: | $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( |
3424: | $scope, |
3425: | $expr->getArgs(), |
3426: | $constructorReflection->getVariants(), |
3427: | $constructorReflection->getNamedArgumentsVariants(), |
3428: | ); |
3429: | $constructorThrowPoint = $this->getConstructorThrowPoint($constructorReflection, $parametersAcceptor, $classReflection, $expr, new Name\FullyQualified($className), $expr->getArgs(), $scope); |
3430: | if ($constructorThrowPoint !== null) { |
3431: | $throwPoints[] = $constructorThrowPoint; |
3432: | } |
3433: | } |
3434: | } else { |
3435: | $throwPoints[] = ThrowPoint::createImplicit($scope, $expr); |
3436: | } |
3437: | |
3438: | if ($constructorReflection !== null) { |
3439: | if (!$constructorReflection->hasSideEffects()->no()) { |
3440: | $certain = $constructorReflection->isPure()->no(); |
3441: | $impurePoints[] = new ImpurePoint( |
3442: | $scope, |
3443: | $expr, |
3444: | 'new', |
3445: | sprintf('instantiation of class %s', $constructorReflection->getDeclaringClass()->getDisplayName()), |
3446: | $certain, |
3447: | ); |
3448: | } |
3449: | } elseif ($classReflection === null) { |
3450: | $impurePoints[] = new ImpurePoint( |
3451: | $scope, |
3452: | $expr, |
3453: | 'new', |
3454: | 'instantiation of unknown class', |
3455: | false, |
3456: | ); |
3457: | } |
3458: | |
3459: | if ($parametersAcceptor !== null) { |
3460: | $expr = ArgumentsNormalizer::reorderNewArguments($parametersAcceptor, $expr) ?? $expr; |
3461: | } |
3462: | |
3463: | } else { |
3464: | $classReflection = $this->reflectionProvider->getAnonymousClassReflection($expr->class, $scope); |
3465: | $constructorResult = null; |
3466: | $this->processStmtNode($expr->class, $scope, static function (Node $node, Scope $scope) use ($nodeCallback, $classReflection, &$constructorResult): void { |
3467: | $nodeCallback($node, $scope); |
3468: | if (!$node instanceof MethodReturnStatementsNode) { |
3469: | return; |
3470: | } |
3471: | if ($constructorResult !== null) { |
3472: | return; |
3473: | } |
3474: | $currentClassReflection = $node->getClassReflection(); |
3475: | if ($currentClassReflection->getName() !== $classReflection->getName()) { |
3476: | return; |
3477: | } |
3478: | if (!$currentClassReflection->hasConstructor()) { |
3479: | return; |
3480: | } |
3481: | if ($currentClassReflection->getConstructor()->getName() !== $node->getMethodReflection()->getName()) { |
3482: | return; |
3483: | } |
3484: | $constructorResult = $node; |
3485: | }, StatementContext::createTopLevel()); |
3486: | if ($constructorResult !== null) { |
3487: | $throwPoints = array_merge($throwPoints, $constructorResult->getStatementResult()->getThrowPoints()); |
3488: | $impurePoints = array_merge($impurePoints, $constructorResult->getImpurePoints()); |
3489: | } |
3490: | if ($classReflection->hasConstructor()) { |
3491: | $constructorReflection = $classReflection->getConstructor(); |
3492: | $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( |
3493: | $scope, |
3494: | $expr->getArgs(), |
3495: | $constructorReflection->getVariants(), |
3496: | $constructorReflection->getNamedArgumentsVariants(), |
3497: | ); |
3498: | } |
3499: | } |
3500: | |
3501: | $result = $this->processArgs($stmt, $constructorReflection, null, $parametersAcceptor, $expr, $scope, $nodeCallback, $context); |
3502: | $scope = $result->getScope(); |
3503: | $hasYield = $hasYield || $result->hasYield(); |
3504: | $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); |
3505: | $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); |
3506: | } elseif ( |
3507: | $expr instanceof Expr\PreInc |
3508: | || $expr instanceof Expr\PostInc |
3509: | || $expr instanceof Expr\PreDec |
3510: | || $expr instanceof Expr\PostDec |
3511: | ) { |
3512: | $result = $this->processExprNode($stmt, $expr->var, $scope, $nodeCallback, $context->enterDeep()); |
3513: | $scope = $result->getScope(); |
3514: | $hasYield = $result->hasYield(); |
3515: | $throwPoints = $result->getThrowPoints(); |
3516: | $impurePoints = $result->getImpurePoints(); |
3517: | |
3518: | $newExpr = $expr; |
3519: | if ($expr instanceof Expr\PostInc) { |
3520: | $newExpr = new Expr\PreInc($expr->var); |
3521: | } elseif ($expr instanceof Expr\PostDec) { |
3522: | $newExpr = new Expr\PreDec($expr->var); |
3523: | } |
3524: | |
3525: | $scope = $this->processAssignVar( |
3526: | $scope, |
3527: | $stmt, |
3528: | $expr->var, |
3529: | $newExpr, |
3530: | static function (Node $node, Scope $scope) use ($nodeCallback): void { |
3531: | if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) { |
3532: | return; |
3533: | } |
3534: | |
3535: | $nodeCallback($node, $scope); |
3536: | }, |
3537: | $context, |
3538: | static fn (MutatingScope $scope): ExpressionResult => new ExpressionResult($scope, false, [], []), |
3539: | false, |
3540: | )->getScope(); |
3541: | } elseif ($expr instanceof Ternary) { |
3542: | $ternaryCondResult = $this->processExprNode($stmt, $expr->cond, $scope, $nodeCallback, $context->enterDeep()); |
3543: | $throwPoints = $ternaryCondResult->getThrowPoints(); |
3544: | $impurePoints = $ternaryCondResult->getImpurePoints(); |
3545: | $ifTrueScope = $ternaryCondResult->getTruthyScope(); |
3546: | $ifFalseScope = $ternaryCondResult->getFalseyScope(); |
3547: | $ifTrueType = null; |
3548: | if ($expr->if !== null) { |
3549: | $ifResult = $this->processExprNode($stmt, $expr->if, $ifTrueScope, $nodeCallback, $context); |
3550: | $throwPoints = array_merge($throwPoints, $ifResult->getThrowPoints()); |
3551: | $impurePoints = array_merge($impurePoints, $ifResult->getImpurePoints()); |
3552: | $ifTrueScope = $ifResult->getScope(); |
3553: | $ifTrueType = $ifTrueScope->getType($expr->if); |
3554: | } |
3555: | |
3556: | $elseResult = $this->processExprNode($stmt, $expr->else, $ifFalseScope, $nodeCallback, $context); |
3557: | $throwPoints = array_merge($throwPoints, $elseResult->getThrowPoints()); |
3558: | $impurePoints = array_merge($impurePoints, $elseResult->getImpurePoints()); |
3559: | $ifFalseScope = $elseResult->getScope(); |
3560: | |
3561: | $condType = $scope->getType($expr->cond); |
3562: | if ($condType->isTrue()->yes()) { |
3563: | $finalScope = $ifTrueScope; |
3564: | } elseif ($condType->isFalse()->yes()) { |
3565: | $finalScope = $ifFalseScope; |
3566: | } else { |
3567: | if ($ifTrueType instanceof NeverType && $ifTrueType->isExplicit()) { |
3568: | $finalScope = $ifFalseScope; |
3569: | } else { |
3570: | $ifFalseType = $ifFalseScope->getType($expr->else); |
3571: | |
3572: | if ($ifFalseType instanceof NeverType && $ifFalseType->isExplicit()) { |
3573: | $finalScope = $ifTrueScope; |
3574: | } else { |
3575: | $finalScope = $ifTrueScope->mergeWith($ifFalseScope); |
3576: | } |
3577: | } |
3578: | } |
3579: | |
3580: | return new ExpressionResult( |
3581: | $finalScope, |
3582: | $ternaryCondResult->hasYield(), |
3583: | $throwPoints, |
3584: | $impurePoints, |
3585: | static fn (): MutatingScope => $finalScope->filterByTruthyValue($expr), |
3586: | static fn (): MutatingScope => $finalScope->filterByFalseyValue($expr), |
3587: | ); |
3588: | |
3589: | } elseif ($expr instanceof Expr\Yield_) { |
3590: | $throwPoints = [ |
3591: | ThrowPoint::createImplicit($scope, $expr), |
3592: | ]; |
3593: | $impurePoints = [ |
3594: | new ImpurePoint( |
3595: | $scope, |
3596: | $expr, |
3597: | 'yield', |
3598: | 'yield', |
3599: | true, |
3600: | ), |
3601: | ]; |
3602: | if ($expr->key !== null) { |
3603: | $keyResult = $this->processExprNode($stmt, $expr->key, $scope, $nodeCallback, $context->enterDeep()); |
3604: | $scope = $keyResult->getScope(); |
3605: | $throwPoints = $keyResult->getThrowPoints(); |
3606: | $impurePoints = array_merge($impurePoints, $keyResult->getImpurePoints()); |
3607: | } |
3608: | if ($expr->value !== null) { |
3609: | $valueResult = $this->processExprNode($stmt, $expr->value, $scope, $nodeCallback, $context->enterDeep()); |
3610: | $scope = $valueResult->getScope(); |
3611: | $throwPoints = array_merge($throwPoints, $valueResult->getThrowPoints()); |
3612: | $impurePoints = array_merge($impurePoints, $valueResult->getImpurePoints()); |
3613: | } |
3614: | $hasYield = true; |
3615: | } elseif ($expr instanceof Expr\Match_) { |
3616: | $deepContext = $context->enterDeep(); |
3617: | $condType = $scope->getType($expr->cond); |
3618: | $condResult = $this->processExprNode($stmt, $expr->cond, $scope, $nodeCallback, $deepContext); |
3619: | $scope = $condResult->getScope(); |
3620: | $hasYield = $condResult->hasYield(); |
3621: | $throwPoints = $condResult->getThrowPoints(); |
3622: | $impurePoints = $condResult->getImpurePoints(); |
3623: | $matchScope = $scope->enterMatch($expr); |
3624: | $armNodes = []; |
3625: | $hasDefaultCond = false; |
3626: | $hasAlwaysTrueCond = false; |
3627: | $arms = $expr->arms; |
3628: | if ($condType->isEnum()->yes()) { |
3629: | |
3630: | |
3631: | |
3632: | |
3633: | $enumCases = $condType->getEnumCases(); |
3634: | if (count($enumCases) > 0) { |
3635: | $indexedEnumCases = []; |
3636: | foreach ($enumCases as $enumCase) { |
3637: | $indexedEnumCases[strtolower($enumCase->getClassName())][$enumCase->getEnumCaseName()] = $enumCase; |
3638: | } |
3639: | $unusedIndexedEnumCases = $indexedEnumCases; |
3640: | foreach ($arms as $i => $arm) { |
3641: | if ($arm->conds === null) { |
3642: | continue; |
3643: | } |
3644: | |
3645: | $condNodes = []; |
3646: | $conditionCases = []; |
3647: | foreach ($arm->conds as $cond) { |
3648: | if (!$cond instanceof Expr\ClassConstFetch) { |
3649: | continue 2; |
3650: | } |
3651: | if (!$cond->class instanceof Name) { |
3652: | continue 2; |
3653: | } |
3654: | if (!$cond->name instanceof Node\Identifier) { |
3655: | continue 2; |
3656: | } |
3657: | $fetchedClassName = $scope->resolveName($cond->class); |
3658: | $loweredFetchedClassName = strtolower($fetchedClassName); |
3659: | if (!array_key_exists($loweredFetchedClassName, $indexedEnumCases)) { |
3660: | continue 2; |
3661: | } |
3662: | |
3663: | if (!array_key_exists($loweredFetchedClassName, $unusedIndexedEnumCases)) { |
3664: | throw new ShouldNotHappenException(); |
3665: | } |
3666: | |
3667: | $caseName = $cond->name->toString(); |
3668: | if (!array_key_exists($caseName, $indexedEnumCases[$loweredFetchedClassName])) { |
3669: | continue 2; |
3670: | } |
3671: | |
3672: | $enumCase = $indexedEnumCases[$loweredFetchedClassName][$caseName]; |
3673: | $conditionCases[] = $enumCase; |
3674: | $armConditionScope = $matchScope; |
3675: | if (!array_key_exists($caseName, $unusedIndexedEnumCases[$loweredFetchedClassName])) { |
3676: | |
3677: | $armConditionScope = $armConditionScope->removeTypeFromExpression( |
3678: | $expr->cond, |
3679: | $enumCase, |
3680: | ); |
3681: | } else { |
3682: | $unusedCasesCount = 0; |
3683: | foreach ($unusedIndexedEnumCases as $cases) { |
3684: | $unusedCasesCount += count($cases); |
3685: | } |
3686: | if ($unusedCasesCount === 1) { |
3687: | $hasAlwaysTrueCond = true; |
3688: | |
3689: | |
3690: | $armConditionScope = $armConditionScope->addTypeToExpression( |
3691: | $expr->cond, |
3692: | $enumCase, |
3693: | ); |
3694: | } |
3695: | } |
3696: | |
3697: | $this->processExprNode($stmt, $cond, $armConditionScope, $nodeCallback, $deepContext); |
3698: | |
3699: | $condNodes[] = new MatchExpressionArmCondition( |
3700: | $cond, |
3701: | $armConditionScope, |
3702: | $cond->getStartLine(), |
3703: | ); |
3704: | |
3705: | unset($unusedIndexedEnumCases[$loweredFetchedClassName][$caseName]); |
3706: | } |
3707: | |
3708: | $conditionCasesCount = count($conditionCases); |
3709: | if ($conditionCasesCount === 0) { |
3710: | throw new ShouldNotHappenException(); |
3711: | } elseif ($conditionCasesCount === 1) { |
3712: | $conditionCaseType = $conditionCases[0]; |
3713: | } else { |
3714: | $conditionCaseType = new UnionType($conditionCases); |
3715: | } |
3716: | |
3717: | $matchArmBodyScope = $matchScope->addTypeToExpression( |
3718: | $expr->cond, |
3719: | $conditionCaseType, |
3720: | ); |
3721: | $matchArmBody = new MatchExpressionArmBody($matchArmBodyScope, $arm->body); |
3722: | $armNodes[$i] = new MatchExpressionArm($matchArmBody, $condNodes, $arm->getStartLine()); |
3723: | |
3724: | $armResult = $this->processExprNode( |
3725: | $stmt, |
3726: | $arm->body, |
3727: | $matchArmBodyScope, |
3728: | $nodeCallback, |
3729: | ExpressionContext::createTopLevel(), |
3730: | ); |
3731: | $armScope = $armResult->getScope(); |
3732: | $scope = $scope->mergeWith($armScope); |
3733: | $hasYield = $hasYield || $armResult->hasYield(); |
3734: | $throwPoints = array_merge($throwPoints, $armResult->getThrowPoints()); |
3735: | $impurePoints = array_merge($impurePoints, $armResult->getImpurePoints()); |
3736: | |
3737: | unset($arms[$i]); |
3738: | } |
3739: | |
3740: | $remainingCases = []; |
3741: | foreach ($unusedIndexedEnumCases as $cases) { |
3742: | foreach ($cases as $case) { |
3743: | $remainingCases[] = $case; |
3744: | } |
3745: | } |
3746: | |
3747: | $remainingCasesCount = count($remainingCases); |
3748: | if ($remainingCasesCount === 0) { |
3749: | $remainingType = new NeverType(); |
3750: | } elseif ($remainingCasesCount === 1) { |
3751: | $remainingType = $remainingCases[0]; |
3752: | } else { |
3753: | $remainingType = new UnionType($remainingCases); |
3754: | } |
3755: | |
3756: | $matchScope = $matchScope->addTypeToExpression($expr->cond, $remainingType); |
3757: | } |
3758: | } |
3759: | foreach ($arms as $i => $arm) { |
3760: | if ($arm->conds === null) { |
3761: | $hasDefaultCond = true; |
3762: | $matchArmBody = new MatchExpressionArmBody($matchScope, $arm->body); |
3763: | $armNodes[$i] = new MatchExpressionArm($matchArmBody, [], $arm->getStartLine()); |
3764: | $armResult = $this->processExprNode($stmt, $arm->body, $matchScope, $nodeCallback, ExpressionContext::createTopLevel()); |
3765: | $matchScope = $armResult->getScope(); |
3766: | $hasYield = $hasYield || $armResult->hasYield(); |
3767: | $throwPoints = array_merge($throwPoints, $armResult->getThrowPoints()); |
3768: | $impurePoints = array_merge($impurePoints, $armResult->getImpurePoints()); |
3769: | $scope = $scope->mergeWith($matchScope); |
3770: | continue; |
3771: | } |
3772: | |
3773: | if (count($arm->conds) === 0) { |
3774: | throw new ShouldNotHappenException(); |
3775: | } |
3776: | |
3777: | $filteringExprs = []; |
3778: | $armCondScope = $matchScope; |
3779: | $condNodes = []; |
3780: | foreach ($arm->conds as $armCond) { |
3781: | $condNodes[] = new MatchExpressionArmCondition($armCond, $armCondScope, $armCond->getStartLine()); |
3782: | $armCondResult = $this->processExprNode($stmt, $armCond, $armCondScope, $nodeCallback, $deepContext); |
3783: | $hasYield = $hasYield || $armCondResult->hasYield(); |
3784: | $throwPoints = array_merge($throwPoints, $armCondResult->getThrowPoints()); |
3785: | $impurePoints = array_merge($impurePoints, $armCondResult->getImpurePoints()); |
3786: | $armCondExpr = new BinaryOp\Identical($expr->cond, $armCond); |
3787: | $armCondResultScope = $armCondResult->getScope(); |
3788: | $armCondType = $this->treatPhpDocTypesAsCertain ? $armCondResultScope->getType($armCondExpr) : $armCondResultScope->getNativeType($armCondExpr); |
3789: | if ($armCondType->isTrue()->yes()) { |
3790: | $hasAlwaysTrueCond = true; |
3791: | } |
3792: | $armCondScope = $armCondResult->getScope()->filterByFalseyValue($armCondExpr); |
3793: | $filteringExprs[] = $armCond; |
3794: | } |
3795: | |
3796: | if (count($filteringExprs) === 1) { |
3797: | $filteringExpr = new BinaryOp\Identical($expr->cond, $filteringExprs[0]); |
3798: | } else { |
3799: | $items = []; |
3800: | foreach ($filteringExprs as $filteringExpr) { |
3801: | $items[] = new Node\ArrayItem($filteringExpr); |
3802: | } |
3803: | $filteringExpr = new FuncCall( |
3804: | new Name\FullyQualified('in_array'), |
3805: | [ |
3806: | new Arg($expr->cond), |
3807: | new Arg(new Array_($items)), |
3808: | new Arg(new ConstFetch(new Name\FullyQualified('true'))), |
3809: | ], |
3810: | ); |
3811: | } |
3812: | |
3813: | $bodyScope = $this->processExprNode($stmt, $filteringExpr, $matchScope, static function (): void { |
3814: | }, $deepContext)->getTruthyScope(); |
3815: | $matchArmBody = new MatchExpressionArmBody($bodyScope, $arm->body); |
3816: | $armNodes[$i] = new MatchExpressionArm($matchArmBody, $condNodes, $arm->getStartLine()); |
3817: | |
3818: | $armResult = $this->processExprNode( |
3819: | $stmt, |
3820: | $arm->body, |
3821: | $bodyScope, |
3822: | $nodeCallback, |
3823: | ExpressionContext::createTopLevel(), |
3824: | ); |
3825: | $armScope = $armResult->getScope(); |
3826: | $scope = $scope->mergeWith($armScope); |
3827: | $hasYield = $hasYield || $armResult->hasYield(); |
3828: | $throwPoints = array_merge($throwPoints, $armResult->getThrowPoints()); |
3829: | $impurePoints = array_merge($impurePoints, $armResult->getImpurePoints()); |
3830: | $matchScope = $matchScope->filterByFalseyValue($filteringExpr); |
3831: | } |
3832: | |
3833: | $remainingType = $matchScope->getType($expr->cond); |
3834: | if (!$hasDefaultCond && !$hasAlwaysTrueCond && !$remainingType instanceof NeverType) { |
3835: | $throwPoints[] = ThrowPoint::createExplicit($scope, new ObjectType(UnhandledMatchError::class), $expr, false); |
3836: | } |
3837: | |
3838: | ksort($armNodes, SORT_NUMERIC); |
3839: | |
3840: | $nodeCallback(new MatchExpressionNode($expr->cond, array_values($armNodes), $expr, $matchScope), $scope); |
3841: | } elseif ($expr instanceof AlwaysRememberedExpr) { |
3842: | $result = $this->processExprNode($stmt, $expr->getExpr(), $scope, $nodeCallback, $context); |
3843: | $hasYield = $result->hasYield(); |
3844: | $throwPoints = $result->getThrowPoints(); |
3845: | $impurePoints = $result->getImpurePoints(); |
3846: | $scope = $result->getScope(); |
3847: | } elseif ($expr instanceof Expr\Throw_) { |
3848: | $hasYield = false; |
3849: | $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, ExpressionContext::createDeep()); |
3850: | $throwPoints = $result->getThrowPoints(); |
3851: | $impurePoints = $result->getImpurePoints(); |
3852: | $throwPoints[] = ThrowPoint::createExplicit($scope, $scope->getType($expr->expr), $expr, false); |
3853: | } elseif ($expr instanceof FunctionCallableNode) { |
3854: | $throwPoints = []; |
3855: | $impurePoints = []; |
3856: | $hasYield = false; |
3857: | if ($expr->getName() instanceof Expr) { |
3858: | $result = $this->processExprNode($stmt, $expr->getName(), $scope, $nodeCallback, ExpressionContext::createDeep()); |
3859: | $scope = $result->getScope(); |
3860: | $hasYield = $result->hasYield(); |
3861: | $throwPoints = $result->getThrowPoints(); |
3862: | $impurePoints = $result->getImpurePoints(); |
3863: | } |
3864: | } elseif ($expr instanceof MethodCallableNode) { |
3865: | $result = $this->processExprNode($stmt, $expr->getVar(), $scope, $nodeCallback, ExpressionContext::createDeep()); |
3866: | $scope = $result->getScope(); |
3867: | $hasYield = $result->hasYield(); |
3868: | $throwPoints = $result->getThrowPoints(); |
3869: | $impurePoints = $result->getImpurePoints(); |
3870: | if ($expr->getName() instanceof Expr) { |
3871: | $nameResult = $this->processExprNode($stmt, $expr->getVar(), $scope, $nodeCallback, ExpressionContext::createDeep()); |
3872: | $scope = $nameResult->getScope(); |
3873: | $hasYield = $hasYield || $nameResult->hasYield(); |
3874: | $throwPoints = array_merge($throwPoints, $nameResult->getThrowPoints()); |
3875: | $impurePoints = array_merge($impurePoints, $nameResult->getImpurePoints()); |
3876: | } |
3877: | } elseif ($expr instanceof StaticMethodCallableNode) { |
3878: | $throwPoints = []; |
3879: | $impurePoints = []; |
3880: | $hasYield = false; |
3881: | if ($expr->getClass() instanceof Expr) { |
3882: | $classResult = $this->processExprNode($stmt, $expr->getClass(), $scope, $nodeCallback, ExpressionContext::createDeep()); |
3883: | $scope = $classResult->getScope(); |
3884: | $hasYield = $classResult->hasYield(); |
3885: | $throwPoints = $classResult->getThrowPoints(); |
3886: | $impurePoints = $classResult->getImpurePoints(); |
3887: | } |
3888: | if ($expr->getName() instanceof Expr) { |
3889: | $nameResult = $this->processExprNode($stmt, $expr->getName(), $scope, $nodeCallback, ExpressionContext::createDeep()); |
3890: | $scope = $nameResult->getScope(); |
3891: | $hasYield = $hasYield || $nameResult->hasYield(); |
3892: | $throwPoints = array_merge($throwPoints, $nameResult->getThrowPoints()); |
3893: | $impurePoints = array_merge($impurePoints, $nameResult->getImpurePoints()); |
3894: | } |
3895: | } elseif ($expr instanceof InstantiationCallableNode) { |
3896: | $throwPoints = []; |
3897: | $impurePoints = []; |
3898: | $hasYield = false; |
3899: | if ($expr->getClass() instanceof Expr) { |
3900: | $classResult = $this->processExprNode($stmt, $expr->getClass(), $scope, $nodeCallback, ExpressionContext::createDeep()); |
3901: | $scope = $classResult->getScope(); |
3902: | $hasYield = $classResult->hasYield(); |
3903: | $throwPoints = $classResult->getThrowPoints(); |
3904: | $impurePoints = $classResult->getImpurePoints(); |
3905: | } |
3906: | } elseif ($expr instanceof Node\Scalar) { |
3907: | $hasYield = false; |
3908: | $throwPoints = []; |
3909: | $impurePoints = []; |
3910: | } elseif ($expr instanceof ConstFetch) { |
3911: | $hasYield = false; |
3912: | $throwPoints = []; |
3913: | $impurePoints = []; |
3914: | $nodeCallback($expr->name, $scope); |
3915: | } else { |
3916: | $hasYield = false; |
3917: | $throwPoints = []; |
3918: | $impurePoints = []; |
3919: | } |
3920: | |
3921: | return new ExpressionResult( |
3922: | $scope, |
3923: | $hasYield, |
3924: | $throwPoints, |
3925: | $impurePoints, |
3926: | static fn (): MutatingScope => $scope->filterByTruthyValue($expr), |
3927: | static fn (): MutatingScope => $scope->filterByFalseyValue($expr), |
3928: | ); |
3929: | } |
3930: | |
3931: | private function getArrayFunctionAppendingType(FunctionReflection $functionReflection, Scope $scope, FuncCall $expr): Type |
3932: | { |
3933: | $arrayArg = $expr->getArgs()[0]->value; |
3934: | $arrayType = $scope->getType($arrayArg); |
3935: | $callArgs = array_slice($expr->getArgs(), 1); |
3936: | |
3937: | |
3938: | |
3939: | |
3940: | |
3941: | $setOffsetValueTypes = static function (Scope $scope, array $callArgs, callable $setOffsetValueType, ?bool &$nonConstantArrayWasUnpacked = null): void { |
3942: | foreach ($callArgs as $callArg) { |
3943: | $callArgType = $scope->getType($callArg->value); |
3944: | if ($callArg->unpack) { |
3945: | $constantArrays = $callArgType->getConstantArrays(); |
3946: | if (count($constantArrays) === 1) { |
3947: | $iterableValueTypes = $constantArrays[0]->getValueTypes(); |
3948: | } else { |
3949: | $iterableValueTypes = [$callArgType->getIterableValueType()]; |
3950: | $nonConstantArrayWasUnpacked = true; |
3951: | } |
3952: | |
3953: | $isOptional = !$callArgType->isIterableAtLeastOnce()->yes(); |
3954: | foreach ($iterableValueTypes as $iterableValueType) { |
3955: | if ($iterableValueType instanceof UnionType) { |
3956: | foreach ($iterableValueType->getTypes() as $innerType) { |
3957: | $setOffsetValueType(null, $innerType, $isOptional); |
3958: | } |
3959: | } else { |
3960: | $setOffsetValueType(null, $iterableValueType, $isOptional); |
3961: | } |
3962: | } |
3963: | continue; |
3964: | } |
3965: | $setOffsetValueType(null, $callArgType, false); |
3966: | } |
3967: | }; |
3968: | |
3969: | $constantArrays = $arrayType->getConstantArrays(); |
3970: | if (count($constantArrays) > 0) { |
3971: | $newArrayTypes = []; |
3972: | $prepend = $functionReflection->getName() === 'array_unshift'; |
3973: | foreach ($constantArrays as $constantArray) { |
3974: | $arrayTypeBuilder = $prepend ? ConstantArrayTypeBuilder::createEmpty() : ConstantArrayTypeBuilder::createFromConstantArray($constantArray); |
3975: | |
3976: | $setOffsetValueTypes( |
3977: | $scope, |
3978: | $callArgs, |
3979: | static function (?Type $offsetType, Type $valueType, bool $optional) use (&$arrayTypeBuilder): void { |
3980: | $arrayTypeBuilder->setOffsetValueType($offsetType, $valueType, $optional); |
3981: | }, |
3982: | $nonConstantArrayWasUnpacked, |
3983: | ); |
3984: | |
3985: | if ($prepend) { |
3986: | $keyTypes = $constantArray->getKeyTypes(); |
3987: | $valueTypes = $constantArray->getValueTypes(); |
3988: | foreach ($keyTypes as $k => $keyType) { |
3989: | $arrayTypeBuilder->setOffsetValueType( |
3990: | count($keyType->getConstantStrings()) === 1 ? $keyType->getConstantStrings()[0] : null, |
3991: | $valueTypes[$k], |
3992: | $constantArray->isOptionalKey($k), |
3993: | ); |
3994: | } |
3995: | } |
3996: | |
3997: | $constantArray = $arrayTypeBuilder->getArray(); |
3998: | |
3999: | if ($constantArray->isConstantArray()->yes() && $nonConstantArrayWasUnpacked) { |
4000: | $array = new ArrayType($constantArray->generalize(GeneralizePrecision::lessSpecific())->getIterableKeyType(), $constantArray->getIterableValueType()); |
4001: | $isList = $constantArray->isList()->yes(); |
4002: | $constantArray = $constantArray->isIterableAtLeastOnce()->yes() |
4003: | ? TypeCombinator::intersect($array, new NonEmptyArrayType()) |
4004: | : $array; |
4005: | $constantArray = $isList |
4006: | ? TypeCombinator::intersect($constantArray, new AccessoryArrayListType()) |
4007: | : $constantArray; |
4008: | } |
4009: | |
4010: | $newArrayTypes[] = $constantArray; |
4011: | } |
4012: | |
4013: | return TypeCombinator::union(...$newArrayTypes); |
4014: | } |
4015: | |
4016: | $setOffsetValueTypes( |
4017: | $scope, |
4018: | $callArgs, |
4019: | static function (?Type $offsetType, Type $valueType, bool $optional) use (&$arrayType): void { |
4020: | $isIterableAtLeastOnce = $arrayType->isIterableAtLeastOnce()->yes() || !$optional; |
4021: | $arrayType = $arrayType->setOffsetValueType($offsetType, $valueType); |
4022: | if ($isIterableAtLeastOnce) { |
4023: | return; |
4024: | } |
4025: | |
4026: | $arrayType = TypeCombinator::union($arrayType, new ConstantArrayType([], [])); |
4027: | }, |
4028: | ); |
4029: | |
4030: | return $arrayType; |
4031: | } |
4032: | |
4033: | private function getArraySortPreserveListFunctionType(Type $type): Type |
4034: | { |
4035: | $isIterableAtLeastOnce = $type->isIterableAtLeastOnce(); |
4036: | if ($isIterableAtLeastOnce->no()) { |
4037: | return $type; |
4038: | } |
4039: | |
4040: | return TypeTraverser::map($type, static function (Type $type, callable $traverse) use ($isIterableAtLeastOnce): Type { |
4041: | if ($type instanceof UnionType || $type instanceof IntersectionType) { |
4042: | return $traverse($type); |
4043: | } |
4044: | |
4045: | if (!$type instanceof ArrayType && !$type instanceof ConstantArrayType) { |
4046: | return $type; |
4047: | } |
4048: | |
4049: | $newArrayType = TypeCombinator::intersect(new ArrayType(new IntegerType(), $type->getIterableValueType()), new AccessoryArrayListType()); |
4050: | if ($isIterableAtLeastOnce->yes()) { |
4051: | $newArrayType = TypeCombinator::intersect($newArrayType, new NonEmptyArrayType()); |
4052: | } |
4053: | |
4054: | return $newArrayType; |
4055: | }); |
4056: | } |
4057: | |
4058: | private function getArraySortDoNotPreserveListFunctionType(Type $type): Type |
4059: | { |
4060: | $isIterableAtLeastOnce = $type->isIterableAtLeastOnce(); |
4061: | if ($isIterableAtLeastOnce->no()) { |
4062: | return $type; |
4063: | } |
4064: | |
4065: | return TypeTraverser::map($type, static function (Type $type, callable $traverse) use ($isIterableAtLeastOnce): Type { |
4066: | if ($type instanceof UnionType) { |
4067: | return $traverse($type); |
4068: | } |
4069: | |
4070: | $constantArrays = $type->getConstantArrays(); |
4071: | if (count($constantArrays) > 0) { |
4072: | $types = []; |
4073: | foreach ($constantArrays as $constantArray) { |
4074: | $types[] = new ConstantArrayType( |
4075: | $constantArray->getKeyTypes(), |
4076: | $constantArray->getValueTypes(), |
4077: | $constantArray->getNextAutoIndexes(), |
4078: | $constantArray->getOptionalKeys(), |
4079: | $constantArray->isList()->and(TrinaryLogic::createMaybe()), |
4080: | ); |
4081: | } |
4082: | |
4083: | return TypeCombinator::union(...$types); |
4084: | } |
4085: | |
4086: | $newArrayType = new ArrayType($type->getIterableKeyType(), $type->getIterableValueType()); |
4087: | if ($isIterableAtLeastOnce->yes()) { |
4088: | $newArrayType = TypeCombinator::intersect($newArrayType, new NonEmptyArrayType()); |
4089: | } |
4090: | |
4091: | return $newArrayType; |
4092: | }); |
4093: | } |
4094: | |
4095: | private function getFunctionThrowPoint( |
4096: | FunctionReflection $functionReflection, |
4097: | ?ParametersAcceptor $parametersAcceptor, |
4098: | FuncCall $funcCall, |
4099: | MutatingScope $scope, |
4100: | ): ?ThrowPoint |
4101: | { |
4102: | $normalizedFuncCall = $funcCall; |
4103: | if ($parametersAcceptor !== null) { |
4104: | $normalizedFuncCall = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $funcCall); |
4105: | } |
4106: | |
4107: | if ($normalizedFuncCall !== null) { |
4108: | foreach ($this->dynamicThrowTypeExtensionProvider->getDynamicFunctionThrowTypeExtensions() as $extension) { |
4109: | if (!$extension->isFunctionSupported($functionReflection)) { |
4110: | continue; |
4111: | } |
4112: | |
4113: | $throwType = $extension->getThrowTypeFromFunctionCall($functionReflection, $normalizedFuncCall, $scope); |
4114: | if ($throwType === null) { |
4115: | return null; |
4116: | } |
4117: | |
4118: | return ThrowPoint::createExplicit($scope, $throwType, $funcCall, false); |
4119: | } |
4120: | } |
4121: | |
4122: | $throwType = $functionReflection->getThrowType(); |
4123: | if ($throwType === null && $parametersAcceptor !== null) { |
4124: | $returnType = $parametersAcceptor->getReturnType(); |
4125: | if ($returnType instanceof NeverType && $returnType->isExplicit()) { |
4126: | $throwType = new ObjectType(Throwable::class); |
4127: | } |
4128: | } |
4129: | |
4130: | if ($throwType !== null) { |
4131: | if (!$throwType->isVoid()->yes()) { |
4132: | return ThrowPoint::createExplicit($scope, $throwType, $funcCall, true); |
4133: | } |
4134: | } elseif ($this->implicitThrows) { |
4135: | $requiredParameters = null; |
4136: | if ($parametersAcceptor !== null) { |
4137: | $requiredParameters = 0; |
4138: | foreach ($parametersAcceptor->getParameters() as $parameter) { |
4139: | if ($parameter->isOptional()) { |
4140: | continue; |
4141: | } |
4142: | |
4143: | $requiredParameters++; |
4144: | } |
4145: | } |
4146: | if ( |
4147: | !$functionReflection->isBuiltin() |
4148: | || $requiredParameters === null |
4149: | || $requiredParameters > 0 |
4150: | || count($funcCall->getArgs()) > 0 |
4151: | ) { |
4152: | $functionReturnedType = $scope->getType($funcCall); |
4153: | if (!(new ObjectType(Throwable::class))->isSuperTypeOf($functionReturnedType)->yes()) { |
4154: | return ThrowPoint::createImplicit($scope, $funcCall); |
4155: | } |
4156: | } |
4157: | } |
4158: | |
4159: | return null; |
4160: | } |
4161: | |
4162: | private function getMethodThrowPoint(MethodReflection $methodReflection, ParametersAcceptor $parametersAcceptor, MethodCall $methodCall, MutatingScope $scope): ?ThrowPoint |
4163: | { |
4164: | $normalizedMethodCall = ArgumentsNormalizer::reorderMethodArguments($parametersAcceptor, $methodCall); |
4165: | if ($normalizedMethodCall !== null) { |
4166: | foreach ($this->dynamicThrowTypeExtensionProvider->getDynamicMethodThrowTypeExtensions() as $extension) { |
4167: | if (!$extension->isMethodSupported($methodReflection)) { |
4168: | continue; |
4169: | } |
4170: | |
4171: | $throwType = $extension->getThrowTypeFromMethodCall($methodReflection, $normalizedMethodCall, $scope); |
4172: | if ($throwType === null) { |
4173: | return null; |
4174: | } |
4175: | |
4176: | return ThrowPoint::createExplicit($scope, $throwType, $methodCall, false); |
4177: | } |
4178: | } |
4179: | |
4180: | $throwType = $methodReflection->getThrowType(); |
4181: | if ($throwType === null) { |
4182: | $returnType = $parametersAcceptor->getReturnType(); |
4183: | if ($returnType instanceof NeverType && $returnType->isExplicit()) { |
4184: | $throwType = new ObjectType(Throwable::class); |
4185: | } |
4186: | } |
4187: | |
4188: | if ($throwType !== null) { |
4189: | if (!$throwType->isVoid()->yes()) { |
4190: | return ThrowPoint::createExplicit($scope, $throwType, $methodCall, true); |
4191: | } |
4192: | } elseif ($this->implicitThrows) { |
4193: | $methodReturnedType = $scope->getType($methodCall); |
4194: | if (!(new ObjectType(Throwable::class))->isSuperTypeOf($methodReturnedType)->yes()) { |
4195: | return ThrowPoint::createImplicit($scope, $methodCall); |
4196: | } |
4197: | } |
4198: | |
4199: | return null; |
4200: | } |
4201: | |
4202: | |
4203: | |
4204: | |
4205: | private function getConstructorThrowPoint(MethodReflection $constructorReflection, ParametersAcceptor $parametersAcceptor, ClassReflection $classReflection, New_ $new, Name $className, array $args, MutatingScope $scope): ?ThrowPoint |
4206: | { |
4207: | $methodCall = new StaticCall($className, $constructorReflection->getName(), $args); |
4208: | $normalizedMethodCall = ArgumentsNormalizer::reorderStaticCallArguments($parametersAcceptor, $methodCall); |
4209: | if ($normalizedMethodCall !== null) { |
4210: | foreach ($this->dynamicThrowTypeExtensionProvider->getDynamicStaticMethodThrowTypeExtensions() as $extension) { |
4211: | if (!$extension->isStaticMethodSupported($constructorReflection)) { |
4212: | continue; |
4213: | } |
4214: | |
4215: | $throwType = $extension->getThrowTypeFromStaticMethodCall($constructorReflection, $normalizedMethodCall, $scope); |
4216: | if ($throwType === null) { |
4217: | return null; |
4218: | } |
4219: | |
4220: | return ThrowPoint::createExplicit($scope, $throwType, $new, false); |
4221: | } |
4222: | } |
4223: | |
4224: | if ($constructorReflection->getThrowType() !== null) { |
4225: | $throwType = $constructorReflection->getThrowType(); |
4226: | if (!$throwType->isVoid()->yes()) { |
4227: | return ThrowPoint::createExplicit($scope, $throwType, $new, true); |
4228: | } |
4229: | } elseif ($this->implicitThrows) { |
4230: | if ($classReflection->getName() !== Throwable::class && !$classReflection->isSubclassOf(Throwable::class)) { |
4231: | return ThrowPoint::createImplicit($scope, $methodCall); |
4232: | } |
4233: | } |
4234: | |
4235: | return null; |
4236: | } |
4237: | |
4238: | private function getStaticMethodThrowPoint(MethodReflection $methodReflection, ParametersAcceptor $parametersAcceptor, StaticCall $methodCall, MutatingScope $scope): ?ThrowPoint |
4239: | { |
4240: | $normalizedMethodCall = ArgumentsNormalizer::reorderStaticCallArguments($parametersAcceptor, $methodCall); |
4241: | if ($normalizedMethodCall !== null) { |
4242: | foreach ($this->dynamicThrowTypeExtensionProvider->getDynamicStaticMethodThrowTypeExtensions() as $extension) { |
4243: | if (!$extension->isStaticMethodSupported($methodReflection)) { |
4244: | continue; |
4245: | } |
4246: | |
4247: | $throwType = $extension->getThrowTypeFromStaticMethodCall($methodReflection, $normalizedMethodCall, $scope); |
4248: | if ($throwType === null) { |
4249: | return null; |
4250: | } |
4251: | |
4252: | return ThrowPoint::createExplicit($scope, $throwType, $methodCall, false); |
4253: | } |
4254: | } |
4255: | |
4256: | if ($methodReflection->getThrowType() !== null) { |
4257: | $throwType = $methodReflection->getThrowType(); |
4258: | if (!$throwType->isVoid()->yes()) { |
4259: | return ThrowPoint::createExplicit($scope, $throwType, $methodCall, true); |
4260: | } |
4261: | } elseif ($this->implicitThrows) { |
4262: | $methodReturnedType = $scope->getType($methodCall); |
4263: | if (!(new ObjectType(Throwable::class))->isSuperTypeOf($methodReturnedType)->yes()) { |
4264: | return ThrowPoint::createImplicit($scope, $methodCall); |
4265: | } |
4266: | } |
4267: | |
4268: | return null; |
4269: | } |
4270: | |
4271: | |
4272: | |
4273: | |
4274: | private function getPropertyReadThrowPointsFromGetHook( |
4275: | MutatingScope $scope, |
4276: | PropertyFetch $propertyFetch, |
4277: | PhpPropertyReflection $propertyReflection, |
4278: | ): array |
4279: | { |
4280: | return $this->getThrowPointsFromPropertyHook($scope, $propertyFetch, $propertyReflection, 'get'); |
4281: | } |
4282: | |
4283: | |
4284: | |
4285: | |
4286: | private function getPropertyAssignThrowPointsFromSetHook( |
4287: | MutatingScope $scope, |
4288: | PropertyFetch $propertyFetch, |
4289: | PhpPropertyReflection $propertyReflection, |
4290: | ): array |
4291: | { |
4292: | return $this->getThrowPointsFromPropertyHook($scope, $propertyFetch, $propertyReflection, 'set'); |
4293: | } |
4294: | |
4295: | |
4296: | |
4297: | |
4298: | |
4299: | private function getThrowPointsFromPropertyHook( |
4300: | MutatingScope $scope, |
4301: | PropertyFetch $propertyFetch, |
4302: | PhpPropertyReflection $propertyReflection, |
4303: | string $hookName, |
4304: | ): array |
4305: | { |
4306: | $scopeFunction = $scope->getFunction(); |
4307: | if ( |
4308: | $scopeFunction instanceof PhpMethodFromParserNodeReflection |
4309: | && $scopeFunction->isPropertyHook() |
4310: | && $propertyFetch->var instanceof Variable |
4311: | && $propertyFetch->var->name === 'this' |
4312: | && $propertyFetch->name instanceof Identifier |
4313: | && $propertyFetch->name->toString() === $scopeFunction->getHookedPropertyName() |
4314: | ) { |
4315: | return []; |
4316: | } |
4317: | $declaringClass = $propertyReflection->getDeclaringClass(); |
4318: | if (!$propertyReflection->hasHook($hookName)) { |
4319: | if ( |
4320: | $propertyReflection->isPrivate() |
4321: | || $propertyReflection->isFinal()->yes() |
4322: | || $declaringClass->isFinal() |
4323: | ) { |
4324: | return []; |
4325: | } |
4326: | |
4327: | if ($this->implicitThrows) { |
4328: | return [ThrowPoint::createImplicit($scope, $propertyFetch)]; |
4329: | } |
4330: | |
4331: | return []; |
4332: | } |
4333: | |
4334: | $getHook = $propertyReflection->getHook($hookName); |
4335: | $throwType = $getHook->getThrowType(); |
4336: | |
4337: | if ($throwType !== null) { |
4338: | if (!$throwType->isVoid()->yes()) { |
4339: | return [ThrowPoint::createExplicit($scope, $throwType, $propertyFetch, true)]; |
4340: | } |
4341: | } elseif ($this->implicitThrows) { |
4342: | return [ThrowPoint::createImplicit($scope, $propertyFetch)]; |
4343: | } |
4344: | |
4345: | return []; |
4346: | } |
4347: | |
4348: | |
4349: | |
4350: | |
4351: | private function getAssignedVariables(Expr $expr): array |
4352: | { |
4353: | if ($expr instanceof Expr\Variable) { |
4354: | if (is_string($expr->name)) { |
4355: | return [$expr->name]; |
4356: | } |
4357: | |
4358: | return []; |
4359: | } |
4360: | |
4361: | if ($expr instanceof Expr\List_) { |
4362: | $names = []; |
4363: | foreach ($expr->items as $item) { |
4364: | if ($item === null) { |
4365: | continue; |
4366: | } |
4367: | |
4368: | $names = array_merge($names, $this->getAssignedVariables($item->value)); |
4369: | } |
4370: | |
4371: | return $names; |
4372: | } |
4373: | |
4374: | if ($expr instanceof ArrayDimFetch) { |
4375: | return $this->getAssignedVariables($expr->var); |
4376: | } |
4377: | |
4378: | return []; |
4379: | } |
4380: | |
4381: | |
4382: | |
4383: | |
4384: | private function callNodeCallbackWithExpression( |
4385: | callable $nodeCallback, |
4386: | Expr $expr, |
4387: | MutatingScope $scope, |
4388: | ExpressionContext $context, |
4389: | ): void |
4390: | { |
4391: | if ($context->isDeep()) { |
4392: | $scope = $scope->exitFirstLevelStatements(); |
4393: | } |
4394: | $nodeCallback($expr, $scope); |
4395: | } |
4396: | |
4397: | |
4398: | |
4399: | |
4400: | private function processClosureNode( |
4401: | Node\Stmt $stmt, |
4402: | Expr\Closure $expr, |
4403: | MutatingScope $scope, |
4404: | callable $nodeCallback, |
4405: | ExpressionContext $context, |
4406: | ?Type $passedToType, |
4407: | ): ProcessClosureResult |
4408: | { |
4409: | foreach ($expr->params as $param) { |
4410: | $this->processParamNode($stmt, $param, $scope, $nodeCallback); |
4411: | } |
4412: | |
4413: | $byRefUses = []; |
4414: | |
4415: | $closureCallArgs = $expr->getAttribute(ClosureArgVisitor::ATTRIBUTE_NAME); |
4416: | $callableParameters = $this->createCallableParameters( |
4417: | $scope, |
4418: | $expr, |
4419: | $closureCallArgs, |
4420: | $passedToType, |
4421: | ); |
4422: | |
4423: | $useScope = $scope; |
4424: | foreach ($expr->uses as $use) { |
4425: | if ($use->byRef) { |
4426: | $byRefUses[] = $use; |
4427: | $useScope = $useScope->enterExpressionAssign($use->var); |
4428: | |
4429: | $inAssignRightSideVariableName = $context->getInAssignRightSideVariableName(); |
4430: | $inAssignRightSideType = $context->getInAssignRightSideType(); |
4431: | $inAssignRightSideNativeType = $context->getInAssignRightSideNativeType(); |
4432: | if ( |
4433: | $inAssignRightSideVariableName === $use->var->name |
4434: | && $inAssignRightSideType !== null |
4435: | && $inAssignRightSideNativeType !== null |
4436: | ) { |
4437: | if ($inAssignRightSideType instanceof ClosureType) { |
4438: | $variableType = $inAssignRightSideType; |
4439: | } else { |
4440: | $alreadyHasVariableType = $scope->hasVariableType($inAssignRightSideVariableName); |
4441: | if ($alreadyHasVariableType->no()) { |
4442: | $variableType = TypeCombinator::union(new NullType(), $inAssignRightSideType); |
4443: | } else { |
4444: | $variableType = TypeCombinator::union($scope->getVariableType($inAssignRightSideVariableName), $inAssignRightSideType); |
4445: | } |
4446: | } |
4447: | if ($inAssignRightSideNativeType instanceof ClosureType) { |
4448: | $variableNativeType = $inAssignRightSideNativeType; |
4449: | } else { |
4450: | $alreadyHasVariableType = $scope->hasVariableType($inAssignRightSideVariableName); |
4451: | if ($alreadyHasVariableType->no()) { |
4452: | $variableNativeType = TypeCombinator::union(new NullType(), $inAssignRightSideNativeType); |
4453: | } else { |
4454: | $variableNativeType = TypeCombinator::union($scope->getVariableType($inAssignRightSideVariableName), $inAssignRightSideNativeType); |
4455: | } |
4456: | } |
4457: | $scope = $scope->assignVariable($inAssignRightSideVariableName, $variableType, $variableNativeType, TrinaryLogic::createYes()); |
4458: | } |
4459: | } |
4460: | $this->processExprNode($stmt, $use->var, $useScope, $nodeCallback, $context); |
4461: | if (!$use->byRef) { |
4462: | continue; |
4463: | } |
4464: | |
4465: | $useScope = $useScope->exitExpressionAssign($use->var); |
4466: | } |
4467: | |
4468: | if ($expr->returnType !== null) { |
4469: | $nodeCallback($expr->returnType, $scope); |
4470: | } |
4471: | |
4472: | $closureScope = $scope->enterAnonymousFunction($expr, $callableParameters); |
4473: | $closureScope = $closureScope->processClosureScope($scope, null, $byRefUses); |
4474: | $closureType = $closureScope->getAnonymousFunctionReflection(); |
4475: | if (!$closureType instanceof ClosureType) { |
4476: | throw new ShouldNotHappenException(); |
4477: | } |
4478: | |
4479: | $nodeCallback(new InClosureNode($closureType, $expr), $closureScope); |
4480: | |
4481: | $executionEnds = []; |
4482: | $gatheredReturnStatements = []; |
4483: | $gatheredYieldStatements = []; |
4484: | $closureImpurePoints = []; |
4485: | $invalidateExpressions = []; |
4486: | $closureStmtsCallback = static function (Node $node, Scope $scope) use ($nodeCallback, &$executionEnds, &$gatheredReturnStatements, &$gatheredYieldStatements, &$closureScope, &$closureImpurePoints, &$invalidateExpressions): void { |
4487: | $nodeCallback($node, $scope); |
4488: | if ($scope->getAnonymousFunctionReflection() !== $closureScope->getAnonymousFunctionReflection()) { |
4489: | return; |
4490: | } |
4491: | if ($node instanceof PropertyAssignNode) { |
4492: | $closureImpurePoints[] = new ImpurePoint( |
4493: | $scope, |
4494: | $node, |
4495: | 'propertyAssign', |
4496: | 'property assignment', |
4497: | true, |
4498: | ); |
4499: | return; |
4500: | } |
4501: | if ($node instanceof ExecutionEndNode) { |
4502: | $executionEnds[] = $node; |
4503: | return; |
4504: | } |
4505: | if ($node instanceof InvalidateExprNode) { |
4506: | $invalidateExpressions[] = $node; |
4507: | return; |
4508: | } |
4509: | if ($node instanceof Expr\Yield_ || $node instanceof Expr\YieldFrom) { |
4510: | $gatheredYieldStatements[] = $node; |
4511: | } |
4512: | if (!$node instanceof Return_) { |
4513: | return; |
4514: | } |
4515: | |
4516: | $gatheredReturnStatements[] = new ReturnStatement($scope, $node); |
4517: | }; |
4518: | |
4519: | if (count($byRefUses) === 0) { |
4520: | $statementResult = $this->processStmtNodes($expr, $expr->stmts, $closureScope, $closureStmtsCallback, StatementContext::createTopLevel()); |
4521: | $nodeCallback(new ClosureReturnStatementsNode( |
4522: | $expr, |
4523: | $gatheredReturnStatements, |
4524: | $gatheredYieldStatements, |
4525: | $statementResult, |
4526: | $executionEnds, |
4527: | array_merge($statementResult->getImpurePoints(), $closureImpurePoints), |
4528: | ), $closureScope); |
4529: | |
4530: | return new ProcessClosureResult($scope, $statementResult->getThrowPoints(), $statementResult->getImpurePoints(), $invalidateExpressions); |
4531: | } |
4532: | |
4533: | $count = 0; |
4534: | $closureResultScope = null; |
4535: | do { |
4536: | $prevScope = $closureScope; |
4537: | |
4538: | $intermediaryClosureScopeResult = $this->processStmtNodes($expr, $expr->stmts, $closureScope, static function (): void { |
4539: | }, StatementContext::createTopLevel()); |
4540: | $intermediaryClosureScope = $intermediaryClosureScopeResult->getScope(); |
4541: | foreach ($intermediaryClosureScopeResult->getExitPoints() as $exitPoint) { |
4542: | $intermediaryClosureScope = $intermediaryClosureScope->mergeWith($exitPoint->getScope()); |
4543: | } |
4544: | |
4545: | if ($expr->getAttribute(ImmediatelyInvokedClosureVisitor::ATTRIBUTE_NAME) === true) { |
4546: | $closureResultScope = $intermediaryClosureScope; |
4547: | break; |
4548: | } |
4549: | |
4550: | $closureScope = $scope->enterAnonymousFunction($expr, $callableParameters); |
4551: | $closureScope = $closureScope->processClosureScope($intermediaryClosureScope, $prevScope, $byRefUses); |
4552: | |
4553: | if ($closureScope->equals($prevScope)) { |
4554: | break; |
4555: | } |
4556: | if ($count >= self::GENERALIZE_AFTER_ITERATION) { |
4557: | $closureScope = $prevScope->generalizeWith($closureScope); |
4558: | } |
4559: | $count++; |
4560: | } while ($count < self::LOOP_SCOPE_ITERATIONS); |
4561: | |
4562: | if ($closureResultScope === null) { |
4563: | $closureResultScope = $closureScope; |
4564: | } |
4565: | |
4566: | $statementResult = $this->processStmtNodes($expr, $expr->stmts, $closureScope, $closureStmtsCallback, StatementContext::createTopLevel()); |
4567: | $nodeCallback(new ClosureReturnStatementsNode( |
4568: | $expr, |
4569: | $gatheredReturnStatements, |
4570: | $gatheredYieldStatements, |
4571: | $statementResult, |
4572: | $executionEnds, |
4573: | array_merge($statementResult->getImpurePoints(), $closureImpurePoints), |
4574: | ), $closureScope); |
4575: | |
4576: | return new ProcessClosureResult($scope->processClosureScope($closureResultScope, null, $byRefUses), $statementResult->getThrowPoints(), $statementResult->getImpurePoints(), $invalidateExpressions); |
4577: | } |
4578: | |
4579: | |
4580: | |
4581: | |
4582: | |
4583: | private function processImmediatelyCalledCallable(MutatingScope $scope, array $invalidatedExpressions, array $uses): MutatingScope |
4584: | { |
4585: | if ($scope->isInClass()) { |
4586: | $uses[] = 'this'; |
4587: | } |
4588: | |
4589: | $finder = new NodeFinder(); |
4590: | foreach ($invalidatedExpressions as $invalidateExpression) { |
4591: | $found = false; |
4592: | foreach ($uses as $use) { |
4593: | $result = $finder->findFirst([$invalidateExpression->getExpr()], static fn ($node) => $node instanceof Variable && $node->name === $use); |
4594: | if ($result === null) { |
4595: | continue; |
4596: | } |
4597: | |
4598: | $found = true; |
4599: | break; |
4600: | } |
4601: | |
4602: | if (!$found) { |
4603: | continue; |
4604: | } |
4605: | |
4606: | $scope = $scope->invalidateExpression($invalidateExpression->getExpr(), true); |
4607: | } |
4608: | |
4609: | return $scope; |
4610: | } |
4611: | |
4612: | |
4613: | |
4614: | |
4615: | private function processArrowFunctionNode( |
4616: | Node\Stmt $stmt, |
4617: | Expr\ArrowFunction $expr, |
4618: | MutatingScope $scope, |
4619: | callable $nodeCallback, |
4620: | ?Type $passedToType, |
4621: | ): ExpressionResult |
4622: | { |
4623: | foreach ($expr->params as $param) { |
4624: | $this->processParamNode($stmt, $param, $scope, $nodeCallback); |
4625: | } |
4626: | if ($expr->returnType !== null) { |
4627: | $nodeCallback($expr->returnType, $scope); |
4628: | } |
4629: | |
4630: | $arrowFunctionCallArgs = $expr->getAttribute(ArrowFunctionArgVisitor::ATTRIBUTE_NAME); |
4631: | $arrowFunctionScope = $scope->enterArrowFunction($expr, $this->createCallableParameters( |
4632: | $scope, |
4633: | $expr, |
4634: | $arrowFunctionCallArgs, |
4635: | $passedToType, |
4636: | )); |
4637: | $arrowFunctionType = $arrowFunctionScope->getAnonymousFunctionReflection(); |
4638: | if (!$arrowFunctionType instanceof ClosureType) { |
4639: | throw new ShouldNotHappenException(); |
4640: | } |
4641: | $nodeCallback(new InArrowFunctionNode($arrowFunctionType, $expr), $arrowFunctionScope); |
4642: | $exprResult = $this->processExprNode($stmt, $expr->expr, $arrowFunctionScope, $nodeCallback, ExpressionContext::createTopLevel()); |
4643: | |
4644: | return new ExpressionResult($scope, false, $exprResult->getThrowPoints(), $exprResult->getImpurePoints()); |
4645: | } |
4646: | |
4647: | |
4648: | |
4649: | |
4650: | |
4651: | public function createCallableParameters(Scope $scope, Expr $closureExpr, ?array $args, ?Type $passedToType): ?array |
4652: | { |
4653: | $callableParameters = null; |
4654: | if ($args !== null) { |
4655: | $closureType = $scope->getType($closureExpr); |
4656: | |
4657: | if ($closureType->isCallable()->no()) { |
4658: | return null; |
4659: | } |
4660: | |
4661: | $acceptors = $closureType->getCallableParametersAcceptors($scope); |
4662: | if (count($acceptors) === 1) { |
4663: | $callableParameters = $acceptors[0]->getParameters(); |
4664: | |
4665: | foreach ($callableParameters as $index => $callableParameter) { |
4666: | if (!isset($args[$index])) { |
4667: | continue; |
4668: | } |
4669: | |
4670: | $type = $scope->getType($args[$index]->value); |
4671: | $callableParameters[$index] = new NativeParameterReflection( |
4672: | $callableParameter->getName(), |
4673: | $callableParameter->isOptional(), |
4674: | $type, |
4675: | $callableParameter->passedByReference(), |
4676: | $callableParameter->isVariadic(), |
4677: | $callableParameter->getDefaultValue(), |
4678: | ); |
4679: | } |
4680: | } |
4681: | } elseif ($passedToType !== null && !$passedToType->isCallable()->no()) { |
4682: | if ($passedToType instanceof UnionType) { |
4683: | $passedToType = TypeCombinator::union(...array_filter( |
4684: | $passedToType->getTypes(), |
4685: | static fn (Type $type) => $type->isCallable()->yes(), |
4686: | )); |
4687: | |
4688: | if ($passedToType->isCallable()->no()) { |
4689: | return null; |
4690: | } |
4691: | } |
4692: | |
4693: | $acceptors = $passedToType->getCallableParametersAcceptors($scope); |
4694: | if (count($acceptors) > 0) { |
4695: | foreach ($acceptors as $acceptor) { |
4696: | if ($callableParameters === null) { |
4697: | $callableParameters = array_map(static fn (ParameterReflection $callableParameter) => new NativeParameterReflection( |
4698: | $callableParameter->getName(), |
4699: | $callableParameter->isOptional(), |
4700: | $callableParameter->getType(), |
4701: | $callableParameter->passedByReference(), |
4702: | $callableParameter->isVariadic(), |
4703: | $callableParameter->getDefaultValue(), |
4704: | ), $acceptor->getParameters()); |
4705: | continue; |
4706: | } |
4707: | |
4708: | $newParameters = []; |
4709: | foreach ($acceptor->getParameters() as $i => $callableParameter) { |
4710: | if (!array_key_exists($i, $callableParameters)) { |
4711: | $newParameters[] = $callableParameter; |
4712: | continue; |
4713: | } |
4714: | |
4715: | $newParameters[] = $callableParameters[$i]->union(new NativeParameterReflection( |
4716: | $callableParameter->getName(), |
4717: | $callableParameter->isOptional(), |
4718: | $callableParameter->getType(), |
4719: | $callableParameter->passedByReference(), |
4720: | $callableParameter->isVariadic(), |
4721: | $callableParameter->getDefaultValue(), |
4722: | )); |
4723: | } |
4724: | |
4725: | $callableParameters = $newParameters; |
4726: | } |
4727: | } |
4728: | } |
4729: | |
4730: | return $callableParameters; |
4731: | } |
4732: | |
4733: | |
4734: | |
4735: | |
4736: | private function processParamNode( |
4737: | Node\Stmt $stmt, |
4738: | Node\Param $param, |
4739: | MutatingScope $scope, |
4740: | callable $nodeCallback, |
4741: | ): void |
4742: | { |
4743: | $this->processAttributeGroups($stmt, $param->attrGroups, $scope, $nodeCallback); |
4744: | $nodeCallback($param, $scope); |
4745: | if ($param->type !== null) { |
4746: | $nodeCallback($param->type, $scope); |
4747: | } |
4748: | if ($param->default === null) { |
4749: | return; |
4750: | } |
4751: | |
4752: | $this->processExprNode($stmt, $param->default, $scope, $nodeCallback, ExpressionContext::createDeep()); |
4753: | } |
4754: | |
4755: | |
4756: | |
4757: | |
4758: | |
4759: | private function processAttributeGroups( |
4760: | Node\Stmt $stmt, |
4761: | array $attrGroups, |
4762: | MutatingScope $scope, |
4763: | callable $nodeCallback, |
4764: | ): void |
4765: | { |
4766: | foreach ($attrGroups as $attrGroup) { |
4767: | foreach ($attrGroup->attrs as $attr) { |
4768: | foreach ($attr->args as $arg) { |
4769: | $this->processExprNode($stmt, $arg->value, $scope, $nodeCallback, ExpressionContext::createDeep()); |
4770: | $nodeCallback($arg, $scope); |
4771: | } |
4772: | $nodeCallback($attr, $scope); |
4773: | } |
4774: | $nodeCallback($attrGroup, $scope); |
4775: | } |
4776: | } |
4777: | |
4778: | |
4779: | |
4780: | |
4781: | |
4782: | private function processPropertyHooks( |
4783: | Node\Stmt $stmt, |
4784: | Identifier|Name|ComplexType|null $nativeTypeNode, |
4785: | ?Type $phpDocType, |
4786: | string $propertyName, |
4787: | array $hooks, |
4788: | MutatingScope $scope, |
4789: | callable $nodeCallback, |
4790: | ): void |
4791: | { |
4792: | if (!$scope->isInClass()) { |
4793: | throw new ShouldNotHappenException(); |
4794: | } |
4795: | |
4796: | $classReflection = $scope->getClassReflection(); |
4797: | |
4798: | foreach ($hooks as $hook) { |
4799: | $nodeCallback($hook, $scope); |
4800: | $this->processAttributeGroups($stmt, $hook->attrGroups, $scope, $nodeCallback); |
4801: | |
4802: | [, $phpDocParameterTypes,,,, $phpDocThrowType,,,,,,,, $phpDocComment] = $this->getPhpDocs($scope, $hook); |
4803: | |
4804: | foreach ($hook->params as $param) { |
4805: | $this->processParamNode($stmt, $param, $scope, $nodeCallback); |
4806: | } |
4807: | |
4808: | [$isDeprecated, $deprecatedDescription] = $this->getDeprecatedAttribute($scope, $hook); |
4809: | |
4810: | $hookScope = $scope->enterPropertyHook( |
4811: | $hook, |
4812: | $propertyName, |
4813: | $nativeTypeNode, |
4814: | $phpDocType, |
4815: | $phpDocParameterTypes, |
4816: | $phpDocThrowType, |
4817: | $deprecatedDescription, |
4818: | $isDeprecated, |
4819: | $phpDocComment, |
4820: | ); |
4821: | $hookReflection = $hookScope->getFunction(); |
4822: | if (!$hookReflection instanceof PhpMethodFromParserNodeReflection) { |
4823: | throw new ShouldNotHappenException(); |
4824: | } |
4825: | |
4826: | if (!$classReflection->hasNativeProperty($propertyName)) { |
4827: | throw new ShouldNotHappenException(); |
4828: | } |
4829: | |
4830: | $propertyReflection = $classReflection->getNativeProperty($propertyName); |
4831: | |
4832: | $nodeCallback(new InPropertyHookNode( |
4833: | $classReflection, |
4834: | $hookReflection, |
4835: | $propertyReflection, |
4836: | $hook, |
4837: | ), $hookScope); |
4838: | |
4839: | $stmts = $hook->getStmts(); |
4840: | if ($stmts === null) { |
4841: | return; |
4842: | } |
4843: | |
4844: | if ($hook->body instanceof Expr) { |
4845: | |
4846: | $traverser = new NodeTraverser( |
4847: | new LineAttributesVisitor($hook->body->getStartLine(), $hook->body->getEndLine()), |
4848: | ); |
4849: | $traverser->traverse($stmts); |
4850: | } |
4851: | |
4852: | $gatheredReturnStatements = []; |
4853: | $executionEnds = []; |
4854: | $methodImpurePoints = []; |
4855: | $statementResult = $this->processStmtNodes(new PropertyHookStatementNode($hook), $stmts, $hookScope, static function (Node $node, Scope $scope) use ($nodeCallback, $hookScope, &$gatheredReturnStatements, &$executionEnds, &$hookImpurePoints): void { |
4856: | $nodeCallback($node, $scope); |
4857: | if ($scope->getFunction() !== $hookScope->getFunction()) { |
4858: | return; |
4859: | } |
4860: | if ($scope->isInAnonymousFunction()) { |
4861: | return; |
4862: | } |
4863: | if ($node instanceof PropertyAssignNode) { |
4864: | $hookImpurePoints[] = new ImpurePoint( |
4865: | $scope, |
4866: | $node, |
4867: | 'propertyAssign', |
4868: | 'property assignment', |
4869: | true, |
4870: | ); |
4871: | return; |
4872: | } |
4873: | if ($node instanceof ExecutionEndNode) { |
4874: | $executionEnds[] = $node; |
4875: | return; |
4876: | } |
4877: | if (!$node instanceof Return_) { |
4878: | return; |
4879: | } |
4880: | |
4881: | $gatheredReturnStatements[] = new ReturnStatement($scope, $node); |
4882: | }, StatementContext::createTopLevel()); |
4883: | |
4884: | $nodeCallback(new PropertyHookReturnStatementsNode( |
4885: | $hook, |
4886: | $gatheredReturnStatements, |
4887: | $statementResult, |
4888: | $executionEnds, |
4889: | array_merge($statementResult->getImpurePoints(), $methodImpurePoints), |
4890: | $classReflection, |
4891: | $hookReflection, |
4892: | $propertyReflection, |
4893: | ), $hookScope); |
4894: | } |
4895: | } |
4896: | |
4897: | |
4898: | |
4899: | |
4900: | |
4901: | private function processArgs( |
4902: | Node\Stmt $stmt, |
4903: | $calleeReflection, |
4904: | ?ExtendedMethodReflection $nakedMethodReflection, |
4905: | ?ParametersAcceptor $parametersAcceptor, |
4906: | CallLike $callLike, |
4907: | MutatingScope $scope, |
4908: | callable $nodeCallback, |
4909: | ExpressionContext $context, |
4910: | ?MutatingScope $closureBindScope = null, |
4911: | ): ExpressionResult |
4912: | { |
4913: | $args = $callLike->getArgs(); |
4914: | |
4915: | if ($parametersAcceptor !== null) { |
4916: | $parameters = $parametersAcceptor->getParameters(); |
4917: | } |
4918: | |
4919: | $hasYield = false; |
4920: | $throwPoints = []; |
4921: | $impurePoints = []; |
4922: | foreach ($args as $i => $arg) { |
4923: | $assignByReference = false; |
4924: | $parameter = null; |
4925: | $parameterType = null; |
4926: | $parameterNativeType = null; |
4927: | if (isset($parameters) && $parametersAcceptor !== null) { |
4928: | if (isset($parameters[$i])) { |
4929: | $assignByReference = $parameters[$i]->passedByReference()->createsNewVariable(); |
4930: | $parameterType = $parameters[$i]->getType(); |
4931: | |
4932: | if ($parameters[$i] instanceof ExtendedParameterReflection) { |
4933: | $parameterNativeType = $parameters[$i]->getNativeType(); |
4934: | } |
4935: | $parameter = $parameters[$i]; |
4936: | } elseif (count($parameters) > 0 && $parametersAcceptor->isVariadic()) { |
4937: | $lastParameter = $parameters[count($parameters) - 1]; |
4938: | $assignByReference = $lastParameter->passedByReference()->createsNewVariable(); |
4939: | $parameterType = $lastParameter->getType(); |
4940: | |
4941: | if ($lastParameter instanceof ExtendedParameterReflection) { |
4942: | $parameterNativeType = $lastParameter->getNativeType(); |
4943: | } |
4944: | $parameter = $lastParameter; |
4945: | } |
4946: | } |
4947: | |
4948: | $lookForUnset = false; |
4949: | if ($assignByReference) { |
4950: | $isBuiltin = false; |
4951: | if ($calleeReflection instanceof FunctionReflection && $calleeReflection->isBuiltin()) { |
4952: | $isBuiltin = true; |
4953: | } elseif ($calleeReflection instanceof ExtendedMethodReflection && $calleeReflection->getDeclaringClass()->isBuiltin()) { |
4954: | $isBuiltin = true; |
4955: | } |
4956: | if ( |
4957: | $isBuiltin |
4958: | || ($parameterNativeType === null || !$parameterNativeType->isNull()->no()) |
4959: | ) { |
4960: | $scope = $this->lookForSetAllowedUndefinedExpressions($scope, $arg->value); |
4961: | $lookForUnset = true; |
4962: | } |
4963: | } |
4964: | |
4965: | if ($calleeReflection !== null) { |
4966: | $scope = $scope->pushInFunctionCall($calleeReflection, $parameter); |
4967: | } |
4968: | |
4969: | $originalArg = $arg->getAttribute(ArgumentsNormalizer::ORIGINAL_ARG_ATTRIBUTE) ?? $arg; |
4970: | $nodeCallback($originalArg, $scope); |
4971: | |
4972: | $originalScope = $scope; |
4973: | $scopeToPass = $scope; |
4974: | if ($i === 0 && $closureBindScope !== null) { |
4975: | $scopeToPass = $closureBindScope; |
4976: | } |
4977: | |
4978: | if ($parameter instanceof ExtendedParameterReflection) { |
4979: | $parameterCallImmediately = $parameter->isImmediatelyInvokedCallable(); |
4980: | if ($parameterCallImmediately->maybe()) { |
4981: | $callCallbackImmediately = $calleeReflection instanceof FunctionReflection; |
4982: | } else { |
4983: | $callCallbackImmediately = $parameterCallImmediately->yes(); |
4984: | } |
4985: | } else { |
4986: | $callCallbackImmediately = $calleeReflection instanceof FunctionReflection; |
4987: | } |
4988: | if ($arg->value instanceof Expr\Closure) { |
4989: | $restoreThisScope = null; |
4990: | if ( |
4991: | $closureBindScope === null |
4992: | && $parameter instanceof ExtendedParameterReflection |
4993: | && $parameter->getClosureThisType() !== null |
4994: | && !$arg->value->static |
4995: | ) { |
4996: | $restoreThisScope = $scopeToPass; |
4997: | $scopeToPass = $scopeToPass->assignVariable('this', $parameter->getClosureThisType(), new ObjectWithoutClassType(), TrinaryLogic::createYes()); |
4998: | } |
4999: | |
5000: | if ($parameter !== null) { |
5001: | $overwritingParameterType = $this->getParameterTypeFromParameterClosureTypeExtension($callLike, $calleeReflection, $parameter, $scopeToPass); |
5002: | |
5003: | if ($overwritingParameterType !== null) { |
5004: | $parameterType = $overwritingParameterType; |
5005: | } |
5006: | } |
5007: | |
5008: | $this->callNodeCallbackWithExpression($nodeCallback, $arg->value, $scopeToPass, $context); |
5009: | $closureResult = $this->processClosureNode($stmt, $arg->value, $scopeToPass, $nodeCallback, $context, $parameterType ?? null); |
5010: | if ($callCallbackImmediately) { |
5011: | $throwPoints = array_merge($throwPoints, array_map(static fn (ThrowPoint $throwPoint) => $throwPoint->isExplicit() ? ThrowPoint::createExplicit($scope, $throwPoint->getType(), $arg->value, $throwPoint->canContainAnyThrowable()) : ThrowPoint::createImplicit($scope, $arg->value), $closureResult->getThrowPoints())); |
5012: | $impurePoints = array_merge($impurePoints, $closureResult->getImpurePoints()); |
5013: | } |
5014: | |
5015: | $uses = []; |
5016: | foreach ($arg->value->uses as $use) { |
5017: | if (!is_string($use->var->name)) { |
5018: | continue; |
5019: | } |
5020: | |
5021: | $uses[] = $use->var->name; |
5022: | } |
5023: | |
5024: | $scope = $closureResult->getScope(); |
5025: | $invalidateExpressions = $closureResult->getInvalidateExpressions(); |
5026: | if ($restoreThisScope !== null) { |
5027: | $nodeFinder = new NodeFinder(); |
5028: | $cb = static fn ($expr) => $expr instanceof Variable && $expr->name === 'this'; |
5029: | foreach ($invalidateExpressions as $j => $invalidateExprNode) { |
5030: | $foundThis = $nodeFinder->findFirst([$invalidateExprNode->getExpr()], $cb); |
5031: | if ($foundThis === null) { |
5032: | continue; |
5033: | } |
5034: | |
5035: | unset($invalidateExpressions[$j]); |
5036: | } |
5037: | $invalidateExpressions = array_values($invalidateExpressions); |
5038: | $scope = $scope->restoreThis($restoreThisScope); |
5039: | } |
5040: | |
5041: | $scope = $this->processImmediatelyCalledCallable($scope, $invalidateExpressions, $uses); |
5042: | } elseif ($arg->value instanceof Expr\ArrowFunction) { |
5043: | if ( |
5044: | $closureBindScope === null |
5045: | && $parameter instanceof ExtendedParameterReflection |
5046: | && $parameter->getClosureThisType() !== null |
5047: | && !$arg->value->static |
5048: | ) { |
5049: | $scopeToPass = $scopeToPass->assignVariable('this', $parameter->getClosureThisType(), new ObjectWithoutClassType(), TrinaryLogic::createYes()); |
5050: | } |
5051: | |
5052: | if ($parameter !== null) { |
5053: | $overwritingParameterType = $this->getParameterTypeFromParameterClosureTypeExtension($callLike, $calleeReflection, $parameter, $scopeToPass); |
5054: | |
5055: | if ($overwritingParameterType !== null) { |
5056: | $parameterType = $overwritingParameterType; |
5057: | } |
5058: | } |
5059: | |
5060: | $this->callNodeCallbackWithExpression($nodeCallback, $arg->value, $scopeToPass, $context); |
5061: | $arrowFunctionResult = $this->processArrowFunctionNode($stmt, $arg->value, $scopeToPass, $nodeCallback, $parameterType ?? null); |
5062: | if ($callCallbackImmediately) { |
5063: | $throwPoints = array_merge($throwPoints, array_map(static fn (ThrowPoint $throwPoint) => $throwPoint->isExplicit() ? ThrowPoint::createExplicit($scope, $throwPoint->getType(), $arg->value, $throwPoint->canContainAnyThrowable()) : ThrowPoint::createImplicit($scope, $arg->value), $arrowFunctionResult->getThrowPoints())); |
5064: | $impurePoints = array_merge($impurePoints, $arrowFunctionResult->getImpurePoints()); |
5065: | } |
5066: | } else { |
5067: | $exprType = $scope->getType($arg->value); |
5068: | $exprResult = $this->processExprNode($stmt, $arg->value, $scopeToPass, $nodeCallback, $context->enterDeep()); |
5069: | $throwPoints = array_merge($throwPoints, $exprResult->getThrowPoints()); |
5070: | $impurePoints = array_merge($impurePoints, $exprResult->getImpurePoints()); |
5071: | $scope = $exprResult->getScope(); |
5072: | $hasYield = $hasYield || $exprResult->hasYield(); |
5073: | |
5074: | if ($exprType->isCallable()->yes()) { |
5075: | $acceptors = $exprType->getCallableParametersAcceptors($scope); |
5076: | if (count($acceptors) === 1) { |
5077: | $scope = $this->processImmediatelyCalledCallable($scope, $acceptors[0]->getInvalidateExpressions(), $acceptors[0]->getUsedVariables()); |
5078: | if ($callCallbackImmediately) { |
5079: | $callableThrowPoints = array_map(static fn (SimpleThrowPoint $throwPoint) => $throwPoint->isExplicit() ? ThrowPoint::createExplicit($scope, $throwPoint->getType(), $arg->value, $throwPoint->canContainAnyThrowable()) : ThrowPoint::createImplicit($scope, $arg->value), $acceptors[0]->getThrowPoints()); |
5080: | if (!$this->implicitThrows) { |
5081: | $callableThrowPoints = array_values(array_filter($callableThrowPoints, static fn (ThrowPoint $throwPoint) => $throwPoint->isExplicit())); |
5082: | } |
5083: | $throwPoints = array_merge($throwPoints, $callableThrowPoints); |
5084: | $impurePoints = array_merge($impurePoints, array_map(static fn (SimpleImpurePoint $impurePoint) => new ImpurePoint($scope, $arg->value, $impurePoint->getIdentifier(), $impurePoint->getDescription(), $impurePoint->isCertain()), $acceptors[0]->getImpurePoints())); |
5085: | } |
5086: | } |
5087: | } |
5088: | } |
5089: | |
5090: | if ($assignByReference && $lookForUnset) { |
5091: | $scope = $this->lookForUnsetAllowedUndefinedExpressions($scope, $arg->value); |
5092: | } |
5093: | |
5094: | if ($calleeReflection !== null) { |
5095: | $scope = $scope->popInFunctionCall(); |
5096: | } |
5097: | |
5098: | if ($i !== 0 || $closureBindScope === null) { |
5099: | continue; |
5100: | } |
5101: | |
5102: | $scope = $scope->restoreOriginalScopeAfterClosureBind($originalScope); |
5103: | } |
5104: | foreach ($args as $i => $arg) { |
5105: | if (!isset($parameters) || $parametersAcceptor === null) { |
5106: | continue; |
5107: | } |
5108: | |
5109: | $byRefType = new MixedType(); |
5110: | $assignByReference = false; |
5111: | $currentParameter = null; |
5112: | if (isset($parameters[$i])) { |
5113: | $currentParameter = $parameters[$i]; |
5114: | } elseif (count($parameters) > 0 && $parametersAcceptor->isVariadic()) { |
5115: | $currentParameter = $parameters[count($parameters) - 1]; |
5116: | } |
5117: | |
5118: | if ($currentParameter !== null) { |
5119: | $assignByReference = $currentParameter->passedByReference()->createsNewVariable(); |
5120: | if ($assignByReference) { |
5121: | if ($currentParameter instanceof ExtendedParameterReflection && $currentParameter->getOutType() !== null) { |
5122: | $byRefType = $currentParameter->getOutType(); |
5123: | } elseif ( |
5124: | $calleeReflection instanceof MethodReflection |
5125: | && !$calleeReflection->getDeclaringClass()->isBuiltin() |
5126: | ) { |
5127: | $byRefType = $currentParameter->getType(); |
5128: | } elseif ( |
5129: | $calleeReflection instanceof FunctionReflection |
5130: | && !$calleeReflection->isBuiltin() |
5131: | ) { |
5132: | $byRefType = $currentParameter->getType(); |
5133: | } |
5134: | } |
5135: | } |
5136: | |
5137: | if ($assignByReference) { |
5138: | if ($currentParameter === null) { |
5139: | throw new ShouldNotHappenException(); |
5140: | } |
5141: | |
5142: | $argValue = $arg->value; |
5143: | if (!$argValue instanceof Variable || $argValue->name !== 'this') { |
5144: | $paramOutType = $this->getParameterOutExtensionsType($callLike, $calleeReflection, $currentParameter, $scope); |
5145: | if ($paramOutType !== null) { |
5146: | $byRefType = $paramOutType; |
5147: | } |
5148: | |
5149: | $result = $this->processAssignVar( |
5150: | $scope, |
5151: | $stmt, |
5152: | $argValue, |
5153: | new TypeExpr($byRefType), |
5154: | static function (Node $node, Scope $scope) use ($nodeCallback): void { |
5155: | if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) { |
5156: | return; |
5157: | } |
5158: | |
5159: | $nodeCallback($node, $scope); |
5160: | }, |
5161: | $context, |
5162: | static fn (MutatingScope $scope): ExpressionResult => new ExpressionResult($scope, false, [], []), |
5163: | true, |
5164: | ); |
5165: | $scope = $result->getScope(); |
5166: | } |
5167: | } elseif ($calleeReflection !== null && $calleeReflection->hasSideEffects()->yes()) { |
5168: | $argType = $scope->getType($arg->value); |
5169: | if (!$argType->isObject()->no()) { |
5170: | $nakedReturnType = null; |
5171: | if ($nakedMethodReflection !== null) { |
5172: | $nakedParametersAcceptor = ParametersAcceptorSelector::selectFromArgs( |
5173: | $scope, |
5174: | $args, |
5175: | $nakedMethodReflection->getVariants(), |
5176: | $nakedMethodReflection->getNamedArgumentsVariants(), |
5177: | ); |
5178: | $nakedReturnType = $nakedParametersAcceptor->getReturnType(); |
5179: | } |
5180: | if ( |
5181: | $nakedReturnType === null |
5182: | || !(new ThisType($nakedMethodReflection->getDeclaringClass()))->isSuperTypeOf($nakedReturnType)->yes() |
5183: | || $nakedMethodReflection->isPure()->no() |
5184: | ) { |
5185: | $nodeCallback(new InvalidateExprNode($arg->value), $scope); |
5186: | $scope = $scope->invalidateExpression($arg->value, true); |
5187: | } |
5188: | } elseif (!(new ResourceType())->isSuperTypeOf($argType)->no()) { |
5189: | $nodeCallback(new InvalidateExprNode($arg->value), $scope); |
5190: | $scope = $scope->invalidateExpression($arg->value, true); |
5191: | } |
5192: | } |
5193: | } |
5194: | |
5195: | return new ExpressionResult($scope, $hasYield, $throwPoints, $impurePoints); |
5196: | } |
5197: | |
5198: | |
5199: | |
5200: | |
5201: | private function getParameterTypeFromParameterClosureTypeExtension(CallLike $callLike, $calleeReflection, ParameterReflection $parameter, MutatingScope $scope): ?Type |
5202: | { |
5203: | if ($callLike instanceof FuncCall && $calleeReflection instanceof FunctionReflection) { |
5204: | foreach ($this->parameterClosureTypeExtensionProvider->getFunctionParameterClosureTypeExtensions() as $functionParameterClosureTypeExtension) { |
5205: | if ($functionParameterClosureTypeExtension->isFunctionSupported($calleeReflection, $parameter)) { |
5206: | return $functionParameterClosureTypeExtension->getTypeFromFunctionCall($calleeReflection, $callLike, $parameter, $scope); |
5207: | } |
5208: | } |
5209: | } elseif ($calleeReflection instanceof MethodReflection) { |
5210: | if ($callLike instanceof StaticCall) { |
5211: | foreach ($this->parameterClosureTypeExtensionProvider->getStaticMethodParameterClosureTypeExtensions() as $staticMethodParameterClosureTypeExtension) { |
5212: | if ($staticMethodParameterClosureTypeExtension->isStaticMethodSupported($calleeReflection, $parameter)) { |
5213: | return $staticMethodParameterClosureTypeExtension->getTypeFromStaticMethodCall($calleeReflection, $callLike, $parameter, $scope); |
5214: | } |
5215: | } |
5216: | } elseif ($callLike instanceof MethodCall) { |
5217: | foreach ($this->parameterClosureTypeExtensionProvider->getMethodParameterClosureTypeExtensions() as $methodParameterClosureTypeExtension) { |
5218: | if ($methodParameterClosureTypeExtension->isMethodSupported($calleeReflection, $parameter)) { |
5219: | return $methodParameterClosureTypeExtension->getTypeFromMethodCall($calleeReflection, $callLike, $parameter, $scope); |
5220: | } |
5221: | } |
5222: | } |
5223: | } |
5224: | |
5225: | return null; |
5226: | } |
5227: | |
5228: | |
5229: | |
5230: | |
5231: | private function getParameterOutExtensionsType(CallLike $callLike, $calleeReflection, ParameterReflection $currentParameter, MutatingScope $scope): ?Type |
5232: | { |
5233: | $paramOutTypes = []; |
5234: | if ($callLike instanceof FuncCall && $calleeReflection instanceof FunctionReflection) { |
5235: | foreach ($this->parameterOutTypeExtensionProvider->getFunctionParameterOutTypeExtensions() as $functionParameterOutTypeExtension) { |
5236: | if (!$functionParameterOutTypeExtension->isFunctionSupported($calleeReflection, $currentParameter)) { |
5237: | continue; |
5238: | } |
5239: | |
5240: | $resolvedType = $functionParameterOutTypeExtension->getParameterOutTypeFromFunctionCall($calleeReflection, $callLike, $currentParameter, $scope); |
5241: | if ($resolvedType === null) { |
5242: | continue; |
5243: | } |
5244: | $paramOutTypes[] = $resolvedType; |
5245: | } |
5246: | } elseif ($callLike instanceof MethodCall && $calleeReflection instanceof MethodReflection) { |
5247: | foreach ($this->parameterOutTypeExtensionProvider->getMethodParameterOutTypeExtensions() as $methodParameterOutTypeExtension) { |
5248: | if (!$methodParameterOutTypeExtension->isMethodSupported($calleeReflection, $currentParameter)) { |
5249: | continue; |
5250: | } |
5251: | |
5252: | $resolvedType = $methodParameterOutTypeExtension->getParameterOutTypeFromMethodCall($calleeReflection, $callLike, $currentParameter, $scope); |
5253: | if ($resolvedType === null) { |
5254: | continue; |
5255: | } |
5256: | $paramOutTypes[] = $resolvedType; |
5257: | } |
5258: | } elseif ($callLike instanceof StaticCall && $calleeReflection instanceof MethodReflection) { |
5259: | foreach ($this->parameterOutTypeExtensionProvider->getStaticMethodParameterOutTypeExtensions() as $staticMethodParameterOutTypeExtension) { |
5260: | if (!$staticMethodParameterOutTypeExtension->isStaticMethodSupported($calleeReflection, $currentParameter)) { |
5261: | continue; |
5262: | } |
5263: | |
5264: | $resolvedType = $staticMethodParameterOutTypeExtension->getParameterOutTypeFromStaticMethodCall($calleeReflection, $callLike, $currentParameter, $scope); |
5265: | if ($resolvedType === null) { |
5266: | continue; |
5267: | } |
5268: | $paramOutTypes[] = $resolvedType; |
5269: | } |
5270: | } |
5271: | |
5272: | if (count($paramOutTypes) === 1) { |
5273: | return $paramOutTypes[0]; |
5274: | } |
5275: | |
5276: | if (count($paramOutTypes) > 1) { |
5277: | return TypeCombinator::union(...$paramOutTypes); |
5278: | } |
5279: | |
5280: | return null; |
5281: | } |
5282: | |
5283: | |
5284: | |
5285: | |
5286: | |
5287: | private function processAssignVar( |
5288: | MutatingScope $scope, |
5289: | Node\Stmt $stmt, |
5290: | Expr $var, |
5291: | Expr $assignedExpr, |
5292: | callable $nodeCallback, |
5293: | ExpressionContext $context, |
5294: | Closure $processExprCallback, |
5295: | bool $enterExpressionAssign, |
5296: | ): ExpressionResult |
5297: | { |
5298: | $nodeCallback($var, $enterExpressionAssign ? $scope->enterExpressionAssign($var) : $scope); |
5299: | $hasYield = false; |
5300: | $throwPoints = []; |
5301: | $impurePoints = []; |
5302: | $isAssignOp = $assignedExpr instanceof Expr\AssignOp && !$enterExpressionAssign; |
5303: | if ($var instanceof Variable && is_string($var->name)) { |
5304: | $result = $processExprCallback($scope); |
5305: | $hasYield = $result->hasYield(); |
5306: | $throwPoints = $result->getThrowPoints(); |
5307: | $impurePoints = $result->getImpurePoints(); |
5308: | if (in_array($var->name, Scope::SUPERGLOBAL_VARIABLES, true)) { |
5309: | $impurePoints[] = new ImpurePoint($scope, $var, 'superglobal', 'assign to superglobal variable', true); |
5310: | } |
5311: | $assignedExpr = $this->unwrapAssign($assignedExpr); |
5312: | $type = $scope->getType($assignedExpr); |
5313: | |
5314: | $conditionalExpressions = []; |
5315: | if ($assignedExpr instanceof Ternary) { |
5316: | $if = $assignedExpr->if; |
5317: | if ($if === null) { |
5318: | $if = $assignedExpr->cond; |
5319: | } |
5320: | $condScope = $this->processExprNode($stmt, $assignedExpr->cond, $scope, static function (): void { |
5321: | }, ExpressionContext::createDeep())->getScope(); |
5322: | $truthySpecifiedTypes = $this->typeSpecifier->specifyTypesInCondition($condScope, $assignedExpr->cond, TypeSpecifierContext::createTruthy()); |
5323: | $falseySpecifiedTypes = $this->typeSpecifier->specifyTypesInCondition($condScope, $assignedExpr->cond, TypeSpecifierContext::createFalsey()); |
5324: | $truthyScope = $condScope->filterBySpecifiedTypes($truthySpecifiedTypes); |
5325: | $falsyScope = $condScope->filterBySpecifiedTypes($falseySpecifiedTypes); |
5326: | $truthyType = $truthyScope->getType($if); |
5327: | $falseyType = $falsyScope->getType($assignedExpr->else); |
5328: | |
5329: | if ( |
5330: | $truthyType->isSuperTypeOf($falseyType)->no() |
5331: | && $falseyType->isSuperTypeOf($truthyType)->no() |
5332: | ) { |
5333: | $conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($condScope, $var->name, $conditionalExpressions, $truthySpecifiedTypes, $truthyType); |
5334: | $conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($condScope, $var->name, $conditionalExpressions, $truthySpecifiedTypes, $truthyType); |
5335: | $conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($condScope, $var->name, $conditionalExpressions, $falseySpecifiedTypes, $falseyType); |
5336: | $conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($condScope, $var->name, $conditionalExpressions, $falseySpecifiedTypes, $falseyType); |
5337: | } |
5338: | } |
5339: | |
5340: | $scope = $result->getScope(); |
5341: | $truthySpecifiedTypes = $this->typeSpecifier->specifyTypesInCondition($scope, $assignedExpr, TypeSpecifierContext::createTruthy()); |
5342: | $falseySpecifiedTypes = $this->typeSpecifier->specifyTypesInCondition($scope, $assignedExpr, TypeSpecifierContext::createFalsey()); |
5343: | |
5344: | $truthyType = TypeCombinator::removeFalsey($type); |
5345: | $falseyType = TypeCombinator::intersect($type, StaticTypeFactory::falsey()); |
5346: | |
5347: | $conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $truthySpecifiedTypes, $truthyType); |
5348: | $conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $truthySpecifiedTypes, $truthyType); |
5349: | $conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $falseySpecifiedTypes, $falseyType); |
5350: | $conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $falseySpecifiedTypes, $falseyType); |
5351: | |
5352: | $nodeCallback(new VariableAssignNode($var, $assignedExpr, $isAssignOp), $result->getScope()); |
5353: | $scope = $scope->assignVariable($var->name, $type, $scope->getNativeType($assignedExpr), TrinaryLogic::createYes()); |
5354: | foreach ($conditionalExpressions as $exprString => $holders) { |
5355: | $scope = $scope->addConditionalExpressions($exprString, $holders); |
5356: | } |
5357: | } elseif ($var instanceof ArrayDimFetch) { |
5358: | $dimFetchStack = []; |
5359: | $originalVar = $var; |
5360: | $assignedPropertyExpr = $assignedExpr; |
5361: | while ($var instanceof ArrayDimFetch) { |
5362: | $varForSetOffsetValue = $var->var; |
5363: | if ($varForSetOffsetValue instanceof PropertyFetch || $varForSetOffsetValue instanceof StaticPropertyFetch) { |
5364: | $varForSetOffsetValue = new OriginalPropertyTypeExpr($varForSetOffsetValue); |
5365: | } |
5366: | $assignedPropertyExpr = new SetOffsetValueTypeExpr( |
5367: | $varForSetOffsetValue, |
5368: | $var->dim, |
5369: | $assignedPropertyExpr, |
5370: | ); |
5371: | $dimFetchStack[] = $var; |
5372: | $var = $var->var; |
5373: | } |
5374: | |
5375: | |
5376: | if ($enterExpressionAssign) { |
5377: | $scope = $scope->enterExpressionAssign($var); |
5378: | } |
5379: | $result = $this->processExprNode($stmt, $var, $scope, $nodeCallback, $context->enterDeep()); |
5380: | $hasYield = $result->hasYield(); |
5381: | $throwPoints = $result->getThrowPoints(); |
5382: | $impurePoints = $result->getImpurePoints(); |
5383: | $scope = $result->getScope(); |
5384: | if ($enterExpressionAssign) { |
5385: | $scope = $scope->exitExpressionAssign($var); |
5386: | } |
5387: | |
5388: | |
5389: | $offsetTypes = []; |
5390: | $offsetNativeTypes = []; |
5391: | $dimFetchStack = array_reverse($dimFetchStack); |
5392: | $lastDimKey = array_key_last($dimFetchStack); |
5393: | foreach ($dimFetchStack as $key => $dimFetch) { |
5394: | $dimExpr = $dimFetch->dim; |
5395: | |
5396: | |
5397: | if ($key !== $lastDimKey) { |
5398: | $nodeCallback($dimFetch, $enterExpressionAssign ? $scope->enterExpressionAssign($dimFetch) : $scope); |
5399: | } |
5400: | |
5401: | if ($dimExpr === null) { |
5402: | $offsetTypes[] = null; |
5403: | $offsetNativeTypes[] = null; |
5404: | |
5405: | } else { |
5406: | $offsetTypes[] = $scope->getType($dimExpr); |
5407: | $offsetNativeTypes[] = $scope->getNativeType($dimExpr); |
5408: | |
5409: | if ($enterExpressionAssign) { |
5410: | $scope->enterExpressionAssign($dimExpr); |
5411: | } |
5412: | $result = $this->processExprNode($stmt, $dimExpr, $scope, $nodeCallback, $context->enterDeep()); |
5413: | $hasYield = $hasYield || $result->hasYield(); |
5414: | $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); |
5415: | $scope = $result->getScope(); |
5416: | |
5417: | if ($enterExpressionAssign) { |
5418: | $scope = $scope->exitExpressionAssign($dimExpr); |
5419: | } |
5420: | } |
5421: | } |
5422: | |
5423: | $valueToWrite = $scope->getType($assignedExpr); |
5424: | $nativeValueToWrite = $scope->getNativeType($assignedExpr); |
5425: | $originalValueToWrite = $valueToWrite; |
5426: | $originalNativeValueToWrite = $nativeValueToWrite; |
5427: | |
5428: | |
5429: | $result = $processExprCallback($scope); |
5430: | $hasYield = $hasYield || $result->hasYield(); |
5431: | $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); |
5432: | $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); |
5433: | $scope = $result->getScope(); |
5434: | |
5435: | $varType = $scope->getType($var); |
5436: | $varNativeType = $scope->getNativeType($var); |
5437: | |
5438: | |
5439: | if ($varType instanceof ErrorType) { |
5440: | $varType = new ConstantArrayType([], []); |
5441: | } |
5442: | if ($varNativeType instanceof ErrorType) { |
5443: | $varNativeType = new ConstantArrayType([], []); |
5444: | } |
5445: | $offsetValueType = $varType; |
5446: | $offsetNativeValueType = $varNativeType; |
5447: | |
5448: | $valueToWrite = $this->produceArrayDimFetchAssignValueToWrite($offsetTypes, $offsetValueType, $valueToWrite); |
5449: | |
5450: | if (!$offsetValueType->equals($offsetNativeValueType) || !$valueToWrite->equals($nativeValueToWrite)) { |
5451: | $nativeValueToWrite = $this->produceArrayDimFetchAssignValueToWrite($offsetNativeTypes, $offsetNativeValueType, $nativeValueToWrite); |
5452: | } else { |
5453: | $rewritten = false; |
5454: | foreach ($offsetTypes as $i => $offsetType) { |
5455: | $offsetNativeType = $offsetNativeTypes[$i]; |
5456: | if ($offsetType === null) { |
5457: | if ($offsetNativeType !== null) { |
5458: | throw new ShouldNotHappenException(); |
5459: | } |
5460: | |
5461: | continue; |
5462: | } elseif ($offsetNativeType === null) { |
5463: | throw new ShouldNotHappenException(); |
5464: | } |
5465: | if ($offsetType->equals($offsetNativeType)) { |
5466: | continue; |
5467: | } |
5468: | |
5469: | $nativeValueToWrite = $this->produceArrayDimFetchAssignValueToWrite($offsetNativeTypes, $offsetNativeValueType, $nativeValueToWrite); |
5470: | $rewritten = true; |
5471: | break; |
5472: | } |
5473: | |
5474: | if (!$rewritten) { |
5475: | $nativeValueToWrite = $valueToWrite; |
5476: | } |
5477: | } |
5478: | |
5479: | if ($varType->isArray()->yes() || !(new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->yes()) { |
5480: | if ($var instanceof Variable && is_string($var->name)) { |
5481: | $nodeCallback(new VariableAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scope); |
5482: | $scope = $scope->assignVariable($var->name, $valueToWrite, $nativeValueToWrite, TrinaryLogic::createYes()); |
5483: | } else { |
5484: | if ($var instanceof PropertyFetch || $var instanceof StaticPropertyFetch) { |
5485: | $nodeCallback(new PropertyAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scope); |
5486: | if ($var instanceof PropertyFetch && $var->name instanceof Node\Identifier && !$isAssignOp) { |
5487: | $scope = $scope->assignInitializedProperty($scope->getType($var->var), $var->name->toString()); |
5488: | } |
5489: | } |
5490: | $scope = $scope->assignExpression( |
5491: | $var, |
5492: | $valueToWrite, |
5493: | $nativeValueToWrite, |
5494: | ); |
5495: | } |
5496: | |
5497: | if ($originalVar->dim instanceof Variable || $originalVar->dim instanceof Node\Scalar) { |
5498: | $currentVarType = $scope->getType($originalVar); |
5499: | if (!$originalValueToWrite->isSuperTypeOf($currentVarType)->yes()) { |
5500: | $scope = $scope->assignExpression( |
5501: | $originalVar, |
5502: | $originalValueToWrite, |
5503: | $originalNativeValueToWrite, |
5504: | ); |
5505: | } |
5506: | } |
5507: | } else { |
5508: | if ($var instanceof Variable) { |
5509: | $nodeCallback(new VariableAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scope); |
5510: | } elseif ($var instanceof PropertyFetch || $var instanceof StaticPropertyFetch) { |
5511: | $nodeCallback(new PropertyAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scope); |
5512: | if ($var instanceof PropertyFetch && $var->name instanceof Node\Identifier && !$isAssignOp) { |
5513: | $scope = $scope->assignInitializedProperty($scope->getType($var->var), $var->name->toString()); |
5514: | } |
5515: | } |
5516: | } |
5517: | |
5518: | if (!$varType->isArray()->yes() && !(new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->no()) { |
5519: | $throwPoints = array_merge($throwPoints, $this->processExprNode( |
5520: | $stmt, |
5521: | new MethodCall($var, 'offsetSet'), |
5522: | $scope, |
5523: | static function (): void { |
5524: | }, |
5525: | $context, |
5526: | )->getThrowPoints()); |
5527: | } |
5528: | } elseif ($var instanceof PropertyFetch) { |
5529: | $objectResult = $this->processExprNode($stmt, $var->var, $scope, $nodeCallback, $context); |
5530: | $hasYield = $objectResult->hasYield(); |
5531: | $throwPoints = $objectResult->getThrowPoints(); |
5532: | $impurePoints = $objectResult->getImpurePoints(); |
5533: | $scope = $objectResult->getScope(); |
5534: | |
5535: | $propertyName = null; |
5536: | if ($var->name instanceof Node\Identifier) { |
5537: | $propertyName = $var->name->name; |
5538: | } else { |
5539: | $propertyNameResult = $this->processExprNode($stmt, $var->name, $scope, $nodeCallback, $context); |
5540: | $hasYield = $hasYield || $propertyNameResult->hasYield(); |
5541: | $throwPoints = array_merge($throwPoints, $propertyNameResult->getThrowPoints()); |
5542: | $impurePoints = array_merge($impurePoints, $propertyNameResult->getImpurePoints()); |
5543: | $scope = $propertyNameResult->getScope(); |
5544: | } |
5545: | |
5546: | $result = $processExprCallback($scope); |
5547: | $hasYield = $hasYield || $result->hasYield(); |
5548: | $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); |
5549: | $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); |
5550: | $scope = $result->getScope(); |
5551: | |
5552: | if ($var->name instanceof Expr && $this->phpVersion->supportsPropertyHooks()) { |
5553: | $throwPoints[] = ThrowPoint::createImplicit($scope, $var); |
5554: | } |
5555: | |
5556: | $propertyHolderType = $scope->getType($var->var); |
5557: | if ($propertyName !== null && $propertyHolderType->hasProperty($propertyName)->yes()) { |
5558: | $propertyReflection = $propertyHolderType->getProperty($propertyName, $scope); |
5559: | $assignedExprType = $scope->getType($assignedExpr); |
5560: | $nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope); |
5561: | if ($propertyReflection->canChangeTypeAfterAssignment()) { |
5562: | if ($propertyReflection->hasNativeType() && $scope->isDeclareStrictTypes()) { |
5563: | $propertyNativeType = $propertyReflection->getNativeType(); |
5564: | |
5565: | $scope = $scope->assignExpression($var, TypeCombinator::intersect($assignedExprType->toCoercedArgumentType(true), $propertyNativeType), TypeCombinator::intersect($scope->getNativeType($assignedExpr)->toCoercedArgumentType(true), $propertyNativeType)); |
5566: | } else { |
5567: | $scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr)); |
5568: | } |
5569: | } |
5570: | $declaringClass = $propertyReflection->getDeclaringClass(); |
5571: | if ($declaringClass->hasNativeProperty($propertyName)) { |
5572: | $nativeProperty = $declaringClass->getNativeProperty($propertyName); |
5573: | if ( |
5574: | !$nativeProperty->getNativeType()->accepts($assignedExprType, true)->yes() |
5575: | ) { |
5576: | $throwPoints[] = ThrowPoint::createExplicit($scope, new ObjectType(TypeError::class), $assignedExpr, false); |
5577: | } |
5578: | if ($this->phpVersion->supportsPropertyHooks()) { |
5579: | $throwPoints = array_merge($throwPoints, $this->getPropertyAssignThrowPointsFromSetHook($scope, $var, $nativeProperty)); |
5580: | } |
5581: | if ($enterExpressionAssign) { |
5582: | $scope = $scope->assignInitializedProperty($propertyHolderType, $propertyName); |
5583: | } |
5584: | } |
5585: | } else { |
5586: | |
5587: | $assignedExprType = $scope->getType($assignedExpr); |
5588: | $nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope); |
5589: | $scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr)); |
5590: | |
5591: | if (!$propertyHolderType->hasMethod('__set')->no()) { |
5592: | $throwPoints = array_merge($throwPoints, $this->processExprNode( |
5593: | $stmt, |
5594: | new MethodCall($var->var, '__set'), |
5595: | $scope, |
5596: | static function (): void { |
5597: | }, |
5598: | $context, |
5599: | )->getThrowPoints()); |
5600: | } |
5601: | } |
5602: | |
5603: | } elseif ($var instanceof Expr\StaticPropertyFetch) { |
5604: | if ($var->class instanceof Node\Name) { |
5605: | $propertyHolderType = $scope->resolveTypeByName($var->class); |
5606: | } else { |
5607: | $this->processExprNode($stmt, $var->class, $scope, $nodeCallback, $context); |
5608: | $propertyHolderType = $scope->getType($var->class); |
5609: | } |
5610: | |
5611: | $propertyName = null; |
5612: | if ($var->name instanceof Node\Identifier) { |
5613: | $propertyName = $var->name->name; |
5614: | } else { |
5615: | $propertyNameResult = $this->processExprNode($stmt, $var->name, $scope, $nodeCallback, $context); |
5616: | $hasYield = $propertyNameResult->hasYield(); |
5617: | $throwPoints = $propertyNameResult->getThrowPoints(); |
5618: | $impurePoints = $propertyNameResult->getImpurePoints(); |
5619: | $scope = $propertyNameResult->getScope(); |
5620: | } |
5621: | |
5622: | $result = $processExprCallback($scope); |
5623: | $hasYield = $hasYield || $result->hasYield(); |
5624: | $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); |
5625: | $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); |
5626: | $scope = $result->getScope(); |
5627: | |
5628: | if ($propertyName !== null) { |
5629: | $propertyReflection = $scope->getPropertyReflection($propertyHolderType, $propertyName); |
5630: | $assignedExprType = $scope->getType($assignedExpr); |
5631: | $nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope); |
5632: | if ($propertyReflection !== null && $propertyReflection->canChangeTypeAfterAssignment()) { |
5633: | if ($propertyReflection->hasNativeType() && $scope->isDeclareStrictTypes()) { |
5634: | $propertyNativeType = $propertyReflection->getNativeType(); |
5635: | |
5636: | $scope = $scope->assignExpression($var, TypeCombinator::intersect($assignedExprType->toCoercedArgumentType(true), $propertyNativeType), TypeCombinator::intersect($scope->getNativeType($assignedExpr)->toCoercedArgumentType(true), $propertyNativeType)); |
5637: | } else { |
5638: | $scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr)); |
5639: | } |
5640: | } |
5641: | } else { |
5642: | |
5643: | $assignedExprType = $scope->getType($assignedExpr); |
5644: | $nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope); |
5645: | $scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr)); |
5646: | } |
5647: | } elseif ($var instanceof List_) { |
5648: | $result = $processExprCallback($scope); |
5649: | $hasYield = $result->hasYield(); |
5650: | $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); |
5651: | $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); |
5652: | $scope = $result->getScope(); |
5653: | foreach ($var->items as $i => $arrayItem) { |
5654: | if ($arrayItem === null) { |
5655: | continue; |
5656: | } |
5657: | |
5658: | $itemScope = $scope; |
5659: | if ($enterExpressionAssign) { |
5660: | $itemScope = $itemScope->enterExpressionAssign($arrayItem->value); |
5661: | } |
5662: | $itemScope = $this->lookForSetAllowedUndefinedExpressions($itemScope, $arrayItem->value); |
5663: | $nodeCallback($arrayItem, $itemScope); |
5664: | if ($arrayItem->key !== null) { |
5665: | $keyResult = $this->processExprNode($stmt, $arrayItem->key, $itemScope, $nodeCallback, $context->enterDeep()); |
5666: | $hasYield = $hasYield || $keyResult->hasYield(); |
5667: | $throwPoints = array_merge($throwPoints, $keyResult->getThrowPoints()); |
5668: | $impurePoints = array_merge($impurePoints, $keyResult->getImpurePoints()); |
5669: | $itemScope = $keyResult->getScope(); |
5670: | } |
5671: | |
5672: | $valueResult = $this->processExprNode($stmt, $arrayItem->value, $itemScope, $nodeCallback, $context->enterDeep()); |
5673: | $hasYield = $hasYield || $valueResult->hasYield(); |
5674: | $throwPoints = array_merge($throwPoints, $valueResult->getThrowPoints()); |
5675: | $impurePoints = array_merge($impurePoints, $valueResult->getImpurePoints()); |
5676: | |
5677: | if ($arrayItem->key === null) { |
5678: | $dimExpr = new Node\Scalar\Int_($i); |
5679: | } else { |
5680: | $dimExpr = $arrayItem->key; |
5681: | } |
5682: | $result = $this->processAssignVar( |
5683: | $scope, |
5684: | $stmt, |
5685: | $arrayItem->value, |
5686: | new GetOffsetValueTypeExpr($assignedExpr, $dimExpr), |
5687: | $nodeCallback, |
5688: | $context, |
5689: | static fn (MutatingScope $scope): ExpressionResult => new ExpressionResult($scope, false, [], []), |
5690: | $enterExpressionAssign, |
5691: | ); |
5692: | $scope = $result->getScope(); |
5693: | $hasYield = $hasYield || $result->hasYield(); |
5694: | $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); |
5695: | $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); |
5696: | } |
5697: | } elseif ($var instanceof ExistingArrayDimFetch) { |
5698: | $dimFetchStack = []; |
5699: | $assignedPropertyExpr = $assignedExpr; |
5700: | while ($var instanceof ExistingArrayDimFetch) { |
5701: | $varForSetOffsetValue = $var->getVar(); |
5702: | if ($varForSetOffsetValue instanceof PropertyFetch || $varForSetOffsetValue instanceof StaticPropertyFetch) { |
5703: | $varForSetOffsetValue = new OriginalPropertyTypeExpr($varForSetOffsetValue); |
5704: | } |
5705: | $assignedPropertyExpr = new SetExistingOffsetValueTypeExpr( |
5706: | $varForSetOffsetValue, |
5707: | $var->getDim(), |
5708: | $assignedPropertyExpr, |
5709: | ); |
5710: | $dimFetchStack[] = $var; |
5711: | $var = $var->getVar(); |
5712: | } |
5713: | |
5714: | $offsetTypes = []; |
5715: | $offsetNativeTypes = []; |
5716: | foreach (array_reverse($dimFetchStack) as $dimFetch) { |
5717: | $dimExpr = $dimFetch->getDim(); |
5718: | $offsetTypes[] = $scope->getType($dimExpr); |
5719: | $offsetNativeTypes[] = $scope->getNativeType($dimExpr); |
5720: | } |
5721: | |
5722: | $valueToWrite = $scope->getType($assignedExpr); |
5723: | $nativeValueToWrite = $scope->getNativeType($assignedExpr); |
5724: | $varType = $scope->getType($var); |
5725: | $varNativeType = $scope->getNativeType($var); |
5726: | |
5727: | $offsetValueType = $varType; |
5728: | $offsetNativeValueType = $varNativeType; |
5729: | $offsetValueTypeStack = [$offsetValueType]; |
5730: | $offsetValueNativeTypeStack = [$offsetNativeValueType]; |
5731: | foreach (array_slice($offsetTypes, 0, -1) as $offsetType) { |
5732: | $offsetValueType = $offsetValueType->getOffsetValueType($offsetType); |
5733: | $offsetValueTypeStack[] = $offsetValueType; |
5734: | } |
5735: | foreach (array_slice($offsetNativeTypes, 0, -1) as $offsetNativeType) { |
5736: | $offsetNativeValueType = $offsetNativeValueType->getOffsetValueType($offsetNativeType); |
5737: | $offsetValueNativeTypeStack[] = $offsetNativeValueType; |
5738: | } |
5739: | |
5740: | foreach (array_reverse($offsetTypes) as $offsetType) { |
5741: | |
5742: | $offsetValueType = array_pop($offsetValueTypeStack); |
5743: | $valueToWrite = $offsetValueType->setExistingOffsetValueType($offsetType, $valueToWrite); |
5744: | } |
5745: | foreach (array_reverse($offsetNativeTypes) as $offsetNativeType) { |
5746: | |
5747: | $offsetNativeValueType = array_pop($offsetValueNativeTypeStack); |
5748: | $nativeValueToWrite = $offsetNativeValueType->setExistingOffsetValueType($offsetNativeType, $nativeValueToWrite); |
5749: | } |
5750: | |
5751: | if ($var instanceof Variable && is_string($var->name)) { |
5752: | $nodeCallback(new VariableAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scope); |
5753: | $scope = $scope->assignVariable($var->name, $valueToWrite, $nativeValueToWrite, TrinaryLogic::createYes()); |
5754: | } else { |
5755: | if ($var instanceof PropertyFetch || $var instanceof StaticPropertyFetch) { |
5756: | $nodeCallback(new PropertyAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scope); |
5757: | } |
5758: | $scope = $scope->assignExpression( |
5759: | $var, |
5760: | $valueToWrite, |
5761: | $nativeValueToWrite, |
5762: | ); |
5763: | } |
5764: | } |
5765: | |
5766: | return new ExpressionResult($scope, $hasYield, $throwPoints, $impurePoints); |
5767: | } |
5768: | |
5769: | |
5770: | |
5771: | |
5772: | private function produceArrayDimFetchAssignValueToWrite(array $offsetTypes, Type $offsetValueType, Type $valueToWrite): Type |
5773: | { |
5774: | $offsetValueTypeStack = [$offsetValueType]; |
5775: | foreach (array_slice($offsetTypes, 0, -1) as $offsetType) { |
5776: | if ($offsetType === null) { |
5777: | $offsetValueType = new ConstantArrayType([], []); |
5778: | |
5779: | } else { |
5780: | $offsetValueType = $offsetValueType->getOffsetValueType($offsetType); |
5781: | if ($offsetValueType instanceof ErrorType) { |
5782: | $offsetValueType = new ConstantArrayType([], []); |
5783: | } |
5784: | } |
5785: | |
5786: | $offsetValueTypeStack[] = $offsetValueType; |
5787: | } |
5788: | |
5789: | foreach (array_reverse($offsetTypes) as $i => $offsetType) { |
5790: | |
5791: | $offsetValueType = array_pop($offsetValueTypeStack); |
5792: | if ( |
5793: | !$offsetValueType instanceof MixedType |
5794: | && !$offsetValueType->isConstantArray()->yes() |
5795: | ) { |
5796: | $types = [ |
5797: | new ArrayType(new MixedType(), new MixedType()), |
5798: | new ObjectType(ArrayAccess::class), |
5799: | new NullType(), |
5800: | ]; |
5801: | if ($offsetType !== null && $offsetType->isInteger()->yes()) { |
5802: | $types[] = new StringType(); |
5803: | } |
5804: | $offsetValueType = TypeCombinator::intersect($offsetValueType, TypeCombinator::union(...$types)); |
5805: | } |
5806: | $valueToWrite = $offsetValueType->setOffsetValueType($offsetType, $valueToWrite, $i === 0); |
5807: | } |
5808: | |
5809: | return $valueToWrite; |
5810: | } |
5811: | |
5812: | private function unwrapAssign(Expr $expr): Expr |
5813: | { |
5814: | if ($expr instanceof Assign) { |
5815: | return $this->unwrapAssign($expr->expr); |
5816: | } |
5817: | |
5818: | return $expr; |
5819: | } |
5820: | |
5821: | |
5822: | |
5823: | |
5824: | |
5825: | private function processSureTypesForConditionalExpressionsAfterAssign(Scope $scope, string $variableName, array $conditionalExpressions, SpecifiedTypes $specifiedTypes, Type $variableType): array |
5826: | { |
5827: | foreach ($specifiedTypes->getSureTypes() as $exprString => [$expr, $exprType]) { |
5828: | if (!$expr instanceof Variable) { |
5829: | continue; |
5830: | } |
5831: | if (!is_string($expr->name)) { |
5832: | continue; |
5833: | } |
5834: | |
5835: | if ($expr->name === $variableName) { |
5836: | continue; |
5837: | } |
5838: | |
5839: | if (!isset($conditionalExpressions[$exprString])) { |
5840: | $conditionalExpressions[$exprString] = []; |
5841: | } |
5842: | |
5843: | $holder = new ConditionalExpressionHolder([ |
5844: | '$' . $variableName => ExpressionTypeHolder::createYes(new Variable($variableName), $variableType), |
5845: | ], ExpressionTypeHolder::createYes( |
5846: | $expr, |
5847: | TypeCombinator::intersect($scope->getType($expr), $exprType), |
5848: | )); |
5849: | $conditionalExpressions[$exprString][$holder->getKey()] = $holder; |
5850: | } |
5851: | |
5852: | return $conditionalExpressions; |
5853: | } |
5854: | |
5855: | |
5856: | |
5857: | |
5858: | |
5859: | private function processSureNotTypesForConditionalExpressionsAfterAssign(Scope $scope, string $variableName, array $conditionalExpressions, SpecifiedTypes $specifiedTypes, Type $variableType): array |
5860: | { |
5861: | foreach ($specifiedTypes->getSureNotTypes() as $exprString => [$expr, $exprType]) { |
5862: | if (!$expr instanceof Variable) { |
5863: | continue; |
5864: | } |
5865: | if (!is_string($expr->name)) { |
5866: | continue; |
5867: | } |
5868: | |
5869: | if ($expr->name === $variableName) { |
5870: | continue; |
5871: | } |
5872: | |
5873: | if (!isset($conditionalExpressions[$exprString])) { |
5874: | $conditionalExpressions[$exprString] = []; |
5875: | } |
5876: | |
5877: | $holder = new ConditionalExpressionHolder([ |
5878: | '$' . $variableName => ExpressionTypeHolder::createYes(new Variable($variableName), $variableType), |
5879: | ], ExpressionTypeHolder::createYes( |
5880: | $expr, |
5881: | TypeCombinator::remove($scope->getType($expr), $exprType), |
5882: | )); |
5883: | $conditionalExpressions[$exprString][$holder->getKey()] = $holder; |
5884: | } |
5885: | |
5886: | return $conditionalExpressions; |
5887: | } |
5888: | |
5889: | |
5890: | |
5891: | |
5892: | private function processStmtVarAnnotation(MutatingScope $scope, Node\Stmt $stmt, ?Expr $defaultExpr, callable $nodeCallback): MutatingScope |
5893: | { |
5894: | $function = $scope->getFunction(); |
5895: | $variableLessTags = []; |
5896: | |
5897: | foreach ($stmt->getComments() as $comment) { |
5898: | if (!$comment instanceof Doc) { |
5899: | continue; |
5900: | } |
5901: | |
5902: | $resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc( |
5903: | $scope->getFile(), |
5904: | $scope->isInClass() ? $scope->getClassReflection()->getName() : null, |
5905: | $scope->isInTrait() ? $scope->getTraitReflection()->getName() : null, |
5906: | $function !== null ? $function->getName() : null, |
5907: | $comment->getText(), |
5908: | ); |
5909: | |
5910: | $assignedVariable = null; |
5911: | if ( |
5912: | $stmt instanceof Node\Stmt\Expression |
5913: | && ($stmt->expr instanceof Assign || $stmt->expr instanceof AssignRef) |
5914: | && $stmt->expr->var instanceof Variable |
5915: | && is_string($stmt->expr->var->name) |
5916: | ) { |
5917: | $assignedVariable = $stmt->expr->var->name; |
5918: | } |
5919: | |
5920: | foreach ($resolvedPhpDoc->getVarTags() as $name => $varTag) { |
5921: | if (is_int($name)) { |
5922: | $variableLessTags[] = $varTag; |
5923: | continue; |
5924: | } |
5925: | |
5926: | if ($name === $assignedVariable) { |
5927: | continue; |
5928: | } |
5929: | |
5930: | $certainty = $scope->hasVariableType($name); |
5931: | if ($certainty->no()) { |
5932: | continue; |
5933: | } |
5934: | |
5935: | if ($scope->isInClass() && $scope->getFunction() === null) { |
5936: | continue; |
5937: | } |
5938: | |
5939: | if ($scope->canAnyVariableExist()) { |
5940: | $certainty = TrinaryLogic::createYes(); |
5941: | } |
5942: | |
5943: | $variableNode = new Variable($name, $stmt->getAttributes()); |
5944: | $originalType = $scope->getVariableType($name); |
5945: | if (!$originalType->equals($varTag->getType())) { |
5946: | $nodeCallback(new VarTagChangedExpressionTypeNode($varTag, $variableNode), $scope); |
5947: | } |
5948: | |
5949: | $scope = $scope->assignVariable( |
5950: | $name, |
5951: | $varTag->getType(), |
5952: | $scope->getNativeType($variableNode), |
5953: | $certainty, |
5954: | ); |
5955: | } |
5956: | } |
5957: | |
5958: | if (count($variableLessTags) === 1 && $defaultExpr !== null) { |
5959: | $originalType = $scope->getType($defaultExpr); |
5960: | $varTag = $variableLessTags[0]; |
5961: | if (!$originalType->equals($varTag->getType())) { |
5962: | $nodeCallback(new VarTagChangedExpressionTypeNode($varTag, $defaultExpr), $scope); |
5963: | } |
5964: | $scope = $scope->assignExpression($defaultExpr, $varTag->getType(), new MixedType()); |
5965: | } |
5966: | |
5967: | return $scope; |
5968: | } |
5969: | |
5970: | |
5971: | |
5972: | |
5973: | private function processVarAnnotation(MutatingScope $scope, array $variableNames, Node\Stmt $node, bool &$changed = false): MutatingScope |
5974: | { |
5975: | $function = $scope->getFunction(); |
5976: | $varTags = []; |
5977: | foreach ($node->getComments() as $comment) { |
5978: | if (!$comment instanceof Doc) { |
5979: | continue; |
5980: | } |
5981: | |
5982: | $resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc( |
5983: | $scope->getFile(), |
5984: | $scope->isInClass() ? $scope->getClassReflection()->getName() : null, |
5985: | $scope->isInTrait() ? $scope->getTraitReflection()->getName() : null, |
5986: | $function !== null ? $function->getName() : null, |
5987: | $comment->getText(), |
5988: | ); |
5989: | foreach ($resolvedPhpDoc->getVarTags() as $key => $varTag) { |
5990: | $varTags[$key] = $varTag; |
5991: | } |
5992: | } |
5993: | |
5994: | if (count($varTags) === 0) { |
5995: | return $scope; |
5996: | } |
5997: | |
5998: | foreach ($variableNames as $variableName) { |
5999: | if (!isset($varTags[$variableName])) { |
6000: | continue; |
6001: | } |
6002: | |
6003: | $variableType = $varTags[$variableName]->getType(); |
6004: | $changed = true; |
6005: | $scope = $scope->assignVariable($variableName, $variableType, new MixedType(), TrinaryLogic::createYes()); |
6006: | } |
6007: | |
6008: | if (count($variableNames) === 1 && count($varTags) === 1 && isset($varTags[0])) { |
6009: | $variableType = $varTags[0]->getType(); |
6010: | $changed = true; |
6011: | $scope = $scope->assignVariable($variableNames[0], $variableType, new MixedType(), TrinaryLogic::createYes()); |
6012: | } |
6013: | |
6014: | return $scope; |
6015: | } |
6016: | |
6017: | private function enterForeach(MutatingScope $scope, MutatingScope $originalScope, Foreach_ $stmt): MutatingScope |
6018: | { |
6019: | if ($stmt->expr instanceof Variable && is_string($stmt->expr->name)) { |
6020: | $scope = $this->processVarAnnotation($scope, [$stmt->expr->name], $stmt); |
6021: | } |
6022: | $iterateeType = $originalScope->getType($stmt->expr); |
6023: | if ( |
6024: | ($stmt->valueVar instanceof Variable && is_string($stmt->valueVar->name)) |
6025: | && ($stmt->keyVar === null || ($stmt->keyVar instanceof Variable && is_string($stmt->keyVar->name))) |
6026: | ) { |
6027: | $keyVarName = null; |
6028: | if ($stmt->keyVar instanceof Variable && is_string($stmt->keyVar->name)) { |
6029: | $keyVarName = $stmt->keyVar->name; |
6030: | } |
6031: | $scope = $scope->enterForeach( |
6032: | $originalScope, |
6033: | $stmt->expr, |
6034: | $stmt->valueVar->name, |
6035: | $keyVarName, |
6036: | ); |
6037: | $vars = [$stmt->valueVar->name]; |
6038: | if ($keyVarName !== null) { |
6039: | $vars[] = $keyVarName; |
6040: | } |
6041: | } else { |
6042: | $scope = $this->processAssignVar( |
6043: | $scope, |
6044: | $stmt, |
6045: | $stmt->valueVar, |
6046: | new GetIterableValueTypeExpr($stmt->expr), |
6047: | static function (): void { |
6048: | }, |
6049: | ExpressionContext::createDeep(), |
6050: | static fn (MutatingScope $scope): ExpressionResult => new ExpressionResult($scope, false, [], []), |
6051: | true, |
6052: | )->getScope(); |
6053: | $vars = $this->getAssignedVariables($stmt->valueVar); |
6054: | if ( |
6055: | $stmt->keyVar instanceof Variable && is_string($stmt->keyVar->name) |
6056: | ) { |
6057: | $scope = $scope->enterForeachKey($originalScope, $stmt->expr, $stmt->keyVar->name); |
6058: | $vars[] = $stmt->keyVar->name; |
6059: | } elseif ($stmt->keyVar !== null) { |
6060: | $scope = $this->processAssignVar( |
6061: | $scope, |
6062: | $stmt, |
6063: | $stmt->keyVar, |
6064: | new GetIterableKeyTypeExpr($stmt->expr), |
6065: | static function (): void { |
6066: | }, |
6067: | ExpressionContext::createDeep(), |
6068: | static fn (MutatingScope $scope): ExpressionResult => new ExpressionResult($scope, false, [], []), |
6069: | true, |
6070: | )->getScope(); |
6071: | $vars = array_merge($vars, $this->getAssignedVariables($stmt->keyVar)); |
6072: | } |
6073: | } |
6074: | |
6075: | $constantArrays = $iterateeType->getConstantArrays(); |
6076: | if ( |
6077: | $stmt->getDocComment() === null |
6078: | && $iterateeType->isConstantArray()->yes() |
6079: | && count($constantArrays) === 1 |
6080: | && $stmt->valueVar instanceof Variable && is_string($stmt->valueVar->name) |
6081: | && $stmt->keyVar instanceof Variable && is_string($stmt->keyVar->name) |
6082: | ) { |
6083: | $valueConditionalHolders = []; |
6084: | $arrayDimFetchConditionalHolders = []; |
6085: | foreach ($constantArrays[0]->getKeyTypes() as $i => $keyType) { |
6086: | $valueType = $constantArrays[0]->getValueTypes()[$i]; |
6087: | $holder = new ConditionalExpressionHolder([ |
6088: | '$' . $stmt->keyVar->name => ExpressionTypeHolder::createYes(new Variable($stmt->keyVar->name), $keyType), |
6089: | ], new ExpressionTypeHolder($stmt->valueVar, $valueType, TrinaryLogic::createYes())); |
6090: | $valueConditionalHolders[$holder->getKey()] = $holder; |
6091: | $arrayDimFetchHolder = new ConditionalExpressionHolder([ |
6092: | '$' . $stmt->keyVar->name => ExpressionTypeHolder::createYes(new Variable($stmt->keyVar->name), $keyType), |
6093: | ], new ExpressionTypeHolder(new ArrayDimFetch($stmt->expr, $stmt->keyVar), $valueType, TrinaryLogic::createYes())); |
6094: | $arrayDimFetchConditionalHolders[$arrayDimFetchHolder->getKey()] = $arrayDimFetchHolder; |
6095: | } |
6096: | |
6097: | $scope = $scope->addConditionalExpressions( |
6098: | '$' . $stmt->valueVar->name, |
6099: | $valueConditionalHolders, |
6100: | ); |
6101: | if ($stmt->expr instanceof Variable && is_string($stmt->expr->name)) { |
6102: | $scope = $scope->addConditionalExpressions( |
6103: | sprintf('$%s[$%s]', $stmt->expr->name, $stmt->keyVar->name), |
6104: | $arrayDimFetchConditionalHolders, |
6105: | ); |
6106: | } |
6107: | } |
6108: | |
6109: | return $this->processVarAnnotation($scope, $vars, $stmt); |
6110: | } |
6111: | |
6112: | |
6113: | |
6114: | |
6115: | private function processTraitUse(Node\Stmt\TraitUse $node, MutatingScope $classScope, callable $nodeCallback): void |
6116: | { |
6117: | $parentTraitNames = []; |
6118: | $parent = $classScope->getParentScope(); |
6119: | while ($parent !== null) { |
6120: | if ($parent->isInTrait()) { |
6121: | $parentTraitNames[] = $parent->getTraitReflection()->getName(); |
6122: | } |
6123: | $parent = $parent->getParentScope(); |
6124: | } |
6125: | |
6126: | foreach ($node->traits as $trait) { |
6127: | $traitName = (string) $trait; |
6128: | if (in_array($traitName, $parentTraitNames, true)) { |
6129: | continue; |
6130: | } |
6131: | if (!$this->reflectionProvider->hasClass($traitName)) { |
6132: | continue; |
6133: | } |
6134: | $traitReflection = $this->reflectionProvider->getClass($traitName); |
6135: | $traitFileName = $traitReflection->getFileName(); |
6136: | if ($traitFileName === null) { |
6137: | continue; |
6138: | } |
6139: | $fileName = $this->fileHelper->normalizePath($traitFileName); |
6140: | if (!isset($this->analysedFiles[$fileName])) { |
6141: | continue; |
6142: | } |
6143: | $adaptations = []; |
6144: | foreach ($node->adaptations as $adaptation) { |
6145: | if ($adaptation->trait === null) { |
6146: | $adaptations[] = $adaptation; |
6147: | continue; |
6148: | } |
6149: | if ($adaptation->trait->toLowerString() !== $trait->toLowerString()) { |
6150: | continue; |
6151: | } |
6152: | |
6153: | $adaptations[] = $adaptation; |
6154: | } |
6155: | $parserNodes = $this->parser->parseFile($fileName); |
6156: | $this->processNodesForTraitUse($parserNodes, $traitReflection, $classScope, $adaptations, $nodeCallback); |
6157: | } |
6158: | } |
6159: | |
6160: | |
6161: | |
6162: | |
6163: | |
6164: | |
6165: | private function processNodesForTraitUse($node, ClassReflection $traitReflection, MutatingScope $scope, array $adaptations, callable $nodeCallback): void |
6166: | { |
6167: | if ($node instanceof Node) { |
6168: | if ($node instanceof Node\Stmt\Trait_ && $traitReflection->getName() === (string) $node->namespacedName && $traitReflection->getNativeReflection()->getStartLine() === $node->getStartLine()) { |
6169: | $methodModifiers = []; |
6170: | $methodNames = []; |
6171: | foreach ($adaptations as $adaptation) { |
6172: | if (!$adaptation instanceof Node\Stmt\TraitUseAdaptation\Alias) { |
6173: | continue; |
6174: | } |
6175: | |
6176: | $methodName = $adaptation->method->toLowerString(); |
6177: | if ($adaptation->newModifier !== null) { |
6178: | $methodModifiers[$methodName] = $adaptation->newModifier; |
6179: | } |
6180: | |
6181: | if ($adaptation->newName === null) { |
6182: | continue; |
6183: | } |
6184: | |
6185: | $methodNames[$methodName] = $adaptation->newName; |
6186: | } |
6187: | |
6188: | $stmts = $node->stmts; |
6189: | foreach ($stmts as $i => $stmt) { |
6190: | if (!$stmt instanceof Node\Stmt\ClassMethod) { |
6191: | continue; |
6192: | } |
6193: | $methodName = $stmt->name->toLowerString(); |
6194: | $methodAst = clone $stmt; |
6195: | $stmts[$i] = $methodAst; |
6196: | if (array_key_exists($methodName, $methodModifiers)) { |
6197: | $methodAst->flags = ($methodAst->flags & ~ Modifiers::VISIBILITY_MASK) | $methodModifiers[$methodName]; |
6198: | } |
6199: | |
6200: | if (!array_key_exists($methodName, $methodNames)) { |
6201: | continue; |
6202: | } |
6203: | |
6204: | $methodAst->setAttribute('originalTraitMethodName', $methodAst->name->toLowerString()); |
6205: | $methodAst->name = $methodNames[$methodName]; |
6206: | } |
6207: | |
6208: | if (!$scope->isInClass()) { |
6209: | throw new ShouldNotHappenException(); |
6210: | } |
6211: | $traitScope = $scope->enterTrait($traitReflection); |
6212: | $nodeCallback(new InTraitNode($node, $traitReflection, $scope->getClassReflection()), $traitScope); |
6213: | $this->processStmtNodes($node, $stmts, $traitScope, $nodeCallback, StatementContext::createTopLevel()); |
6214: | return; |
6215: | } |
6216: | if ($node instanceof Node\Stmt\ClassLike) { |
6217: | return; |
6218: | } |
6219: | if ($node instanceof Node\FunctionLike) { |
6220: | return; |
6221: | } |
6222: | foreach ($node->getSubNodeNames() as $subNodeName) { |
6223: | $subNode = $node->{$subNodeName}; |
6224: | $this->processNodesForTraitUse($subNode, $traitReflection, $scope, $adaptations, $nodeCallback); |
6225: | } |
6226: | } elseif (is_array($node)) { |
6227: | foreach ($node as $subNode) { |
6228: | $this->processNodesForTraitUse($subNode, $traitReflection, $scope, $adaptations, $nodeCallback); |
6229: | } |
6230: | } |
6231: | } |
6232: | |
6233: | private function processCalledMethod(MethodReflection $methodReflection): ?MutatingScope |
6234: | { |
6235: | $declaringClass = $methodReflection->getDeclaringClass(); |
6236: | if ($declaringClass->isAnonymous()) { |
6237: | return null; |
6238: | } |
6239: | if ($declaringClass->getFileName() === null) { |
6240: | return null; |
6241: | } |
6242: | |
6243: | $stackName = sprintf('%s::%s', $declaringClass->getName(), $methodReflection->getName()); |
6244: | if (array_key_exists($stackName, $this->calledMethodResults)) { |
6245: | return $this->calledMethodResults[$stackName]; |
6246: | } |
6247: | |
6248: | if (array_key_exists($stackName, $this->calledMethodStack)) { |
6249: | return null; |
6250: | } |
6251: | |
6252: | if (count($this->calledMethodStack) > 0) { |
6253: | return null; |
6254: | } |
6255: | |
6256: | $this->calledMethodStack[$stackName] = true; |
6257: | |
6258: | $fileName = $this->fileHelper->normalizePath($declaringClass->getFileName()); |
6259: | if (!isset($this->analysedFiles[$fileName])) { |
6260: | return null; |
6261: | } |
6262: | $parserNodes = $this->parser->parseFile($fileName); |
6263: | |
6264: | $returnStatement = null; |
6265: | $this->processNodesForCalledMethod($parserNodes, $fileName, $methodReflection, static function (Node $node, Scope $scope) use ($methodReflection, &$returnStatement): void { |
6266: | if (!$node instanceof MethodReturnStatementsNode) { |
6267: | return; |
6268: | } |
6269: | |
6270: | if ($node->getClassReflection()->getName() !== $methodReflection->getDeclaringClass()->getName()) { |
6271: | return; |
6272: | } |
6273: | |
6274: | if ($returnStatement !== null) { |
6275: | return; |
6276: | } |
6277: | |
6278: | $returnStatement = $node; |
6279: | }); |
6280: | |
6281: | $calledMethodEndScope = null; |
6282: | if ($returnStatement !== null) { |
6283: | foreach ($returnStatement->getExecutionEnds() as $executionEnd) { |
6284: | $statementResult = $executionEnd->getStatementResult(); |
6285: | $endNode = $executionEnd->getNode(); |
6286: | if ($endNode instanceof Node\Stmt\Expression) { |
6287: | $exprType = $statementResult->getScope()->getType($endNode->expr); |
6288: | if ($exprType instanceof NeverType && $exprType->isExplicit()) { |
6289: | continue; |
6290: | } |
6291: | } |
6292: | if ($calledMethodEndScope === null) { |
6293: | $calledMethodEndScope = $statementResult->getScope(); |
6294: | continue; |
6295: | } |
6296: | |
6297: | $calledMethodEndScope = $calledMethodEndScope->mergeWith($statementResult->getScope()); |
6298: | } |
6299: | foreach ($returnStatement->getReturnStatements() as $statement) { |
6300: | if ($calledMethodEndScope === null) { |
6301: | $calledMethodEndScope = $statement->getScope(); |
6302: | continue; |
6303: | } |
6304: | |
6305: | $calledMethodEndScope = $calledMethodEndScope->mergeWith($statement->getScope()); |
6306: | } |
6307: | } |
6308: | |
6309: | unset($this->calledMethodStack[$stackName]); |
6310: | |
6311: | $this->calledMethodResults[$stackName] = $calledMethodEndScope; |
6312: | |
6313: | return $calledMethodEndScope; |
6314: | } |
6315: | |
6316: | |
6317: | |
6318: | |
6319: | |
6320: | private function processNodesForCalledMethod($node, string $fileName, MethodReflection $methodReflection, callable $nodeCallback): void |
6321: | { |
6322: | if ($node instanceof Node) { |
6323: | $declaringClass = $methodReflection->getDeclaringClass(); |
6324: | if ( |
6325: | $node instanceof Node\Stmt\Class_ |
6326: | && $node->namespacedName !== null |
6327: | && $declaringClass->getName() === (string) $node->namespacedName |
6328: | && $declaringClass->getNativeReflection()->getStartLine() === $node->getStartLine() |
6329: | ) { |
6330: | |
6331: | $stmts = $node->stmts; |
6332: | foreach ($stmts as $stmt) { |
6333: | if (!$stmt instanceof Node\Stmt\ClassMethod) { |
6334: | continue; |
6335: | } |
6336: | |
6337: | if ($stmt->name->toString() !== $methodReflection->getName()) { |
6338: | continue; |
6339: | } |
6340: | |
6341: | if ($stmt->getEndLine() - $stmt->getStartLine() > 50) { |
6342: | continue; |
6343: | } |
6344: | |
6345: | $scope = $this->scopeFactory->create(ScopeContext::create($fileName))->enterClass($declaringClass); |
6346: | $this->processStmtNode($stmt, $scope, $nodeCallback, StatementContext::createTopLevel()); |
6347: | } |
6348: | return; |
6349: | } |
6350: | if ($node instanceof Node\Stmt\ClassLike) { |
6351: | return; |
6352: | } |
6353: | if ($node instanceof Node\FunctionLike) { |
6354: | return; |
6355: | } |
6356: | foreach ($node->getSubNodeNames() as $subNodeName) { |
6357: | $subNode = $node->{$subNodeName}; |
6358: | $this->processNodesForCalledMethod($subNode, $fileName, $methodReflection, $nodeCallback); |
6359: | } |
6360: | } elseif (is_array($node)) { |
6361: | foreach ($node as $subNode) { |
6362: | $this->processNodesForCalledMethod($subNode, $fileName, $methodReflection, $nodeCallback); |
6363: | } |
6364: | } |
6365: | } |
6366: | |
6367: | |
6368: | |
6369: | |
6370: | public function getPhpDocs(Scope $scope, Node\FunctionLike|Node\Stmt\Property $node): array |
6371: | { |
6372: | $templateTypeMap = TemplateTypeMap::createEmpty(); |
6373: | $phpDocParameterTypes = []; |
6374: | $phpDocImmediatelyInvokedCallableParameters = []; |
6375: | $phpDocClosureThisTypeParameters = []; |
6376: | $phpDocReturnType = null; |
6377: | $phpDocThrowType = null; |
6378: | $deprecatedDescription = null; |
6379: | $isDeprecated = false; |
6380: | $isInternal = false; |
6381: | $isFinal = false; |
6382: | $isPure = null; |
6383: | $isAllowedPrivateMutation = false; |
6384: | $acceptsNamedArguments = true; |
6385: | $isReadOnly = $scope->isInClass() && $scope->getClassReflection()->isImmutable(); |
6386: | $asserts = Assertions::createEmpty(); |
6387: | $selfOutType = null; |
6388: | $docComment = $node->getDocComment() !== null |
6389: | ? $node->getDocComment()->getText() |
6390: | : null; |
6391: | |
6392: | $file = $scope->getFile(); |
6393: | $class = $scope->isInClass() ? $scope->getClassReflection()->getName() : null; |
6394: | $trait = $scope->isInTrait() ? $scope->getTraitReflection()->getName() : null; |
6395: | $resolvedPhpDoc = null; |
6396: | $functionName = null; |
6397: | $phpDocParameterOutTypes = []; |
6398: | |
6399: | if ($node instanceof Node\Stmt\ClassMethod) { |
6400: | if (!$scope->isInClass()) { |
6401: | throw new ShouldNotHappenException(); |
6402: | } |
6403: | $functionName = $node->name->name; |
6404: | $positionalParameterNames = array_map(static function (Node\Param $param): string { |
6405: | if (!$param->var instanceof Variable || !is_string($param->var->name)) { |
6406: | throw new ShouldNotHappenException(); |
6407: | } |
6408: | |
6409: | return $param->var->name; |
6410: | }, $node->getParams()); |
6411: | $resolvedPhpDoc = $this->phpDocInheritanceResolver->resolvePhpDocForMethod( |
6412: | $docComment, |
6413: | $file, |
6414: | $scope->getClassReflection(), |
6415: | $trait, |
6416: | $node->name->name, |
6417: | $positionalParameterNames, |
6418: | ); |
6419: | |
6420: | if ($node->name->toLowerString() === '__construct') { |
6421: | foreach ($node->params as $param) { |
6422: | if ($param->flags === 0) { |
6423: | continue; |
6424: | } |
6425: | |
6426: | if ($param->getDocComment() === null) { |
6427: | continue; |
6428: | } |
6429: | |
6430: | if ( |
6431: | !$param->var instanceof Variable |
6432: | || !is_string($param->var->name) |
6433: | ) { |
6434: | throw new ShouldNotHappenException(); |
6435: | } |
6436: | |
6437: | $paramPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc( |
6438: | $file, |
6439: | $class, |
6440: | $trait, |
6441: | '__construct', |
6442: | $param->getDocComment()->getText(), |
6443: | ); |
6444: | $varTags = $paramPhpDoc->getVarTags(); |
6445: | if (isset($varTags[0]) && count($varTags) === 1) { |
6446: | $phpDocType = $varTags[0]->getType(); |
6447: | } elseif (isset($varTags[$param->var->name])) { |
6448: | $phpDocType = $varTags[$param->var->name]->getType(); |
6449: | } else { |
6450: | continue; |
6451: | } |
6452: | |
6453: | $phpDocParameterTypes[$param->var->name] = $phpDocType; |
6454: | } |
6455: | } |
6456: | } elseif ($node instanceof Node\Stmt\Function_) { |
6457: | $functionName = trim($scope->getNamespace() . '\\' . $node->name->name, '\\'); |
6458: | } elseif ($node instanceof Node\PropertyHook) { |
6459: | $propertyName = $node->getAttribute('propertyName'); |
6460: | if ($propertyName !== null) { |
6461: | $functionName = sprintf('$%s::%s', $propertyName, $node->name->toString()); |
6462: | } |
6463: | } |
6464: | |
6465: | if ($docComment !== null && $resolvedPhpDoc === null) { |
6466: | $resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc( |
6467: | $file, |
6468: | $class, |
6469: | $trait, |
6470: | $functionName, |
6471: | $docComment, |
6472: | ); |
6473: | } |
6474: | |
6475: | $varTags = []; |
6476: | if ($resolvedPhpDoc !== null) { |
6477: | $templateTypeMap = $resolvedPhpDoc->getTemplateTypeMap(); |
6478: | $phpDocImmediatelyInvokedCallableParameters = $resolvedPhpDoc->getParamsImmediatelyInvokedCallable(); |
6479: | foreach ($resolvedPhpDoc->getParamTags() as $paramName => $paramTag) { |
6480: | if (array_key_exists($paramName, $phpDocParameterTypes)) { |
6481: | continue; |
6482: | } |
6483: | $paramType = $paramTag->getType(); |
6484: | if ($scope->isInClass()) { |
6485: | $paramType = $this->transformStaticType($scope->getClassReflection(), $paramType); |
6486: | } |
6487: | $phpDocParameterTypes[$paramName] = $paramType; |
6488: | } |
6489: | foreach ($resolvedPhpDoc->getParamClosureThisTags() as $paramName => $paramClosureThisTag) { |
6490: | if (array_key_exists($paramName, $phpDocClosureThisTypeParameters)) { |
6491: | continue; |
6492: | } |
6493: | $paramClosureThisType = $paramClosureThisTag->getType(); |
6494: | if ($scope->isInClass()) { |
6495: | $paramClosureThisType = $this->transformStaticType($scope->getClassReflection(), $paramClosureThisType); |
6496: | } |
6497: | $phpDocClosureThisTypeParameters[$paramName] = $paramClosureThisType; |
6498: | } |
6499: | |
6500: | foreach ($resolvedPhpDoc->getParamOutTags() as $paramName => $paramOutTag) { |
6501: | $phpDocParameterOutTypes[$paramName] = $paramOutTag->getType(); |
6502: | } |
6503: | if ($node instanceof Node\FunctionLike) { |
6504: | $nativeReturnType = $scope->getFunctionType($node->getReturnType(), false, false); |
6505: | $phpDocReturnType = $this->getPhpDocReturnType($resolvedPhpDoc, $nativeReturnType); |
6506: | if ($phpDocReturnType !== null && $scope->isInClass()) { |
6507: | $phpDocReturnType = $this->transformStaticType($scope->getClassReflection(), $phpDocReturnType); |
6508: | } |
6509: | } |
6510: | $phpDocThrowType = $resolvedPhpDoc->getThrowsTag() !== null ? $resolvedPhpDoc->getThrowsTag()->getType() : null; |
6511: | $deprecatedDescription = $resolvedPhpDoc->getDeprecatedTag() !== null ? $resolvedPhpDoc->getDeprecatedTag()->getMessage() : null; |
6512: | $isDeprecated = $resolvedPhpDoc->isDeprecated(); |
6513: | $isInternal = $resolvedPhpDoc->isInternal(); |
6514: | $isFinal = $resolvedPhpDoc->isFinal(); |
6515: | $isPure = $resolvedPhpDoc->isPure(); |
6516: | $isAllowedPrivateMutation = $resolvedPhpDoc->isAllowedPrivateMutation(); |
6517: | $acceptsNamedArguments = $resolvedPhpDoc->acceptsNamedArguments(); |
6518: | if ($acceptsNamedArguments && $scope->isInClass()) { |
6519: | $acceptsNamedArguments = $scope->getClassReflection()->acceptsNamedArguments(); |
6520: | } |
6521: | $isReadOnly = $isReadOnly || $resolvedPhpDoc->isReadOnly(); |
6522: | $asserts = Assertions::createFromResolvedPhpDocBlock($resolvedPhpDoc); |
6523: | $selfOutType = $resolvedPhpDoc->getSelfOutTag() !== null ? $resolvedPhpDoc->getSelfOutTag()->getType() : null; |
6524: | $varTags = $resolvedPhpDoc->getVarTags(); |
6525: | } |
6526: | |
6527: | return [$templateTypeMap, $phpDocParameterTypes, $phpDocImmediatelyInvokedCallableParameters, $phpDocClosureThisTypeParameters, $phpDocReturnType, $phpDocThrowType, $deprecatedDescription, $isDeprecated, $isInternal, $isFinal, $isPure, $acceptsNamedArguments, $isReadOnly, $docComment, $asserts, $selfOutType, $phpDocParameterOutTypes, $varTags, $isAllowedPrivateMutation]; |
6528: | } |
6529: | |
6530: | private function transformStaticType(ClassReflection $declaringClass, Type $type): Type |
6531: | { |
6532: | return TypeTraverser::map($type, static function (Type $type, callable $traverse) use ($declaringClass): Type { |
6533: | if ($type instanceof StaticType) { |
6534: | $changedType = $type->changeBaseClass($declaringClass); |
6535: | if ($declaringClass->isFinal() && !$type instanceof ThisType) { |
6536: | $changedType = $changedType->getStaticObjectType(); |
6537: | } |
6538: | return $traverse($changedType); |
6539: | } |
6540: | |
6541: | return $traverse($type); |
6542: | }); |
6543: | } |
6544: | |
6545: | private function getPhpDocReturnType(ResolvedPhpDocBlock $resolvedPhpDoc, Type $nativeReturnType): ?Type |
6546: | { |
6547: | $returnTag = $resolvedPhpDoc->getReturnTag(); |
6548: | |
6549: | if ($returnTag === null) { |
6550: | return null; |
6551: | } |
6552: | |
6553: | $phpDocReturnType = $returnTag->getType(); |
6554: | |
6555: | if ($returnTag->isExplicit()) { |
6556: | return $phpDocReturnType; |
6557: | } |
6558: | |
6559: | if ($nativeReturnType->isSuperTypeOf(TemplateTypeHelper::resolveToBounds($phpDocReturnType))->yes()) { |
6560: | return $phpDocReturnType; |
6561: | } |
6562: | |
6563: | return null; |
6564: | } |
6565: | |
6566: | |
6567: | |
6568: | |
6569: | |
6570: | private function getNextUnreachableStatements(array $nodes, bool $earlyBinding): array |
6571: | { |
6572: | $stmts = []; |
6573: | $isPassedUnreachableStatement = false; |
6574: | foreach ($nodes as $node) { |
6575: | if ($earlyBinding && ($node instanceof Node\Stmt\Function_ || $node instanceof Node\Stmt\ClassLike || $node instanceof Node\Stmt\HaltCompiler)) { |
6576: | continue; |
6577: | } |
6578: | if ($isPassedUnreachableStatement && $node instanceof Node\Stmt) { |
6579: | $stmts[] = $node; |
6580: | continue; |
6581: | } |
6582: | if ($node instanceof Node\Stmt\Nop) { |
6583: | continue; |
6584: | } |
6585: | if (!$node instanceof Node\Stmt) { |
6586: | continue; |
6587: | } |
6588: | $stmts[] = $node; |
6589: | $isPassedUnreachableStatement = true; |
6590: | } |
6591: | return $stmts; |
6592: | } |
6593: | |
6594: | } |
6595: | |