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
22 changes: 21 additions & 1 deletion src/Reflection/ParametersAcceptorSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,27 @@ public static function selectFromArgs(
}
if ($originalArg->unpack) {
$unpack = true;
$types[$index] = $type->getIterableValueType();
$constantArrays = $type->getConstantArrays();
if (count($constantArrays) > 0) {
foreach ($constantArrays as $constantArray) {
$values = $constantArray->getValueTypes();
foreach ($constantArray->getKeyTypes() as $j => $keyType) {
$valueType = $values[$j];
$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
28 changes: 28 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-8257.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace Bug8257;

use function PHPStan\Testing\assertType;

interface TreeMapper
{
/**
* @template T of object
*
* @param string|class-string<T> $signature
* @param mixed $source
* @return (
* $signature is class-string<T>
* ? T
* : mixed
* )
*/
public function map(string $signature, $source);
}

/** @var TreeMapper $tm */
$tm;

class A {}

assertType('Bug8257\A', $tm->map(...[A::class, []]));
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Classes/InstantiationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,12 @@ public function testBug13440(): void
$this->analyse([__DIR__ . '/data/bug-13440.php'], []);
}

#[RequiresPhp('>= 8.0')]
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,10 @@ public function testBug10559(): void
$this->analyse([__DIR__ . '/data/bug-10559.php'], []);
}

#[RequiresPhp('>= 8.0')]
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']);
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3908,6 +3908,19 @@ public function testBug1501(): void
$this->analyse([__DIR__ . '/data/bug-1501.php'], []);
}

public function testBug7369(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->analyse([__DIR__ . '/data/bug-7369.php'], [
[
'Cannot call method isAllowed() on bool.',
40,
],
]);
}

public function testBug11463(): void
{
$this->checkThisOnly = false;
Expand Down
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']);
43 changes: 43 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-7369.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

namespace Bug7369;

interface AccountInterface {}

interface AccessResultInterface {
public function isAllowed(): bool;
}

interface AccessibleInterface {

/**
* @return ($return_as_object is true ? AccessResultInterface : bool)
*/
public function access(string $operation, AccountInterface $account = NULL, bool $return_as_object = FALSE);
}

$class = new class() implements AccessibleInterface {
/**
* {@inheritDoc}
*/
public function access(
string $operation,
AccountInterface $account = null,
bool $return_as_object = false
) {
if ($return_as_object) {
return new class () implements AccessResultInterface {
public function isAllowed(): bool {
return true;
}
};
}
return false;
}
};

$class->access('view', null, true)->isAllowed();
$class->access('view', null, false)->isAllowed();

$params = ['view', null, true];
$class->access(...$params)->isAllowed();
Loading