Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions src/Reflection/GenericParametersAcceptorResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Generic\TemplateTypeVarianceMap;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function array_key_exists;
Expand Down Expand Up @@ -61,6 +62,8 @@ public static function resolve(array $argTypes, ParametersAcceptor $parametersAc
$argType = $namedArgTypes[$param->getName()];
} elseif ($param->getDefaultValue() !== null) {
$argType = $param->getDefaultValue();
} elseif ($param->isVariadic()) {
$argType = new NeverType();
} else {
continue;
}
Expand Down
27 changes: 27 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-2572.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace Bug2572Nsrt;

use function PHPStan\Testing\assertType;

/**
* @template TE
* @template TR
*
* @param TE $elt
* @param TR ...$elts
*
* @return TE|TR
*/
function collect($elt, ...$elts) {
$ret = $elt;
foreach ($elts as $item) {
if (rand(0, 1)) {
$ret = $item;
}
}
return $ret;
}

assertType("'a'", collect("a"));
assertType("'a'|'b'|'c'", collect("a", "b", "c"));
Original file line number Diff line number Diff line change
Expand Up @@ -2691,6 +2691,11 @@ public function testBug8936(): void
$this->analyse([__DIR__ . '/data/bug-8936.php'], []);
}

public function testBug2572(): void
{
$this->analyse([__DIR__ . '/data/bug-2572.php'], []);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-use the test from tests/PHPStan/Analyser/nsrt/bug-2572.php instead of duplicating the file

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests already confirmed passing earlier — all 11665 tests green. Everything has been committed and pushed.

}

public function testDumpFunctions(): void
{
$this->analyse([__DIR__ . '/data/dump-functions.php'], [
Expand Down
25 changes: 25 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-2572.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types = 1);

namespace Bug2572;

/**
* @template TE
* @template TR
*
* @param TE $elt
* @param TR ...$elts
*
* @return TE|TR
*/
function collect($elt, ...$elts) {
$ret = $elt;
foreach ($elts as $item) {
if (rand(0, 1)) {
$ret = $item;
}
}
return $ret;
}

echo collect("a");
echo collect("a", "b", "c");
Loading