Skip to content

Commit 3b6f0bf

Browse files
committed
range() of numeric-strings can produce array of float|int
1 parent 9c19d6f commit 3b6f0bf

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/Type/Php/RangeFunctionReturnTypeExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
105105

106106
$numberType = new UnionType([new IntegerType(), new FloatType()]);
107107
$isNumber = $numberType->isSuperTypeOf($argType)->yes();
108-
if ($isNumber) {
108+
$isNumericString = $argType->isNumericString()->yes();
109+
if ($isNumber || $isNumericString) {
109110
return new ArrayType(new IntegerType(), $numberType);
110111
}
111112

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ public function dataFileAsserts(): iterable
503503
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/bug-5562.php');
504504
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5615.php');
505505
yield from $this->gatherAssertTypes(__DIR__ . '/data/array_map_multiple.php');
506+
yield from $this->gatherAssertTypes(__DIR__ . '/data/range-numeric-string.php');
506507
}
507508

508509
/**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace RangeNumericString;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
10+
/**
11+
* @param numeric-string $a
12+
* @param numeric-string $b
13+
*/
14+
public function doFoo(
15+
string $a,
16+
string $b
17+
): void
18+
{
19+
assertType('array<int, float|int>', range($a, $b));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)