From b579b25cad69746e2b389e2bb09602cf7339efa6 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 8 Apr 2024 11:30:50 +0200 Subject: [PATCH] test numeric with maybe zero length --- src/Type/Php/StrRepeatFunctionReturnTypeExtension.php | 10 ++++++---- tests/PHPStan/Analyser/data/literal-string.php | 5 +++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Type/Php/StrRepeatFunctionReturnTypeExtension.php b/src/Type/Php/StrRepeatFunctionReturnTypeExtension.php index 95e9b9de21e..3ba27a7f9e8 100644 --- a/src/Type/Php/StrRepeatFunctionReturnTypeExtension.php +++ b/src/Type/Php/StrRepeatFunctionReturnTypeExtension.php @@ -41,17 +41,17 @@ public function getTypeFromFunctionCall( return new StringType(); } - $inputType = $scope->getType($args[0]->value); $multiplierType = $scope->getType($args[1]->value); if ((new ConstantIntegerType(0))->isSuperTypeOf($multiplierType)->yes()) { return new ConstantStringType(''); } - if ($multiplierType instanceof ConstantIntegerType && $multiplierType->getValue() < 0) { + if (IntegerRangeType::fromInterval(null, 0)->isSuperTypeOf($multiplierType)->yes()) { return new NeverType(); } + $inputType = $scope->getType($args[0]->value); if ( $inputType instanceof ConstantStringType && $multiplierType instanceof ConstantIntegerType @@ -75,7 +75,10 @@ public function getTypeFromFunctionCall( if ($inputType->isLiteralString()->yes()) { $accessoryTypes[] = new AccessoryLiteralStringType(); - if ($inputType->isNumericString()->yes()) { + if ( + $inputType->isNumericString()->yes() + && IntegerRangeType::fromInterval(1, null)->isSuperTypeOf($multiplierType)->yes() + ) { $onlyNumbers = true; foreach ($inputType->getConstantStrings() as $constantString) { if (Strings::match($constantString->getValue(), '#^[0-9]+$#') === null) { @@ -94,7 +97,6 @@ public function getTypeFromFunctionCall( $accessoryTypes[] = new StringType(); return new IntersectionType($accessoryTypes); } - return new StringType(); } diff --git a/tests/PHPStan/Analyser/data/literal-string.php b/tests/PHPStan/Analyser/data/literal-string.php index c6eb323dee2..ff63036d9b0 100644 --- a/tests/PHPStan/Analyser/data/literal-string.php +++ b/tests/PHPStan/Analyser/data/literal-string.php @@ -56,6 +56,11 @@ public function doFoo($literalString, string $string, $numericString) assertType("literal-string&non-falsy-string", str_repeat('1e9', $x)); assertType("literal-string&non-falsy-string&numeric-string", str_repeat('19', $x)); + $x = rand(0,2); + assertType("literal-string", str_repeat('19', $x)); + + $x = rand(-10,-1); + assertType("*NEVER*", str_repeat('19', $x)); assertType("'?,?,?,'", str_repeat('?,', 3)); assertType("*NEVER*", str_repeat('?,', -3));