Skip to content
Open
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
32 changes: 32 additions & 0 deletions src/Type/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,38 @@ public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
$myTypes = $this->types;
}

// When every member of the union is a template type (e.g. T1|T2) and the received
// type only matches some of them, the unmatched template types cannot be inferred
// from this argument. Bind them to never so the union narrows to the matched members
// instead of widening each unmatched template to its bound.
if (count($myTypes) > 1) {
$templateMembers = [];
foreach ($myTypes as $type) {
if (!$type instanceof TemplateType) {
$templateMembers = [];
break;
}
$templateMembers[] = $type;
}

if (count($templateMembers) > 0) {
$matchedTypes = TemplateTypeMap::createEmpty();
$unmatchedNames = [];
foreach ($templateMembers as $type) {
$inferred = $type->inferTemplateTypes($receivedType);
if ($inferred->isEmpty()) {
$unmatchedNames[] = $type->getName();
continue;
}
$matchedTypes = $matchedTypes->union($inferred);
}

if (!$matchedTypes->isEmpty() && count($unmatchedNames) > 0) {
return $matchedTypes->union(new TemplateTypeMap(array_fill_keys($unmatchedNames, new NeverType(true))));
}
}
}

foreach ($myTypes as $type) {
if ($type instanceof TemplateType || ($type instanceof GenericClassStringType && $type->getGenericType() instanceof TemplateType)) {
continue;
Expand Down
42 changes: 42 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-2579.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);

namespace Bug2579;

use function PHPStan\Testing\assertType;

class A {}
class B {}
class A1 extends A {}
class B1 extends B {}

/**
* @template T1 of A
* @template T2 of B
* @param T1|T2 $type
* @return T1|T2
*/
function f(object $type) {
return $type;
}

function test(): void
{
assertType('Bug2579\\A1', f(new A1()));
assertType('Bug2579\\B1', f(new B1()));
}

/**
* @template T1 of A
* @template T2 of B
* @template T3 of \Countable
* @param T1|T2|T3 $type
* @return T1|T2|T3
*/
function g(object $type) {
return $type;
}

function test3(): void
{
assertType('Bug2579\\A1', g(new A1()));
}
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,23 @@ public function testBug14720(): void
$this->analyse([__DIR__ . '/data/bug-14720.php'], []);
}

public function testBug2579(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->analyse([__DIR__ . '/data/bug-2579.php'], [
[
'Call to an undefined method Bug2579Methods\A1::bar().',
30,
],
[
'Call to an undefined method Bug2579Methods\B1::foo().',
31,
],
]);
}

public function testClosureBind(): void
{
$this->checkThisOnly = false;
Expand Down
32 changes: 32 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-2579.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);

namespace Bug2579Methods;

class A {}
class B {}
class A1 extends A {
public function foo(): void {}
}
class B1 extends B {
public function bar(): void {}
}

/**
* @template T1 of A
* @template T2 of B
* @param T1|T2 $type
* @return T1|T2
*/
function f(object $type)
{
return new $type();
}

function test(): void
{
(f(new A1()))->foo(); // should pass
(f(new B1()))->bar(); // should pass

(f(new A1()))->bar(); // should fail
(f(new B1()))->foo(); // should fail
}
Loading