Skip to content

Commit

Permalink
Fix inferring TemplateUnionType
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 1, 2021
1 parent f6e4f87 commit 1b5710a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Type/Generic/TemplateTypeTrait.php
Expand Up @@ -162,7 +162,7 @@ public function isSubTypeOf(Type $type): TrinaryLogic

public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
{
if ($receivedType instanceof UnionType || $receivedType instanceof IntersectionType) {
if (!$receivedType instanceof TemplateType && ($receivedType instanceof UnionType || $receivedType instanceof IntersectionType)) {
return $receivedType->inferTemplateTypesOn($this);
}

Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -499,6 +499,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/math.php');

yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-1870.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/bug-5562.php');
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Expand Up @@ -2124,4 +2124,12 @@ public function testBug3530(): void
$this->analyse([__DIR__ . '/data/bug-3530.php'], []);
}

public function testBug5562(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->analyse([__DIR__ . '/data/bug-5562.php'], []);
}

}
34 changes: 34 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-5562.php
@@ -0,0 +1,34 @@
<?php

namespace Bug5562;

use function PHPStan\Testing\assertType;

class Foo
{

/**
* @template T of int|string
* @param T $test
* @return T
*/
public function foo($test)
{
assertType('T of int|string (method Bug5562\Foo::foo(), argument)', $test);
$bar = $this->bar($test);
assertType('T of int|string (method Bug5562\Foo::foo(), argument)', $bar);

return $bar;
}

/**
* @template T of int|string
* @param T $test
* @return T
*/
public function bar($test)
{
return $test;
}

}

0 comments on commit 1b5710a

Please sign in to comment.