Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 20 additions & 1 deletion src/Reflection/ParametersAcceptorSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,26 @@ public static function selectFromArgs(
}
if ($originalArg->unpack) {
$unpack = true;
$types[$index] = $type->getIterableValueType();
$constantArrays = $type->getConstantArrays();
if (count($constantArrays) > 0) {
foreach ($constantArrays as $constantArray) {
foreach ($constantArray->getKeyTypes() as $j => $keyType) {
$valueType = $constantArray->getValueTypes()[$j];
Comment thread
VincentLanglet marked this conversation as resolved.
Outdated
$valueIndex = $keyType->getValue();
if (is_string($valueIndex)) {
$hasName = true;
} else {
$valueIndex = $i + $j;
}

$types[$valueIndex] = isset($types[$valueIndex])
? TypeCombinator::union($types[$valueIndex], $valueType)
: $valueType;
}
}
} else {
$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 @@ -579,6 +579,11 @@ public function testBug13440(): void
$this->analyse([__DIR__ . '/data/bug-13440.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 @@ -2735,4 +2735,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']);
52 changes: 52 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-12363.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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']);

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

$aaa = new A(...[5, 'b']);

$aaaa = new A(...[1 => 5, 2 => 'b']);

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

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

$aa = new B(1, ...[5, 'y' => 'b']);

$aaa = new B(1, ...[5, 'b']);

$aaaa = new B(1, ...[1 => 5, 2 => 'b']);
Loading