Skip to content
Closed
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 @@ -59,6 +59,9 @@ public static function resolve(array $argTypes, ParametersAcceptor $parametersAc
foreach ($parameters as $param) {
if (isset($namedArgTypes[$param->getName()])) {
$argType = $namedArgTypes[$param->getName()];
if ($argType instanceof ErrorType && $param->getDefaultValue() !== null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Checking ErrorType seems too permissive.

I wonder if we should introduce custom class extending ErrorType (or some custom data to this class like a reason) and update

return new ErrorType();

Cause something like

		$d = $a->getFoo();
		assertType('*ERROR*', $d);
		Testing::testMethod($d);
		assertType('Bug12687\A&Bug12687\I', $d);

seems wrong to me

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll add coverage with #5109

$argType = $param->getDefaultValue();
}
} elseif ($param->getDefaultValue() !== null) {
$argType = $param->getDefaultValue();
} else {
Expand Down
42 changes: 42 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-12687.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);

namespace Bug12687;

use function PHPStan\Testing\assertType;

class A
{
}

interface I
{
function method(): void;
}

class Testing
{
/**
* @param A|null $arg
* @param-out ($arg is null ? A&I : A) $arg
*/
public static function testMethod(?A &$arg = null): void
{
if ($arg === null) {
$arg = new A();
}
}

public function doTest(): void
{
$a = new A();
Testing::testMethod($a);
assertType('Bug12687\A', $a);

$b = null;
Testing::testMethod($b);
assertType('Bug12687\A&Bug12687\I', $b);

Testing::testMethod($c);
assertType('Bug12687\A&Bug12687\I', $c);
}
}
Loading