Skip to content

Commit

Permalink
Fix native type of variadic parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 18, 2022
1 parent 3aa878f commit a5447db
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3036,7 +3036,16 @@ private function enterFunctionLike(
}
}
$variableTypes[$parameter->getName()] = VariableTypeHolder::createYes($parameterType);
$nativeExpressionTypes[sprintf('$%s', $parameter->getName())] = $parameter->getNativeType();

$nativeParameterType = $parameter->getNativeType();
if ($parameter->isVariadic()) {
if ($this->phpVersion->supportsNamedArguments()) {
$nativeParameterType = new ArrayType(new UnionType([new IntegerType(), new StringType()]), $nativeParameterType);
} else {
$nativeParameterType = new ArrayType(new IntegerType(), $nativeParameterType);
}
}
$nativeExpressionTypes[sprintf('$%s', $parameter->getName())] = $nativeParameterType;
}

if ($preserveThis && array_key_exists('this', $this->variableTypes)) {
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,13 @@ public function dataFileAsserts(): iterable

yield from $this->gatherAssertTypes(__DIR__ . '/data/weird-array_key_exists-issue.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/equal.php');

if (PHP_VERSION_ID >= 80000) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5698-php8.php');
} else {
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5698-php7.php');
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6404.php');
}

Expand Down
16 changes: 16 additions & 0 deletions tests/PHPStan/Analyser/data/bug-5698-php7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Bug5698;

use function PHPStan\Testing\assertNativeType;
use function PHPStan\Testing\assertType;

class FooPHP7
{

function foo(int ...$foo): void {
assertType('array<int, int>', $foo);
assertNativeType('array<int, int>', $foo);
}

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Analyser/data/bug-5698-php8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Bug5698;

use function PHPStan\Testing\assertNativeType;
use function PHPStan\Testing\assertType;

class FooPHP8
{

function foo(int ...$foo): void {
assertType('array<int|string, int>', $foo);
assertNativeType('array<int|string, int>', $foo);
}

}

0 comments on commit a5447db

Please sign in to comment.