Skip to content

Commit

Permalink
test numeric with maybe zero length
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Apr 12, 2024
1 parent fa250e7 commit b579b25
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Type/Php/StrRepeatFunctionReturnTypeExtension.php
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -94,7 +97,6 @@ public function getTypeFromFunctionCall(
$accessoryTypes[] = new StringType();
return new IntersectionType($accessoryTypes);
}

return new StringType();
}

Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Analyser/data/literal-string.php
Expand Up @@ -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));

Expand Down

0 comments on commit b579b25

Please sign in to comment.