Skip to content

Commit

Permalink
range() of numeric-strings can produce array of float|int
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 12, 2021
1 parent 9c19d6f commit 3b6f0bf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Type/Php/RangeFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,

$numberType = new UnionType([new IntegerType(), new FloatType()]);
$isNumber = $numberType->isSuperTypeOf($argType)->yes();
if ($isNumber) {
$isNumericString = $argType->isNumericString()->yes();
if ($isNumber || $isNumericString) {
return new ArrayType(new IntegerType(), $numberType);
}

Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/bug-5562.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5615.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/array_map_multiple.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/range-numeric-string.php');
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Analyser/data/range-numeric-string.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace RangeNumericString;

use function PHPStan\Testing\assertType;

class Foo
{

/**
* @param numeric-string $a
* @param numeric-string $b
*/
public function doFoo(
string $a,
string $b
): void
{
assertType('array<int, float|int>', range($a, $b));
}

}

0 comments on commit 3b6f0bf

Please sign in to comment.