Skip to content

Commit cb68632

Browse files
committed
Consider int range offset in truthy context
1 parent b71049f commit cb68632

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public function specifyTypesInCondition(
280280
$sizeType = IntegerRangeType::createAllGreaterThan($leftType->getValue());
281281
}
282282
} elseif ($leftType instanceof IntegerRangeType) {
283-
$sizeType = $leftType;
283+
$sizeType = $leftType->shift($offset);
284284
}
285285

286286
$specifiedTypes = $this->specifyTypesForCountFuncCall($expr->right, $argType, $sizeType, $context, $scope, $expr);
@@ -987,23 +987,14 @@ private function specifyTypesForCountFuncCall(FuncCall $countFuncCall, Type $typ
987987

988988
$arraySize = $type->getArraySize();
989989
$isSize = $sizeType->isSuperTypeOf($arraySize);
990-
if ($context->truthy()) {
991-
if ($isSize->no()) {
992-
return new NeverType();
993-
}
994-
995-
$constArray = $this->turnListIntoConstantArray($type, $sizeType);
996-
if ($constArray !== null) {
997-
$type = $constArray;
998-
}
990+
if ($context->truthy() && $isSize->no()) {
991+
return new NeverType();
999992
}
1000-
if ($context->falsey()) {
1001-
if (!$isSize->yes()) {
1002-
return new NeverType();
1003-
}
993+
if ($context->falsey() && !$isSize->yes()) {
994+
return new NeverType();
1004995
}
1005996

1006-
return $type;
997+
return $this->turnListIntoConstantArray($type, $sizeType) ?? $type;
1007998
});
1008999

10091000
return $this->create($countFuncCall->getArgs()[0]->value, $resultType, $context, $scope)->setRootExpr($rootExpr);

tests/PHPStan/Analyser/nsrt/bug-4700.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ function(array $array, int $count): void {
4141
if (isset($array['e'])) $a[] = $array['e'];
4242
if (count($a) > $count) {
4343
assertType('int<2, 5>', count($a));
44-
assertType('array{0: mixed~null, 1?: mixed~null, 2?: mixed~null, 3?: mixed~null, 4?: mixed~null}', $a);
44+
assertType('list{0: mixed~null, 1: mixed~null, 2?: mixed~null, 3?: mixed~null, 4?: mixed~null}', $a);
4545
} else {
46-
assertType('0', count($a));
47-
assertType('array{}', $a);
46+
assertType('int<0, 5>', count($a)); // Could be int<0, 1>
47+
assertType('array{}|array{0: mixed~null, 1?: mixed~null, 2?: mixed~null, 3?: mixed~null, 4?: mixed~null}', $a); // Could be array{}|array{0: mixed~null}
4848
}
4949
};

0 commit comments

Comments
 (0)