Skip to content

Commit a5447db

Browse files
committed
Fix native type of variadic parameter
1 parent 3aa878f commit a5447db

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

src/Analyser/MutatingScope.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3036,7 +3036,16 @@ private function enterFunctionLike(
30363036
}
30373037
}
30383038
$variableTypes[$parameter->getName()] = VariableTypeHolder::createYes($parameterType);
3039-
$nativeExpressionTypes[sprintf('$%s', $parameter->getName())] = $parameter->getNativeType();
3039+
3040+
$nativeParameterType = $parameter->getNativeType();
3041+
if ($parameter->isVariadic()) {
3042+
if ($this->phpVersion->supportsNamedArguments()) {
3043+
$nativeParameterType = new ArrayType(new UnionType([new IntegerType(), new StringType()]), $nativeParameterType);
3044+
} else {
3045+
$nativeParameterType = new ArrayType(new IntegerType(), $nativeParameterType);
3046+
}
3047+
}
3048+
$nativeExpressionTypes[sprintf('$%s', $parameter->getName())] = $nativeParameterType;
30403049
}
30413050

30423051
if ($preserveThis && array_key_exists('this', $this->variableTypes)) {

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,13 @@ public function dataFileAsserts(): iterable
609609

610610
yield from $this->gatherAssertTypes(__DIR__ . '/data/weird-array_key_exists-issue.php');
611611
yield from $this->gatherAssertTypes(__DIR__ . '/data/equal.php');
612+
613+
if (PHP_VERSION_ID >= 80000) {
614+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5698-php8.php');
615+
} else {
616+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5698-php7.php');
617+
}
618+
612619
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6404.php');
613620
}
614621

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Bug5698;
4+
5+
use function PHPStan\Testing\assertNativeType;
6+
use function PHPStan\Testing\assertType;
7+
8+
class FooPHP7
9+
{
10+
11+
function foo(int ...$foo): void {
12+
assertType('array<int, int>', $foo);
13+
assertNativeType('array<int, int>', $foo);
14+
}
15+
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Bug5698;
4+
5+
use function PHPStan\Testing\assertNativeType;
6+
use function PHPStan\Testing\assertType;
7+
8+
class FooPHP8
9+
{
10+
11+
function foo(int ...$foo): void {
12+
assertType('array<int|string, int>', $foo);
13+
assertNativeType('array<int|string, int>', $foo);
14+
}
15+
16+
}

0 commit comments

Comments
 (0)