Skip to content

Commit 8d16a7c

Browse files
committed
Simplify
1 parent cb35a10 commit 8d16a7c

File tree

2 files changed

+15
-27
lines changed

2 files changed

+15
-27
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -987,47 +987,35 @@ private function specifyTypesForConstantBinaryExpression(
987987
$argType = $scope->getType($exprNode->getArgs()[0]->value);
988988

989989
if (count($exprNode->getArgs()) === 1) {
990-
$isNormalCount = true;
990+
$isNormalCount = TrinaryLogic::createYes();
991991
} else {
992992
$mode = $scope->getType($exprNode->getArgs()[1]->value);
993-
if (!$mode->isInteger()->yes()) {
994-
return new SpecifiedTypes();
995-
}
996-
997-
$isNormalCount = (new ConstantIntegerType(COUNT_NORMAL))->isSuperTypeOf($mode)->yes();
998-
if (!$isNormalCount) {
999-
$isNormalCount = $argType->getIterableValueType()->isArray()->no();
1000-
}
993+
$isNormalCount = (new ConstantIntegerType(COUNT_NORMAL))->isSuperTypeOf($mode)->or($argType->getIterableValueType()->isArray()->negate());
1001994
}
1002995

1003996
if (
1004-
$isNormalCount
997+
$isNormalCount->yes()
1005998
&& $argType instanceof UnionType
1006-
&& $argType->isList()->yes()
1007999
) {
10081000
$result = [];
10091001
foreach ($argType->getTypes() as $innerType) {
1010-
if (!$innerType->isConstantArray()->yes()) {
1011-
$result = null;
1012-
break;
1013-
}
1014-
10151002
$arraySize = $innerType->getArraySize();
1016-
if (!$arraySize instanceof ConstantIntegerType) {
1017-
$result = null;
1018-
break;
1003+
$isSize = $constantType->isSuperTypeOf($arraySize);
1004+
if ($context->truthy()) {
1005+
if ($isSize->no()) {
1006+
continue;
1007+
}
10191008
}
1020-
1021-
if (!$constantType->isSuperTypeOf($arraySize)->yes()) {
1022-
continue;
1009+
if ($context->falsey()) {
1010+
if (!$isSize->yes()) {
1011+
continue;
1012+
}
10231013
}
10241014

10251015
$result[] = $innerType;
10261016
}
10271017

1028-
if ($result !== null) {
1029-
return $this->create($exprNode->getArgs()[0]->value, TypeCombinator::union(...$result), $context, false, $scope, $rootExpr);
1030-
}
1018+
return $this->create($exprNode->getArgs()[0]->value, TypeCombinator::union(...$result), $context, false, $scope, $rootExpr);
10311019
}
10321020

10331021
if ($context->truthy() || $constantType->getValue() === 0) {
@@ -1038,7 +1026,7 @@ private function specifyTypesForConstantBinaryExpression(
10381026

10391027
if ($argType->isArray()->yes()) {
10401028
$funcTypes = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
1041-
if ($isNormalCount && $argType->isList()->yes() && $context->truthy() && $constantType->getValue() < ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
1029+
if ($isNormalCount->yes() && $argType->isList()->yes() && $context->truthy() && $constantType->getValue() < ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
10421030
$valueTypesBuilder = ConstantArrayTypeBuilder::createEmpty();
10431031
$itemType = $argType->getIterableValueType();
10441032
for ($i = 0; $i < $constantType->getValue(); $i++) {

tests/PHPStan/Analyser/nsrt/narrow-tagged-union.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function arrayIntRangeSize(): void
103103
if (count($x) === 1) {
104104
assertType("array{'xy'}|array{0: 'ab', 1?: 'xy'}", $x);
105105
} else {
106-
assertType("array{}|array{'xy'}|array{0: 'ab', 1?: 'xy'}", $x);
106+
assertType("array{}|array{0: 'ab', 1?: 'xy'}", $x);
107107
}
108108
assertType("array{}|array{'xy'}|array{0: 'ab', 1?: 'xy'}", $x);
109109
}

0 commit comments

Comments
 (0)