1: | <?php declare(strict_types = 1); |
2: | |
3: | namespace PHPStan\Analyser; |
4: | |
5: | use Countable; |
6: | use PhpParser\Node; |
7: | use PhpParser\Node\Expr; |
8: | use PhpParser\Node\Expr\ArrayDimFetch; |
9: | use PhpParser\Node\Expr\BinaryOp\BooleanAnd; |
10: | use PhpParser\Node\Expr\BinaryOp\BooleanOr; |
11: | use PhpParser\Node\Expr\BinaryOp\LogicalAnd; |
12: | use PhpParser\Node\Expr\BinaryOp\LogicalOr; |
13: | use PhpParser\Node\Expr\ClassConstFetch; |
14: | use PhpParser\Node\Expr\ConstFetch; |
15: | use PhpParser\Node\Expr\FuncCall; |
16: | use PhpParser\Node\Expr\Instanceof_; |
17: | use PhpParser\Node\Expr\MethodCall; |
18: | use PhpParser\Node\Expr\PropertyFetch; |
19: | use PhpParser\Node\Expr\StaticCall; |
20: | use PhpParser\Node\Expr\StaticPropertyFetch; |
21: | use PhpParser\Node\Name; |
22: | use PHPStan\Node\Expr\AlwaysRememberedExpr; |
23: | use PHPStan\Node\IssetExpr; |
24: | use PHPStan\Node\Printer\ExprPrinter; |
25: | use PHPStan\Php\PhpVersion; |
26: | use PHPStan\Reflection\Assertions; |
27: | use PHPStan\Reflection\ExtendedParametersAcceptor; |
28: | use PHPStan\Reflection\ParametersAcceptor; |
29: | use PHPStan\Reflection\ParametersAcceptorSelector; |
30: | use PHPStan\Reflection\ReflectionProvider; |
31: | use PHPStan\Reflection\ResolvedFunctionVariant; |
32: | use PHPStan\Rules\Arrays\AllowedArrayKeysTypes; |
33: | use PHPStan\ShouldNotHappenException; |
34: | use PHPStan\TrinaryLogic; |
35: | use PHPStan\Type\Accessory\AccessoryArrayListType; |
36: | use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; |
37: | use PHPStan\Type\Accessory\AccessoryNonFalsyStringType; |
38: | use PHPStan\Type\Accessory\HasOffsetType; |
39: | use PHPStan\Type\Accessory\HasPropertyType; |
40: | use PHPStan\Type\Accessory\NonEmptyArrayType; |
41: | use PHPStan\Type\ArrayType; |
42: | use PHPStan\Type\BooleanType; |
43: | use PHPStan\Type\ConditionalTypeForParameter; |
44: | use PHPStan\Type\Constant\ConstantArrayType; |
45: | use PHPStan\Type\Constant\ConstantArrayTypeBuilder; |
46: | use PHPStan\Type\Constant\ConstantBooleanType; |
47: | use PHPStan\Type\Constant\ConstantFloatType; |
48: | use PHPStan\Type\Constant\ConstantIntegerType; |
49: | use PHPStan\Type\Constant\ConstantStringType; |
50: | use PHPStan\Type\ConstantScalarType; |
51: | use PHPStan\Type\FloatType; |
52: | use PHPStan\Type\FunctionTypeSpecifyingExtension; |
53: | use PHPStan\Type\Generic\GenericClassStringType; |
54: | use PHPStan\Type\Generic\TemplateType; |
55: | use PHPStan\Type\Generic\TemplateTypeHelper; |
56: | use PHPStan\Type\Generic\TemplateTypeVariance; |
57: | use PHPStan\Type\Generic\TemplateTypeVarianceMap; |
58: | use PHPStan\Type\IntegerRangeType; |
59: | use PHPStan\Type\IntegerType; |
60: | use PHPStan\Type\IntersectionType; |
61: | use PHPStan\Type\MethodTypeSpecifyingExtension; |
62: | use PHPStan\Type\MixedType; |
63: | use PHPStan\Type\NeverType; |
64: | use PHPStan\Type\NonexistentParentClassType; |
65: | use PHPStan\Type\NullType; |
66: | use PHPStan\Type\ObjectType; |
67: | use PHPStan\Type\ObjectWithoutClassType; |
68: | use PHPStan\Type\ResourceType; |
69: | use PHPStan\Type\StaticMethodTypeSpecifyingExtension; |
70: | use PHPStan\Type\StaticType; |
71: | use PHPStan\Type\StaticTypeFactory; |
72: | use PHPStan\Type\StringType; |
73: | use PHPStan\Type\Type; |
74: | use PHPStan\Type\TypeCombinator; |
75: | use PHPStan\Type\TypeTraverser; |
76: | use PHPStan\Type\UnionType; |
77: | use function array_key_exists; |
78: | use function array_map; |
79: | use function array_merge; |
80: | use function array_reverse; |
81: | use function array_shift; |
82: | use function count; |
83: | use function in_array; |
84: | use function is_string; |
85: | use function strtolower; |
86: | use function substr; |
87: | use const COUNT_NORMAL; |
88: | |
89: | final class TypeSpecifier |
90: | { |
91: | |
92: | |
93: | private ?array $methodTypeSpecifyingExtensionsByClass = null; |
94: | |
95: | |
96: | private ?array $staticMethodTypeSpecifyingExtensionsByClass = null; |
97: | |
98: | |
99: | |
100: | |
101: | |
102: | |
103: | public function __construct( |
104: | private ExprPrinter $exprPrinter, |
105: | private ReflectionProvider $reflectionProvider, |
106: | private PhpVersion $phpVersion, |
107: | private array $functionTypeSpecifyingExtensions, |
108: | private array $methodTypeSpecifyingExtensions, |
109: | private array $staticMethodTypeSpecifyingExtensions, |
110: | private bool $rememberPossiblyImpureFunctionValues, |
111: | ) |
112: | { |
113: | foreach (array_merge($functionTypeSpecifyingExtensions, $methodTypeSpecifyingExtensions, $staticMethodTypeSpecifyingExtensions) as $extension) { |
114: | if (!($extension instanceof TypeSpecifierAwareExtension)) { |
115: | continue; |
116: | } |
117: | |
118: | $extension->setTypeSpecifier($this); |
119: | } |
120: | } |
121: | |
122: | |
123: | public function specifyTypesInCondition( |
124: | Scope $scope, |
125: | Expr $expr, |
126: | TypeSpecifierContext $context, |
127: | ): SpecifiedTypes |
128: | { |
129: | if ($expr instanceof Expr\CallLike && $expr->isFirstClassCallable()) { |
130: | return (new SpecifiedTypes([], []))->setRootExpr($expr); |
131: | } |
132: | |
133: | if ($expr instanceof Instanceof_) { |
134: | $exprNode = $expr->expr; |
135: | if ($expr->class instanceof Name) { |
136: | $className = (string) $expr->class; |
137: | $lowercasedClassName = strtolower($className); |
138: | if ($lowercasedClassName === 'self' && $scope->isInClass()) { |
139: | $type = new ObjectType($scope->getClassReflection()->getName()); |
140: | } elseif ($lowercasedClassName === 'static' && $scope->isInClass()) { |
141: | $type = new StaticType($scope->getClassReflection()); |
142: | } elseif ($lowercasedClassName === 'parent') { |
143: | if ( |
144: | $scope->isInClass() |
145: | && $scope->getClassReflection()->getParentClass() !== null |
146: | ) { |
147: | $type = new ObjectType($scope->getClassReflection()->getParentClass()->getName()); |
148: | } else { |
149: | $type = new NonexistentParentClassType(); |
150: | } |
151: | } else { |
152: | $type = new ObjectType($className); |
153: | } |
154: | return $this->create($exprNode, $type, $context, $scope)->setRootExpr($expr); |
155: | } |
156: | |
157: | $classType = $scope->getType($expr->class); |
158: | $uncertainty = false; |
159: | $type = TypeTraverser::map($classType, static function (Type $type, callable $traverse) use (&$uncertainty): Type { |
160: | if ($type instanceof UnionType || $type instanceof IntersectionType) { |
161: | return $traverse($type); |
162: | } |
163: | if ($type->getObjectClassNames() !== []) { |
164: | $uncertainty = true; |
165: | return $type; |
166: | } |
167: | if ($type instanceof GenericClassStringType) { |
168: | $uncertainty = true; |
169: | return $type->getGenericType(); |
170: | } |
171: | if ($type instanceof ConstantStringType) { |
172: | return new ObjectType($type->getValue()); |
173: | } |
174: | return new MixedType(); |
175: | }); |
176: | |
177: | if (!$type->isSuperTypeOf(new MixedType())->yes()) { |
178: | if ($context->true()) { |
179: | $type = TypeCombinator::intersect( |
180: | $type, |
181: | new ObjectWithoutClassType(), |
182: | ); |
183: | return $this->create($exprNode, $type, $context, $scope)->setRootExpr($expr); |
184: | } elseif ($context->false() && !$uncertainty) { |
185: | $exprType = $scope->getType($expr->expr); |
186: | if (!$type->isSuperTypeOf($exprType)->yes()) { |
187: | return $this->create($exprNode, $type, $context, $scope)->setRootExpr($expr); |
188: | } |
189: | } |
190: | } |
191: | if ($context->true()) { |
192: | return $this->create($exprNode, new ObjectWithoutClassType(), $context, $scope)->setRootExpr($exprNode); |
193: | } |
194: | } elseif ($expr instanceof Node\Expr\BinaryOp\Identical) { |
195: | return $this->resolveIdentical($expr, $scope, $context); |
196: | |
197: | } elseif ($expr instanceof Node\Expr\BinaryOp\NotIdentical) { |
198: | return $this->specifyTypesInCondition( |
199: | $scope, |
200: | new Node\Expr\BooleanNot(new Node\Expr\BinaryOp\Identical($expr->left, $expr->right)), |
201: | $context, |
202: | )->setRootExpr($expr); |
203: | } elseif ($expr instanceof Expr\Cast\Bool_) { |
204: | return $this->specifyTypesInCondition( |
205: | $scope, |
206: | new Node\Expr\BinaryOp\Equal($expr->expr, new ConstFetch(new Name\FullyQualified('true'))), |
207: | $context, |
208: | )->setRootExpr($expr); |
209: | } elseif ($expr instanceof Expr\Cast\String_) { |
210: | return $this->specifyTypesInCondition( |
211: | $scope, |
212: | new Node\Expr\BinaryOp\NotEqual($expr->expr, new Node\Scalar\String_('')), |
213: | $context, |
214: | )->setRootExpr($expr); |
215: | } elseif ($expr instanceof Expr\Cast\Int_) { |
216: | return $this->specifyTypesInCondition( |
217: | $scope, |
218: | new Node\Expr\BinaryOp\NotEqual($expr->expr, new Node\Scalar\LNumber(0)), |
219: | $context, |
220: | )->setRootExpr($expr); |
221: | } elseif ($expr instanceof Expr\Cast\Double) { |
222: | return $this->specifyTypesInCondition( |
223: | $scope, |
224: | new Node\Expr\BinaryOp\NotEqual($expr->expr, new Node\Scalar\DNumber(0.0)), |
225: | $context, |
226: | )->setRootExpr($expr); |
227: | } elseif ($expr instanceof Node\Expr\BinaryOp\Equal) { |
228: | return $this->resolveEqual($expr, $scope, $context); |
229: | } elseif ($expr instanceof Node\Expr\BinaryOp\NotEqual) { |
230: | return $this->specifyTypesInCondition( |
231: | $scope, |
232: | new Node\Expr\BooleanNot(new Node\Expr\BinaryOp\Equal($expr->left, $expr->right)), |
233: | $context, |
234: | )->setRootExpr($expr); |
235: | |
236: | } elseif ($expr instanceof Node\Expr\BinaryOp\Smaller || $expr instanceof Node\Expr\BinaryOp\SmallerOrEqual) { |
237: | |
238: | if ( |
239: | $expr->left instanceof FuncCall |
240: | && count($expr->left->getArgs()) >= 1 |
241: | && $expr->left->name instanceof Name |
242: | && in_array(strtolower((string) $expr->left->name), ['count', 'sizeof', 'strlen', 'mb_strlen', 'preg_match'], true) |
243: | && ( |
244: | !$expr->right instanceof FuncCall |
245: | || !$expr->right->name instanceof Name |
246: | || !in_array(strtolower((string) $expr->right->name), ['count', 'sizeof', 'strlen', 'mb_strlen', 'preg_match'], true) |
247: | ) |
248: | ) { |
249: | $inverseOperator = $expr instanceof Node\Expr\BinaryOp\Smaller |
250: | ? new Node\Expr\BinaryOp\SmallerOrEqual($expr->right, $expr->left) |
251: | : new Node\Expr\BinaryOp\Smaller($expr->right, $expr->left); |
252: | |
253: | return $this->specifyTypesInCondition( |
254: | $scope, |
255: | new Node\Expr\BooleanNot($inverseOperator), |
256: | $context, |
257: | )->setRootExpr($expr); |
258: | } |
259: | |
260: | $orEqual = $expr instanceof Node\Expr\BinaryOp\SmallerOrEqual; |
261: | $offset = $orEqual ? 0 : 1; |
262: | $leftType = $scope->getType($expr->left); |
263: | $result = (new SpecifiedTypes([], []))->setRootExpr($expr); |
264: | |
265: | if ( |
266: | !$context->null() |
267: | && $expr->right instanceof FuncCall |
268: | && count($expr->right->getArgs()) >= 1 |
269: | && $expr->right->name instanceof Name |
270: | && in_array(strtolower((string) $expr->right->name), ['count', 'sizeof'], true) |
271: | && $leftType->isInteger()->yes() |
272: | ) { |
273: | $argType = $scope->getType($expr->right->getArgs()[0]->value); |
274: | |
275: | if ($leftType instanceof ConstantIntegerType) { |
276: | if ($orEqual) { |
277: | $sizeType = IntegerRangeType::createAllGreaterThanOrEqualTo($leftType->getValue()); |
278: | } else { |
279: | $sizeType = IntegerRangeType::createAllGreaterThan($leftType->getValue()); |
280: | } |
281: | } elseif ($leftType instanceof IntegerRangeType) { |
282: | $sizeType = $leftType->shift($offset); |
283: | } else { |
284: | $sizeType = $leftType; |
285: | } |
286: | |
287: | $specifiedTypes = $this->specifyTypesForCountFuncCall($expr->right, $argType, $sizeType, $context, $scope, $expr); |
288: | if ($specifiedTypes !== null) { |
289: | $result = $result->unionWith($specifiedTypes); |
290: | } |
291: | |
292: | if ( |
293: | $context->true() && (IntegerRangeType::createAllGreaterThanOrEqualTo(1 - $offset)->isSuperTypeOf($leftType)->yes()) |
294: | || ($context->false() && (new ConstantIntegerType(1 - $offset))->isSuperTypeOf($leftType)->yes()) |
295: | ) { |
296: | if ($context->truthy() && $argType->isArray()->maybe()) { |
297: | $countables = []; |
298: | if ($argType instanceof UnionType) { |
299: | $countableInterface = new ObjectType(Countable::class); |
300: | foreach ($argType->getTypes() as $innerType) { |
301: | if ($innerType->isArray()->yes()) { |
302: | $innerType = TypeCombinator::intersect(new NonEmptyArrayType(), $innerType); |
303: | $countables[] = $innerType; |
304: | } |
305: | |
306: | if (!$countableInterface->isSuperTypeOf($innerType)->yes()) { |
307: | continue; |
308: | } |
309: | |
310: | $countables[] = $innerType; |
311: | } |
312: | } |
313: | |
314: | if (count($countables) > 0) { |
315: | $countableType = TypeCombinator::union(...$countables); |
316: | |
317: | return $this->create($expr->right->getArgs()[0]->value, $countableType, $context, $scope)->setRootExpr($expr); |
318: | } |
319: | } |
320: | |
321: | if ($argType->isArray()->yes()) { |
322: | $newType = new NonEmptyArrayType(); |
323: | if ($context->true() && $argType->isList()->yes()) { |
324: | $newType = TypeCombinator::intersect($newType, new AccessoryArrayListType()); |
325: | } |
326: | |
327: | $result = $result->unionWith( |
328: | $this->create($expr->right->getArgs()[0]->value, $newType, $context, $scope)->setRootExpr($expr), |
329: | ); |
330: | } |
331: | } |
332: | } |
333: | |
334: | if ( |
335: | !$context->null() |
336: | && $expr->right instanceof FuncCall |
337: | && count($expr->right->getArgs()) >= 3 |
338: | && $expr->right->name instanceof Name |
339: | && in_array(strtolower((string) $expr->right->name), ['preg_match'], true) |
340: | && IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($leftType)->yes() |
341: | ) { |
342: | return $this->specifyTypesInCondition( |
343: | $scope, |
344: | new Expr\BinaryOp\NotIdentical($expr->right, new ConstFetch(new Name('false'))), |
345: | $context, |
346: | )->setRootExpr($expr); |
347: | } |
348: | |
349: | if ( |
350: | !$context->null() |
351: | && $expr->right instanceof FuncCall |
352: | && count($expr->right->getArgs()) === 1 |
353: | && $expr->right->name instanceof Name |
354: | && in_array(strtolower((string) $expr->right->name), ['strlen', 'mb_strlen'], true) |
355: | && $leftType->isInteger()->yes() |
356: | ) { |
357: | if ( |
358: | $context->true() && (IntegerRangeType::createAllGreaterThanOrEqualTo(1 - $offset)->isSuperTypeOf($leftType)->yes()) |
359: | || ($context->false() && (new ConstantIntegerType(1 - $offset))->isSuperTypeOf($leftType)->yes()) |
360: | ) { |
361: | $argType = $scope->getType($expr->right->getArgs()[0]->value); |
362: | if ($argType->isString()->yes()) { |
363: | $accessory = new AccessoryNonEmptyStringType(); |
364: | |
365: | if (IntegerRangeType::createAllGreaterThanOrEqualTo(2 - $offset)->isSuperTypeOf($leftType)->yes()) { |
366: | $accessory = new AccessoryNonFalsyStringType(); |
367: | } |
368: | |
369: | $result = $result->unionWith($this->create($expr->right->getArgs()[0]->value, $accessory, $context, $scope)->setRootExpr($expr)); |
370: | } |
371: | } |
372: | } |
373: | |
374: | if ($leftType instanceof ConstantIntegerType) { |
375: | if ($expr->right instanceof Expr\PostInc) { |
376: | $result = $result->unionWith($this->createRangeTypes( |
377: | $expr, |
378: | $expr->right->var, |
379: | IntegerRangeType::fromInterval($leftType->getValue(), null, $offset + 1), |
380: | $context, |
381: | )); |
382: | } elseif ($expr->right instanceof Expr\PostDec) { |
383: | $result = $result->unionWith($this->createRangeTypes( |
384: | $expr, |
385: | $expr->right->var, |
386: | IntegerRangeType::fromInterval($leftType->getValue(), null, $offset - 1), |
387: | $context, |
388: | )); |
389: | } elseif ($expr->right instanceof Expr\PreInc || $expr->right instanceof Expr\PreDec) { |
390: | $result = $result->unionWith($this->createRangeTypes( |
391: | $expr, |
392: | $expr->right->var, |
393: | IntegerRangeType::fromInterval($leftType->getValue(), null, $offset), |
394: | $context, |
395: | )); |
396: | } |
397: | } |
398: | |
399: | $rightType = $scope->getType($expr->right); |
400: | if ($rightType instanceof ConstantIntegerType) { |
401: | if ($expr->left instanceof Expr\PostInc) { |
402: | $result = $result->unionWith($this->createRangeTypes( |
403: | $expr, |
404: | $expr->left->var, |
405: | IntegerRangeType::fromInterval(null, $rightType->getValue(), -$offset + 1), |
406: | $context, |
407: | )); |
408: | } elseif ($expr->left instanceof Expr\PostDec) { |
409: | $result = $result->unionWith($this->createRangeTypes( |
410: | $expr, |
411: | $expr->left->var, |
412: | IntegerRangeType::fromInterval(null, $rightType->getValue(), -$offset - 1), |
413: | $context, |
414: | )); |
415: | } elseif ($expr->left instanceof Expr\PreInc || $expr->left instanceof Expr\PreDec) { |
416: | $result = $result->unionWith($this->createRangeTypes( |
417: | $expr, |
418: | $expr->left->var, |
419: | IntegerRangeType::fromInterval(null, $rightType->getValue(), -$offset), |
420: | $context, |
421: | )); |
422: | } |
423: | } |
424: | |
425: | if ($context->true()) { |
426: | if (!$expr->left instanceof Node\Scalar) { |
427: | $result = $result->unionWith( |
428: | $this->create( |
429: | $expr->left, |
430: | $orEqual ? $rightType->getSmallerOrEqualType($this->phpVersion) : $rightType->getSmallerType($this->phpVersion), |
431: | TypeSpecifierContext::createTruthy(), |
432: | $scope, |
433: | )->setRootExpr($expr), |
434: | ); |
435: | } |
436: | if (!$expr->right instanceof Node\Scalar) { |
437: | $result = $result->unionWith( |
438: | $this->create( |
439: | $expr->right, |
440: | $orEqual ? $leftType->getGreaterOrEqualType($this->phpVersion) : $leftType->getGreaterType($this->phpVersion), |
441: | TypeSpecifierContext::createTruthy(), |
442: | $scope, |
443: | )->setRootExpr($expr), |
444: | ); |
445: | } |
446: | } elseif ($context->false()) { |
447: | if (!$expr->left instanceof Node\Scalar) { |
448: | $result = $result->unionWith( |
449: | $this->create( |
450: | $expr->left, |
451: | $orEqual ? $rightType->getGreaterType($this->phpVersion) : $rightType->getGreaterOrEqualType($this->phpVersion), |
452: | TypeSpecifierContext::createTruthy(), |
453: | $scope, |
454: | )->setRootExpr($expr), |
455: | ); |
456: | } |
457: | if (!$expr->right instanceof Node\Scalar) { |
458: | $result = $result->unionWith( |
459: | $this->create( |
460: | $expr->right, |
461: | $orEqual ? $leftType->getSmallerType($this->phpVersion) : $leftType->getSmallerOrEqualType($this->phpVersion), |
462: | TypeSpecifierContext::createTruthy(), |
463: | $scope, |
464: | )->setRootExpr($expr), |
465: | ); |
466: | } |
467: | } |
468: | |
469: | return $result; |
470: | |
471: | } elseif ($expr instanceof Node\Expr\BinaryOp\Greater) { |
472: | return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Smaller($expr->right, $expr->left), $context)->setRootExpr($expr); |
473: | |
474: | } elseif ($expr instanceof Node\Expr\BinaryOp\GreaterOrEqual) { |
475: | return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\SmallerOrEqual($expr->right, $expr->left), $context)->setRootExpr($expr); |
476: | |
477: | } elseif ($expr instanceof FuncCall && $expr->name instanceof Name) { |
478: | if ($this->reflectionProvider->hasFunction($expr->name, $scope)) { |
479: | $functionReflection = $this->reflectionProvider->getFunction($expr->name, $scope); |
480: | foreach ($this->getFunctionTypeSpecifyingExtensions() as $extension) { |
481: | if (!$extension->isFunctionSupported($functionReflection, $expr, $context)) { |
482: | continue; |
483: | } |
484: | |
485: | return $extension->specifyTypes($functionReflection, $expr, $scope, $context); |
486: | } |
487: | |
488: | |
489: | $parametersAcceptor = null; |
490: | if (count($expr->getArgs()) > 0) { |
491: | $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $functionReflection->getVariants(), $functionReflection->getNamedArgumentsVariants()); |
492: | |
493: | $specifiedTypes = $this->specifyTypesFromConditionalReturnType($context, $expr, $parametersAcceptor, $scope); |
494: | if ($specifiedTypes !== null) { |
495: | return $specifiedTypes; |
496: | } |
497: | } |
498: | |
499: | $assertions = $functionReflection->getAsserts(); |
500: | if ($assertions->getAll() !== []) { |
501: | $parametersAcceptor ??= ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $functionReflection->getVariants(), $functionReflection->getNamedArgumentsVariants()); |
502: | |
503: | $asserts = $assertions->mapTypes(static fn (Type $type) => TemplateTypeHelper::resolveTemplateTypes( |
504: | $type, |
505: | $parametersAcceptor->getResolvedTemplateTypeMap(), |
506: | $parametersAcceptor instanceof ExtendedParametersAcceptor ? $parametersAcceptor->getCallSiteVarianceMap() : TemplateTypeVarianceMap::createEmpty(), |
507: | TemplateTypeVariance::createInvariant(), |
508: | )); |
509: | $specifiedTypes = $this->specifyTypesFromAsserts($context, $expr, $asserts, $parametersAcceptor, $scope); |
510: | if ($specifiedTypes !== null) { |
511: | return $specifiedTypes; |
512: | } |
513: | } |
514: | } |
515: | |
516: | return $this->handleDefaultTruthyOrFalseyContext($context, $expr, $scope); |
517: | } elseif ($expr instanceof MethodCall && $expr->name instanceof Node\Identifier) { |
518: | $methodCalledOnType = $scope->getType($expr->var); |
519: | $methodReflection = $scope->getMethodReflection($methodCalledOnType, $expr->name->name); |
520: | if ($methodReflection !== null) { |
521: | $referencedClasses = $methodCalledOnType->getObjectClassNames(); |
522: | if ( |
523: | count($referencedClasses) === 1 |
524: | && $this->reflectionProvider->hasClass($referencedClasses[0]) |
525: | ) { |
526: | $methodClassReflection = $this->reflectionProvider->getClass($referencedClasses[0]); |
527: | foreach ($this->getMethodTypeSpecifyingExtensionsForClass($methodClassReflection->getName()) as $extension) { |
528: | if (!$extension->isMethodSupported($methodReflection, $expr, $context)) { |
529: | continue; |
530: | } |
531: | |
532: | return $extension->specifyTypes($methodReflection, $expr, $scope, $context); |
533: | } |
534: | } |
535: | |
536: | |
537: | $parametersAcceptor = null; |
538: | if (count($expr->getArgs()) > 0) { |
539: | $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $methodReflection->getVariants(), $methodReflection->getNamedArgumentsVariants()); |
540: | |
541: | $specifiedTypes = $this->specifyTypesFromConditionalReturnType($context, $expr, $parametersAcceptor, $scope); |
542: | if ($specifiedTypes !== null) { |
543: | return $specifiedTypes; |
544: | } |
545: | } |
546: | |
547: | $assertions = $methodReflection->getAsserts(); |
548: | if ($assertions->getAll() !== []) { |
549: | $parametersAcceptor ??= ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $methodReflection->getVariants(), $methodReflection->getNamedArgumentsVariants()); |
550: | |
551: | $asserts = $assertions->mapTypes(static fn (Type $type) => TemplateTypeHelper::resolveTemplateTypes( |
552: | $type, |
553: | $parametersAcceptor->getResolvedTemplateTypeMap(), |
554: | $parametersAcceptor instanceof ExtendedParametersAcceptor ? $parametersAcceptor->getCallSiteVarianceMap() : TemplateTypeVarianceMap::createEmpty(), |
555: | TemplateTypeVariance::createInvariant(), |
556: | )); |
557: | $specifiedTypes = $this->specifyTypesFromAsserts($context, $expr, $asserts, $parametersAcceptor, $scope); |
558: | if ($specifiedTypes !== null) { |
559: | return $specifiedTypes; |
560: | } |
561: | } |
562: | } |
563: | |
564: | return $this->handleDefaultTruthyOrFalseyContext($context, $expr, $scope); |
565: | } elseif ($expr instanceof StaticCall && $expr->name instanceof Node\Identifier) { |
566: | if ($expr->class instanceof Name) { |
567: | $calleeType = $scope->resolveTypeByName($expr->class); |
568: | } else { |
569: | $calleeType = $scope->getType($expr->class); |
570: | } |
571: | |
572: | $staticMethodReflection = $scope->getMethodReflection($calleeType, $expr->name->name); |
573: | if ($staticMethodReflection !== null) { |
574: | $referencedClasses = $calleeType->getObjectClassNames(); |
575: | if ( |
576: | count($referencedClasses) === 1 |
577: | && $this->reflectionProvider->hasClass($referencedClasses[0]) |
578: | ) { |
579: | $staticMethodClassReflection = $this->reflectionProvider->getClass($referencedClasses[0]); |
580: | foreach ($this->getStaticMethodTypeSpecifyingExtensionsForClass($staticMethodClassReflection->getName()) as $extension) { |
581: | if (!$extension->isStaticMethodSupported($staticMethodReflection, $expr, $context)) { |
582: | continue; |
583: | } |
584: | |
585: | return $extension->specifyTypes($staticMethodReflection, $expr, $scope, $context); |
586: | } |
587: | } |
588: | |
589: | |
590: | $parametersAcceptor = null; |
591: | if (count($expr->getArgs()) > 0) { |
592: | $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $staticMethodReflection->getVariants(), $staticMethodReflection->getNamedArgumentsVariants()); |
593: | |
594: | $specifiedTypes = $this->specifyTypesFromConditionalReturnType($context, $expr, $parametersAcceptor, $scope); |
595: | if ($specifiedTypes !== null) { |
596: | return $specifiedTypes; |
597: | } |
598: | } |
599: | |
600: | $assertions = $staticMethodReflection->getAsserts(); |
601: | if ($assertions->getAll() !== []) { |
602: | $parametersAcceptor ??= ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $staticMethodReflection->getVariants(), $staticMethodReflection->getNamedArgumentsVariants()); |
603: | |
604: | $asserts = $assertions->mapTypes(static fn (Type $type) => TemplateTypeHelper::resolveTemplateTypes( |
605: | $type, |
606: | $parametersAcceptor->getResolvedTemplateTypeMap(), |
607: | $parametersAcceptor instanceof ExtendedParametersAcceptor ? $parametersAcceptor->getCallSiteVarianceMap() : TemplateTypeVarianceMap::createEmpty(), |
608: | TemplateTypeVariance::createInvariant(), |
609: | )); |
610: | $specifiedTypes = $this->specifyTypesFromAsserts($context, $expr, $asserts, $parametersAcceptor, $scope); |
611: | if ($specifiedTypes !== null) { |
612: | return $specifiedTypes; |
613: | } |
614: | } |
615: | } |
616: | |
617: | return $this->handleDefaultTruthyOrFalseyContext($context, $expr, $scope); |
618: | } elseif ($expr instanceof BooleanAnd || $expr instanceof LogicalAnd) { |
619: | if (!$scope instanceof MutatingScope) { |
620: | throw new ShouldNotHappenException(); |
621: | } |
622: | $leftTypes = $this->specifyTypesInCondition($scope, $expr->left, $context)->setRootExpr($expr); |
623: | $rightScope = $scope->filterByTruthyValue($expr->left); |
624: | $rightTypes = $this->specifyTypesInCondition($rightScope, $expr->right, $context)->setRootExpr($expr); |
625: | $types = $context->true() ? $leftTypes->unionWith($rightTypes) : $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($rightScope)); |
626: | if ($context->false()) { |
627: | return (new SpecifiedTypes( |
628: | $types->getSureTypes(), |
629: | $types->getSureNotTypes(), |
630: | ))->setNewConditionalExpressionHolders(array_merge( |
631: | $this->processBooleanNotSureConditionalTypes($scope, $leftTypes, $rightTypes), |
632: | $this->processBooleanNotSureConditionalTypes($scope, $rightTypes, $leftTypes), |
633: | $this->processBooleanSureConditionalTypes($scope, $leftTypes, $rightTypes), |
634: | $this->processBooleanSureConditionalTypes($scope, $rightTypes, $leftTypes), |
635: | ))->setRootExpr($expr); |
636: | } |
637: | |
638: | return $types; |
639: | } elseif ($expr instanceof BooleanOr || $expr instanceof LogicalOr) { |
640: | if (!$scope instanceof MutatingScope) { |
641: | throw new ShouldNotHappenException(); |
642: | } |
643: | $leftTypes = $this->specifyTypesInCondition($scope, $expr->left, $context)->setRootExpr($expr); |
644: | $rightScope = $scope->filterByFalseyValue($expr->left); |
645: | $rightTypes = $this->specifyTypesInCondition($rightScope, $expr->right, $context)->setRootExpr($expr); |
646: | $types = $context->true() ? $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($rightScope)) : $leftTypes->unionWith($rightTypes); |
647: | if ($context->true()) { |
648: | return (new SpecifiedTypes( |
649: | $types->getSureTypes(), |
650: | $types->getSureNotTypes(), |
651: | ))->setNewConditionalExpressionHolders(array_merge( |
652: | $this->processBooleanNotSureConditionalTypes($scope, $leftTypes, $rightTypes), |
653: | $this->processBooleanNotSureConditionalTypes($scope, $rightTypes, $leftTypes), |
654: | $this->processBooleanSureConditionalTypes($scope, $leftTypes, $rightTypes), |
655: | $this->processBooleanSureConditionalTypes($scope, $rightTypes, $leftTypes), |
656: | ))->setRootExpr($expr); |
657: | } |
658: | |
659: | return $types; |
660: | } elseif ($expr instanceof Node\Expr\BooleanNot && !$context->null()) { |
661: | return $this->specifyTypesInCondition($scope, $expr->expr, $context->negate())->setRootExpr($expr); |
662: | } elseif ($expr instanceof Node\Expr\Assign) { |
663: | if (!$scope instanceof MutatingScope) { |
664: | throw new ShouldNotHappenException(); |
665: | } |
666: | |
667: | if ($context->null()) { |
668: | $specifiedTypes = $this->specifyTypesInCondition($scope->exitFirstLevelStatements(), $expr->expr, $context)->setRootExpr($expr); |
669: | |
670: | |
671: | if ( |
672: | $expr->expr instanceof FuncCall |
673: | && $expr->expr->name instanceof Name |
674: | && in_array($expr->expr->name->toLowerString(), ['array_key_first', 'array_key_last'], true) |
675: | && count($expr->expr->getArgs()) >= 1 |
676: | ) { |
677: | $arrayArg = $expr->expr->getArgs()[0]->value; |
678: | $arrayType = $scope->getType($arrayArg); |
679: | if ( |
680: | $arrayType->isArray()->yes() |
681: | && $arrayType->isIterableAtLeastOnce()->yes() |
682: | ) { |
683: | $dimFetch = new ArrayDimFetch($arrayArg, $expr->var); |
684: | $iterableValueType = $expr->expr->name->toLowerString() === 'array_key_first' |
685: | ? $arrayType->getFirstIterableValueType() |
686: | : $arrayType->getLastIterableValueType(); |
687: | |
688: | return $specifiedTypes->unionWith( |
689: | $this->create($dimFetch, $iterableValueType, TypeSpecifierContext::createTrue(), $scope), |
690: | ); |
691: | } |
692: | } |
693: | |
694: | |
695: | if ( |
696: | $expr->expr instanceof Expr\BinaryOp\Minus |
697: | && $expr->expr->left instanceof FuncCall |
698: | && $expr->expr->left->name instanceof Name |
699: | && in_array($expr->expr->left->name->toLowerString(), ['count', 'sizeof'], true) |
700: | && count($expr->expr->left->getArgs()) >= 1 |
701: | && $expr->expr->right instanceof Node\Scalar\Int_ |
702: | && $expr->expr->right->value === 1 |
703: | ) { |
704: | $arrayArg = $expr->expr->left->getArgs()[0]->value; |
705: | $arrayType = $scope->getType($arrayArg); |
706: | if ( |
707: | $arrayType->isList()->yes() |
708: | && $arrayType->isIterableAtLeastOnce()->yes() |
709: | ) { |
710: | $dimFetch = new ArrayDimFetch($arrayArg, $expr->var); |
711: | |
712: | return $specifiedTypes->unionWith( |
713: | $this->create($dimFetch, $arrayType->getLastIterableValueType(), TypeSpecifierContext::createTrue(), $scope), |
714: | ); |
715: | } |
716: | } |
717: | |
718: | return $specifiedTypes; |
719: | } |
720: | |
721: | $specifiedTypes = $this->specifyTypesInCondition($scope->exitFirstLevelStatements(), $expr->var, $context)->setRootExpr($expr); |
722: | |
723: | if ($context->true()) { |
724: | |
725: | if ( |
726: | $expr->expr instanceof FuncCall |
727: | && $expr->expr->name instanceof Name |
728: | && $expr->expr->name->toLowerString() === 'array_search' |
729: | && count($expr->expr->getArgs()) >= 2 |
730: | ) { |
731: | $arrayArg = $expr->expr->getArgs()[1]->value; |
732: | $arrayType = $scope->getType($arrayArg); |
733: | |
734: | if ($arrayType->isArray()->yes()) { |
735: | $dimFetch = new ArrayDimFetch($arrayArg, $expr->var); |
736: | $iterableValueType = $arrayType->getIterableValueType(); |
737: | |
738: | return $specifiedTypes->unionWith( |
739: | $this->create($dimFetch, $iterableValueType, TypeSpecifierContext::createTrue(), $scope), |
740: | ); |
741: | } |
742: | } |
743: | } |
744: | return $specifiedTypes; |
745: | } elseif ( |
746: | $expr instanceof Expr\Isset_ |
747: | && count($expr->vars) > 0 |
748: | && !$context->null() |
749: | ) { |
750: | |
751: | if (count($expr->vars) > 1) { |
752: | $issets = []; |
753: | foreach ($expr->vars as $var) { |
754: | $issets[] = new Expr\Isset_([$var], $expr->getAttributes()); |
755: | } |
756: | |
757: | $first = array_shift($issets); |
758: | $andChain = null; |
759: | foreach ($issets as $isset) { |
760: | if ($andChain === null) { |
761: | $andChain = new BooleanAnd($first, $isset); |
762: | continue; |
763: | } |
764: | |
765: | $andChain = new BooleanAnd($andChain, $isset); |
766: | } |
767: | |
768: | if ($andChain === null) { |
769: | throw new ShouldNotHappenException(); |
770: | } |
771: | |
772: | return $this->specifyTypesInCondition($scope, $andChain, $context)->setRootExpr($expr); |
773: | } |
774: | |
775: | $issetExpr = $expr->vars[0]; |
776: | |
777: | if (!$context->true()) { |
778: | if (!$scope instanceof MutatingScope) { |
779: | throw new ShouldNotHappenException(); |
780: | } |
781: | |
782: | $isset = $scope->issetCheck($issetExpr, static fn () => true); |
783: | |
784: | if ($isset === false) { |
785: | return new SpecifiedTypes(); |
786: | } |
787: | |
788: | $type = $scope->getType($issetExpr); |
789: | $isNullable = !$type->isNull()->no(); |
790: | $exprType = $this->create( |
791: | $issetExpr, |
792: | new NullType(), |
793: | $context->negate(), |
794: | $scope, |
795: | )->setRootExpr($expr); |
796: | |
797: | if ($issetExpr instanceof Expr\Variable && is_string($issetExpr->name)) { |
798: | if ($isset === true) { |
799: | if ($isNullable) { |
800: | return $exprType; |
801: | } |
802: | |
803: | |
804: | return $exprType->unionWith($this->create( |
805: | new IssetExpr($issetExpr), |
806: | new NullType(), |
807: | $context, |
808: | $scope, |
809: | ))->setRootExpr($expr); |
810: | } |
811: | |
812: | if ($isNullable) { |
813: | |
814: | return $exprType->unionWith($this->create( |
815: | new IssetExpr($issetExpr), |
816: | new NullType(), |
817: | $context->negate(), |
818: | $scope, |
819: | ))->setRootExpr($expr); |
820: | } |
821: | |
822: | |
823: | return $this->create( |
824: | new IssetExpr($issetExpr), |
825: | new NullType(), |
826: | $context, |
827: | $scope, |
828: | )->setRootExpr($expr); |
829: | } |
830: | |
831: | if ($isNullable && $isset === true) { |
832: | return $exprType; |
833: | } |
834: | |
835: | return new SpecifiedTypes(); |
836: | } |
837: | |
838: | $tmpVars = [$issetExpr]; |
839: | while ( |
840: | $issetExpr instanceof ArrayDimFetch |
841: | || $issetExpr instanceof PropertyFetch |
842: | || ( |
843: | $issetExpr instanceof StaticPropertyFetch |
844: | && $issetExpr->class instanceof Expr |
845: | ) |
846: | ) { |
847: | if ($issetExpr instanceof StaticPropertyFetch) { |
848: | |
849: | $issetExpr = $issetExpr->class; |
850: | } else { |
851: | $issetExpr = $issetExpr->var; |
852: | } |
853: | $tmpVars[] = $issetExpr; |
854: | } |
855: | $vars = array_reverse($tmpVars); |
856: | |
857: | $types = new SpecifiedTypes(); |
858: | foreach ($vars as $var) { |
859: | |
860: | if ($var instanceof Expr\Variable && is_string($var->name)) { |
861: | if ($scope->hasVariableType($var->name)->no()) { |
862: | return (new SpecifiedTypes([], []))->setRootExpr($expr); |
863: | } |
864: | } |
865: | |
866: | if ( |
867: | $var instanceof ArrayDimFetch |
868: | && $var->dim !== null |
869: | && !$scope->getType($var->var) instanceof MixedType |
870: | ) { |
871: | $dimType = $scope->getType($var->dim); |
872: | |
873: | if ($dimType instanceof ConstantIntegerType || $dimType instanceof ConstantStringType) { |
874: | $types = $types->unionWith( |
875: | $this->create( |
876: | $var->var, |
877: | new HasOffsetType($dimType), |
878: | $context, |
879: | $scope, |
880: | )->setRootExpr($expr), |
881: | ); |
882: | } else { |
883: | $varType = $scope->getType($var->var); |
884: | $narrowedKey = AllowedArrayKeysTypes::narrowOffsetKeyType($varType, $dimType); |
885: | if ($narrowedKey !== null) { |
886: | $types = $types->unionWith( |
887: | $this->create( |
888: | $var->dim, |
889: | $narrowedKey, |
890: | $context, |
891: | $scope, |
892: | )->setRootExpr($expr), |
893: | ); |
894: | } |
895: | } |
896: | } |
897: | |
898: | if ( |
899: | $var instanceof PropertyFetch |
900: | && $var->name instanceof Node\Identifier |
901: | ) { |
902: | $types = $types->unionWith( |
903: | $this->create($var->var, new IntersectionType([ |
904: | new ObjectWithoutClassType(), |
905: | new HasPropertyType($var->name->toString()), |
906: | ]), TypeSpecifierContext::createTruthy(), $scope)->setRootExpr($expr), |
907: | ); |
908: | } elseif ( |
909: | $var instanceof StaticPropertyFetch |
910: | && $var->class instanceof Expr |
911: | && $var->name instanceof Node\VarLikeIdentifier |
912: | ) { |
913: | $types = $types->unionWith( |
914: | $this->create($var->class, new IntersectionType([ |
915: | new ObjectWithoutClassType(), |
916: | new HasPropertyType($var->name->toString()), |
917: | ]), TypeSpecifierContext::createTruthy(), $scope)->setRootExpr($expr), |
918: | ); |
919: | } |
920: | |
921: | $types = $types->unionWith( |
922: | $this->create($var, new NullType(), TypeSpecifierContext::createFalse(), $scope)->setRootExpr($expr), |
923: | ); |
924: | } |
925: | |
926: | return $types; |
927: | } elseif ( |
928: | $expr instanceof Expr\BinaryOp\Coalesce |
929: | && !$context->null() |
930: | ) { |
931: | if (!$context->true()) { |
932: | if (!$scope instanceof MutatingScope) { |
933: | throw new ShouldNotHappenException(); |
934: | } |
935: | |
936: | $isset = $scope->issetCheck($expr->left, static fn () => true); |
937: | |
938: | if ($isset !== true) { |
939: | return new SpecifiedTypes(); |
940: | } |
941: | |
942: | return $this->create( |
943: | $expr->left, |
944: | new NullType(), |
945: | $context->negate(), |
946: | $scope, |
947: | )->setRootExpr($expr); |
948: | } |
949: | |
950: | if ((new ConstantBooleanType(false))->isSuperTypeOf($scope->getType($expr->right)->toBoolean())->yes()) { |
951: | return $this->create( |
952: | $expr->left, |
953: | new NullType(), |
954: | TypeSpecifierContext::createFalse(), |
955: | $scope, |
956: | )->setRootExpr($expr); |
957: | } |
958: | |
959: | } elseif ( |
960: | $expr instanceof Expr\Empty_ |
961: | ) { |
962: | if (!$scope instanceof MutatingScope) { |
963: | throw new ShouldNotHappenException(); |
964: | } |
965: | |
966: | $isset = $scope->issetCheck($expr->expr, static fn () => true); |
967: | if ($isset === false) { |
968: | return new SpecifiedTypes(); |
969: | } |
970: | |
971: | return $this->specifyTypesInCondition($scope, new BooleanOr( |
972: | new Expr\BooleanNot(new Expr\Isset_([$expr->expr])), |
973: | new Expr\BooleanNot($expr->expr), |
974: | ), $context)->setRootExpr($expr); |
975: | } elseif ($expr instanceof Expr\ErrorSuppress) { |
976: | return $this->specifyTypesInCondition($scope, $expr->expr, $context)->setRootExpr($expr); |
977: | } elseif ( |
978: | $expr instanceof Expr\Ternary |
979: | && !$context->null() |
980: | && $scope->getType($expr->else)->isFalse()->yes() |
981: | ) { |
982: | $conditionExpr = $expr->cond; |
983: | if ($expr->if !== null) { |
984: | $conditionExpr = new BooleanAnd($conditionExpr, $expr->if); |
985: | } |
986: | |
987: | return $this->specifyTypesInCondition($scope, $conditionExpr, $context)->setRootExpr($expr); |
988: | |
989: | } elseif ($expr instanceof Expr\NullsafePropertyFetch && !$context->null()) { |
990: | $types = $this->specifyTypesInCondition( |
991: | $scope, |
992: | new BooleanAnd( |
993: | new Expr\BinaryOp\NotIdentical($expr->var, new ConstFetch(new Name('null'))), |
994: | new PropertyFetch($expr->var, $expr->name), |
995: | ), |
996: | $context, |
997: | )->setRootExpr($expr); |
998: | |
999: | $nullSafeTypes = $this->handleDefaultTruthyOrFalseyContext($context, $expr, $scope); |
1000: | return $context->true() ? $types->unionWith($nullSafeTypes) : $types->normalize($scope)->intersectWith($nullSafeTypes->normalize($scope)); |
1001: | } elseif ($expr instanceof Expr\NullsafeMethodCall && !$context->null()) { |
1002: | $types = $this->specifyTypesInCondition( |
1003: | $scope, |
1004: | new BooleanAnd( |
1005: | new Expr\BinaryOp\NotIdentical($expr->var, new ConstFetch(new Name('null'))), |
1006: | new MethodCall($expr->var, $expr->name, $expr->args), |
1007: | ), |
1008: | $context, |
1009: | )->setRootExpr($expr); |
1010: | |
1011: | $nullSafeTypes = $this->handleDefaultTruthyOrFalseyContext($context, $expr, $scope); |
1012: | return $context->true() ? $types->unionWith($nullSafeTypes) : $types->normalize($scope)->intersectWith($nullSafeTypes->normalize($scope)); |
1013: | } elseif ( |
1014: | $expr instanceof Expr\New_ |
1015: | && $expr->class instanceof Name |
1016: | && $this->reflectionProvider->hasClass($expr->class->toString()) |
1017: | ) { |
1018: | $classReflection = $this->reflectionProvider->getClass($expr->class->toString()); |
1019: | |
1020: | if ($classReflection->hasConstructor()) { |
1021: | $methodReflection = $classReflection->getConstructor(); |
1022: | $asserts = $methodReflection->getAsserts(); |
1023: | |
1024: | if ($asserts->getAll() !== []) { |
1025: | $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $methodReflection->getVariants(), $methodReflection->getNamedArgumentsVariants()); |
1026: | |
1027: | $asserts = $asserts->mapTypes(static fn (Type $type) => TemplateTypeHelper::resolveTemplateTypes( |
1028: | $type, |
1029: | $parametersAcceptor->getResolvedTemplateTypeMap(), |
1030: | $parametersAcceptor instanceof ExtendedParametersAcceptor ? $parametersAcceptor->getCallSiteVarianceMap() : TemplateTypeVarianceMap::createEmpty(), |
1031: | TemplateTypeVariance::createInvariant(), |
1032: | )); |
1033: | |
1034: | $specifiedTypes = $this->specifyTypesFromAsserts($context, $expr, $asserts, $parametersAcceptor, $scope); |
1035: | |
1036: | if ($specifiedTypes !== null) { |
1037: | return $specifiedTypes; |
1038: | } |
1039: | } |
1040: | } |
1041: | } elseif (!$context->null()) { |
1042: | return $this->handleDefaultTruthyOrFalseyContext($context, $expr, $scope); |
1043: | } |
1044: | |
1045: | return (new SpecifiedTypes([], []))->setRootExpr($expr); |
1046: | } |
1047: | |
1048: | private function specifyTypesForCountFuncCall( |
1049: | FuncCall $countFuncCall, |
1050: | Type $type, |
1051: | Type $sizeType, |
1052: | TypeSpecifierContext $context, |
1053: | Scope $scope, |
1054: | Expr $rootExpr, |
1055: | ): ?SpecifiedTypes |
1056: | { |
1057: | if (count($countFuncCall->getArgs()) === 1) { |
1058: | $isNormalCount = TrinaryLogic::createYes(); |
1059: | } else { |
1060: | $mode = $scope->getType($countFuncCall->getArgs()[1]->value); |
1061: | $isNormalCount = (new ConstantIntegerType(COUNT_NORMAL))->isSuperTypeOf($mode)->result->or($type->getIterableValueType()->isArray()->negate()); |
1062: | } |
1063: | |
1064: | $isConstantArray = $type->isConstantArray(); |
1065: | $isList = $type->isList(); |
1066: | if ( |
1067: | !$isNormalCount->yes() |
1068: | || (!$isConstantArray->yes() && !$isList->yes()) |
1069: | || $type->isIterableAtLeastOnce()->no() |
1070: | ) { |
1071: | return null; |
1072: | } |
1073: | |
1074: | $resultTypes = []; |
1075: | foreach ($type->getArrays() as $arrayType) { |
1076: | $isSizeSuperTypeOfArraySize = $sizeType->isSuperTypeOf($arrayType->getArraySize()); |
1077: | if ($isSizeSuperTypeOfArraySize->no()) { |
1078: | continue; |
1079: | } |
1080: | |
1081: | if ($context->falsey() && $isSizeSuperTypeOfArraySize->maybe()) { |
1082: | continue; |
1083: | } |
1084: | |
1085: | if ( |
1086: | $sizeType instanceof ConstantIntegerType |
1087: | && $sizeType->getValue() < ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT |
1088: | && ( |
1089: | $isList->yes() |
1090: | || $isConstantArray->yes() && $arrayType->getKeyType()->isSuperTypeOf(IntegerRangeType::fromInterval(0, $sizeType->getValue() - 1))->yes() |
1091: | ) |
1092: | ) { |
1093: | |
1094: | $valueTypesBuilder = ConstantArrayTypeBuilder::createEmpty(); |
1095: | for ($i = 0; $i < $sizeType->getValue(); $i++) { |
1096: | $offsetType = new ConstantIntegerType($i); |
1097: | $valueTypesBuilder->setOffsetValueType($offsetType, $arrayType->getOffsetValueType($offsetType)); |
1098: | } |
1099: | $resultTypes[] = $valueTypesBuilder->getArray(); |
1100: | continue; |
1101: | } |
1102: | |
1103: | if ( |
1104: | $sizeType instanceof IntegerRangeType |
1105: | && $sizeType->getMin() !== null |
1106: | && ( |
1107: | $isList->yes() |
1108: | || $isConstantArray->yes() && $arrayType->getKeyType()->isSuperTypeOf(IntegerRangeType::fromInterval(0, $sizeType->getMin() - 1))->yes() |
1109: | ) |
1110: | ) { |
1111: | |
1112: | $valueTypesBuilder = ConstantArrayTypeBuilder::createEmpty(); |
1113: | for ($i = 0; $i < $sizeType->getMin(); $i++) { |
1114: | $offsetType = new ConstantIntegerType($i); |
1115: | $valueTypesBuilder->setOffsetValueType($offsetType, $arrayType->getOffsetValueType($offsetType)); |
1116: | } |
1117: | if ($sizeType->getMax() !== null) { |
1118: | for ($i = $sizeType->getMin(); $i < $sizeType->getMax(); $i++) { |
1119: | $offsetType = new ConstantIntegerType($i); |
1120: | $valueTypesBuilder->setOffsetValueType($offsetType, $arrayType->getOffsetValueType($offsetType), true); |
1121: | } |
1122: | } elseif ($arrayType->isConstantArray()->yes()) { |
1123: | for ($i = $sizeType->getMin();; $i++) { |
1124: | $offsetType = new ConstantIntegerType($i); |
1125: | $hasOffset = $arrayType->hasOffsetValueType($offsetType); |
1126: | if ($hasOffset->no()) { |
1127: | break; |
1128: | } |
1129: | $valueTypesBuilder->setOffsetValueType($offsetType, $arrayType->getOffsetValueType($offsetType), !$hasOffset->yes()); |
1130: | } |
1131: | } else { |
1132: | $resultTypes[] = TypeCombinator::intersect($arrayType, new NonEmptyArrayType()); |
1133: | continue; |
1134: | } |
1135: | |
1136: | $resultTypes[] = $valueTypesBuilder->getArray(); |
1137: | continue; |
1138: | } |
1139: | |
1140: | $resultTypes[] = $arrayType; |
1141: | } |
1142: | |
1143: | return $this->create($countFuncCall->getArgs()[0]->value, TypeCombinator::union(...$resultTypes), $context, $scope)->setRootExpr($rootExpr); |
1144: | } |
1145: | |
1146: | private function specifyTypesForConstantBinaryExpression( |
1147: | Expr $exprNode, |
1148: | Type $constantType, |
1149: | TypeSpecifierContext $context, |
1150: | Scope $scope, |
1151: | Expr $rootExpr, |
1152: | ): ?SpecifiedTypes |
1153: | { |
1154: | if (!$context->null() && $constantType->isFalse()->yes()) { |
1155: | $types = $this->create($exprNode, $constantType, $context, $scope)->setRootExpr($rootExpr); |
1156: | if ($exprNode instanceof Expr\NullsafeMethodCall || $exprNode instanceof Expr\NullsafePropertyFetch) { |
1157: | return $types; |
1158: | } |
1159: | |
1160: | return $types->unionWith($this->specifyTypesInCondition( |
1161: | $scope, |
1162: | $exprNode, |
1163: | $context->true() ? TypeSpecifierContext::createFalse() : TypeSpecifierContext::createFalse()->negate(), |
1164: | )->setRootExpr($rootExpr)); |
1165: | } |
1166: | |
1167: | if (!$context->null() && $constantType->isTrue()->yes()) { |
1168: | $types = $this->create($exprNode, $constantType, $context, $scope)->setRootExpr($rootExpr); |
1169: | if ($exprNode instanceof Expr\NullsafeMethodCall || $exprNode instanceof Expr\NullsafePropertyFetch) { |
1170: | return $types; |
1171: | } |
1172: | |
1173: | return $types->unionWith($this->specifyTypesInCondition( |
1174: | $scope, |
1175: | $exprNode, |
1176: | $context->true() ? TypeSpecifierContext::createTrue() : TypeSpecifierContext::createTrue()->negate(), |
1177: | )->setRootExpr($rootExpr)); |
1178: | } |
1179: | |
1180: | return null; |
1181: | } |
1182: | |
1183: | private function specifyTypesForConstantStringBinaryExpression( |
1184: | Expr $exprNode, |
1185: | Type $constantType, |
1186: | TypeSpecifierContext $context, |
1187: | Scope $scope, |
1188: | Expr $rootExpr, |
1189: | ): ?SpecifiedTypes |
1190: | { |
1191: | $scalarValues = $constantType->getConstantScalarValues(); |
1192: | if (count($scalarValues) !== 1 || !is_string($scalarValues[0])) { |
1193: | return null; |
1194: | } |
1195: | $constantStringValue = $scalarValues[0]; |
1196: | |
1197: | if ( |
1198: | $exprNode instanceof FuncCall |
1199: | && $exprNode->name instanceof Name |
1200: | && strtolower($exprNode->name->toString()) === 'gettype' |
1201: | && isset($exprNode->getArgs()[0]) |
1202: | ) { |
1203: | $type = null; |
1204: | if ($constantStringValue === 'string') { |
1205: | $type = new StringType(); |
1206: | } |
1207: | if ($constantStringValue === 'array') { |
1208: | $type = new ArrayType(new MixedType(), new MixedType()); |
1209: | } |
1210: | if ($constantStringValue === 'boolean') { |
1211: | $type = new BooleanType(); |
1212: | } |
1213: | if (in_array($constantStringValue, ['resource', 'resource (closed)'], true)) { |
1214: | $type = new ResourceType(); |
1215: | } |
1216: | if ($constantStringValue === 'integer') { |
1217: | $type = new IntegerType(); |
1218: | } |
1219: | if ($constantStringValue === 'double') { |
1220: | $type = new FloatType(); |
1221: | } |
1222: | if ($constantStringValue === 'NULL') { |
1223: | $type = new NullType(); |
1224: | } |
1225: | if ($constantStringValue === 'object') { |
1226: | $type = new ObjectWithoutClassType(); |
1227: | } |
1228: | |
1229: | if ($type !== null) { |
1230: | $callType = $this->create($exprNode, $constantType, $context, $scope)->setRootExpr($rootExpr); |
1231: | $argType = $this->create($exprNode->getArgs()[0]->value, $type, $context, $scope)->setRootExpr($rootExpr); |
1232: | return $callType->unionWith($argType); |
1233: | } |
1234: | } |
1235: | |
1236: | if ( |
1237: | $context->true() |
1238: | && $exprNode instanceof FuncCall |
1239: | && $exprNode->name instanceof Name |
1240: | && strtolower((string) $exprNode->name) === 'get_parent_class' |
1241: | && isset($exprNode->getArgs()[0]) |
1242: | ) { |
1243: | $argType = $scope->getType($exprNode->getArgs()[0]->value); |
1244: | $objectType = new ObjectType($constantStringValue); |
1245: | $classStringType = new GenericClassStringType($objectType); |
1246: | |
1247: | if ($argType->isString()->yes()) { |
1248: | return $this->create( |
1249: | $exprNode->getArgs()[0]->value, |
1250: | $classStringType, |
1251: | $context, |
1252: | $scope, |
1253: | )->setRootExpr($rootExpr); |
1254: | } |
1255: | |
1256: | if ($argType->isObject()->yes()) { |
1257: | return $this->create( |
1258: | $exprNode->getArgs()[0]->value, |
1259: | $objectType, |
1260: | $context, |
1261: | $scope, |
1262: | )->setRootExpr($rootExpr); |
1263: | } |
1264: | |
1265: | return $this->create( |
1266: | $exprNode->getArgs()[0]->value, |
1267: | TypeCombinator::union($objectType, $classStringType), |
1268: | $context, |
1269: | $scope, |
1270: | )->setRootExpr($rootExpr); |
1271: | } |
1272: | |
1273: | return null; |
1274: | } |
1275: | |
1276: | private function handleDefaultTruthyOrFalseyContext(TypeSpecifierContext $context, Expr $expr, Scope $scope): SpecifiedTypes |
1277: | { |
1278: | if ($context->null()) { |
1279: | return (new SpecifiedTypes([], []))->setRootExpr($expr); |
1280: | } |
1281: | if (!$context->truthy()) { |
1282: | $type = StaticTypeFactory::truthy(); |
1283: | return $this->create($expr, $type, TypeSpecifierContext::createFalse(), $scope)->setRootExpr($expr); |
1284: | } elseif (!$context->falsey()) { |
1285: | $type = StaticTypeFactory::falsey(); |
1286: | return $this->create($expr, $type, TypeSpecifierContext::createFalse(), $scope)->setRootExpr($expr); |
1287: | } |
1288: | |
1289: | return (new SpecifiedTypes([], []))->setRootExpr($expr); |
1290: | } |
1291: | |
1292: | private function specifyTypesFromConditionalReturnType( |
1293: | TypeSpecifierContext $context, |
1294: | Expr\CallLike $call, |
1295: | ParametersAcceptor $parametersAcceptor, |
1296: | Scope $scope, |
1297: | ): ?SpecifiedTypes |
1298: | { |
1299: | if (!$parametersAcceptor instanceof ResolvedFunctionVariant) { |
1300: | return null; |
1301: | } |
1302: | |
1303: | $returnType = $parametersAcceptor->getOriginalParametersAcceptor()->getReturnType(); |
1304: | if (!$returnType instanceof ConditionalTypeForParameter) { |
1305: | return null; |
1306: | } |
1307: | |
1308: | if ($context->true()) { |
1309: | $leftType = new ConstantBooleanType(true); |
1310: | $rightType = new ConstantBooleanType(false); |
1311: | } elseif ($context->false()) { |
1312: | $leftType = new ConstantBooleanType(false); |
1313: | $rightType = new ConstantBooleanType(true); |
1314: | } elseif ($context->null()) { |
1315: | $leftType = new MixedType(); |
1316: | $rightType = new NeverType(); |
1317: | } else { |
1318: | return null; |
1319: | } |
1320: | |
1321: | $argsMap = []; |
1322: | $parameters = $parametersAcceptor->getParameters(); |
1323: | foreach ($call->getArgs() as $i => $arg) { |
1324: | if ($arg->unpack) { |
1325: | continue; |
1326: | } |
1327: | |
1328: | if ($arg->name !== null) { |
1329: | $paramName = $arg->name->toString(); |
1330: | } elseif (isset($parameters[$i])) { |
1331: | $paramName = $parameters[$i]->getName(); |
1332: | } else { |
1333: | continue; |
1334: | } |
1335: | |
1336: | $argsMap['$' . $paramName] = $arg->value; |
1337: | } |
1338: | |
1339: | return $this->getConditionalSpecifiedTypes($returnType, $leftType, $rightType, $scope, $argsMap); |
1340: | } |
1341: | |
1342: | |
1343: | |
1344: | |
1345: | public function getConditionalSpecifiedTypes( |
1346: | ConditionalTypeForParameter $conditionalType, |
1347: | Type $leftType, |
1348: | Type $rightType, |
1349: | Scope $scope, |
1350: | array $argsMap, |
1351: | ): ?SpecifiedTypes |
1352: | { |
1353: | $parameterName = $conditionalType->getParameterName(); |
1354: | if (!array_key_exists($parameterName, $argsMap)) { |
1355: | return null; |
1356: | } |
1357: | |
1358: | $targetType = $conditionalType->getTarget(); |
1359: | $ifType = $conditionalType->getIf(); |
1360: | $elseType = $conditionalType->getElse(); |
1361: | |
1362: | if ($leftType->isSuperTypeOf($ifType)->yes() && $rightType->isSuperTypeOf($elseType)->yes()) { |
1363: | $context = $conditionalType->isNegated() ? TypeSpecifierContext::createFalse() : TypeSpecifierContext::createTrue(); |
1364: | } elseif ($leftType->isSuperTypeOf($elseType)->yes() && $rightType->isSuperTypeOf($ifType)->yes()) { |
1365: | $context = $conditionalType->isNegated() ? TypeSpecifierContext::createTrue() : TypeSpecifierContext::createFalse(); |
1366: | } else { |
1367: | return null; |
1368: | } |
1369: | |
1370: | $specifiedTypes = $this->create( |
1371: | $argsMap[$parameterName], |
1372: | $targetType, |
1373: | $context, |
1374: | $scope, |
1375: | ); |
1376: | |
1377: | if ($targetType instanceof ConstantBooleanType) { |
1378: | if (!$targetType->getValue()) { |
1379: | $context = $context->negate(); |
1380: | } |
1381: | |
1382: | $specifiedTypes = $specifiedTypes->unionWith($this->specifyTypesInCondition($scope, $argsMap[$parameterName], $context)); |
1383: | } |
1384: | |
1385: | return $specifiedTypes; |
1386: | } |
1387: | |
1388: | private function specifyTypesFromAsserts(TypeSpecifierContext $context, Expr\CallLike $call, Assertions $assertions, ParametersAcceptor $parametersAcceptor, Scope $scope): ?SpecifiedTypes |
1389: | { |
1390: | if ($context->null()) { |
1391: | $asserts = $assertions->getAsserts(); |
1392: | } elseif ($context->true()) { |
1393: | $asserts = $assertions->getAssertsIfTrue(); |
1394: | } elseif ($context->false()) { |
1395: | $asserts = $assertions->getAssertsIfFalse(); |
1396: | } else { |
1397: | throw new ShouldNotHappenException(); |
1398: | } |
1399: | |
1400: | if (count($asserts) === 0) { |
1401: | return null; |
1402: | } |
1403: | |
1404: | $argsMap = []; |
1405: | $parameters = $parametersAcceptor->getParameters(); |
1406: | foreach ($call->getArgs() as $i => $arg) { |
1407: | if ($arg->unpack) { |
1408: | continue; |
1409: | } |
1410: | |
1411: | if ($arg->name !== null) { |
1412: | $paramName = $arg->name->toString(); |
1413: | } elseif (isset($parameters[$i])) { |
1414: | $paramName = $parameters[$i]->getName(); |
1415: | } elseif (count($parameters) > 0 && $parametersAcceptor->isVariadic()) { |
1416: | $lastParameter = $parameters[count($parameters) - 1]; |
1417: | $paramName = $lastParameter->getName(); |
1418: | } else { |
1419: | continue; |
1420: | } |
1421: | |
1422: | $argsMap[$paramName][] = $arg->value; |
1423: | } |
1424: | |
1425: | if ($call instanceof MethodCall) { |
1426: | $argsMap['this'] = [$call->var]; |
1427: | } |
1428: | |
1429: | |
1430: | $types = null; |
1431: | |
1432: | foreach ($asserts as $assert) { |
1433: | foreach ($argsMap[substr($assert->getParameter()->getParameterName(), 1)] ?? [] as $parameterExpr) { |
1434: | $assertedType = TypeTraverser::map($assert->getType(), static function (Type $type, callable $traverse) use ($argsMap, $scope): Type { |
1435: | if ($type instanceof ConditionalTypeForParameter) { |
1436: | $parameterName = substr($type->getParameterName(), 1); |
1437: | if (array_key_exists($parameterName, $argsMap)) { |
1438: | $argType = TypeCombinator::union(...array_map(static fn (Expr $expr) => $scope->getType($expr), $argsMap[$parameterName])); |
1439: | $type = $type->toConditional($argType); |
1440: | } |
1441: | } |
1442: | |
1443: | return $traverse($type); |
1444: | }); |
1445: | |
1446: | $assertExpr = $assert->getParameter()->getExpr($parameterExpr); |
1447: | |
1448: | $templateTypeMap = $parametersAcceptor->getResolvedTemplateTypeMap(); |
1449: | $containsUnresolvedTemplate = false; |
1450: | TypeTraverser::map( |
1451: | $assert->getOriginalType(), |
1452: | static function (Type $type, callable $traverse) use ($templateTypeMap, &$containsUnresolvedTemplate) { |
1453: | if ($type instanceof TemplateType && $type->getScope()->getClassName() !== null) { |
1454: | $resolvedType = $templateTypeMap->getType($type->getName()); |
1455: | if ($resolvedType === null || $type->getBound()->equals($resolvedType)) { |
1456: | $containsUnresolvedTemplate = true; |
1457: | return $type; |
1458: | } |
1459: | } |
1460: | |
1461: | return $traverse($type); |
1462: | }, |
1463: | ); |
1464: | |
1465: | $newTypes = $this->create( |
1466: | $assertExpr, |
1467: | $assertedType, |
1468: | $assert->isNegated() ? TypeSpecifierContext::createFalse() : TypeSpecifierContext::createTrue(), |
1469: | $scope, |
1470: | )->setRootExpr($containsUnresolvedTemplate || $assert->isEquality() ? $call : null); |
1471: | $types = $types !== null ? $types->unionWith($newTypes) : $newTypes; |
1472: | |
1473: | if (!$context->null() || !$assertedType instanceof ConstantBooleanType) { |
1474: | continue; |
1475: | } |
1476: | |
1477: | $subContext = $assertedType->getValue() ? TypeSpecifierContext::createTrue() : TypeSpecifierContext::createFalse(); |
1478: | if ($assert->isNegated()) { |
1479: | $subContext = $subContext->negate(); |
1480: | } |
1481: | |
1482: | $types = $types->unionWith($this->specifyTypesInCondition( |
1483: | $scope, |
1484: | $assertExpr, |
1485: | $subContext, |
1486: | )); |
1487: | } |
1488: | } |
1489: | |
1490: | return $types; |
1491: | } |
1492: | |
1493: | |
1494: | |
1495: | |
1496: | private function processBooleanSureConditionalTypes(Scope $scope, SpecifiedTypes $leftTypes, SpecifiedTypes $rightTypes): array |
1497: | { |
1498: | $conditionExpressionTypes = []; |
1499: | foreach ($leftTypes->getSureTypes() as $exprString => [$expr, $type]) { |
1500: | if (!$expr instanceof Expr\Variable) { |
1501: | continue; |
1502: | } |
1503: | if (!is_string($expr->name)) { |
1504: | continue; |
1505: | } |
1506: | |
1507: | $conditionExpressionTypes[$exprString] = ExpressionTypeHolder::createYes( |
1508: | $expr, |
1509: | TypeCombinator::remove($scope->getType($expr), $type), |
1510: | ); |
1511: | } |
1512: | |
1513: | if (count($conditionExpressionTypes) > 0) { |
1514: | $holders = []; |
1515: | foreach ($rightTypes->getSureTypes() as $exprString => [$expr, $type]) { |
1516: | if (!$expr instanceof Expr\Variable) { |
1517: | continue; |
1518: | } |
1519: | if (!is_string($expr->name)) { |
1520: | continue; |
1521: | } |
1522: | |
1523: | if (!isset($holders[$exprString])) { |
1524: | $holders[$exprString] = []; |
1525: | } |
1526: | |
1527: | $conditions = $conditionExpressionTypes; |
1528: | foreach ($conditions as $conditionExprString => $conditionExprTypeHolder) { |
1529: | $conditionExpr = $conditionExprTypeHolder->getExpr(); |
1530: | if (!$conditionExpr instanceof Expr\Variable) { |
1531: | continue; |
1532: | } |
1533: | if (!is_string($conditionExpr->name)) { |
1534: | continue; |
1535: | } |
1536: | if ($conditionExpr->name !== $expr->name) { |
1537: | continue; |
1538: | } |
1539: | |
1540: | unset($conditions[$conditionExprString]); |
1541: | } |
1542: | |
1543: | if (count($conditions) === 0) { |
1544: | continue; |
1545: | } |
1546: | |
1547: | $holder = new ConditionalExpressionHolder( |
1548: | $conditions, |
1549: | new ExpressionTypeHolder($expr, TypeCombinator::intersect($scope->getType($expr), $type), TrinaryLogic::createYes()), |
1550: | ); |
1551: | $holders[$exprString][$holder->getKey()] = $holder; |
1552: | } |
1553: | |
1554: | return $holders; |
1555: | } |
1556: | |
1557: | return []; |
1558: | } |
1559: | |
1560: | |
1561: | |
1562: | |
1563: | private function processBooleanNotSureConditionalTypes(Scope $scope, SpecifiedTypes $leftTypes, SpecifiedTypes $rightTypes): array |
1564: | { |
1565: | $conditionExpressionTypes = []; |
1566: | foreach ($leftTypes->getSureNotTypes() as $exprString => [$expr, $type]) { |
1567: | if (!$expr instanceof Expr\Variable) { |
1568: | continue; |
1569: | } |
1570: | if (!is_string($expr->name)) { |
1571: | continue; |
1572: | } |
1573: | |
1574: | $conditionExpressionTypes[$exprString] = ExpressionTypeHolder::createYes( |
1575: | $expr, |
1576: | TypeCombinator::intersect($scope->getType($expr), $type), |
1577: | ); |
1578: | } |
1579: | |
1580: | if (count($conditionExpressionTypes) > 0) { |
1581: | $holders = []; |
1582: | foreach ($rightTypes->getSureNotTypes() as $exprString => [$expr, $type]) { |
1583: | if (!$expr instanceof Expr\Variable) { |
1584: | continue; |
1585: | } |
1586: | if (!is_string($expr->name)) { |
1587: | continue; |
1588: | } |
1589: | |
1590: | if (!isset($holders[$exprString])) { |
1591: | $holders[$exprString] = []; |
1592: | } |
1593: | |
1594: | $conditions = $conditionExpressionTypes; |
1595: | foreach ($conditions as $conditionExprString => $conditionExprTypeHolder) { |
1596: | $conditionExpr = $conditionExprTypeHolder->getExpr(); |
1597: | if (!$conditionExpr instanceof Expr\Variable) { |
1598: | continue; |
1599: | } |
1600: | if (!is_string($conditionExpr->name)) { |
1601: | continue; |
1602: | } |
1603: | if ($conditionExpr->name !== $expr->name) { |
1604: | continue; |
1605: | } |
1606: | |
1607: | unset($conditions[$conditionExprString]); |
1608: | } |
1609: | |
1610: | if (count($conditions) === 0) { |
1611: | continue; |
1612: | } |
1613: | |
1614: | $holder = new ConditionalExpressionHolder( |
1615: | $conditions, |
1616: | new ExpressionTypeHolder($expr, TypeCombinator::remove($scope->getType($expr), $type), TrinaryLogic::createYes()), |
1617: | ); |
1618: | $holders[$exprString][$holder->getKey()] = $holder; |
1619: | } |
1620: | |
1621: | return $holders; |
1622: | } |
1623: | |
1624: | return []; |
1625: | } |
1626: | |
1627: | |
1628: | |
1629: | |
1630: | private function findTypeExpressionsFromBinaryOperation(Scope $scope, Node\Expr\BinaryOp $binaryOperation): ?array |
1631: | { |
1632: | $leftType = $scope->getType($binaryOperation->left); |
1633: | $rightType = $scope->getType($binaryOperation->right); |
1634: | |
1635: | $rightExpr = $binaryOperation->right; |
1636: | if ($rightExpr instanceof AlwaysRememberedExpr) { |
1637: | $rightExpr = $rightExpr->getExpr(); |
1638: | } |
1639: | |
1640: | $leftExpr = $binaryOperation->left; |
1641: | if ($leftExpr instanceof AlwaysRememberedExpr) { |
1642: | $leftExpr = $leftExpr->getExpr(); |
1643: | } |
1644: | |
1645: | if ( |
1646: | $leftType instanceof ConstantScalarType |
1647: | && !$rightExpr instanceof ConstFetch |
1648: | && !$rightExpr instanceof ClassConstFetch |
1649: | ) { |
1650: | return [$binaryOperation->right, $leftType, $rightType]; |
1651: | } elseif ( |
1652: | $rightType instanceof ConstantScalarType |
1653: | && !$leftExpr instanceof ConstFetch |
1654: | && !$leftExpr instanceof ClassConstFetch |
1655: | ) { |
1656: | return [$binaryOperation->left, $rightType, $leftType]; |
1657: | } |
1658: | |
1659: | return null; |
1660: | } |
1661: | |
1662: | |
1663: | public function create( |
1664: | Expr $expr, |
1665: | Type $type, |
1666: | TypeSpecifierContext $context, |
1667: | Scope $scope, |
1668: | ): SpecifiedTypes |
1669: | { |
1670: | if ($expr instanceof Instanceof_ || $expr instanceof Expr\List_) { |
1671: | return (new SpecifiedTypes([], []))->setRootExpr($expr); |
1672: | } |
1673: | |
1674: | $specifiedExprs = []; |
1675: | if ($expr instanceof AlwaysRememberedExpr) { |
1676: | $specifiedExprs[] = $expr; |
1677: | $expr = $expr->expr; |
1678: | } |
1679: | |
1680: | if ($expr instanceof Expr\Assign) { |
1681: | $specifiedExprs[] = $expr->var; |
1682: | $specifiedExprs[] = $expr->expr; |
1683: | |
1684: | while ($expr->expr instanceof Expr\Assign) { |
1685: | $specifiedExprs[] = $expr->expr->var; |
1686: | $expr = $expr->expr; |
1687: | } |
1688: | } elseif ($expr instanceof Expr\AssignOp\Coalesce) { |
1689: | $specifiedExprs[] = $expr->var; |
1690: | } else { |
1691: | $specifiedExprs[] = $expr; |
1692: | } |
1693: | |
1694: | $types = null; |
1695: | |
1696: | foreach ($specifiedExprs as $specifiedExpr) { |
1697: | $newTypes = $this->createForExpr($specifiedExpr, $type, $context, $scope); |
1698: | |
1699: | if ($types === null) { |
1700: | $types = $newTypes; |
1701: | } else { |
1702: | $types = $types->unionWith($newTypes); |
1703: | } |
1704: | } |
1705: | |
1706: | return $types; |
1707: | } |
1708: | |
1709: | private function createForExpr( |
1710: | Expr $expr, |
1711: | Type $type, |
1712: | TypeSpecifierContext $context, |
1713: | Scope $scope, |
1714: | ): SpecifiedTypes |
1715: | { |
1716: | if ($context->true()) { |
1717: | $containsNull = !$type->isNull()->no() && !$scope->getType($expr)->isNull()->no(); |
1718: | } elseif ($context->false()) { |
1719: | $containsNull = !TypeCombinator::containsNull($type) && !$scope->getType($expr)->isNull()->no(); |
1720: | } |
1721: | |
1722: | $originalExpr = $expr; |
1723: | if (isset($containsNull) && !$containsNull) { |
1724: | $expr = NullsafeOperatorHelper::getNullsafeShortcircuitedExpr($expr); |
1725: | } |
1726: | |
1727: | if ( |
1728: | !$context->null() |
1729: | && $expr instanceof Expr\BinaryOp\Coalesce |
1730: | ) { |
1731: | $rightIsSuperType = $type->isSuperTypeOf($scope->getType($expr->right)); |
1732: | if (($context->true() && $rightIsSuperType->no()) || ($context->false() && $rightIsSuperType->yes())) { |
1733: | $expr = $expr->left; |
1734: | } |
1735: | } |
1736: | |
1737: | if ( |
1738: | $expr instanceof FuncCall |
1739: | && $expr->name instanceof Name |
1740: | ) { |
1741: | $has = $this->reflectionProvider->hasFunction($expr->name, $scope); |
1742: | if (!$has) { |
1743: | |
1744: | return new SpecifiedTypes([], []); |
1745: | } |
1746: | |
1747: | $functionReflection = $this->reflectionProvider->getFunction($expr->name, $scope); |
1748: | $hasSideEffects = $functionReflection->hasSideEffects(); |
1749: | if ($hasSideEffects->yes()) { |
1750: | return new SpecifiedTypes([], []); |
1751: | } |
1752: | |
1753: | if (!$this->rememberPossiblyImpureFunctionValues && !$hasSideEffects->no()) { |
1754: | return new SpecifiedTypes([], []); |
1755: | } |
1756: | } |
1757: | |
1758: | if ( |
1759: | $expr instanceof MethodCall |
1760: | && $expr->name instanceof Node\Identifier |
1761: | ) { |
1762: | $methodName = $expr->name->toString(); |
1763: | $calledOnType = $scope->getType($expr->var); |
1764: | $methodReflection = $scope->getMethodReflection($calledOnType, $methodName); |
1765: | if ( |
1766: | $methodReflection === null |
1767: | || $methodReflection->hasSideEffects()->yes() |
1768: | || (!$this->rememberPossiblyImpureFunctionValues && !$methodReflection->hasSideEffects()->no()) |
1769: | ) { |
1770: | if (isset($containsNull) && !$containsNull) { |
1771: | return $this->createNullsafeTypes($originalExpr, $scope, $context, $type); |
1772: | } |
1773: | |
1774: | return new SpecifiedTypes([], []); |
1775: | } |
1776: | } |
1777: | |
1778: | if ( |
1779: | $expr instanceof StaticCall |
1780: | && $expr->name instanceof Node\Identifier |
1781: | ) { |
1782: | $methodName = $expr->name->toString(); |
1783: | if ($expr->class instanceof Name) { |
1784: | $calledOnType = $scope->resolveTypeByName($expr->class); |
1785: | } else { |
1786: | $calledOnType = $scope->getType($expr->class); |
1787: | } |
1788: | |
1789: | $methodReflection = $scope->getMethodReflection($calledOnType, $methodName); |
1790: | if ( |
1791: | $methodReflection === null |
1792: | || $methodReflection->hasSideEffects()->yes() |
1793: | || (!$this->rememberPossiblyImpureFunctionValues && !$methodReflection->hasSideEffects()->no()) |
1794: | ) { |
1795: | if (isset($containsNull) && !$containsNull) { |
1796: | return $this->createNullsafeTypes($originalExpr, $scope, $context, $type); |
1797: | } |
1798: | |
1799: | return new SpecifiedTypes([], []); |
1800: | } |
1801: | } |
1802: | |
1803: | $sureTypes = []; |
1804: | $sureNotTypes = []; |
1805: | $exprString = $this->exprPrinter->printExpr($expr); |
1806: | $originalExprString = $this->exprPrinter->printExpr($originalExpr); |
1807: | if ($context->false()) { |
1808: | $sureNotTypes[$exprString] = [$expr, $type]; |
1809: | if ($exprString !== $originalExprString) { |
1810: | $sureNotTypes[$originalExprString] = [$originalExpr, $type]; |
1811: | } |
1812: | } elseif ($context->true()) { |
1813: | $sureTypes[$exprString] = [$expr, $type]; |
1814: | if ($exprString !== $originalExprString) { |
1815: | $sureTypes[$originalExprString] = [$originalExpr, $type]; |
1816: | } |
1817: | } |
1818: | |
1819: | $types = new SpecifiedTypes($sureTypes, $sureNotTypes); |
1820: | if (isset($containsNull) && !$containsNull) { |
1821: | return $this->createNullsafeTypes($originalExpr, $scope, $context, $type)->unionWith($types); |
1822: | } |
1823: | |
1824: | return $types; |
1825: | } |
1826: | |
1827: | private function createNullsafeTypes(Expr $expr, Scope $scope, TypeSpecifierContext $context, ?Type $type): SpecifiedTypes |
1828: | { |
1829: | if ($expr instanceof Expr\NullsafePropertyFetch) { |
1830: | if ($type !== null) { |
1831: | $propertyFetchTypes = $this->create(new PropertyFetch($expr->var, $expr->name), $type, $context, $scope); |
1832: | } else { |
1833: | $propertyFetchTypes = $this->create(new PropertyFetch($expr->var, $expr->name), new NullType(), TypeSpecifierContext::createFalse(), $scope); |
1834: | } |
1835: | |
1836: | return $propertyFetchTypes->unionWith( |
1837: | $this->create($expr->var, new NullType(), TypeSpecifierContext::createFalse(), $scope), |
1838: | ); |
1839: | } |
1840: | |
1841: | if ($expr instanceof Expr\NullsafeMethodCall) { |
1842: | if ($type !== null) { |
1843: | $methodCallTypes = $this->create(new MethodCall($expr->var, $expr->name, $expr->args), $type, $context, $scope); |
1844: | } else { |
1845: | $methodCallTypes = $this->create(new MethodCall($expr->var, $expr->name, $expr->args), new NullType(), TypeSpecifierContext::createFalse(), $scope); |
1846: | } |
1847: | |
1848: | return $methodCallTypes->unionWith( |
1849: | $this->create($expr->var, new NullType(), TypeSpecifierContext::createFalse(), $scope), |
1850: | ); |
1851: | } |
1852: | |
1853: | if ($expr instanceof Expr\PropertyFetch) { |
1854: | return $this->createNullsafeTypes($expr->var, $scope, $context, null); |
1855: | } |
1856: | |
1857: | if ($expr instanceof Expr\MethodCall) { |
1858: | return $this->createNullsafeTypes($expr->var, $scope, $context, null); |
1859: | } |
1860: | |
1861: | if ($expr instanceof Expr\ArrayDimFetch) { |
1862: | return $this->createNullsafeTypes($expr->var, $scope, $context, null); |
1863: | } |
1864: | |
1865: | if ($expr instanceof Expr\StaticPropertyFetch && $expr->class instanceof Expr) { |
1866: | return $this->createNullsafeTypes($expr->class, $scope, $context, null); |
1867: | } |
1868: | |
1869: | if ($expr instanceof Expr\StaticCall && $expr->class instanceof Expr) { |
1870: | return $this->createNullsafeTypes($expr->class, $scope, $context, null); |
1871: | } |
1872: | |
1873: | return new SpecifiedTypes([], []); |
1874: | } |
1875: | |
1876: | private function createRangeTypes(?Expr $rootExpr, Expr $expr, Type $type, TypeSpecifierContext $context): SpecifiedTypes |
1877: | { |
1878: | $sureNotTypes = []; |
1879: | |
1880: | if ($type instanceof IntegerRangeType || $type instanceof ConstantIntegerType) { |
1881: | $exprString = $this->exprPrinter->printExpr($expr); |
1882: | if ($context->false()) { |
1883: | $sureNotTypes[$exprString] = [$expr, $type]; |
1884: | } elseif ($context->true()) { |
1885: | $inverted = TypeCombinator::remove(new IntegerType(), $type); |
1886: | $sureNotTypes[$exprString] = [$expr, $inverted]; |
1887: | } |
1888: | } |
1889: | |
1890: | return (new SpecifiedTypes([], $sureNotTypes))->setRootExpr($rootExpr); |
1891: | } |
1892: | |
1893: | |
1894: | |
1895: | |
1896: | private function getFunctionTypeSpecifyingExtensions(): array |
1897: | { |
1898: | return $this->functionTypeSpecifyingExtensions; |
1899: | } |
1900: | |
1901: | |
1902: | |
1903: | |
1904: | private function getMethodTypeSpecifyingExtensionsForClass(string $className): array |
1905: | { |
1906: | if ($this->methodTypeSpecifyingExtensionsByClass === null) { |
1907: | $byClass = []; |
1908: | foreach ($this->methodTypeSpecifyingExtensions as $extension) { |
1909: | $byClass[$extension->getClass()][] = $extension; |
1910: | } |
1911: | |
1912: | $this->methodTypeSpecifyingExtensionsByClass = $byClass; |
1913: | } |
1914: | return $this->getTypeSpecifyingExtensionsForType($this->methodTypeSpecifyingExtensionsByClass, $className); |
1915: | } |
1916: | |
1917: | |
1918: | |
1919: | |
1920: | private function getStaticMethodTypeSpecifyingExtensionsForClass(string $className): array |
1921: | { |
1922: | if ($this->staticMethodTypeSpecifyingExtensionsByClass === null) { |
1923: | $byClass = []; |
1924: | foreach ($this->staticMethodTypeSpecifyingExtensions as $extension) { |
1925: | $byClass[$extension->getClass()][] = $extension; |
1926: | } |
1927: | |
1928: | $this->staticMethodTypeSpecifyingExtensionsByClass = $byClass; |
1929: | } |
1930: | return $this->getTypeSpecifyingExtensionsForType($this->staticMethodTypeSpecifyingExtensionsByClass, $className); |
1931: | } |
1932: | |
1933: | |
1934: | |
1935: | |
1936: | |
1937: | private function getTypeSpecifyingExtensionsForType(array $extensions, string $className): array |
1938: | { |
1939: | $extensionsForClass = [[]]; |
1940: | $class = $this->reflectionProvider->getClass($className); |
1941: | foreach (array_merge([$className], $class->getParentClassesNames(), $class->getNativeReflection()->getInterfaceNames()) as $extensionClassName) { |
1942: | if (!isset($extensions[$extensionClassName])) { |
1943: | continue; |
1944: | } |
1945: | |
1946: | $extensionsForClass[] = $extensions[$extensionClassName]; |
1947: | } |
1948: | |
1949: | return array_merge(...$extensionsForClass); |
1950: | } |
1951: | |
1952: | public function resolveEqual(Expr\BinaryOp\Equal $expr, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes |
1953: | { |
1954: | $expressions = $this->findTypeExpressionsFromBinaryOperation($scope, $expr); |
1955: | if ($expressions !== null) { |
1956: | $exprNode = $expressions[0]; |
1957: | $constantType = $expressions[1]; |
1958: | $otherType = $expressions[2]; |
1959: | |
1960: | if (!$context->null() && $constantType->getValue() === null) { |
1961: | $trueTypes = [ |
1962: | new NullType(), |
1963: | new ConstantBooleanType(false), |
1964: | new ConstantIntegerType(0), |
1965: | new ConstantFloatType(0.0), |
1966: | new ConstantStringType(''), |
1967: | new ConstantArrayType([], []), |
1968: | ]; |
1969: | return $this->create($exprNode, new UnionType($trueTypes), $context, $scope)->setRootExpr($expr); |
1970: | } |
1971: | |
1972: | if (!$context->null() && $constantType->getValue() === false) { |
1973: | return $this->specifyTypesInCondition( |
1974: | $scope, |
1975: | $exprNode, |
1976: | $context->true() ? TypeSpecifierContext::createFalsey() : TypeSpecifierContext::createFalsey()->negate(), |
1977: | )->setRootExpr($expr); |
1978: | } |
1979: | |
1980: | if (!$context->null() && $constantType->getValue() === true) { |
1981: | return $this->specifyTypesInCondition( |
1982: | $scope, |
1983: | $exprNode, |
1984: | $context->true() ? TypeSpecifierContext::createTruthy() : TypeSpecifierContext::createTruthy()->negate(), |
1985: | )->setRootExpr($expr); |
1986: | } |
1987: | |
1988: | if (!$context->null() && $constantType->getValue() === 0 && !$otherType->isInteger()->yes() && !$otherType->isBoolean()->yes()) { |
1989: | |
1990: | |
1991: | |
1992: | if ($context->true()) { |
1993: | $trueTypes = [ |
1994: | new NullType(), |
1995: | new ConstantBooleanType(false), |
1996: | new ConstantIntegerType(0), |
1997: | new ConstantFloatType(0.0), |
1998: | new StringType(), |
1999: | ]; |
2000: | } else { |
2001: | $trueTypes = [ |
2002: | new NullType(), |
2003: | new ConstantBooleanType(false), |
2004: | new ConstantIntegerType(0), |
2005: | new ConstantFloatType(0.0), |
2006: | new ConstantStringType('0'), |
2007: | ]; |
2008: | } |
2009: | return $this->create($exprNode, new UnionType($trueTypes), $context, $scope)->setRootExpr($expr); |
2010: | } |
2011: | |
2012: | if (!$context->null() && $constantType->getValue() === '') { |
2013: | |
2014: | |
2015: | |
2016: | if ($context->true()) { |
2017: | $trueTypes = [ |
2018: | new NullType(), |
2019: | new ConstantBooleanType(false), |
2020: | new ConstantIntegerType(0), |
2021: | new ConstantFloatType(0.0), |
2022: | new ConstantStringType(''), |
2023: | ]; |
2024: | } else { |
2025: | $trueTypes = [ |
2026: | new NullType(), |
2027: | new ConstantBooleanType(false), |
2028: | new ConstantStringType(''), |
2029: | ]; |
2030: | } |
2031: | return $this->create($exprNode, new UnionType($trueTypes), $context, $scope)->setRootExpr($expr); |
2032: | } |
2033: | |
2034: | if ( |
2035: | $exprNode instanceof FuncCall |
2036: | && $exprNode->name instanceof Name |
2037: | && in_array(strtolower($exprNode->name->toString()), ['gettype', 'get_class', 'get_debug_type'], true) |
2038: | && isset($exprNode->getArgs()[0]) |
2039: | && $constantType->isString()->yes() |
2040: | ) { |
2041: | return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context)->setRootExpr($expr); |
2042: | } |
2043: | |
2044: | if ( |
2045: | $context->true() |
2046: | && $exprNode instanceof FuncCall |
2047: | && $exprNode->name instanceof Name |
2048: | && $exprNode->name->toLowerString() === 'preg_match' |
2049: | && (new ConstantIntegerType(1))->isSuperTypeOf($constantType)->yes() |
2050: | ) { |
2051: | return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context)->setRootExpr($expr); |
2052: | } |
2053: | } |
2054: | |
2055: | $leftType = $scope->getType($expr->left); |
2056: | $rightType = $scope->getType($expr->right); |
2057: | |
2058: | $leftBooleanType = $leftType->toBoolean(); |
2059: | if ($leftBooleanType instanceof ConstantBooleanType && $rightType->isBoolean()->yes()) { |
2060: | return $this->specifyTypesInCondition( |
2061: | $scope, |
2062: | new Expr\BinaryOp\Identical( |
2063: | new ConstFetch(new Name($leftBooleanType->getValue() ? 'true' : 'false')), |
2064: | $expr->right, |
2065: | ), |
2066: | $context, |
2067: | )->setRootExpr($expr); |
2068: | } |
2069: | |
2070: | $rightBooleanType = $rightType->toBoolean(); |
2071: | if ($rightBooleanType instanceof ConstantBooleanType && $leftType->isBoolean()->yes()) { |
2072: | return $this->specifyTypesInCondition( |
2073: | $scope, |
2074: | new Expr\BinaryOp\Identical( |
2075: | $expr->left, |
2076: | new ConstFetch(new Name($rightBooleanType->getValue() ? 'true' : 'false')), |
2077: | ), |
2078: | $context, |
2079: | )->setRootExpr($expr); |
2080: | } |
2081: | |
2082: | if ( |
2083: | !$context->null() |
2084: | && $rightType->isArray()->yes() |
2085: | && $leftType->isConstantArray()->yes() && $leftType->isIterableAtLeastOnce()->no() |
2086: | ) { |
2087: | return $this->create($expr->right, new NonEmptyArrayType(), $context->negate(), $scope)->setRootExpr($expr); |
2088: | } |
2089: | |
2090: | if ( |
2091: | !$context->null() |
2092: | && $leftType->isArray()->yes() |
2093: | && $rightType->isConstantArray()->yes() && $rightType->isIterableAtLeastOnce()->no() |
2094: | ) { |
2095: | return $this->create($expr->left, new NonEmptyArrayType(), $context->negate(), $scope)->setRootExpr($expr); |
2096: | } |
2097: | |
2098: | if ( |
2099: | ($leftType->isString()->yes() && $rightType->isString()->yes()) |
2100: | || ($leftType->isInteger()->yes() && $rightType->isInteger()->yes()) |
2101: | || ($leftType->isFloat()->yes() && $rightType->isFloat()->yes()) |
2102: | || ($leftType->isEnum()->yes() && $rightType->isEnum()->yes()) |
2103: | ) { |
2104: | return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context)->setRootExpr($expr); |
2105: | } |
2106: | |
2107: | $leftExprString = $this->exprPrinter->printExpr($expr->left); |
2108: | $rightExprString = $this->exprPrinter->printExpr($expr->right); |
2109: | if ($leftExprString === $rightExprString) { |
2110: | if (!$expr->left instanceof Expr\Variable || !$expr->right instanceof Expr\Variable) { |
2111: | return (new SpecifiedTypes([], []))->setRootExpr($expr); |
2112: | } |
2113: | } |
2114: | |
2115: | $leftTypes = $this->create($expr->left, $leftType, $context, $scope)->setRootExpr($expr); |
2116: | $rightTypes = $this->create($expr->right, $rightType, $context, $scope)->setRootExpr($expr); |
2117: | |
2118: | return $context->true() |
2119: | ? $leftTypes->unionWith($rightTypes) |
2120: | : $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($scope)); |
2121: | } |
2122: | |
2123: | public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes |
2124: | { |
2125: | |
2126: | $leftExpr = $expr->left; |
2127: | $rightExpr = $expr->right; |
2128: | if ($rightExpr instanceof FuncCall && !$leftExpr instanceof FuncCall) { |
2129: | [$leftExpr, $rightExpr] = [$rightExpr, $leftExpr]; |
2130: | } |
2131: | |
2132: | $unwrappedLeftExpr = $leftExpr; |
2133: | if ($leftExpr instanceof AlwaysRememberedExpr) { |
2134: | $unwrappedLeftExpr = $leftExpr->getExpr(); |
2135: | } |
2136: | $unwrappedRightExpr = $rightExpr; |
2137: | if ($rightExpr instanceof AlwaysRememberedExpr) { |
2138: | $unwrappedRightExpr = $rightExpr->getExpr(); |
2139: | } |
2140: | |
2141: | $rightType = $scope->getType($rightExpr); |
2142: | |
2143: | |
2144: | if ( |
2145: | !$context->null() |
2146: | && $unwrappedLeftExpr instanceof FuncCall |
2147: | && count($unwrappedLeftExpr->getArgs()) >= 1 |
2148: | && $unwrappedLeftExpr->name instanceof Name |
2149: | && in_array(strtolower((string) $unwrappedLeftExpr->name), ['count', 'sizeof'], true) |
2150: | && $rightType->isInteger()->yes() |
2151: | ) { |
2152: | if (IntegerRangeType::fromInterval(null, -1)->isSuperTypeOf($rightType)->yes()) { |
2153: | return $this->create($unwrappedLeftExpr->getArgs()[0]->value, new NeverType(), $context, $scope)->setRootExpr($expr); |
2154: | } |
2155: | |
2156: | $argType = $scope->getType($unwrappedLeftExpr->getArgs()[0]->value); |
2157: | $isZero = (new ConstantIntegerType(0))->isSuperTypeOf($rightType); |
2158: | if ($isZero->yes()) { |
2159: | $funcTypes = $this->create($unwrappedLeftExpr, $rightType, $context, $scope)->setRootExpr($expr); |
2160: | |
2161: | if ($context->truthy() && !$argType->isArray()->yes()) { |
2162: | $newArgType = new UnionType([ |
2163: | new ObjectType(Countable::class), |
2164: | new ConstantArrayType([], []), |
2165: | ]); |
2166: | } else { |
2167: | $newArgType = new ConstantArrayType([], []); |
2168: | } |
2169: | |
2170: | return $funcTypes->unionWith( |
2171: | $this->create($unwrappedLeftExpr->getArgs()[0]->value, $newArgType, $context, $scope)->setRootExpr($expr), |
2172: | ); |
2173: | } |
2174: | |
2175: | $specifiedTypes = $this->specifyTypesForCountFuncCall($unwrappedLeftExpr, $argType, $rightType, $context, $scope, $expr); |
2176: | if ($specifiedTypes !== null) { |
2177: | return $specifiedTypes; |
2178: | } |
2179: | |
2180: | if ($context->truthy() && $argType->isArray()->yes()) { |
2181: | $funcTypes = $this->create($unwrappedLeftExpr, $rightType, $context, $scope)->setRootExpr($expr); |
2182: | if (IntegerRangeType::fromInterval(1, null)->isSuperTypeOf($rightType)->yes()) { |
2183: | return $funcTypes->unionWith( |
2184: | $this->create($unwrappedLeftExpr->getArgs()[0]->value, new NonEmptyArrayType(), $context, $scope)->setRootExpr($expr), |
2185: | ); |
2186: | } |
2187: | |
2188: | return $funcTypes; |
2189: | } |
2190: | } |
2191: | |
2192: | |
2193: | if ( |
2194: | !$context->null() |
2195: | && $unwrappedLeftExpr instanceof FuncCall |
2196: | && count($unwrappedLeftExpr->getArgs()) === 1 |
2197: | && $unwrappedLeftExpr->name instanceof Name |
2198: | && in_array(strtolower((string) $unwrappedLeftExpr->name), ['strlen', 'mb_strlen'], true) |
2199: | && $rightType->isInteger()->yes() |
2200: | ) { |
2201: | if (IntegerRangeType::fromInterval(null, -1)->isSuperTypeOf($rightType)->yes()) { |
2202: | return $this->create($unwrappedLeftExpr->getArgs()[0]->value, new NeverType(), $context, $scope)->setRootExpr($expr); |
2203: | } |
2204: | |
2205: | $isZero = (new ConstantIntegerType(0))->isSuperTypeOf($rightType); |
2206: | if ($isZero->yes()) { |
2207: | $funcTypes = $this->create($unwrappedLeftExpr, $rightType, $context, $scope)->setRootExpr($expr); |
2208: | return $funcTypes->unionWith( |
2209: | $this->create($unwrappedLeftExpr->getArgs()[0]->value, new ConstantStringType(''), $context, $scope)->setRootExpr($expr), |
2210: | ); |
2211: | } |
2212: | |
2213: | if ($context->truthy() && IntegerRangeType::fromInterval(1, null)->isSuperTypeOf($rightType)->yes()) { |
2214: | $argType = $scope->getType($unwrappedLeftExpr->getArgs()[0]->value); |
2215: | if ($argType->isString()->yes()) { |
2216: | $funcTypes = $this->create($unwrappedLeftExpr, $rightType, $context, $scope)->setRootExpr($expr); |
2217: | |
2218: | $accessory = new AccessoryNonEmptyStringType(); |
2219: | if (IntegerRangeType::fromInterval(2, null)->isSuperTypeOf($rightType)->yes()) { |
2220: | $accessory = new AccessoryNonFalsyStringType(); |
2221: | } |
2222: | $valueTypes = $this->create($unwrappedLeftExpr->getArgs()[0]->value, $accessory, $context, $scope)->setRootExpr($expr); |
2223: | |
2224: | return $funcTypes->unionWith($valueTypes); |
2225: | } |
2226: | } |
2227: | } |
2228: | |
2229: | |
2230: | if ( |
2231: | $context->true() |
2232: | && $unwrappedLeftExpr instanceof FuncCall |
2233: | && $unwrappedLeftExpr->name instanceof Name |
2234: | && $unwrappedLeftExpr->name->toLowerString() === 'preg_match' |
2235: | && (new ConstantIntegerType(1))->isSuperTypeOf($rightType)->yes() |
2236: | ) { |
2237: | return $this->specifyTypesInCondition( |
2238: | $scope, |
2239: | $leftExpr, |
2240: | $context, |
2241: | )->setRootExpr($expr); |
2242: | } |
2243: | |
2244: | |
2245: | if ( |
2246: | $context->true() |
2247: | && $unwrappedLeftExpr instanceof FuncCall |
2248: | && $unwrappedLeftExpr->name instanceof Name |
2249: | && in_array(strtolower($unwrappedLeftExpr->name->toString()), ['get_class', 'get_debug_type'], true) |
2250: | && isset($unwrappedLeftExpr->getArgs()[0]) |
2251: | ) { |
2252: | if ($rightType instanceof ConstantStringType && $this->reflectionProvider->hasClass($rightType->getValue())) { |
2253: | return $this->create( |
2254: | $unwrappedLeftExpr->getArgs()[0]->value, |
2255: | new ObjectType($rightType->getValue(), null, $this->reflectionProvider->getClass($rightType->getValue())->asFinal()), |
2256: | $context, |
2257: | $scope, |
2258: | )->unionWith($this->create($leftExpr, $rightType, $context, $scope))->setRootExpr($expr); |
2259: | } |
2260: | if ($rightType->getClassStringObjectType()->isObject()->yes()) { |
2261: | return $this->create( |
2262: | $unwrappedLeftExpr->getArgs()[0]->value, |
2263: | $rightType->getClassStringObjectType(), |
2264: | $context, |
2265: | $scope, |
2266: | )->unionWith($this->create($leftExpr, $rightType, $context, $scope))->setRootExpr($expr); |
2267: | } |
2268: | } |
2269: | |
2270: | if ( |
2271: | $context->truthy() |
2272: | && $unwrappedLeftExpr instanceof FuncCall |
2273: | && $unwrappedLeftExpr->name instanceof Name |
2274: | && in_array(strtolower($unwrappedLeftExpr->name->toString()), [ |
2275: | 'substr', 'strstr', 'stristr', 'strchr', 'strrchr', 'strtolower', 'strtoupper', 'ucfirst', 'lcfirst', |
2276: | 'mb_substr', 'mb_strstr', 'mb_stristr', 'mb_strchr', 'mb_strrchr', 'mb_strtolower', 'mb_strtoupper', 'mb_ucfirst', 'mb_lcfirst', |
2277: | 'ucwords', 'mb_convert_case', 'mb_convert_kana', |
2278: | ], true) |
2279: | && isset($unwrappedLeftExpr->getArgs()[0]) |
2280: | && $rightType->isNonEmptyString()->yes() |
2281: | ) { |
2282: | $argType = $scope->getType($unwrappedLeftExpr->getArgs()[0]->value); |
2283: | |
2284: | if ($argType->isString()->yes()) { |
2285: | if ($rightType->isNonFalsyString()->yes()) { |
2286: | return $this->create( |
2287: | $unwrappedLeftExpr->getArgs()[0]->value, |
2288: | TypeCombinator::intersect($argType, new AccessoryNonFalsyStringType()), |
2289: | $context, |
2290: | $scope, |
2291: | )->setRootExpr($expr); |
2292: | } |
2293: | |
2294: | return $this->create( |
2295: | $unwrappedLeftExpr->getArgs()[0]->value, |
2296: | TypeCombinator::intersect($argType, new AccessoryNonEmptyStringType()), |
2297: | $context, |
2298: | $scope, |
2299: | )->setRootExpr($expr); |
2300: | } |
2301: | } |
2302: | |
2303: | if ($rightType->isString()->yes()) { |
2304: | $types = null; |
2305: | foreach ($rightType->getConstantStrings() as $constantString) { |
2306: | $specifiedType = $this->specifyTypesForConstantStringBinaryExpression($unwrappedLeftExpr, $constantString, $context, $scope, $expr); |
2307: | |
2308: | if ($specifiedType === null) { |
2309: | continue; |
2310: | } |
2311: | if ($types === null) { |
2312: | $types = $specifiedType; |
2313: | continue; |
2314: | } |
2315: | |
2316: | $types = $types->intersectWith($specifiedType); |
2317: | } |
2318: | |
2319: | if ($types !== null) { |
2320: | if ($leftExpr !== $unwrappedLeftExpr) { |
2321: | $types = $types->unionWith($this->create($leftExpr, $rightType, $context, $scope)->setRootExpr($expr)); |
2322: | } |
2323: | return $types; |
2324: | } |
2325: | } |
2326: | |
2327: | $expressions = $this->findTypeExpressionsFromBinaryOperation($scope, $expr); |
2328: | if ($expressions !== null) { |
2329: | $exprNode = $expressions[0]; |
2330: | $constantType = $expressions[1]; |
2331: | |
2332: | $unwrappedExprNode = $exprNode; |
2333: | if ($exprNode instanceof AlwaysRememberedExpr) { |
2334: | $unwrappedExprNode = $exprNode->getExpr(); |
2335: | } |
2336: | |
2337: | $specifiedType = $this->specifyTypesForConstantBinaryExpression($unwrappedExprNode, $constantType, $context, $scope, $expr); |
2338: | if ($specifiedType !== null) { |
2339: | if ($exprNode !== $unwrappedExprNode) { |
2340: | $specifiedType = $specifiedType->unionWith( |
2341: | $this->create($exprNode, $constantType, $context, $scope)->setRootExpr($expr), |
2342: | ); |
2343: | } |
2344: | return $specifiedType; |
2345: | } |
2346: | } |
2347: | |
2348: | |
2349: | if ( |
2350: | $context->true() && |
2351: | $unwrappedLeftExpr instanceof ClassConstFetch && |
2352: | $unwrappedLeftExpr->class instanceof Expr && |
2353: | $unwrappedLeftExpr->name instanceof Node\Identifier && |
2354: | $unwrappedRightExpr instanceof ClassConstFetch && |
2355: | $rightType instanceof ConstantStringType && |
2356: | $rightType->getValue() !== '' && |
2357: | strtolower($unwrappedLeftExpr->name->toString()) === 'class' |
2358: | ) { |
2359: | if ($this->reflectionProvider->hasClass($rightType->getValue())) { |
2360: | return $this->create( |
2361: | $unwrappedLeftExpr->class, |
2362: | new ObjectType($rightType->getValue(), null, $this->reflectionProvider->getClass($rightType->getValue())->asFinal()), |
2363: | $context, |
2364: | $scope, |
2365: | )->unionWith($this->create($leftExpr, $rightType, $context, $scope))->setRootExpr($expr); |
2366: | } |
2367: | return $this->specifyTypesInCondition( |
2368: | $scope, |
2369: | new Instanceof_( |
2370: | $unwrappedLeftExpr->class, |
2371: | new Name($rightType->getValue()), |
2372: | ), |
2373: | $context, |
2374: | )->unionWith($this->create($leftExpr, $rightType, $context, $scope))->setRootExpr($expr); |
2375: | } |
2376: | |
2377: | $leftType = $scope->getType($leftExpr); |
2378: | |
2379: | |
2380: | if ( |
2381: | $context->true() && |
2382: | $unwrappedRightExpr instanceof ClassConstFetch && |
2383: | $unwrappedRightExpr->class instanceof Expr && |
2384: | $unwrappedRightExpr->name instanceof Node\Identifier && |
2385: | $unwrappedLeftExpr instanceof ClassConstFetch && |
2386: | $leftType instanceof ConstantStringType && |
2387: | $leftType->getValue() !== '' && |
2388: | strtolower($unwrappedRightExpr->name->toString()) === 'class' |
2389: | ) { |
2390: | if ($this->reflectionProvider->hasClass($leftType->getValue())) { |
2391: | return $this->create( |
2392: | $unwrappedRightExpr->class, |
2393: | new ObjectType($leftType->getValue(), null, $this->reflectionProvider->getClass($leftType->getValue())->asFinal()), |
2394: | $context, |
2395: | $scope, |
2396: | )->unionWith($this->create($rightExpr, $leftType, $context, $scope)->setRootExpr($expr)); |
2397: | } |
2398: | |
2399: | return $this->specifyTypesInCondition( |
2400: | $scope, |
2401: | new Instanceof_( |
2402: | $unwrappedRightExpr->class, |
2403: | new Name($leftType->getValue()), |
2404: | ), |
2405: | $context, |
2406: | )->unionWith($this->create($rightExpr, $leftType, $context, $scope)->setRootExpr($expr)); |
2407: | } |
2408: | |
2409: | if ($context->false()) { |
2410: | $identicalType = $scope->getType($expr); |
2411: | if ($identicalType instanceof ConstantBooleanType) { |
2412: | $never = new NeverType(); |
2413: | $contextForTypes = $identicalType->getValue() ? $context->negate() : $context; |
2414: | $leftTypes = $this->create($leftExpr, $never, $contextForTypes, $scope)->setRootExpr($expr); |
2415: | $rightTypes = $this->create($rightExpr, $never, $contextForTypes, $scope)->setRootExpr($expr); |
2416: | if ($leftExpr instanceof AlwaysRememberedExpr) { |
2417: | $leftTypes = $leftTypes->unionWith( |
2418: | $this->create($unwrappedLeftExpr, $never, $contextForTypes, $scope)->setRootExpr($expr), |
2419: | ); |
2420: | } |
2421: | if ($rightExpr instanceof AlwaysRememberedExpr) { |
2422: | $rightTypes = $rightTypes->unionWith( |
2423: | $this->create($unwrappedRightExpr, $never, $contextForTypes, $scope)->setRootExpr($expr), |
2424: | ); |
2425: | } |
2426: | return $leftTypes->unionWith($rightTypes); |
2427: | } |
2428: | } |
2429: | |
2430: | $types = null; |
2431: | if ( |
2432: | count($leftType->getFiniteTypes()) === 1 |
2433: | || ( |
2434: | $context->true() |
2435: | && $leftType->isConstantValue()->yes() |
2436: | && !$rightType->equals($leftType) |
2437: | && $rightType->isSuperTypeOf($leftType)->yes()) |
2438: | ) { |
2439: | $types = $this->create( |
2440: | $rightExpr, |
2441: | $leftType, |
2442: | $context, |
2443: | $scope, |
2444: | )->setRootExpr($expr); |
2445: | if ($rightExpr instanceof AlwaysRememberedExpr) { |
2446: | $types = $types->unionWith($this->create( |
2447: | $unwrappedRightExpr, |
2448: | $leftType, |
2449: | $context, |
2450: | $scope, |
2451: | ))->setRootExpr($expr); |
2452: | } |
2453: | } |
2454: | if ( |
2455: | count($rightType->getFiniteTypes()) === 1 |
2456: | || ( |
2457: | $context->true() |
2458: | && $rightType->isConstantValue()->yes() |
2459: | && !$leftType->equals($rightType) |
2460: | && $leftType->isSuperTypeOf($rightType)->yes() |
2461: | ) |
2462: | ) { |
2463: | $leftTypes = $this->create( |
2464: | $leftExpr, |
2465: | $rightType, |
2466: | $context, |
2467: | $scope, |
2468: | )->setRootExpr($expr); |
2469: | if ($leftExpr instanceof AlwaysRememberedExpr) { |
2470: | $leftTypes = $leftTypes->unionWith($this->create( |
2471: | $unwrappedLeftExpr, |
2472: | $rightType, |
2473: | $context, |
2474: | $scope, |
2475: | ))->setRootExpr($expr); |
2476: | } |
2477: | if ($types !== null) { |
2478: | $types = $types->unionWith($leftTypes); |
2479: | } else { |
2480: | $types = $leftTypes; |
2481: | } |
2482: | } |
2483: | |
2484: | if ($types !== null) { |
2485: | return $types; |
2486: | } |
2487: | |
2488: | $leftExprString = $this->exprPrinter->printExpr($unwrappedLeftExpr); |
2489: | $rightExprString = $this->exprPrinter->printExpr($unwrappedRightExpr); |
2490: | if ($leftExprString === $rightExprString) { |
2491: | if (!$unwrappedLeftExpr instanceof Expr\Variable || !$unwrappedRightExpr instanceof Expr\Variable) { |
2492: | return (new SpecifiedTypes([], []))->setRootExpr($expr); |
2493: | } |
2494: | } |
2495: | |
2496: | if ($context->true()) { |
2497: | $leftTypes = $this->create($leftExpr, $rightType, $context, $scope)->setRootExpr($expr); |
2498: | $rightTypes = $this->create($rightExpr, $leftType, $context, $scope)->setRootExpr($expr); |
2499: | if ($leftExpr instanceof AlwaysRememberedExpr) { |
2500: | $leftTypes = $leftTypes->unionWith( |
2501: | $this->create($unwrappedLeftExpr, $rightType, $context, $scope)->setRootExpr($expr), |
2502: | ); |
2503: | } |
2504: | if ($rightExpr instanceof AlwaysRememberedExpr) { |
2505: | $rightTypes = $rightTypes->unionWith( |
2506: | $this->create($unwrappedRightExpr, $leftType, $context, $scope)->setRootExpr($expr), |
2507: | ); |
2508: | } |
2509: | return $leftTypes->unionWith($rightTypes); |
2510: | } elseif ($context->false()) { |
2511: | return $this->create($leftExpr, $leftType, $context, $scope)->setRootExpr($expr)->normalize($scope) |
2512: | ->intersectWith($this->create($rightExpr, $rightType, $context, $scope)->setRootExpr($expr)->normalize($scope)); |
2513: | } |
2514: | |
2515: | return (new SpecifiedTypes([], []))->setRootExpr($expr); |
2516: | } |
2517: | |
2518: | } |
2519: | |