Skip to content
Open
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
31 changes: 30 additions & 1 deletion src/Reflection/ParametersAcceptorSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,36 @@ public static function selectFromArgs(
}
if ($originalArg->unpack) {
$unpack = true;
$types[$index] = $type->getIterableValueType();
$constantArrays = $type->getConstantArrays();
$expanded = false;
if (count($constantArrays) > 0) {
$allStringKeys = true;
foreach ($constantArrays as $constantArray) {
foreach ($constantArray->getKeyTypes() as $keyType) {
if (!$keyType->isString()->yes()) {
$allStringKeys = false;
break 2;
}
}
}
if ($allStringKeys) {
foreach ($constantArrays as $constantArray) {
foreach ($constantArray->getKeyTypes() as $j => $keyType) {
$keyName = $keyType->getValue();
if (!isset($types[$keyName])) {
$types[$keyName] = $constantArray->getValueTypes()[$j];
} else {
$types[$keyName] = TypeCombinator::union($types[$keyName], $constantArray->getValueTypes()[$j]);
}
}
}
$hasName = true;
$expanded = true;
}
}
if (!$expanded) {
$types[$index] = $type->getIterableValueType();
}
} else {
$types[$index] = $type;
}
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Classes/InstantiationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,11 @@ public function testBug14097(): void
$this->analyse([__DIR__ . '/data/bug-14097.php'], []);
}

public function testBug12363(): void
{
$this->analyse([__DIR__ . '/../Methods/data/bug-12363.php'], []);
}

public function testNewStaticWithConsistentConstructor(): void
{
$this->analyse([__DIR__ . '/data/instantiation-new-static-consistent-constructor.php'], [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2730,4 +2730,9 @@ public function testBug10559(): void
$this->analyse([__DIR__ . '/data/bug-10559.php'], []);
}

public function testBug12363(): void
{
$this->analyse([__DIR__ . '/data/bug-12363.php'], []);
}

}
26 changes: 26 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-12363.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug12363;

/**
* @template Y of 'a'|'b'
* @param Y $y
*/
function f(int $x, string $y = 'a'): void {}

// Spreading associative array with required + optional template param
f(...['x' => 5, 'y' => 'b']);

// Without spread - should also work
f(5, 'b');

/**
* @template Y of 'a'|'b'
* @param Y $y
*/
function g(string $y = 'a'): void {}

// Without preceding required arg - already works
g(...['y' => 'b']);
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-12363.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php // lint >= 8.1

declare(strict_types = 1);

namespace Bug12363Methods;

/**
* @template Y of 'a'|'b'
*/
class A
{
/**
* @param Y $y
*/
public function __construct(
public readonly int $x,
public readonly string $y = 'a',
) {
}
}

$a = new A(...['x' => 5, 'y' => 'b']);
Loading