1: | <?php declare(strict_types = 1); |
2: | |
3: | namespace PHPStan\Reflection; |
4: | |
5: | use Nette\Utils\Strings; |
6: | use PhpParser\Node\Arg; |
7: | use PhpParser\Node\Expr; |
8: | use PhpParser\Node\Expr\BinaryOp; |
9: | use PhpParser\Node\Expr\ClassConstFetch; |
10: | use PhpParser\Node\Expr\ConstFetch; |
11: | use PhpParser\Node\Expr\PropertyFetch; |
12: | use PhpParser\Node\Identifier; |
13: | use PhpParser\Node\Name; |
14: | use PhpParser\Node\Scalar\DNumber; |
15: | use PhpParser\Node\Scalar\LNumber; |
16: | use PhpParser\Node\Scalar\MagicConst; |
17: | use PhpParser\Node\Scalar\MagicConst\Dir; |
18: | use PhpParser\Node\Scalar\MagicConst\File; |
19: | use PhpParser\Node\Scalar\MagicConst\Line; |
20: | use PhpParser\Node\Scalar\String_; |
21: | use PHPStan\Analyser\ConstantResolver; |
22: | use PHPStan\Analyser\OutOfClassScope; |
23: | use PHPStan\DependencyInjection\Type\OperatorTypeSpecifyingExtensionRegistryProvider; |
24: | use PHPStan\Node\Expr\TypeExpr; |
25: | use PHPStan\Php\PhpVersion; |
26: | use PHPStan\Reflection\ReflectionProvider\ReflectionProviderProvider; |
27: | use PHPStan\ShouldNotHappenException; |
28: | use PHPStan\Type\Accessory\AccessoryArrayListType; |
29: | use PHPStan\Type\Accessory\AccessoryLiteralStringType; |
30: | use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; |
31: | use PHPStan\Type\Accessory\AccessoryNonFalsyStringType; |
32: | use PHPStan\Type\Accessory\AccessoryNumericStringType; |
33: | use PHPStan\Type\Accessory\NonEmptyArrayType; |
34: | use PHPStan\Type\ArrayType; |
35: | use PHPStan\Type\BenevolentUnionType; |
36: | use PHPStan\Type\BooleanType; |
37: | use PHPStan\Type\ClassStringType; |
38: | use PHPStan\Type\Constant\ConstantArrayType; |
39: | use PHPStan\Type\Constant\ConstantArrayTypeBuilder; |
40: | use PHPStan\Type\Constant\ConstantBooleanType; |
41: | use PHPStan\Type\Constant\ConstantFloatType; |
42: | use PHPStan\Type\Constant\ConstantIntegerType; |
43: | use PHPStan\Type\Constant\ConstantStringType; |
44: | use PHPStan\Type\Constant\OversizedArrayBuilder; |
45: | use PHPStan\Type\ConstantScalarType; |
46: | use PHPStan\Type\ConstantTypeHelper; |
47: | use PHPStan\Type\Enum\EnumCaseObjectType; |
48: | use PHPStan\Type\ErrorType; |
49: | use PHPStan\Type\FloatType; |
50: | use PHPStan\Type\GeneralizePrecision; |
51: | use PHPStan\Type\Generic\GenericClassStringType; |
52: | use PHPStan\Type\Generic\TemplateType; |
53: | use PHPStan\Type\IntegerRangeType; |
54: | use PHPStan\Type\IntegerType; |
55: | use PHPStan\Type\IntersectionType; |
56: | use PHPStan\Type\MixedType; |
57: | use PHPStan\Type\NeverType; |
58: | use PHPStan\Type\NullType; |
59: | use PHPStan\Type\ObjectType; |
60: | use PHPStan\Type\ObjectWithoutClassType; |
61: | use PHPStan\Type\StaticType; |
62: | use PHPStan\Type\StringType; |
63: | use PHPStan\Type\ThisType; |
64: | use PHPStan\Type\Type; |
65: | use PHPStan\Type\TypeCombinator; |
66: | use PHPStan\Type\TypehintHelper; |
67: | use PHPStan\Type\TypeTraverser; |
68: | use PHPStan\Type\TypeUtils; |
69: | use PHPStan\Type\TypeWithClassName; |
70: | use PHPStan\Type\UnionType; |
71: | use function array_key_exists; |
72: | use function array_keys; |
73: | use function array_merge; |
74: | use function assert; |
75: | use function ceil; |
76: | use function count; |
77: | use function dirname; |
78: | use function floor; |
79: | use function in_array; |
80: | use function intval; |
81: | use function is_finite; |
82: | use function is_float; |
83: | use function is_int; |
84: | use function is_numeric; |
85: | use function max; |
86: | use function min; |
87: | use function sprintf; |
88: | use function strtolower; |
89: | use const INF; |
90: | |
91: | final class InitializerExprTypeResolver |
92: | { |
93: | |
94: | public const CALCULATE_SCALARS_LIMIT = 128; |
95: | |
96: | |
97: | private array $currentlyResolvingClassConstant = []; |
98: | |
99: | public function __construct( |
100: | private ConstantResolver $constantResolver, |
101: | private ReflectionProviderProvider $reflectionProviderProvider, |
102: | private PhpVersion $phpVersion, |
103: | private OperatorTypeSpecifyingExtensionRegistryProvider $operatorTypeSpecifyingExtensionRegistryProvider, |
104: | private OversizedArrayBuilder $oversizedArrayBuilder, |
105: | private bool $usePathConstantsAsConstantString = false, |
106: | ) |
107: | { |
108: | } |
109: | |
110: | |
111: | public function getType(Expr $expr, InitializerExprContext $context): Type |
112: | { |
113: | if ($expr instanceof TypeExpr) { |
114: | return $expr->getExprType(); |
115: | } |
116: | if ($expr instanceof LNumber) { |
117: | return new ConstantIntegerType($expr->value); |
118: | } |
119: | if ($expr instanceof DNumber) { |
120: | return new ConstantFloatType($expr->value); |
121: | } |
122: | if ($expr instanceof String_) { |
123: | return new ConstantStringType($expr->value); |
124: | } |
125: | if ($expr instanceof ConstFetch) { |
126: | $constName = (string) $expr->name; |
127: | $loweredConstName = strtolower($constName); |
128: | if ($loweredConstName === 'true') { |
129: | return new ConstantBooleanType(true); |
130: | } elseif ($loweredConstName === 'false') { |
131: | return new ConstantBooleanType(false); |
132: | } elseif ($loweredConstName === 'null') { |
133: | return new NullType(); |
134: | } |
135: | |
136: | $constant = $this->constantResolver->resolveConstant($expr->name, $context); |
137: | if ($constant !== null) { |
138: | return $constant; |
139: | } |
140: | |
141: | return new ErrorType(); |
142: | } |
143: | if ($expr instanceof File) { |
144: | $file = $context->getFile(); |
145: | if ($file === null) { |
146: | return new StringType(); |
147: | } |
148: | $stringType = new ConstantStringType($file); |
149: | return $this->usePathConstantsAsConstantString ? $stringType : $stringType->generalize(GeneralizePrecision::moreSpecific()); |
150: | } |
151: | if ($expr instanceof Dir) { |
152: | $file = $context->getFile(); |
153: | if ($file === null) { |
154: | return new StringType(); |
155: | } |
156: | $stringType = new ConstantStringType(dirname($file)); |
157: | return $this->usePathConstantsAsConstantString ? $stringType : $stringType->generalize(GeneralizePrecision::moreSpecific()); |
158: | } |
159: | if ($expr instanceof Line) { |
160: | return new ConstantIntegerType($expr->getStartLine()); |
161: | } |
162: | if ($expr instanceof Expr\New_) { |
163: | if ($expr->class instanceof Name) { |
164: | return new ObjectType((string) $expr->class); |
165: | } |
166: | |
167: | return new ObjectWithoutClassType(); |
168: | } |
169: | if ($expr instanceof Expr\Array_) { |
170: | return $this->getArrayType($expr, fn (Expr $expr): Type => $this->getType($expr, $context)); |
171: | } |
172: | if ($expr instanceof Expr\ArrayDimFetch && $expr->dim !== null) { |
173: | $var = $this->getType($expr->var, $context); |
174: | $dim = $this->getType($expr->dim, $context); |
175: | return $var->getOffsetValueType($dim); |
176: | } |
177: | if ($expr instanceof ClassConstFetch && $expr->name instanceof Identifier) { |
178: | return $this->getClassConstFetchType($expr->class, $expr->name->toString(), $context->getClassName(), fn (Expr $expr): Type => $this->getType($expr, $context)); |
179: | } |
180: | if ($expr instanceof Expr\UnaryPlus) { |
181: | return $this->getType($expr->expr, $context)->toNumber(); |
182: | } |
183: | if ($expr instanceof Expr\UnaryMinus) { |
184: | return $this->getUnaryMinusType($expr->expr, fn (Expr $expr): Type => $this->getType($expr, $context)); |
185: | } |
186: | if ($expr instanceof Expr\BinaryOp\Coalesce) { |
187: | $leftType = $this->getType($expr->left, $context); |
188: | $rightType = $this->getType($expr->right, $context); |
189: | |
190: | return TypeCombinator::union(TypeCombinator::removeNull($leftType), $rightType); |
191: | } |
192: | |
193: | if ($expr instanceof Expr\Ternary) { |
194: | $condType = $this->getType($expr->cond, $context); |
195: | $elseType = $this->getType($expr->else, $context); |
196: | if ($expr->if === null) { |
197: | return TypeCombinator::union( |
198: | TypeCombinator::removeFalsey($condType), |
199: | $elseType, |
200: | ); |
201: | } |
202: | |
203: | $ifType = $this->getType($expr->if, $context); |
204: | |
205: | return TypeCombinator::union( |
206: | TypeCombinator::removeFalsey($ifType), |
207: | $elseType, |
208: | ); |
209: | } |
210: | |
211: | if ($expr instanceof Expr\FuncCall && $expr->name instanceof Name && $expr->name->toLowerString() === 'constant') { |
212: | $firstArg = $expr->args[0] ?? null; |
213: | if ($firstArg instanceof Arg && $firstArg->value instanceof String_) { |
214: | $constant = $this->constantResolver->resolvePredefinedConstant($firstArg->value->value); |
215: | if ($constant !== null) { |
216: | return $constant; |
217: | } |
218: | } |
219: | } |
220: | |
221: | if ($expr instanceof Expr\BooleanNot) { |
222: | $exprBooleanType = $this->getType($expr->expr, $context)->toBoolean(); |
223: | |
224: | if ($exprBooleanType instanceof ConstantBooleanType) { |
225: | return new ConstantBooleanType(!$exprBooleanType->getValue()); |
226: | } |
227: | |
228: | return new BooleanType(); |
229: | } |
230: | |
231: | if ($expr instanceof Expr\BitwiseNot) { |
232: | return $this->getBitwiseNotType($expr->expr, fn (Expr $expr): Type => $this->getType($expr, $context)); |
233: | } |
234: | |
235: | if ($expr instanceof Expr\BinaryOp\Concat) { |
236: | return $this->getConcatType($expr->left, $expr->right, fn (Expr $expr): Type => $this->getType($expr, $context)); |
237: | } |
238: | |
239: | if ($expr instanceof Expr\BinaryOp\BitwiseAnd) { |
240: | return $this->getBitwiseAndType($expr->left, $expr->right, fn (Expr $expr): Type => $this->getType($expr, $context)); |
241: | } |
242: | |
243: | if ($expr instanceof Expr\BinaryOp\BitwiseOr) { |
244: | return $this->getBitwiseOrType($expr->left, $expr->right, fn (Expr $expr): Type => $this->getType($expr, $context)); |
245: | } |
246: | |
247: | if ($expr instanceof Expr\BinaryOp\BitwiseXor) { |
248: | return $this->getBitwiseXorType($expr->left, $expr->right, fn (Expr $expr): Type => $this->getType($expr, $context)); |
249: | } |
250: | |
251: | if ($expr instanceof Expr\BinaryOp\Spaceship) { |
252: | return $this->getSpaceshipType($expr->left, $expr->right, fn (Expr $expr): Type => $this->getType($expr, $context)); |
253: | } |
254: | |
255: | if ( |
256: | $expr instanceof Expr\BinaryOp\BooleanAnd |
257: | || $expr instanceof Expr\BinaryOp\LogicalAnd |
258: | || $expr instanceof Expr\BinaryOp\BooleanOr |
259: | || $expr instanceof Expr\BinaryOp\LogicalOr |
260: | ) { |
261: | return new BooleanType(); |
262: | } |
263: | |
264: | if ($expr instanceof Expr\BinaryOp\Div) { |
265: | return $this->getDivType($expr->left, $expr->right, fn (Expr $expr): Type => $this->getType($expr, $context)); |
266: | } |
267: | |
268: | if ($expr instanceof Expr\BinaryOp\Mod) { |
269: | return $this->getModType($expr->left, $expr->right, fn (Expr $expr): Type => $this->getType($expr, $context)); |
270: | } |
271: | |
272: | if ($expr instanceof Expr\BinaryOp\Plus) { |
273: | return $this->getPlusType($expr->left, $expr->right, fn (Expr $expr): Type => $this->getType($expr, $context)); |
274: | } |
275: | |
276: | if ($expr instanceof Expr\BinaryOp\Minus) { |
277: | return $this->getMinusType($expr->left, $expr->right, fn (Expr $expr): Type => $this->getType($expr, $context)); |
278: | } |
279: | |
280: | if ($expr instanceof Expr\BinaryOp\Mul) { |
281: | return $this->getMulType($expr->left, $expr->right, fn (Expr $expr): Type => $this->getType($expr, $context)); |
282: | } |
283: | |
284: | if ($expr instanceof Expr\BinaryOp\Pow) { |
285: | return $this->getPowType($expr->left, $expr->right, fn (Expr $expr): Type => $this->getType($expr, $context)); |
286: | } |
287: | |
288: | if ($expr instanceof Expr\BinaryOp\ShiftLeft) { |
289: | return $this->getShiftLeftType($expr->left, $expr->right, fn (Expr $expr): Type => $this->getType($expr, $context)); |
290: | } |
291: | |
292: | if ($expr instanceof Expr\BinaryOp\ShiftRight) { |
293: | return $this->getShiftRightType($expr->left, $expr->right, fn (Expr $expr): Type => $this->getType($expr, $context)); |
294: | } |
295: | |
296: | if ($expr instanceof BinaryOp\Identical) { |
297: | return $this->resolveIdenticalType( |
298: | $this->getType($expr->left, $context), |
299: | $this->getType($expr->right, $context), |
300: | ); |
301: | } |
302: | |
303: | if ($expr instanceof BinaryOp\NotIdentical) { |
304: | return $this->getType(new Expr\BooleanNot(new BinaryOp\Identical($expr->left, $expr->right)), $context); |
305: | } |
306: | |
307: | if ($expr instanceof BinaryOp\Equal) { |
308: | return $this->resolveEqualType( |
309: | $this->getType($expr->left, $context), |
310: | $this->getType($expr->right, $context), |
311: | ); |
312: | } |
313: | |
314: | if ($expr instanceof BinaryOp\NotEqual) { |
315: | return $this->getType(new Expr\BooleanNot(new BinaryOp\Equal($expr->left, $expr->right)), $context); |
316: | } |
317: | |
318: | if ($expr instanceof Expr\BinaryOp\Smaller) { |
319: | return $this->getType($expr->left, $context)->isSmallerThan($this->getType($expr->right, $context))->toBooleanType(); |
320: | } |
321: | |
322: | if ($expr instanceof Expr\BinaryOp\SmallerOrEqual) { |
323: | return $this->getType($expr->left, $context)->isSmallerThanOrEqual($this->getType($expr->right, $context))->toBooleanType(); |
324: | } |
325: | |
326: | if ($expr instanceof Expr\BinaryOp\Greater) { |
327: | return $this->getType($expr->right, $context)->isSmallerThan($this->getType($expr->left, $context))->toBooleanType(); |
328: | } |
329: | |
330: | if ($expr instanceof Expr\BinaryOp\GreaterOrEqual) { |
331: | return $this->getType($expr->right, $context)->isSmallerThanOrEqual($this->getType($expr->left, $context))->toBooleanType(); |
332: | } |
333: | |
334: | if ($expr instanceof Expr\BinaryOp\LogicalXor) { |
335: | $leftBooleanType = $this->getType($expr->left, $context)->toBoolean(); |
336: | $rightBooleanType = $this->getType($expr->right, $context)->toBoolean(); |
337: | |
338: | if ( |
339: | $leftBooleanType instanceof ConstantBooleanType |
340: | && $rightBooleanType instanceof ConstantBooleanType |
341: | ) { |
342: | return new ConstantBooleanType( |
343: | $leftBooleanType->getValue() xor $rightBooleanType->getValue(), |
344: | ); |
345: | } |
346: | |
347: | return new BooleanType(); |
348: | } |
349: | |
350: | if ($expr instanceof MagicConst\Class_) { |
351: | if ($context->getTraitName() !== null) { |
352: | return TypeCombinator::intersect( |
353: | new ClassStringType(), |
354: | new AccessoryLiteralStringType(), |
355: | ); |
356: | } |
357: | |
358: | if ($context->getClassName() === null) { |
359: | return new ConstantStringType(''); |
360: | } |
361: | |
362: | return new ConstantStringType($context->getClassName(), true); |
363: | } |
364: | |
365: | if ($expr instanceof MagicConst\Namespace_) { |
366: | if ($context->getTraitName() !== null) { |
367: | return TypeCombinator::intersect( |
368: | new StringType(), |
369: | new AccessoryLiteralStringType(), |
370: | ); |
371: | } |
372: | |
373: | return new ConstantStringType($context->getNamespace() ?? ''); |
374: | } |
375: | |
376: | if ($expr instanceof MagicConst\Method) { |
377: | return new ConstantStringType($context->getMethod() ?? ''); |
378: | } |
379: | |
380: | if ($expr instanceof MagicConst\Function_) { |
381: | return new ConstantStringType($context->getFunction() ?? ''); |
382: | } |
383: | |
384: | if ($expr instanceof MagicConst\Trait_) { |
385: | if ($context->getTraitName() === null) { |
386: | return new ConstantStringType(''); |
387: | } |
388: | |
389: | return new ConstantStringType($context->getTraitName(), true); |
390: | } |
391: | |
392: | if ($expr instanceof PropertyFetch && $expr->name instanceof Identifier) { |
393: | $fetchedOnType = $this->getType($expr->var, $context); |
394: | if (!$fetchedOnType->hasProperty($expr->name->name)->yes()) { |
395: | return new ErrorType(); |
396: | } |
397: | |
398: | return $fetchedOnType->getProperty($expr->name->name, new OutOfClassScope())->getReadableType(); |
399: | } |
400: | |
401: | return new MixedType(); |
402: | } |
403: | |
404: | |
405: | |
406: | |
407: | public function getConcatType(Expr $left, Expr $right, callable $getTypeCallback): Type |
408: | { |
409: | $leftType = $getTypeCallback($left); |
410: | $rightType = $getTypeCallback($right); |
411: | |
412: | return $this->resolveConcatType($leftType, $rightType); |
413: | } |
414: | |
415: | public function resolveConcatType(Type $left, Type $right): Type |
416: | { |
417: | $leftStringType = $left->toString(); |
418: | $rightStringType = $right->toString(); |
419: | if (TypeCombinator::union( |
420: | $leftStringType, |
421: | $rightStringType, |
422: | ) instanceof ErrorType) { |
423: | return new ErrorType(); |
424: | } |
425: | |
426: | if ($leftStringType instanceof ConstantStringType && $leftStringType->getValue() === '') { |
427: | return $rightStringType; |
428: | } |
429: | |
430: | if ($rightStringType instanceof ConstantStringType && $rightStringType->getValue() === '') { |
431: | return $leftStringType; |
432: | } |
433: | |
434: | if ($leftStringType instanceof ConstantStringType && $rightStringType instanceof ConstantStringType) { |
435: | return $leftStringType->append($rightStringType); |
436: | } |
437: | |
438: | $leftConstantStrings = $leftStringType->getConstantStrings(); |
439: | $rightConstantStrings = $rightStringType->getConstantStrings(); |
440: | $combinedConstantStringsCount = count($leftConstantStrings) * count($rightConstantStrings); |
441: | |
442: | |
443: | if ($combinedConstantStringsCount > 0 && $combinedConstantStringsCount <= 16) { |
444: | $strings = []; |
445: | |
446: | foreach ($leftConstantStrings as $leftConstantString) { |
447: | if ($leftConstantString->getValue() === '') { |
448: | $strings = array_merge($strings, $rightConstantStrings); |
449: | |
450: | continue; |
451: | } |
452: | |
453: | foreach ($rightConstantStrings as $rightConstantString) { |
454: | if ($rightConstantString->getValue() === '') { |
455: | $strings[] = $leftConstantString; |
456: | |
457: | continue; |
458: | } |
459: | |
460: | $strings[] = $leftConstantString->append($rightConstantString); |
461: | } |
462: | } |
463: | |
464: | if (count($strings) > 0) { |
465: | return TypeCombinator::union(...$strings); |
466: | } |
467: | } |
468: | |
469: | $accessoryTypes = []; |
470: | if ($leftStringType->isNonEmptyString()->and($rightStringType->isNonEmptyString())->yes()) { |
471: | $accessoryTypes[] = new AccessoryNonFalsyStringType(); |
472: | } elseif ($leftStringType->isNonFalsyString()->or($rightStringType->isNonFalsyString())->yes()) { |
473: | $accessoryTypes[] = new AccessoryNonFalsyStringType(); |
474: | } elseif ($leftStringType->isNonEmptyString()->or($rightStringType->isNonEmptyString())->yes()) { |
475: | $accessoryTypes[] = new AccessoryNonEmptyStringType(); |
476: | } |
477: | |
478: | if ($leftStringType->isLiteralString()->and($rightStringType->isLiteralString())->yes()) { |
479: | $accessoryTypes[] = new AccessoryLiteralStringType(); |
480: | } |
481: | |
482: | $leftNumericStringNonEmpty = TypeCombinator::remove($leftStringType, new ConstantStringType('')); |
483: | if ($leftNumericStringNonEmpty->isNumericString()->yes()) { |
484: | $allRightConstantsZeroOrMore = false; |
485: | foreach ($rightConstantStrings as $rightConstantString) { |
486: | if ($rightConstantString->getValue() === '') { |
487: | continue; |
488: | } |
489: | |
490: | if ( |
491: | !is_numeric($rightConstantString->getValue()) |
492: | || Strings::match($rightConstantString->getValue(), '#^[0-9]+$#') === null |
493: | ) { |
494: | $allRightConstantsZeroOrMore = false; |
495: | break; |
496: | } |
497: | |
498: | $allRightConstantsZeroOrMore = true; |
499: | } |
500: | |
501: | $zeroOrMoreInteger = IntegerRangeType::fromInterval(0, null); |
502: | $nonNegativeRight = $allRightConstantsZeroOrMore || $zeroOrMoreInteger->isSuperTypeOf($right)->yes(); |
503: | if ($nonNegativeRight) { |
504: | $accessoryTypes[] = new AccessoryNumericStringType(); |
505: | } |
506: | } |
507: | |
508: | if (count($accessoryTypes) > 0) { |
509: | $accessoryTypes[] = new StringType(); |
510: | return new IntersectionType($accessoryTypes); |
511: | } |
512: | |
513: | return new StringType(); |
514: | } |
515: | |
516: | |
517: | |
518: | |
519: | public function getArrayType(Expr\Array_ $expr, callable $getTypeCallback): Type |
520: | { |
521: | if (count($expr->items) > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) { |
522: | return $this->oversizedArrayBuilder->build($expr, $getTypeCallback); |
523: | } |
524: | |
525: | $arrayBuilder = ConstantArrayTypeBuilder::createEmpty(); |
526: | $isList = null; |
527: | foreach ($expr->items as $arrayItem) { |
528: | if ($arrayItem === null) { |
529: | continue; |
530: | } |
531: | |
532: | $valueType = $getTypeCallback($arrayItem->value); |
533: | if ($arrayItem->unpack) { |
534: | $constantArrays = $valueType->getConstantArrays(); |
535: | if (count($constantArrays) === 1) { |
536: | $constantArrayType = $constantArrays[0]; |
537: | $hasStringKey = false; |
538: | foreach ($constantArrayType->getKeyTypes() as $keyType) { |
539: | if ($keyType->isString()->yes()) { |
540: | $hasStringKey = true; |
541: | break; |
542: | } |
543: | } |
544: | |
545: | foreach ($constantArrayType->getValueTypes() as $i => $innerValueType) { |
546: | if ($hasStringKey && $this->phpVersion->supportsArrayUnpackingWithStringKeys()) { |
547: | $arrayBuilder->setOffsetValueType($constantArrayType->getKeyTypes()[$i], $innerValueType, $constantArrayType->isOptionalKey($i)); |
548: | } else { |
549: | $arrayBuilder->setOffsetValueType(null, $innerValueType, $constantArrayType->isOptionalKey($i)); |
550: | } |
551: | } |
552: | } else { |
553: | $arrayBuilder->degradeToGeneralArray(); |
554: | |
555: | if ($this->phpVersion->supportsArrayUnpackingWithStringKeys() && !$valueType->getIterableKeyType()->isString()->no()) { |
556: | $isList = false; |
557: | $offsetType = $valueType->getIterableKeyType(); |
558: | } else { |
559: | $isList ??= $arrayBuilder->isList(); |
560: | $offsetType = new IntegerType(); |
561: | } |
562: | |
563: | $arrayBuilder->setOffsetValueType($offsetType, $valueType->getIterableValueType(), !$valueType->isIterableAtLeastOnce()->yes()); |
564: | } |
565: | } else { |
566: | $arrayBuilder->setOffsetValueType( |
567: | $arrayItem->key !== null ? $getTypeCallback($arrayItem->key) : null, |
568: | $valueType, |
569: | ); |
570: | } |
571: | } |
572: | |
573: | $arrayType = $arrayBuilder->getArray(); |
574: | if ($isList === true) { |
575: | return AccessoryArrayListType::intersectWith($arrayType); |
576: | } |
577: | |
578: | return $arrayType; |
579: | } |
580: | |
581: | |
582: | |
583: | |
584: | public function getBitwiseAndType(Expr $left, Expr $right, callable $getTypeCallback): Type |
585: | { |
586: | $leftType = $getTypeCallback($left); |
587: | $rightType = $getTypeCallback($right); |
588: | |
589: | if ($leftType instanceof NeverType || $rightType instanceof NeverType) { |
590: | return $this->getNeverType($leftType, $rightType); |
591: | } |
592: | |
593: | $leftTypes = $leftType->getConstantScalarTypes(); |
594: | $rightTypes = $rightType->getConstantScalarTypes(); |
595: | $leftTypesCount = count($leftTypes); |
596: | $rightTypesCount = count($rightTypes); |
597: | if ($leftTypesCount > 0 && $rightTypesCount > 0) { |
598: | $resultTypes = []; |
599: | $generalize = $leftTypesCount * $rightTypesCount > self::CALCULATE_SCALARS_LIMIT; |
600: | foreach ($leftTypes as $leftTypeInner) { |
601: | foreach ($rightTypes as $rightTypeInner) { |
602: | if ($leftTypeInner instanceof ConstantStringType && $rightTypeInner instanceof ConstantStringType) { |
603: | $resultType = $this->getTypeFromValue($leftTypeInner->getValue() & $rightTypeInner->getValue()); |
604: | } else { |
605: | $leftNumberType = $leftTypeInner->toNumber(); |
606: | $rightNumberType = $rightTypeInner->toNumber(); |
607: | |
608: | if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) { |
609: | return new ErrorType(); |
610: | } |
611: | |
612: | if (!$leftNumberType instanceof ConstantScalarType || !$rightNumberType instanceof ConstantScalarType) { |
613: | throw new ShouldNotHappenException(); |
614: | } |
615: | |
616: | $resultType = $this->getTypeFromValue($leftNumberType->getValue() & $rightNumberType->getValue()); |
617: | } |
618: | if ($generalize) { |
619: | $resultType = $resultType->generalize(GeneralizePrecision::lessSpecific()); |
620: | } |
621: | $resultTypes[] = $resultType; |
622: | } |
623: | } |
624: | return TypeCombinator::union(...$resultTypes); |
625: | } |
626: | |
627: | if ($leftType->isString()->yes() && $rightType->isString()->yes()) { |
628: | return new StringType(); |
629: | } |
630: | |
631: | $leftNumberType = $leftType->toNumber(); |
632: | $rightNumberType = $rightType->toNumber(); |
633: | |
634: | if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) { |
635: | return new ErrorType(); |
636: | } |
637: | |
638: | if ($rightNumberType instanceof ConstantIntegerType && $rightNumberType->getValue() >= 0) { |
639: | return IntegerRangeType::fromInterval(0, $rightNumberType->getValue()); |
640: | } |
641: | if ($leftNumberType instanceof ConstantIntegerType && $leftNumberType->getValue() >= 0) { |
642: | return IntegerRangeType::fromInterval(0, $leftNumberType->getValue()); |
643: | } |
644: | |
645: | return new IntegerType(); |
646: | } |
647: | |
648: | |
649: | |
650: | |
651: | public function getBitwiseOrType(Expr $left, Expr $right, callable $getTypeCallback): Type |
652: | { |
653: | $leftType = $getTypeCallback($left); |
654: | $rightType = $getTypeCallback($right); |
655: | |
656: | if ($leftType instanceof NeverType || $rightType instanceof NeverType) { |
657: | return $this->getNeverType($leftType, $rightType); |
658: | } |
659: | |
660: | $leftTypes = $leftType->getConstantScalarTypes(); |
661: | $rightTypes = $rightType->getConstantScalarTypes(); |
662: | $leftTypesCount = count($leftTypes); |
663: | $rightTypesCount = count($rightTypes); |
664: | if ($leftTypesCount > 0 && $rightTypesCount > 0) { |
665: | $resultTypes = []; |
666: | $generalize = $leftTypesCount * $rightTypesCount > self::CALCULATE_SCALARS_LIMIT; |
667: | foreach ($leftTypes as $leftTypeInner) { |
668: | foreach ($rightTypes as $rightTypeInner) { |
669: | if ($leftTypeInner instanceof ConstantStringType && $rightTypeInner instanceof ConstantStringType) { |
670: | $resultType = $this->getTypeFromValue($leftTypeInner->getValue() | $rightTypeInner->getValue()); |
671: | } else { |
672: | $leftNumberType = $leftTypeInner->toNumber(); |
673: | $rightNumberType = $rightTypeInner->toNumber(); |
674: | |
675: | if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) { |
676: | return new ErrorType(); |
677: | } |
678: | |
679: | if (!$leftNumberType instanceof ConstantScalarType || !$rightNumberType instanceof ConstantScalarType) { |
680: | throw new ShouldNotHappenException(); |
681: | } |
682: | |
683: | $resultType = $this->getTypeFromValue($leftNumberType->getValue() | $rightNumberType->getValue()); |
684: | } |
685: | if ($generalize) { |
686: | $resultType = $resultType->generalize(GeneralizePrecision::lessSpecific()); |
687: | } |
688: | $resultTypes[] = $resultType; |
689: | } |
690: | } |
691: | return TypeCombinator::union(...$resultTypes); |
692: | } |
693: | |
694: | if ($leftType->isString()->yes() && $rightType->isString()->yes()) { |
695: | return new StringType(); |
696: | } |
697: | |
698: | if (TypeCombinator::union($leftType->toNumber(), $rightType->toNumber()) instanceof ErrorType) { |
699: | return new ErrorType(); |
700: | } |
701: | |
702: | return new IntegerType(); |
703: | } |
704: | |
705: | |
706: | |
707: | |
708: | public function getBitwiseXorType(Expr $left, Expr $right, callable $getTypeCallback): Type |
709: | { |
710: | $leftType = $getTypeCallback($left); |
711: | $rightType = $getTypeCallback($right); |
712: | |
713: | if ($leftType instanceof NeverType || $rightType instanceof NeverType) { |
714: | return $this->getNeverType($leftType, $rightType); |
715: | } |
716: | |
717: | $leftTypes = $leftType->getConstantScalarTypes(); |
718: | $rightTypes = $rightType->getConstantScalarTypes(); |
719: | $leftTypesCount = count($leftTypes); |
720: | $rightTypesCount = count($rightTypes); |
721: | if ($leftTypesCount > 0 && $rightTypesCount > 0) { |
722: | $resultTypes = []; |
723: | $generalize = $leftTypesCount * $rightTypesCount > self::CALCULATE_SCALARS_LIMIT; |
724: | foreach ($leftTypes as $leftTypeInner) { |
725: | foreach ($rightTypes as $rightTypeInner) { |
726: | if ($leftTypeInner instanceof ConstantStringType && $rightTypeInner instanceof ConstantStringType) { |
727: | $resultType = $this->getTypeFromValue($leftTypeInner->getValue() ^ $rightTypeInner->getValue()); |
728: | } else { |
729: | $leftNumberType = $leftTypeInner->toNumber(); |
730: | $rightNumberType = $rightTypeInner->toNumber(); |
731: | |
732: | if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) { |
733: | return new ErrorType(); |
734: | } |
735: | |
736: | if (!$leftNumberType instanceof ConstantScalarType || !$rightNumberType instanceof ConstantScalarType) { |
737: | throw new ShouldNotHappenException(); |
738: | } |
739: | |
740: | $resultType = $this->getTypeFromValue($leftNumberType->getValue() ^ $rightNumberType->getValue()); |
741: | } |
742: | if ($generalize) { |
743: | $resultType = $resultType->generalize(GeneralizePrecision::lessSpecific()); |
744: | } |
745: | $resultTypes[] = $resultType; |
746: | } |
747: | } |
748: | return TypeCombinator::union(...$resultTypes); |
749: | } |
750: | |
751: | if ($leftType->isString()->yes() && $rightType->isString()->yes()) { |
752: | return new StringType(); |
753: | } |
754: | |
755: | if (TypeCombinator::union($leftType->toNumber(), $rightType->toNumber()) instanceof ErrorType) { |
756: | return new ErrorType(); |
757: | } |
758: | |
759: | return new IntegerType(); |
760: | } |
761: | |
762: | |
763: | |
764: | |
765: | public function getSpaceshipType(Expr $left, Expr $right, callable $getTypeCallback): Type |
766: | { |
767: | $callbackLeftType = $getTypeCallback($left); |
768: | $callbackRightType = $getTypeCallback($right); |
769: | |
770: | if ($callbackLeftType instanceof NeverType || $callbackRightType instanceof NeverType) { |
771: | return $this->getNeverType($callbackLeftType, $callbackRightType); |
772: | } |
773: | |
774: | $leftTypes = $callbackLeftType->getConstantScalarTypes(); |
775: | $rightTypes = $callbackRightType->getConstantScalarTypes(); |
776: | |
777: | $leftTypesCount = count($leftTypes); |
778: | $rightTypesCount = count($rightTypes); |
779: | if ($leftTypesCount > 0 && $rightTypesCount > 0 && $leftTypesCount * $rightTypesCount <= self::CALCULATE_SCALARS_LIMIT) { |
780: | $resultTypes = []; |
781: | foreach ($leftTypes as $leftType) { |
782: | foreach ($rightTypes as $rightType) { |
783: | $leftValue = $leftType->getValue(); |
784: | $rightValue = $rightType->getValue(); |
785: | $resultType = $this->getTypeFromValue($leftValue <=> $rightValue); |
786: | $resultTypes[] = $resultType; |
787: | } |
788: | } |
789: | return TypeCombinator::union(...$resultTypes); |
790: | } |
791: | |
792: | return IntegerRangeType::fromInterval(-1, 1); |
793: | } |
794: | |
795: | |
796: | |
797: | |
798: | public function getDivType(Expr $left, Expr $right, callable $getTypeCallback): Type |
799: | { |
800: | $leftType = $getTypeCallback($left); |
801: | $rightType = $getTypeCallback($right); |
802: | |
803: | $leftTypes = $leftType->getConstantScalarTypes(); |
804: | $rightTypes = $rightType->getConstantScalarTypes(); |
805: | $leftTypesCount = count($leftTypes); |
806: | $rightTypesCount = count($rightTypes); |
807: | if ($leftTypesCount > 0 && $rightTypesCount > 0) { |
808: | $resultTypes = []; |
809: | $generalize = $leftTypesCount * $rightTypesCount > self::CALCULATE_SCALARS_LIMIT; |
810: | foreach ($leftTypes as $leftTypeInner) { |
811: | foreach ($rightTypes as $rightTypeInner) { |
812: | $leftNumberType = $leftTypeInner->toNumber(); |
813: | $rightNumberType = $rightTypeInner->toNumber(); |
814: | |
815: | if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) { |
816: | return new ErrorType(); |
817: | } |
818: | |
819: | if (!$leftNumberType instanceof ConstantScalarType || !$rightNumberType instanceof ConstantScalarType) { |
820: | throw new ShouldNotHappenException(); |
821: | } |
822: | |
823: | if (in_array($rightNumberType->getValue(), [0, 0.0], true)) { |
824: | return new ErrorType(); |
825: | } |
826: | |
827: | $resultType = $this->getTypeFromValue($leftNumberType->getValue() / $rightNumberType->getValue()); |
828: | if ($generalize) { |
829: | $resultType = $resultType->generalize(GeneralizePrecision::lessSpecific()); |
830: | } |
831: | $resultTypes[] = $resultType; |
832: | } |
833: | } |
834: | return TypeCombinator::union(...$resultTypes); |
835: | } |
836: | |
837: | $rightScalarValues = $rightType->toNumber()->getConstantScalarValues(); |
838: | foreach ($rightScalarValues as $scalarValue) { |
839: | if ($scalarValue === 0 || $scalarValue === 0.0) { |
840: | return new ErrorType(); |
841: | } |
842: | } |
843: | |
844: | return $this->resolveCommonMath(new BinaryOp\Div($left, $right), $leftType, $rightType); |
845: | } |
846: | |
847: | |
848: | |
849: | |
850: | public function getModType(Expr $left, Expr $right, callable $getTypeCallback): Type |
851: | { |
852: | $leftType = $getTypeCallback($left); |
853: | $rightType = $getTypeCallback($right); |
854: | |
855: | if ($leftType instanceof NeverType || $rightType instanceof NeverType) { |
856: | return $this->getNeverType($leftType, $rightType); |
857: | } |
858: | |
859: | if ($leftType->toNumber() instanceof ErrorType || $rightType->toNumber() instanceof ErrorType) { |
860: | return new ErrorType(); |
861: | } |
862: | |
863: | $leftTypes = $leftType->getConstantScalarTypes(); |
864: | $rightTypes = $rightType->getConstantScalarTypes(); |
865: | $leftTypesCount = count($leftTypes); |
866: | $rightTypesCount = count($rightTypes); |
867: | if ($leftTypesCount > 0 && $rightTypesCount > 0) { |
868: | $resultTypes = []; |
869: | $generalize = $leftTypesCount * $rightTypesCount > self::CALCULATE_SCALARS_LIMIT; |
870: | foreach ($leftTypes as $leftTypeInner) { |
871: | foreach ($rightTypes as $rightTypeInner) { |
872: | $leftNumberType = $leftTypeInner->toNumber(); |
873: | $rightNumberType = $rightTypeInner->toNumber(); |
874: | |
875: | if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) { |
876: | return new ErrorType(); |
877: | } |
878: | |
879: | if (!$leftNumberType instanceof ConstantScalarType || !$rightNumberType instanceof ConstantScalarType) { |
880: | throw new ShouldNotHappenException(); |
881: | } |
882: | |
883: | $rightIntegerValue = (int) $rightNumberType->getValue(); |
884: | if ($rightIntegerValue === 0) { |
885: | return new ErrorType(); |
886: | } |
887: | |
888: | $resultType = $this->getTypeFromValue((int) $leftNumberType->getValue() % $rightIntegerValue); |
889: | if ($generalize) { |
890: | $resultType = $resultType->generalize(GeneralizePrecision::lessSpecific()); |
891: | } |
892: | $resultTypes[] = $resultType; |
893: | } |
894: | } |
895: | return TypeCombinator::union(...$resultTypes); |
896: | } |
897: | |
898: | $integerType = $rightType->toInteger(); |
899: | if ($integerType instanceof ConstantIntegerType && $integerType->getValue() === 1) { |
900: | return new ConstantIntegerType(0); |
901: | } |
902: | |
903: | $rightScalarValues = $rightType->toNumber()->getConstantScalarValues(); |
904: | foreach ($rightScalarValues as $scalarValue) { |
905: | |
906: | if ($scalarValue === 0 || $scalarValue === 0.0) { |
907: | return new ErrorType(); |
908: | } |
909: | } |
910: | |
911: | $positiveInt = IntegerRangeType::fromInterval(0, null); |
912: | if ($rightType->isInteger()->yes()) { |
913: | $rangeMin = null; |
914: | $rangeMax = null; |
915: | |
916: | if ($rightType instanceof IntegerRangeType) { |
917: | $rangeMax = $rightType->getMax() !== null ? $rightType->getMax() - 1 : null; |
918: | } elseif ($rightType instanceof ConstantIntegerType) { |
919: | $rangeMax = $rightType->getValue() - 1; |
920: | } elseif ($rightType instanceof UnionType) { |
921: | foreach ($rightType->getTypes() as $type) { |
922: | if ($type instanceof IntegerRangeType) { |
923: | if ($type->getMax() === null) { |
924: | $rangeMax = null; |
925: | } else { |
926: | $rangeMax = max($rangeMax, $type->getMax()); |
927: | } |
928: | } elseif ($type instanceof ConstantIntegerType) { |
929: | $rangeMax = max($rangeMax, $type->getValue() - 1); |
930: | } |
931: | } |
932: | } |
933: | |
934: | if ($positiveInt->isSuperTypeOf($leftType)->yes()) { |
935: | $rangeMin = 0; |
936: | } elseif ($rangeMax !== null) { |
937: | $rangeMin = $rangeMax * -1; |
938: | } |
939: | |
940: | return IntegerRangeType::fromInterval($rangeMin, $rangeMax); |
941: | } elseif ($positiveInt->isSuperTypeOf($leftType)->yes()) { |
942: | return IntegerRangeType::fromInterval(0, null); |
943: | } |
944: | |
945: | return new IntegerType(); |
946: | } |
947: | |
948: | |
949: | |
950: | |
951: | public function getPlusType(Expr $left, Expr $right, callable $getTypeCallback): Type |
952: | { |
953: | $leftType = $getTypeCallback($left); |
954: | $rightType = $getTypeCallback($right); |
955: | |
956: | if ($leftType instanceof NeverType || $rightType instanceof NeverType) { |
957: | return $this->getNeverType($leftType, $rightType); |
958: | } |
959: | |
960: | $leftTypes = $leftType->getConstantScalarTypes(); |
961: | $rightTypes = $rightType->getConstantScalarTypes(); |
962: | $leftTypesCount = count($leftTypes); |
963: | $rightTypesCount = count($rightTypes); |
964: | if ($leftTypesCount > 0 && $rightTypesCount > 0) { |
965: | $resultTypes = []; |
966: | $generalize = $leftTypesCount * $rightTypesCount > self::CALCULATE_SCALARS_LIMIT; |
967: | foreach ($leftTypes as $leftTypeInner) { |
968: | foreach ($rightTypes as $rightTypeInner) { |
969: | $leftNumberType = $leftTypeInner->toNumber(); |
970: | $rightNumberType = $rightTypeInner->toNumber(); |
971: | |
972: | if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) { |
973: | return new ErrorType(); |
974: | } |
975: | |
976: | if (!$leftNumberType instanceof ConstantScalarType || !$rightNumberType instanceof ConstantScalarType) { |
977: | throw new ShouldNotHappenException(); |
978: | } |
979: | |
980: | $resultType = $this->getTypeFromValue($leftNumberType->getValue() + $rightNumberType->getValue()); |
981: | if ($generalize) { |
982: | $resultType = $resultType->generalize(GeneralizePrecision::lessSpecific()); |
983: | } |
984: | $resultTypes[] = $resultType; |
985: | } |
986: | } |
987: | |
988: | return TypeCombinator::union(...$resultTypes); |
989: | } |
990: | |
991: | $leftConstantArrays = $leftType->getConstantArrays(); |
992: | $rightConstantArrays = $rightType->getConstantArrays(); |
993: | |
994: | $leftCount = count($leftConstantArrays); |
995: | $rightCount = count($rightConstantArrays); |
996: | if ($leftCount > 0 && $rightCount > 0 |
997: | && ($leftCount + $rightCount < ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT)) { |
998: | $resultTypes = []; |
999: | foreach ($rightConstantArrays as $rightConstantArray) { |
1000: | foreach ($leftConstantArrays as $leftConstantArray) { |
1001: | $newArrayBuilder = ConstantArrayTypeBuilder::createFromConstantArray($rightConstantArray); |
1002: | foreach ($leftConstantArray->getKeyTypes() as $i => $leftKeyType) { |
1003: | $optional = $leftConstantArray->isOptionalKey($i); |
1004: | $valueType = $leftConstantArray->getOffsetValueType($leftKeyType); |
1005: | if (!$optional) { |
1006: | if ($rightConstantArray->hasOffsetValueType($leftKeyType)->maybe()) { |
1007: | $valueType = TypeCombinator::union($valueType, $rightConstantArray->getOffsetValueType($leftKeyType)); |
1008: | } |
1009: | } |
1010: | $newArrayBuilder->setOffsetValueType( |
1011: | $leftKeyType, |
1012: | $valueType, |
1013: | $optional, |
1014: | ); |
1015: | } |
1016: | $resultTypes[] = $newArrayBuilder->getArray(); |
1017: | } |
1018: | } |
1019: | return TypeCombinator::union(...$resultTypes); |
1020: | } |
1021: | |
1022: | $leftIsArray = $leftType->isArray(); |
1023: | $rightIsArray = $rightType->isArray(); |
1024: | if ($leftIsArray->yes() && $rightIsArray->yes()) { |
1025: | if ($leftType->getIterableKeyType()->equals($rightType->getIterableKeyType())) { |
1026: | |
1027: | $keyType = $leftType->getIterableKeyType(); |
1028: | } else { |
1029: | $keyTypes = []; |
1030: | foreach ([ |
1031: | $leftType->getIterableKeyType(), |
1032: | $rightType->getIterableKeyType(), |
1033: | ] as $keyType) { |
1034: | $keyTypes[] = $keyType; |
1035: | } |
1036: | $keyType = TypeCombinator::union(...$keyTypes); |
1037: | } |
1038: | |
1039: | $arrayType = new ArrayType( |
1040: | $keyType, |
1041: | TypeCombinator::union($leftType->getIterableValueType(), $rightType->getIterableValueType()), |
1042: | ); |
1043: | |
1044: | if ($leftType->isIterableAtLeastOnce()->yes() || $rightType->isIterableAtLeastOnce()->yes()) { |
1045: | $arrayType = TypeCombinator::intersect($arrayType, new NonEmptyArrayType()); |
1046: | } |
1047: | if ($leftType->isList()->yes() && $rightType->isList()->yes()) { |
1048: | $arrayType = AccessoryArrayListType::intersectWith($arrayType); |
1049: | } |
1050: | |
1051: | return $arrayType; |
1052: | } |
1053: | |
1054: | if ($leftType instanceof MixedType && $rightType instanceof MixedType) { |
1055: | if ( |
1056: | ($leftIsArray->no() && $rightIsArray->no()) |
1057: | ) { |
1058: | return new BenevolentUnionType([ |
1059: | new FloatType(), |
1060: | new IntegerType(), |
1061: | ]); |
1062: | } |
1063: | return new BenevolentUnionType([ |
1064: | new FloatType(), |
1065: | new IntegerType(), |
1066: | new ArrayType(new MixedType(), new MixedType()), |
1067: | ]); |
1068: | } |
1069: | |
1070: | if ( |
1071: | ($leftIsArray->yes() && $rightIsArray->no()) |
1072: | || ($leftIsArray->no() && $rightIsArray->yes()) |
1073: | ) { |
1074: | return new ErrorType(); |
1075: | } |
1076: | |
1077: | if ( |
1078: | ($leftIsArray->yes() && $rightIsArray->maybe()) |
1079: | || ($leftIsArray->maybe() && $rightIsArray->yes()) |
1080: | ) { |
1081: | $resultType = new ArrayType(new MixedType(), new MixedType()); |
1082: | if ($leftType->isIterableAtLeastOnce()->yes() || $rightType->isIterableAtLeastOnce()->yes()) { |
1083: | return TypeCombinator::intersect($resultType, new NonEmptyArrayType()); |
1084: | } |
1085: | |
1086: | return $resultType; |
1087: | } |
1088: | |
1089: | if ($leftIsArray->maybe() && $rightIsArray->maybe()) { |
1090: | $plusable = new UnionType([ |
1091: | new StringType(), |
1092: | new FloatType(), |
1093: | new IntegerType(), |
1094: | new ArrayType(new MixedType(), new MixedType()), |
1095: | new BooleanType(), |
1096: | ]); |
1097: | |
1098: | $plusableSuperTypeOfLeft = $plusable->isSuperTypeOf($leftType)->yes(); |
1099: | $plusableSuperTypeOfRight = $plusable->isSuperTypeOf($rightType)->yes(); |
1100: | if ($plusableSuperTypeOfLeft && $plusableSuperTypeOfRight) { |
1101: | return TypeCombinator::union($leftType, $rightType); |
1102: | } |
1103: | if ($plusableSuperTypeOfLeft && $rightType instanceof MixedType) { |
1104: | return $leftType; |
1105: | } |
1106: | if ($plusableSuperTypeOfRight && $leftType instanceof MixedType) { |
1107: | return $rightType; |
1108: | } |
1109: | } |
1110: | |
1111: | return $this->resolveCommonMath(new BinaryOp\Plus($left, $right), $leftType, $rightType); |
1112: | } |
1113: | |
1114: | |
1115: | |
1116: | |
1117: | public function getMinusType(Expr $left, Expr $right, callable $getTypeCallback): Type |
1118: | { |
1119: | $leftType = $getTypeCallback($left); |
1120: | $rightType = $getTypeCallback($right); |
1121: | |
1122: | $leftTypes = $leftType->getConstantScalarTypes(); |
1123: | $rightTypes = $rightType->getConstantScalarTypes(); |
1124: | $leftTypesCount = count($leftTypes); |
1125: | $rightTypesCount = count($rightTypes); |
1126: | if ($leftTypesCount > 0 && $rightTypesCount > 0) { |
1127: | $resultTypes = []; |
1128: | $generalize = $leftTypesCount * $rightTypesCount > self::CALCULATE_SCALARS_LIMIT; |
1129: | foreach ($leftTypes as $leftTypeInner) { |
1130: | foreach ($rightTypes as $rightTypeInner) { |
1131: | $leftNumberType = $leftTypeInner->toNumber(); |
1132: | $rightNumberType = $rightTypeInner->toNumber(); |
1133: | |
1134: | if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) { |
1135: | return new ErrorType(); |
1136: | } |
1137: | |
1138: | if (!$leftNumberType instanceof ConstantScalarType || !$rightNumberType instanceof ConstantScalarType) { |
1139: | throw new ShouldNotHappenException(); |
1140: | } |
1141: | |
1142: | $resultType = $this->getTypeFromValue($leftNumberType->getValue() - $rightNumberType->getValue()); |
1143: | if ($generalize) { |
1144: | $resultType = $resultType->generalize(GeneralizePrecision::lessSpecific()); |
1145: | } |
1146: | $resultTypes[] = $resultType; |
1147: | } |
1148: | } |
1149: | |
1150: | return TypeCombinator::union(...$resultTypes); |
1151: | } |
1152: | |
1153: | return $this->resolveCommonMath(new BinaryOp\Minus($left, $right), $leftType, $rightType); |
1154: | } |
1155: | |
1156: | |
1157: | |
1158: | |
1159: | public function getMulType(Expr $left, Expr $right, callable $getTypeCallback): Type |
1160: | { |
1161: | $leftType = $getTypeCallback($left); |
1162: | $rightType = $getTypeCallback($right); |
1163: | |
1164: | $leftTypes = $leftType->getConstantScalarTypes(); |
1165: | $rightTypes = $rightType->getConstantScalarTypes(); |
1166: | $leftTypesCount = count($leftTypes); |
1167: | $rightTypesCount = count($rightTypes); |
1168: | if ($leftTypesCount > 0 && $rightTypesCount > 0) { |
1169: | $resultTypes = []; |
1170: | $generalize = $leftTypesCount * $rightTypesCount > self::CALCULATE_SCALARS_LIMIT; |
1171: | foreach ($leftTypes as $leftTypeInner) { |
1172: | foreach ($rightTypes as $rightTypeInner) { |
1173: | $leftNumberType = $leftTypeInner->toNumber(); |
1174: | $rightNumberType = $rightTypeInner->toNumber(); |
1175: | |
1176: | if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) { |
1177: | return new ErrorType(); |
1178: | } |
1179: | |
1180: | if (!$leftNumberType instanceof ConstantScalarType || !$rightNumberType instanceof ConstantScalarType) { |
1181: | throw new ShouldNotHappenException(); |
1182: | } |
1183: | |
1184: | $resultType = $this->getTypeFromValue($leftNumberType->getValue() * $rightNumberType->getValue()); |
1185: | if ($generalize) { |
1186: | $resultType = $resultType->generalize(GeneralizePrecision::lessSpecific()); |
1187: | } |
1188: | $resultTypes[] = $resultType; |
1189: | } |
1190: | } |
1191: | |
1192: | return TypeCombinator::union(...$resultTypes); |
1193: | } |
1194: | |
1195: | $floatType = new FloatType(); |
1196: | $leftNumberType = $leftType->toNumber(); |
1197: | if ($leftNumberType instanceof ConstantIntegerType && $leftNumberType->getValue() === 0) { |
1198: | if ($floatType->isSuperTypeOf($rightType)->yes()) { |
1199: | return new ConstantFloatType(0.0); |
1200: | } |
1201: | return new ConstantIntegerType(0); |
1202: | } |
1203: | $rightNumberType = $rightType->toNumber(); |
1204: | if ($rightNumberType instanceof ConstantIntegerType && $rightNumberType->getValue() === 0) { |
1205: | if ($floatType->isSuperTypeOf($leftType)->yes()) { |
1206: | return new ConstantFloatType(0.0); |
1207: | } |
1208: | return new ConstantIntegerType(0); |
1209: | } |
1210: | |
1211: | return $this->resolveCommonMath(new BinaryOp\Mul($left, $right), $leftType, $rightType); |
1212: | } |
1213: | |
1214: | |
1215: | |
1216: | |
1217: | public function getPowType(Expr $left, Expr $right, callable $getTypeCallback): Type |
1218: | { |
1219: | $leftType = $getTypeCallback($left); |
1220: | $rightType = $getTypeCallback($right); |
1221: | |
1222: | $exponentiatedTyped = $leftType->exponentiate($rightType); |
1223: | if (!$exponentiatedTyped instanceof ErrorType) { |
1224: | return $exponentiatedTyped; |
1225: | } |
1226: | |
1227: | $extensionSpecified = $this->callOperatorTypeSpecifyingExtensions(new BinaryOp\Pow($left, $right), $leftType, $rightType); |
1228: | if ($extensionSpecified !== null) { |
1229: | return $extensionSpecified; |
1230: | } |
1231: | |
1232: | return new ErrorType(); |
1233: | } |
1234: | |
1235: | |
1236: | |
1237: | |
1238: | public function getShiftLeftType(Expr $left, Expr $right, callable $getTypeCallback): Type |
1239: | { |
1240: | $leftType = $getTypeCallback($left); |
1241: | $rightType = $getTypeCallback($right); |
1242: | |
1243: | if ($leftType instanceof NeverType || $rightType instanceof NeverType) { |
1244: | return $this->getNeverType($leftType, $rightType); |
1245: | } |
1246: | |
1247: | $leftTypes = $leftType->getConstantScalarTypes(); |
1248: | $rightTypes = $rightType->getConstantScalarTypes(); |
1249: | $leftTypesCount = count($leftTypes); |
1250: | $rightTypesCount = count($rightTypes); |
1251: | if ($leftTypesCount > 0 && $rightTypesCount > 0) { |
1252: | $resultTypes = []; |
1253: | $generalize = $leftTypesCount * $rightTypesCount > self::CALCULATE_SCALARS_LIMIT; |
1254: | foreach ($leftTypes as $leftTypeInner) { |
1255: | foreach ($rightTypes as $rightTypeInner) { |
1256: | $leftNumberType = $leftTypeInner->toNumber(); |
1257: | $rightNumberType = $rightTypeInner->toNumber(); |
1258: | |
1259: | if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) { |
1260: | return new ErrorType(); |
1261: | } |
1262: | |
1263: | if (!$leftNumberType instanceof ConstantScalarType || !$rightNumberType instanceof ConstantScalarType) { |
1264: | throw new ShouldNotHappenException(); |
1265: | } |
1266: | |
1267: | if ($rightNumberType->getValue() < 0) { |
1268: | return new ErrorType(); |
1269: | } |
1270: | |
1271: | $resultType = $this->getTypeFromValue(intval($leftNumberType->getValue()) << intval($rightNumberType->getValue())); |
1272: | if ($generalize) { |
1273: | $resultType = $resultType->generalize(GeneralizePrecision::lessSpecific()); |
1274: | } |
1275: | $resultTypes[] = $resultType; |
1276: | } |
1277: | } |
1278: | |
1279: | return TypeCombinator::union(...$resultTypes); |
1280: | } |
1281: | |
1282: | $leftNumberType = $leftType->toNumber(); |
1283: | $rightNumberType = $rightType->toNumber(); |
1284: | |
1285: | if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) { |
1286: | return new ErrorType(); |
1287: | } |
1288: | |
1289: | return $this->resolveCommonMath(new Expr\BinaryOp\ShiftLeft($left, $right), $leftType, $rightType); |
1290: | } |
1291: | |
1292: | |
1293: | |
1294: | |
1295: | public function getShiftRightType(Expr $left, Expr $right, callable $getTypeCallback): Type |
1296: | { |
1297: | $leftType = $getTypeCallback($left); |
1298: | $rightType = $getTypeCallback($right); |
1299: | |
1300: | if ($leftType instanceof NeverType || $rightType instanceof NeverType) { |
1301: | return $this->getNeverType($leftType, $rightType); |
1302: | } |
1303: | |
1304: | $leftTypes = $leftType->getConstantScalarTypes(); |
1305: | $rightTypes = $rightType->getConstantScalarTypes(); |
1306: | $leftTypesCount = count($leftTypes); |
1307: | $rightTypesCount = count($rightTypes); |
1308: | if ($leftTypesCount > 0 && $rightTypesCount > 0) { |
1309: | $resultTypes = []; |
1310: | $generalize = $leftTypesCount * $rightTypesCount > self::CALCULATE_SCALARS_LIMIT; |
1311: | foreach ($leftTypes as $leftTypeInner) { |
1312: | foreach ($rightTypes as $rightTypeInner) { |
1313: | $leftNumberType = $leftTypeInner->toNumber(); |
1314: | $rightNumberType = $rightTypeInner->toNumber(); |
1315: | |
1316: | if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) { |
1317: | return new ErrorType(); |
1318: | } |
1319: | |
1320: | if (!$leftNumberType instanceof ConstantScalarType || !$rightNumberType instanceof ConstantScalarType) { |
1321: | throw new ShouldNotHappenException(); |
1322: | } |
1323: | |
1324: | if ($rightNumberType->getValue() < 0) { |
1325: | return new ErrorType(); |
1326: | } |
1327: | |
1328: | $resultType = $this->getTypeFromValue(intval($leftNumberType->getValue()) >> intval($rightNumberType->getValue())); |
1329: | if ($generalize) { |
1330: | $resultType = $resultType->generalize(GeneralizePrecision::lessSpecific()); |
1331: | } |
1332: | $resultTypes[] = $resultType; |
1333: | } |
1334: | } |
1335: | |
1336: | return TypeCombinator::union(...$resultTypes); |
1337: | } |
1338: | |
1339: | $leftNumberType = $leftType->toNumber(); |
1340: | $rightNumberType = $rightType->toNumber(); |
1341: | |
1342: | if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) { |
1343: | return new ErrorType(); |
1344: | } |
1345: | |
1346: | return $this->resolveCommonMath(new Expr\BinaryOp\ShiftRight($left, $right), $leftType, $rightType); |
1347: | } |
1348: | |
1349: | public function resolveIdenticalType(Type $leftType, Type $rightType): BooleanType |
1350: | { |
1351: | if ($leftType instanceof NeverType || $rightType instanceof NeverType) { |
1352: | return new ConstantBooleanType(false); |
1353: | } |
1354: | |
1355: | if ($leftType instanceof ConstantScalarType && $rightType instanceof ConstantScalarType) { |
1356: | return new ConstantBooleanType($leftType->getValue() === $rightType->getValue()); |
1357: | } |
1358: | |
1359: | $leftTypeFiniteTypes = $leftType->getFiniteTypes(); |
1360: | $rightTypeFiniteType = $rightType->getFiniteTypes(); |
1361: | if (count($leftTypeFiniteTypes) === 1 && count($rightTypeFiniteType) === 1) { |
1362: | return new ConstantBooleanType($leftTypeFiniteTypes[0]->equals($rightTypeFiniteType[0])); |
1363: | } |
1364: | |
1365: | if ($leftType->isSuperTypeOf($rightType)->no() && $rightType->isSuperTypeOf($leftType)->no()) { |
1366: | return new ConstantBooleanType(false); |
1367: | } |
1368: | |
1369: | if ($leftType instanceof ConstantArrayType && $rightType instanceof ConstantArrayType) { |
1370: | return $this->resolveConstantArrayTypeComparison($leftType, $rightType, fn ($leftValueType, $rightValueType): BooleanType => $this->resolveIdenticalType($leftValueType, $rightValueType)); |
1371: | } |
1372: | |
1373: | return new BooleanType(); |
1374: | } |
1375: | |
1376: | public function resolveEqualType(Type $leftType, Type $rightType): BooleanType |
1377: | { |
1378: | if ( |
1379: | ($leftType->isEnum()->yes() && $rightType->isTrue()->no()) |
1380: | || ($rightType->isEnum()->yes() && $leftType->isTrue()->no()) |
1381: | ) { |
1382: | return $this->resolveIdenticalType($leftType, $rightType); |
1383: | } |
1384: | |
1385: | if ($leftType instanceof ConstantArrayType && $rightType instanceof ConstantArrayType) { |
1386: | return $this->resolveConstantArrayTypeComparison($leftType, $rightType, fn ($leftValueType, $rightValueType): BooleanType => $this->resolveEqualType($leftValueType, $rightValueType)); |
1387: | } |
1388: | |
1389: | return $leftType->looseCompare($rightType, $this->phpVersion); |
1390: | } |
1391: | |
1392: | |
1393: | |
1394: | |
1395: | private function resolveConstantArrayTypeComparison(ConstantArrayType $leftType, ConstantArrayType $rightType, callable $valueComparisonCallback): BooleanType |
1396: | { |
1397: | $leftKeyTypes = $leftType->getKeyTypes(); |
1398: | $rightKeyTypes = $rightType->getKeyTypes(); |
1399: | $leftValueTypes = $leftType->getValueTypes(); |
1400: | $rightValueTypes = $rightType->getValueTypes(); |
1401: | |
1402: | $resultType = new ConstantBooleanType(true); |
1403: | |
1404: | foreach ($leftKeyTypes as $i => $leftKeyType) { |
1405: | $leftOptional = $leftType->isOptionalKey($i); |
1406: | if ($leftOptional) { |
1407: | $resultType = new BooleanType(); |
1408: | } |
1409: | |
1410: | if (count($rightKeyTypes) === 0) { |
1411: | if (!$leftOptional) { |
1412: | return new ConstantBooleanType(false); |
1413: | } |
1414: | continue; |
1415: | } |
1416: | |
1417: | $found = false; |
1418: | foreach ($rightKeyTypes as $j => $rightKeyType) { |
1419: | unset($rightKeyTypes[$j]); |
1420: | |
1421: | if ($leftKeyType->equals($rightKeyType)) { |
1422: | $found = true; |
1423: | break; |
1424: | } elseif (!$rightType->isOptionalKey($j)) { |
1425: | return new ConstantBooleanType(false); |
1426: | } |
1427: | } |
1428: | |
1429: | if (!$found) { |
1430: | if (!$leftOptional) { |
1431: | return new ConstantBooleanType(false); |
1432: | } |
1433: | continue; |
1434: | } |
1435: | |
1436: | if (!isset($j)) { |
1437: | throw new ShouldNotHappenException(); |
1438: | } |
1439: | |
1440: | $rightOptional = $rightType->isOptionalKey($j); |
1441: | if ($rightOptional) { |
1442: | $resultType = new BooleanType(); |
1443: | if ($leftOptional) { |
1444: | continue; |
1445: | } |
1446: | } |
1447: | |
1448: | $leftIdenticalToRight = $valueComparisonCallback($leftValueTypes[$i], $rightValueTypes[$j]); |
1449: | if ($leftIdenticalToRight->isFalse()->yes()) { |
1450: | return new ConstantBooleanType(false); |
1451: | } |
1452: | $resultType = TypeCombinator::union($resultType, $leftIdenticalToRight); |
1453: | } |
1454: | |
1455: | foreach (array_keys($rightKeyTypes) as $j) { |
1456: | if (!$rightType->isOptionalKey($j)) { |
1457: | return new ConstantBooleanType(false); |
1458: | } |
1459: | $resultType = new BooleanType(); |
1460: | } |
1461: | |
1462: | return $resultType->toBoolean(); |
1463: | } |
1464: | |
1465: | private function callOperatorTypeSpecifyingExtensions(Expr\BinaryOp $expr, Type $leftType, Type $rightType): ?Type |
1466: | { |
1467: | $operatorSigil = $expr->getOperatorSigil(); |
1468: | $operatorTypeSpecifyingExtensions = $this->operatorTypeSpecifyingExtensionRegistryProvider->getRegistry()->getOperatorTypeSpecifyingExtensions($operatorSigil, $leftType, $rightType); |
1469: | |
1470: | |
1471: | $extensionTypes = []; |
1472: | |
1473: | foreach ($operatorTypeSpecifyingExtensions as $extension) { |
1474: | $extensionTypes[] = $extension->specifyType($operatorSigil, $leftType, $rightType); |
1475: | } |
1476: | |
1477: | if (count($extensionTypes) > 0) { |
1478: | return TypeCombinator::union(...$extensionTypes); |
1479: | } |
1480: | |
1481: | return null; |
1482: | } |
1483: | |
1484: | |
1485: | |
1486: | |
1487: | private function resolveCommonMath(Expr\BinaryOp $expr, Type $leftType, Type $rightType): Type |
1488: | { |
1489: | $types = TypeCombinator::union($leftType, $rightType); |
1490: | $leftNumberType = $leftType->toNumber(); |
1491: | $rightNumberType = $rightType->toNumber(); |
1492: | |
1493: | if ( |
1494: | !$types instanceof MixedType |
1495: | && ( |
1496: | $rightNumberType instanceof IntegerRangeType |
1497: | || $rightNumberType instanceof ConstantIntegerType |
1498: | || $rightNumberType instanceof UnionType |
1499: | ) |
1500: | ) { |
1501: | if ($leftNumberType instanceof IntegerRangeType || $leftNumberType instanceof ConstantIntegerType) { |
1502: | return $this->integerRangeMath( |
1503: | $leftNumberType, |
1504: | $expr, |
1505: | $rightNumberType, |
1506: | ); |
1507: | } elseif ($leftNumberType instanceof UnionType) { |
1508: | $unionParts = []; |
1509: | |
1510: | foreach ($leftNumberType->getTypes() as $type) { |
1511: | $numberType = $type->toNumber(); |
1512: | if ($numberType instanceof IntegerRangeType || $numberType instanceof ConstantIntegerType) { |
1513: | $unionParts[] = $this->integerRangeMath($numberType, $expr, $rightNumberType); |
1514: | } else { |
1515: | $unionParts[] = $numberType; |
1516: | } |
1517: | } |
1518: | |
1519: | $union = TypeCombinator::union(...$unionParts); |
1520: | if ($leftNumberType instanceof BenevolentUnionType) { |
1521: | return TypeUtils::toBenevolentUnion($union)->toNumber(); |
1522: | } |
1523: | |
1524: | return $union->toNumber(); |
1525: | } |
1526: | } |
1527: | |
1528: | $specifiedTypes = $this->callOperatorTypeSpecifyingExtensions($expr, $leftType, $rightType); |
1529: | if ($specifiedTypes !== null) { |
1530: | return $specifiedTypes; |
1531: | } |
1532: | |
1533: | if ( |
1534: | $leftType->isArray()->yes() |
1535: | || $rightType->isArray()->yes() |
1536: | || $types->isArray()->yes() |
1537: | ) { |
1538: | return new ErrorType(); |
1539: | } |
1540: | |
1541: | if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) { |
1542: | return new ErrorType(); |
1543: | } |
1544: | if ($leftNumberType instanceof NeverType || $rightNumberType instanceof NeverType) { |
1545: | return $this->getNeverType($leftNumberType, $rightNumberType); |
1546: | } |
1547: | |
1548: | if ( |
1549: | $leftNumberType->isFloat()->yes() |
1550: | || $rightNumberType->isFloat()->yes() |
1551: | ) { |
1552: | if ($expr instanceof Expr\BinaryOp\ShiftLeft || $expr instanceof Expr\BinaryOp\ShiftRight) { |
1553: | return new IntegerType(); |
1554: | } |
1555: | return new FloatType(); |
1556: | } |
1557: | |
1558: | $resultType = TypeCombinator::union($leftNumberType, $rightNumberType); |
1559: | if ($expr instanceof Expr\BinaryOp\Div) { |
1560: | if ($types instanceof MixedType || $resultType->isInteger()->yes()) { |
1561: | return new BenevolentUnionType([new IntegerType(), new FloatType()]); |
1562: | } |
1563: | |
1564: | return new UnionType([new IntegerType(), new FloatType()]); |
1565: | } |
1566: | |
1567: | if ($types instanceof MixedType |
1568: | || $leftType instanceof BenevolentUnionType |
1569: | || $rightType instanceof BenevolentUnionType |
1570: | ) { |
1571: | return TypeUtils::toBenevolentUnion($resultType); |
1572: | } |
1573: | |
1574: | return $resultType; |
1575: | } |
1576: | |
1577: | |
1578: | |
1579: | |
1580: | |
1581: | private function integerRangeMath(Type $range, BinaryOp $node, Type $operand): Type |
1582: | { |
1583: | if ($range instanceof IntegerRangeType) { |
1584: | $rangeMin = $range->getMin(); |
1585: | $rangeMax = $range->getMax(); |
1586: | } else { |
1587: | $rangeMin = $range->getValue(); |
1588: | $rangeMax = $rangeMin; |
1589: | } |
1590: | |
1591: | if ($operand instanceof UnionType) { |
1592: | |
1593: | $unionParts = []; |
1594: | |
1595: | foreach ($operand->getTypes() as $type) { |
1596: | $numberType = $type->toNumber(); |
1597: | if ($numberType instanceof IntegerRangeType || $numberType instanceof ConstantIntegerType) { |
1598: | $unionParts[] = $this->integerRangeMath($range, $node, $numberType); |
1599: | } else { |
1600: | $unionParts[] = $type->toNumber(); |
1601: | } |
1602: | } |
1603: | |
1604: | $union = TypeCombinator::union(...$unionParts); |
1605: | if ($operand instanceof BenevolentUnionType) { |
1606: | return TypeUtils::toBenevolentUnion($union)->toNumber(); |
1607: | } |
1608: | |
1609: | return $union->toNumber(); |
1610: | } |
1611: | |
1612: | $operand = $operand->toNumber(); |
1613: | if ($operand instanceof IntegerRangeType) { |
1614: | $operandMin = $operand->getMin(); |
1615: | $operandMax = $operand->getMax(); |
1616: | } elseif ($operand instanceof ConstantIntegerType) { |
1617: | $operandMin = $operand->getValue(); |
1618: | $operandMax = $operand->getValue(); |
1619: | } else { |
1620: | return $operand; |
1621: | } |
1622: | |
1623: | if ($node instanceof BinaryOp\Plus) { |
1624: | if ($operand instanceof ConstantIntegerType) { |
1625: | |
1626: | $min = $rangeMin !== null ? $rangeMin + $operand->getValue() : null; |
1627: | |
1628: | |
1629: | $max = $rangeMax !== null ? $rangeMax + $operand->getValue() : null; |
1630: | } else { |
1631: | |
1632: | $min = $rangeMin !== null && $operand->getMin() !== null ? $rangeMin + $operand->getMin() : null; |
1633: | |
1634: | |
1635: | $max = $rangeMax !== null && $operand->getMax() !== null ? $rangeMax + $operand->getMax() : null; |
1636: | } |
1637: | } elseif ($node instanceof BinaryOp\Minus) { |
1638: | if ($operand instanceof ConstantIntegerType) { |
1639: | |
1640: | $min = $rangeMin !== null ? $rangeMin - $operand->getValue() : null; |
1641: | |
1642: | |
1643: | $max = $rangeMax !== null ? $rangeMax - $operand->getValue() : null; |
1644: | } else { |
1645: | if ($rangeMin === $rangeMax && $rangeMin !== null |
1646: | && ($operand->getMin() === null || $operand->getMax() === null)) { |
1647: | $min = null; |
1648: | $max = $rangeMin; |
1649: | } else { |
1650: | if ($operand->getMin() === null) { |
1651: | $min = null; |
1652: | } elseif ($rangeMin !== null) { |
1653: | if ($operand->getMax() !== null) { |
1654: | |
1655: | $min = $rangeMin - $operand->getMax(); |
1656: | } else { |
1657: | |
1658: | $min = $rangeMin - $operand->getMin(); |
1659: | } |
1660: | } else { |
1661: | $min = null; |
1662: | } |
1663: | |
1664: | if ($operand->getMax() === null) { |
1665: | $min = null; |
1666: | $max = null; |
1667: | } elseif ($rangeMax !== null) { |
1668: | if ($rangeMin !== null && $operand->getMin() === null) { |
1669: | |
1670: | $min = $rangeMin - $operand->getMax(); |
1671: | $max = null; |
1672: | } elseif ($operand->getMin() !== null) { |
1673: | |
1674: | $max = $rangeMax - $operand->getMin(); |
1675: | } else { |
1676: | $max = null; |
1677: | } |
1678: | } else { |
1679: | $max = null; |
1680: | } |
1681: | |
1682: | if ($min !== null && $max !== null && $min > $max) { |
1683: | [$min, $max] = [$max, $min]; |
1684: | } |
1685: | } |
1686: | } |
1687: | } elseif ($node instanceof Expr\BinaryOp\Mul) { |
1688: | $min1 = $rangeMin === 0 || $operandMin === 0 ? 0 : ($rangeMin ?? -INF) * ($operandMin ?? -INF); |
1689: | $min2 = $rangeMin === 0 || $operandMax === 0 ? 0 : ($rangeMin ?? -INF) * ($operandMax ?? INF); |
1690: | $max1 = $rangeMax === 0 || $operandMin === 0 ? 0 : ($rangeMax ?? INF) * ($operandMin ?? -INF); |
1691: | $max2 = $rangeMax === 0 || $operandMax === 0 ? 0 : ($rangeMax ?? INF) * ($operandMax ?? INF); |
1692: | |
1693: | $min = min($min1, $min2, $max1, $max2); |
1694: | $max = max($min1, $min2, $max1, $max2); |
1695: | |
1696: | if (!is_finite($min)) { |
1697: | $min = null; |
1698: | } |
1699: | if (!is_finite($max)) { |
1700: | $max = null; |
1701: | } |
1702: | } elseif ($node instanceof Expr\BinaryOp\Div) { |
1703: | if ($operand instanceof ConstantIntegerType) { |
1704: | $min = $rangeMin !== null && $operand->getValue() !== 0 ? $rangeMin / $operand->getValue() : null; |
1705: | $max = $rangeMax !== null && $operand->getValue() !== 0 ? $rangeMax / $operand->getValue() : null; |
1706: | } else { |
1707: | |
1708: | $operandMin = $operandMin !== 0 ? $operandMin : 1; |
1709: | $operandMax = $operandMax !== 0 ? $operandMax : -1; |
1710: | |
1711: | if ( |
1712: | ($operandMin < 0 || $operandMin === null) |
1713: | && ($operandMax > 0 || $operandMax === null) |
1714: | ) { |
1715: | $negativeOperand = IntegerRangeType::fromInterval($operandMin, 0); |
1716: | assert($negativeOperand instanceof IntegerRangeType); |
1717: | $positiveOperand = IntegerRangeType::fromInterval(0, $operandMax); |
1718: | assert($positiveOperand instanceof IntegerRangeType); |
1719: | |
1720: | $result = TypeCombinator::union( |
1721: | $this->integerRangeMath($range, $node, $negativeOperand), |
1722: | $this->integerRangeMath($range, $node, $positiveOperand), |
1723: | )->toNumber(); |
1724: | |
1725: | if ($result->equals(new UnionType([new IntegerType(), new FloatType()]))) { |
1726: | return new BenevolentUnionType([new IntegerType(), new FloatType()]); |
1727: | } |
1728: | |
1729: | return $result; |
1730: | } |
1731: | if ( |
1732: | ($rangeMin < 0 || $rangeMin === null) |
1733: | && ($rangeMax > 0 || $rangeMax === null) |
1734: | ) { |
1735: | $negativeRange = IntegerRangeType::fromInterval($rangeMin, 0); |
1736: | assert($negativeRange instanceof IntegerRangeType); |
1737: | $positiveRange = IntegerRangeType::fromInterval(0, $rangeMax); |
1738: | assert($positiveRange instanceof IntegerRangeType); |
1739: | |
1740: | $result = TypeCombinator::union( |
1741: | $this->integerRangeMath($negativeRange, $node, $operand), |
1742: | $this->integerRangeMath($positiveRange, $node, $operand), |
1743: | )->toNumber(); |
1744: | |
1745: | if ($result->equals(new UnionType([new IntegerType(), new FloatType()]))) { |
1746: | return new BenevolentUnionType([new IntegerType(), new FloatType()]); |
1747: | } |
1748: | |
1749: | return $result; |
1750: | } |
1751: | |
1752: | $rangeMinSign = ($rangeMin ?? -INF) <=> 0; |
1753: | $rangeMaxSign = ($rangeMax ?? INF) <=> 0; |
1754: | |
1755: | $min1 = $operandMin !== null ? ($rangeMin ?? -INF) / $operandMin : $rangeMinSign * -0.1; |
1756: | $min2 = $operandMax !== null ? ($rangeMin ?? -INF) / $operandMax : $rangeMinSign * 0.1; |
1757: | $max1 = $operandMin !== null ? ($rangeMax ?? INF) / $operandMin : $rangeMaxSign * -0.1; |
1758: | $max2 = $operandMax !== null ? ($rangeMax ?? INF) / $operandMax : $rangeMaxSign * 0.1; |
1759: | |
1760: | $min = min($min1, $min2, $max1, $max2); |
1761: | $max = max($min1, $min2, $max1, $max2); |
1762: | |
1763: | if ($min === -INF) { |
1764: | $min = null; |
1765: | } |
1766: | if ($max === INF) { |
1767: | $max = null; |
1768: | } |
1769: | } |
1770: | |
1771: | if ($min !== null && $max !== null && $min > $max) { |
1772: | [$min, $max] = [$max, $min]; |
1773: | } |
1774: | |
1775: | if (is_float($min)) { |
1776: | $min = (int) ceil($min); |
1777: | } |
1778: | if (is_float($max)) { |
1779: | $max = (int) floor($max); |
1780: | } |
1781: | |
1782: | |
1783: | if ((($range instanceof ConstantIntegerType && $range->getValue() < 0) |
1784: | || ($operand instanceof ConstantIntegerType && $operand->getValue() < 0)) |
1785: | && ($min === null || $max === null)) { |
1786: | [$min, $max] = [$max, $min]; |
1787: | } |
1788: | |
1789: | if ($min === null && $max === null) { |
1790: | return new BenevolentUnionType([new IntegerType(), new FloatType()]); |
1791: | } |
1792: | |
1793: | return TypeCombinator::union(IntegerRangeType::fromInterval($min, $max), new FloatType()); |
1794: | } elseif ($node instanceof Expr\BinaryOp\ShiftLeft) { |
1795: | if (!$operand instanceof ConstantIntegerType) { |
1796: | return new IntegerType(); |
1797: | } |
1798: | if ($operand->getValue() < 0) { |
1799: | return new ErrorType(); |
1800: | } |
1801: | $min = $rangeMin !== null ? intval($rangeMin) << $operand->getValue() : null; |
1802: | $max = $rangeMax !== null ? intval($rangeMax) << $operand->getValue() : null; |
1803: | } elseif ($node instanceof Expr\BinaryOp\ShiftRight) { |
1804: | if (!$operand instanceof ConstantIntegerType) { |
1805: | return new IntegerType(); |
1806: | } |
1807: | if ($operand->getValue() < 0) { |
1808: | return new ErrorType(); |
1809: | } |
1810: | $min = $rangeMin !== null ? intval($rangeMin) >> $operand->getValue() : null; |
1811: | $max = $rangeMax !== null ? intval($rangeMax) >> $operand->getValue() : null; |
1812: | } else { |
1813: | throw new ShouldNotHappenException(); |
1814: | } |
1815: | |
1816: | if (is_float($min)) { |
1817: | $min = null; |
1818: | } |
1819: | if (is_float($max)) { |
1820: | $max = null; |
1821: | } |
1822: | |
1823: | return IntegerRangeType::fromInterval($min, $max); |
1824: | } |
1825: | |
1826: | |
1827: | |
1828: | |
1829: | public function getClassConstFetchTypeByReflection(Name|Expr $class, string $constantName, ?ClassReflection $classReflection, callable $getTypeCallback): Type |
1830: | { |
1831: | $isObject = false; |
1832: | if ($class instanceof Name) { |
1833: | $constantClass = (string) $class; |
1834: | $constantClassType = new ObjectType($constantClass); |
1835: | $namesToResolve = [ |
1836: | 'self', |
1837: | 'parent', |
1838: | ]; |
1839: | if ($classReflection !== null) { |
1840: | if ($classReflection->isFinal()) { |
1841: | $namesToResolve[] = 'static'; |
1842: | } elseif (strtolower($constantClass) === 'static') { |
1843: | if (strtolower($constantName) === 'class') { |
1844: | return new GenericClassStringType(new StaticType($classReflection)); |
1845: | } |
1846: | |
1847: | $namesToResolve[] = 'static'; |
1848: | $isObject = true; |
1849: | } |
1850: | } |
1851: | if (in_array(strtolower($constantClass), $namesToResolve, true)) { |
1852: | $resolvedName = $this->resolveName($class, $classReflection); |
1853: | if (strtolower($resolvedName) === 'parent' && strtolower($constantName) === 'class') { |
1854: | return new ClassStringType(); |
1855: | } |
1856: | $constantClassType = $this->resolveTypeByName($class, $classReflection); |
1857: | } |
1858: | |
1859: | if (strtolower($constantName) === 'class') { |
1860: | return new ConstantStringType($constantClassType->getClassName(), true); |
1861: | } |
1862: | } elseif ($class instanceof String_ && strtolower($constantName) === 'class') { |
1863: | return new ConstantStringType($class->value, true); |
1864: | } else { |
1865: | $constantClassType = $getTypeCallback($class); |
1866: | $isObject = true; |
1867: | } |
1868: | |
1869: | if (strtolower($constantName) === 'class') { |
1870: | return TypeTraverser::map( |
1871: | $constantClassType, |
1872: | function (Type $type, callable $traverse): Type { |
1873: | if ($type instanceof UnionType || $type instanceof IntersectionType) { |
1874: | return $traverse($type); |
1875: | } |
1876: | |
1877: | if ($type instanceof NullType) { |
1878: | return $type; |
1879: | } |
1880: | |
1881: | if ($type instanceof EnumCaseObjectType) { |
1882: | return TypeCombinator::intersect( |
1883: | new GenericClassStringType(new ObjectType($type->getClassName())), |
1884: | new AccessoryLiteralStringType(), |
1885: | ); |
1886: | } |
1887: | |
1888: | $objectClassNames = $type->getObjectClassNames(); |
1889: | if (count($objectClassNames) > 1) { |
1890: | throw new ShouldNotHappenException(); |
1891: | } |
1892: | |
1893: | if ($type instanceof TemplateType && $objectClassNames === []) { |
1894: | return TypeCombinator::intersect( |
1895: | new GenericClassStringType($type), |
1896: | new AccessoryLiteralStringType(), |
1897: | ); |
1898: | } elseif ($objectClassNames !== [] && $this->getReflectionProvider()->hasClass($objectClassNames[0])) { |
1899: | $reflection = $this->getReflectionProvider()->getClass($objectClassNames[0]); |
1900: | if ($reflection->isFinalByKeyword()) { |
1901: | return new ConstantStringType($reflection->getName(), true); |
1902: | } |
1903: | |
1904: | return TypeCombinator::intersect( |
1905: | new GenericClassStringType($type), |
1906: | new AccessoryLiteralStringType(), |
1907: | ); |
1908: | } elseif ($type->isObject()->yes()) { |
1909: | return TypeCombinator::intersect( |
1910: | new ClassStringType(), |
1911: | new AccessoryLiteralStringType(), |
1912: | ); |
1913: | } |
1914: | |
1915: | return new ErrorType(); |
1916: | }, |
1917: | ); |
1918: | } |
1919: | |
1920: | if ($constantClassType->isClassStringType()->yes()) { |
1921: | if ($constantClassType->isConstantScalarValue()->yes()) { |
1922: | $isObject = false; |
1923: | } |
1924: | $constantClassType = $constantClassType->getClassStringObjectType(); |
1925: | } |
1926: | |
1927: | $types = []; |
1928: | foreach ($constantClassType->getObjectClassNames() as $referencedClass) { |
1929: | if (!$this->getReflectionProvider()->hasClass($referencedClass)) { |
1930: | continue; |
1931: | } |
1932: | |
1933: | $constantClassReflection = $this->getReflectionProvider()->getClass($referencedClass); |
1934: | if (!$constantClassReflection->hasConstant($constantName)) { |
1935: | continue; |
1936: | } |
1937: | |
1938: | if ($constantClassReflection->isEnum() && $constantClassReflection->hasEnumCase($constantName)) { |
1939: | $types[] = new EnumCaseObjectType($constantClassReflection->getName(), $constantName); |
1940: | continue; |
1941: | } |
1942: | |
1943: | $resolvingName = sprintf('%s::%s', $constantClassReflection->getName(), $constantName); |
1944: | if (array_key_exists($resolvingName, $this->currentlyResolvingClassConstant)) { |
1945: | $types[] = new MixedType(); |
1946: | continue; |
1947: | } |
1948: | |
1949: | $this->currentlyResolvingClassConstant[$resolvingName] = true; |
1950: | |
1951: | if (!$isObject) { |
1952: | $reflectionConstant = $constantClassReflection->getNativeReflection()->getReflectionConstant($constantName); |
1953: | if ($reflectionConstant === false) { |
1954: | unset($this->currentlyResolvingClassConstant[$resolvingName]); |
1955: | continue; |
1956: | } |
1957: | $reflectionConstantDeclaringClass = $reflectionConstant->getDeclaringClass(); |
1958: | $constantType = $this->getType($reflectionConstant->getValueExpression(), InitializerExprContext::fromClass($reflectionConstantDeclaringClass->getName(), $reflectionConstantDeclaringClass->getFileName() ?: null)); |
1959: | $nativeType = null; |
1960: | if ($reflectionConstant->getType() !== null) { |
1961: | $nativeType = TypehintHelper::decideTypeFromReflection($reflectionConstant->getType(), null, $constantClassReflection); |
1962: | } |
1963: | $types[] = $this->constantResolver->resolveClassConstantType( |
1964: | $constantClassReflection->getName(), |
1965: | $constantName, |
1966: | $constantType, |
1967: | $nativeType, |
1968: | ); |
1969: | unset($this->currentlyResolvingClassConstant[$resolvingName]); |
1970: | continue; |
1971: | } |
1972: | |
1973: | $constantReflection = $constantClassReflection->getConstant($constantName); |
1974: | if ( |
1975: | !$constantClassReflection->isFinal() |
1976: | && !$constantReflection->hasPhpDocType() |
1977: | && !$constantReflection->hasNativeType() |
1978: | ) { |
1979: | unset($this->currentlyResolvingClassConstant[$resolvingName]); |
1980: | return new MixedType(); |
1981: | } |
1982: | |
1983: | if (!$constantClassReflection->isFinal()) { |
1984: | $constantType = $constantReflection->getValueType(); |
1985: | } else { |
1986: | $constantType = $this->getType($constantReflection->getValueExpr(), InitializerExprContext::fromClassReflection($constantReflection->getDeclaringClass())); |
1987: | } |
1988: | |
1989: | $nativeType = $constantReflection->getNativeType(); |
1990: | $constantType = $this->constantResolver->resolveClassConstantType( |
1991: | $constantClassReflection->getName(), |
1992: | $constantName, |
1993: | $constantType, |
1994: | $nativeType, |
1995: | ); |
1996: | unset($this->currentlyResolvingClassConstant[$resolvingName]); |
1997: | $types[] = $constantType; |
1998: | } |
1999: | |
2000: | if (count($types) > 0) { |
2001: | return TypeCombinator::union(...$types); |
2002: | } |
2003: | |
2004: | if (!$constantClassType->hasConstant($constantName)->yes()) { |
2005: | return new ErrorType(); |
2006: | } |
2007: | |
2008: | return $constantClassType->getConstant($constantName)->getValueType(); |
2009: | } |
2010: | |
2011: | |
2012: | |
2013: | |
2014: | public function getClassConstFetchType(Name|Expr $class, string $constantName, ?string $className, callable $getTypeCallback): Type |
2015: | { |
2016: | $classReflection = null; |
2017: | if ($className !== null && $this->getReflectionProvider()->hasClass($className)) { |
2018: | $classReflection = $this->getReflectionProvider()->getClass($className); |
2019: | } |
2020: | |
2021: | return $this->getClassConstFetchTypeByReflection($class, $constantName, $classReflection, $getTypeCallback); |
2022: | } |
2023: | |
2024: | |
2025: | |
2026: | |
2027: | public function getUnaryMinusType(Expr $expr, callable $getTypeCallback): Type |
2028: | { |
2029: | $type = $getTypeCallback($expr)->toNumber(); |
2030: | $scalarValues = $type->getConstantScalarValues(); |
2031: | |
2032: | if (count($scalarValues) > 0) { |
2033: | $newTypes = []; |
2034: | foreach ($scalarValues as $scalarValue) { |
2035: | if (is_int($scalarValue)) { |
2036: | |
2037: | $newValue = -$scalarValue; |
2038: | if (!is_int($newValue)) { |
2039: | return $type; |
2040: | } |
2041: | $newTypes[] = new ConstantIntegerType($newValue); |
2042: | } elseif (is_float($scalarValue)) { |
2043: | $newTypes[] = new ConstantFloatType(-$scalarValue); |
2044: | } |
2045: | } |
2046: | |
2047: | return TypeCombinator::union(...$newTypes); |
2048: | } |
2049: | |
2050: | if ($type instanceof IntegerRangeType) { |
2051: | return $getTypeCallback(new Expr\BinaryOp\Mul($expr, new LNumber(-1))); |
2052: | } |
2053: | |
2054: | return $type; |
2055: | } |
2056: | |
2057: | |
2058: | |
2059: | |
2060: | public function getBitwiseNotType(Expr $expr, callable $getTypeCallback): Type |
2061: | { |
2062: | $exprType = $getTypeCallback($expr); |
2063: | return TypeTraverser::map($exprType, static function (Type $type, callable $traverse): Type { |
2064: | if ($type instanceof UnionType || $type instanceof IntersectionType) { |
2065: | return $traverse($type); |
2066: | } |
2067: | if ($type instanceof ConstantStringType) { |
2068: | return new ConstantStringType(~$type->getValue()); |
2069: | } |
2070: | if ($type->isString()->yes()) { |
2071: | $accessories = [ |
2072: | new StringType(), |
2073: | ]; |
2074: | if ($type->isNonEmptyString()->yes()) { |
2075: | $accessories[] = new AccessoryNonEmptyStringType(); |
2076: | } |
2077: | |
2078: | |
2079: | |
2080: | return TypeCombinator::intersect(...$accessories); |
2081: | } |
2082: | if ($type->isInteger()->yes() || $type->isFloat()->yes()) { |
2083: | return new IntegerType(); |
2084: | } |
2085: | return new ErrorType(); |
2086: | }); |
2087: | } |
2088: | |
2089: | private function resolveName(Name $name, ?ClassReflection $classReflection): string |
2090: | { |
2091: | $originalClass = (string) $name; |
2092: | if ($classReflection !== null) { |
2093: | $lowerClass = strtolower($originalClass); |
2094: | |
2095: | if (in_array($lowerClass, [ |
2096: | 'self', |
2097: | 'static', |
2098: | ], true)) { |
2099: | return $classReflection->getName(); |
2100: | } elseif ($lowerClass === 'parent') { |
2101: | if ($classReflection->getParentClass() !== null) { |
2102: | return $classReflection->getParentClass()->getName(); |
2103: | } |
2104: | } |
2105: | } |
2106: | |
2107: | return $originalClass; |
2108: | } |
2109: | |
2110: | private function resolveTypeByName(Name $name, ?ClassReflection $classReflection): TypeWithClassName |
2111: | { |
2112: | if ($name->toLowerString() === 'static' && $classReflection !== null) { |
2113: | return new StaticType($classReflection); |
2114: | } |
2115: | |
2116: | $originalClass = $this->resolveName($name, $classReflection); |
2117: | if ($classReflection !== null) { |
2118: | $thisType = new ThisType($classReflection); |
2119: | $ancestor = $thisType->getAncestorWithClassName($originalClass); |
2120: | if ($ancestor !== null) { |
2121: | return $ancestor; |
2122: | } |
2123: | } |
2124: | |
2125: | return new ObjectType($originalClass); |
2126: | } |
2127: | |
2128: | |
2129: | |
2130: | |
2131: | private function getTypeFromValue($value): Type |
2132: | { |
2133: | return ConstantTypeHelper::getTypeFromValue($value); |
2134: | } |
2135: | |
2136: | private function getReflectionProvider(): ReflectionProvider |
2137: | { |
2138: | return $this->reflectionProviderProvider->getReflectionProvider(); |
2139: | } |
2140: | |
2141: | private function getNeverType(Type $leftType, Type $rightType): Type |
2142: | { |
2143: | |
2144: | if ($leftType instanceof NeverType && $leftType->isExplicit()) { |
2145: | return $leftType; |
2146: | } |
2147: | if ($rightType instanceof NeverType && $rightType->isExplicit()) { |
2148: | return $rightType; |
2149: | } |
2150: | return new NeverType(); |
2151: | } |
2152: | |
2153: | } |
2154: | |