Skip to content

Commit add6977

Browse files
committed
re-use narrowUnionBySize
1 parent 33a2248 commit add6977

File tree

1 file changed

+11
-29
lines changed

1 file changed

+11
-29
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,36 +1040,11 @@ private function specifyTypesForConstantBinaryExpression(
10401040
) {
10411041
$argType = $scope->getType($exprNode->getArgs()[0]->value);
10421042

1043-
if (count($exprNode->getArgs()) === 1) {
1044-
$isNormalCount = TrinaryLogic::createYes();
1045-
} else {
1046-
$mode = $scope->getType($exprNode->getArgs()[1]->value);
1047-
$isNormalCount = (new ConstantIntegerType(COUNT_NORMAL))->isSuperTypeOf($mode)->or($argType->getIterableValueType()->isArray()->negate());
1048-
}
1049-
1050-
if (
1051-
$isNormalCount->yes()
1052-
&& $argType instanceof UnionType
1053-
) {
1054-
$result = [];
1055-
foreach ($argType->getTypes() as $innerType) {
1056-
$arraySize = $innerType->getArraySize();
1057-
$isSize = $constantType->isSuperTypeOf($arraySize);
1058-
if ($context->truthy()) {
1059-
if ($isSize->no()) {
1060-
continue;
1061-
}
1062-
}
1063-
if ($context->falsey()) {
1064-
if (!$isSize->yes()) {
1065-
continue;
1066-
}
1067-
}
1068-
1069-
$result[] = $innerType;
1043+
if ($argType instanceof UnionType) {
1044+
$narrowed = $this->narrowUnionBySize($exprNode, $argType, $constantType, $context, $scope, $rootExpr);
1045+
if ($narrowed !== null) {
1046+
return $narrowed;
10701047
}
1071-
1072-
return $this->create($exprNode->getArgs()[0]->value, TypeCombinator::union(...$result), $context, false, $scope, $rootExpr);
10731048
}
10741049

10751050
if ($context->truthy() || $constantType->getValue() === 0) {
@@ -1079,6 +1054,13 @@ private function specifyTypesForConstantBinaryExpression(
10791054
}
10801055

10811056
if ($argType->isArray()->yes()) {
1057+
if (count($exprNode->getArgs()) === 1) {
1058+
$isNormalCount = TrinaryLogic::createYes();
1059+
} else {
1060+
$mode = $scope->getType($exprNode->getArgs()[1]->value);
1061+
$isNormalCount = (new ConstantIntegerType(COUNT_NORMAL))->isSuperTypeOf($mode)->or($argType->getIterableValueType()->isArray()->negate());
1062+
}
1063+
10821064
$funcTypes = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
10831065
if ($isNormalCount->yes() && $argType->isList()->yes() && $context->truthy() && $constantType->getValue() < ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
10841066
$valueTypesBuilder = ConstantArrayTypeBuilder::createEmpty();

0 commit comments

Comments
 (0)