Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Reflection/GenericParametersAcceptorResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class GenericParametersAcceptorResolver

/**
* @api
* @param Type[] $argTypes
* @param array<int|string, Type> $argTypes
*/
public static function resolve(array $argTypes, ParametersAcceptor $parametersAcceptor): ParametersAcceptor
{
Expand All @@ -21,6 +21,8 @@ public static function resolve(array $argTypes, ParametersAcceptor $parametersAc
foreach ($parametersAcceptor->getParameters() as $i => $param) {
if (isset($argTypes[$i])) {
$argType = $argTypes[$i];
} elseif (isset($argTypes[$param->getName()])) {
$argType = $argTypes[$param->getName()];
} elseif ($param->getDefaultValue() !== null) {
$argType = $param->getDefaultValue();
} else {
Expand Down
12 changes: 7 additions & 5 deletions src/Reflection/ParametersAcceptorSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use function array_key_last;
use function array_slice;
use function count;
use function sprintf;
Expand Down Expand Up @@ -161,21 +162,22 @@ public static function selectFromArgs(
}
}

foreach ($args as $arg) {
foreach ($args as $i => $arg) {
$type = $scope->getType($arg->value);
$index = $arg->name !== null ? $arg->name->toString() : $i;
if ($arg->unpack) {
$unpack = true;
$types[] = $type->getIterableValueType();
$types[$index] = $type->getIterableValueType();
} else {
$types[] = $type;
$types[$index] = $type;
}
}

return self::selectFromTypes($types, $parametersAcceptors, $unpack);
}

/**
* @param Type[] $types
* @param array<int|string, Type> $types
* @param ParametersAcceptor[] $parametersAcceptors
*/
public static function selectFromTypes(
Expand Down Expand Up @@ -246,7 +248,7 @@ public static function selectFromTypes(
break;
}

$type = $types[count($types) - 1];
$type = $types[array_key_last($types)];
} else {
$type = $types[$i];
}
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,11 @@ public function dataFileAsserts(): iterable

yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6917.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6936-limit.php');

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

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

Expand Down
36 changes: 36 additions & 0 deletions tests/PHPStan/Analyser/data/bug-5262.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

namespace Bug5262;

use function PHPStan\Testing\assertType;

/**
* @template T of TestBase
* @param class-string<T> $testclass
* @return T
*/
function test(bool $optional = false, string $testclass = TestBase::class): TestBase
{
return new $testclass();
}

class TestBase
{
}

class TestChild extends TestBase
{
public function hello(): string
{
return 'world';
}
}

function runTest(): void
{
assertType('Bug5262\TestChild', test(false, TestChild::class));
assertType('Bug5262\TestChild', test(false, testclass: TestChild::class));
assertType('Bug5262\TestChild', test(optional: false, testclass: TestChild::class));
assertType('Bug5262\TestChild', test(testclass: TestChild::class, optional: false));
assertType('Bug5262\TestChild', test(testclass: TestChild::class));
}