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