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
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(true);
} else {
continue;
}
Expand Down
49 changes: 49 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-2572.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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"));

/**
* @template TValue
* @template TArgs
*
* @param TValue|\Closure(TArgs): TValue $value
* @param TArgs ...$args
* @return TValue
*/
function value($value, ...$args)
{
return $value instanceof \Closure ? $value(...$args) : $value;
}

assertType("'foo'", value('foo'));
assertType("'foo'", value('foo', 42));
assertType('42', value(fn () => 42));
assertType('42', value(function ($foo) {
assertType('true', $foo);

return 42;
}, true));
30 changes: 30 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-7704.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types = 1);

namespace Bug7704;

use function PHPStan\Testing\assertType;

/**
* @template T
* @template TRest
*
* @param T $first
* @param TRest ...$rest
*
* @return T|TRest
*/
function intersection($first, ...$rest)
{
$ret = $first;
foreach ($rest as $item) {
if (rand(0, 1)) {
$ret = $item;
}
}
return $ret;
}

assertType("'a'", intersection("a"));
assertType("'a'|'b'|'c'", intersection("a", "b", "c"));
assertType('1', intersection(1));
assertType('1|2|3', intersection(1, 2, 3));
24 changes: 24 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-9360.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types = 1);

namespace Bug9360;

use function PHPStan\Testing\assertType;

/**
* @template T of string|bool
*
* @param T ...$args
*
* @return (T is string ? string : bool)
*/
function environment(string|bool ...$args): string|bool
{
if (count($args) === 0) {
return true;
}
return (string) $args[0];
}

assertType('string', environment());
assertType('string', environment('APP_ENV'));
assertType('string', environment('APP_ENV', 'production'));
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2691,6 +2691,21 @@ public function testBug8936(): void
$this->analyse([__DIR__ . '/data/bug-8936.php'], []);
}

public function testBug2572(): void
{
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-2572.php'], []);
}

public function testBug7704(): void
{
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-7704.php'], []);
}

public function testBug9360(): void
{
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-9360.php'], []);
}

public function testDumpFunctions(): void
{
$this->analyse([__DIR__ . '/data/dump-functions.php'], [
Expand Down
Loading