Skip to content

Commit 75991d1

Browse files
committed
Consider int range offset in truthy context
1 parent 22ef97b commit 75991d1

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

src/Analyser/TypeSpecifier.php

+6-15
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public function specifyTypesInCondition(
285285
$sizeType = IntegerRangeType::createAllGreaterThan($leftType->getValue());
286286
}
287287
} elseif ($leftType instanceof IntegerRangeType) {
288-
$sizeType = $leftType;
288+
$sizeType = $leftType->shift($offset);
289289
}
290290

291291
$specifiedTypes = $this->specifyTypesForCountFuncCall($expr->right, $argType, $sizeType, $context, $scope, $rootExpr);
@@ -1025,23 +1025,14 @@ private function specifyTypesForCountFuncCall(FuncCall $countFuncCall, Type $typ
10251025

10261026
$arraySize = $type->getArraySize();
10271027
$isSize = $sizeType->isSuperTypeOf($arraySize);
1028-
if ($context->truthy()) {
1029-
if ($isSize->no()) {
1030-
return new NeverType();
1031-
}
1032-
1033-
$constArray = $this->turnListIntoConstantArray($type, $sizeType);
1034-
if ($constArray !== null) {
1035-
$type = $constArray;
1036-
}
1028+
if ($context->truthy() && $isSize->no()) {
1029+
return new NeverType();
10371030
}
1038-
if ($context->falsey()) {
1039-
if (!$isSize->yes()) {
1040-
return new NeverType();
1041-
}
1031+
if ($context->falsey() && !$isSize->yes()) {
1032+
return new NeverType();
10421033
}
10431034

1044-
return $type;
1035+
return $this->turnListIntoConstantArray($type, $sizeType) ?? $type;
10451036
});
10461037

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

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

+3-3
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('array{0: mixed~null, 1: mixed~null, 2?: mixed~null, 3?: mixed~null, 4?: mixed~null}&list', $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)