From 7cf4b1ae1c0db24b98482ec161692df28555e600 Mon Sep 17 00:00:00 2001 From: Martin Herndl Date: Thu, 5 Dec 2024 15:17:35 +0100 Subject: [PATCH] Consider offset for truthy int range --- src/Analyser/TypeSpecifier.php | 4 +++- tests/PHPStan/Analyser/nsrt/bug-4700.php | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Analyser/TypeSpecifier.php b/src/Analyser/TypeSpecifier.php index 3ebae58be1..ead0763a10 100644 --- a/src/Analyser/TypeSpecifier.php +++ b/src/Analyser/TypeSpecifier.php @@ -285,7 +285,9 @@ public function specifyTypesInCondition( $sizeType = IntegerRangeType::createAllGreaterThan($leftType->getValue()); } } elseif ($leftType instanceof IntegerRangeType) { - $sizeType = $leftType; + $sizeType = $context->true() + ? IntegerRangeType::fromInterval($leftType->getMin() + $offset, $leftType->getMax()) + : $leftType; } $specifiedTypes = $this->specifyTypesForCountFuncCall($expr->right, $argType, $sizeType, $context, $scope, $rootExpr); diff --git a/tests/PHPStan/Analyser/nsrt/bug-4700.php b/tests/PHPStan/Analyser/nsrt/bug-4700.php index 202aca765c..010f6fdc36 100644 --- a/tests/PHPStan/Analyser/nsrt/bug-4700.php +++ b/tests/PHPStan/Analyser/nsrt/bug-4700.php @@ -41,7 +41,7 @@ function(array $array, int $count): void { if (isset($array['e'])) $a[] = $array['e']; if (count($a) > $count) { assertType('int<2, 5>', count($a)); - assertType('array{0: mixed~null, 1?: mixed~null, 2?: mixed~null, 3?: mixed~null, 4?: mixed~null}', $a); + assertType('array{0: mixed~null, 1: mixed~null, 2?: mixed~null, 3?: mixed~null, 4?: mixed~null}&list', $a); } else { assertType('0', count($a)); assertType('array{}', $a);