Skip to content

Commit

Permalink
don't crash on negative multiplier
Browse files Browse the repository at this point in the history
  • Loading branch information
clxmstaab committed Feb 22, 2022
1 parent 954e089 commit dd3af96
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Type/Php/StrRepeatFunctionReturnTypeExtension.php
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\NeverType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use function count;
Expand Down Expand Up @@ -42,6 +43,10 @@ public function getTypeFromFunctionCall(
return new ConstantStringType('');
}

if ($multiplierType instanceof ConstantIntegerType && $multiplierType->getValue() < 0){
return new NeverType();
}

if ($inputType instanceof ConstantStringType && $multiplierType instanceof ConstantIntegerType) {
return new ConstantStringType(str_repeat($inputType->getValue(), $multiplierType->getValue()));
}
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/data/literal-string.php
Expand Up @@ -28,6 +28,7 @@ public function doFoo($literalString, string $string)
assertType("''", str_repeat('', 10));
assertType("'foofoofoofoofoofoofoofoofoofoo'", str_repeat('foo', 10));
assertType("'?,?,?,'", str_repeat('?,', 3));
assertType("*NEVER*", str_repeat('?,', -3));

assertType('non-empty-string', str_pad($string, 5, $string));
assertType('non-empty-string', str_pad($literalString, 5, $string));
Expand Down

0 comments on commit dd3af96

Please sign in to comment.