From 1b5710a3aa87a3d475d565c8ded12de170ee6cb6 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Wed, 1 Sep 2021 09:34:30 +0200 Subject: [PATCH] Fix inferring TemplateUnionType --- src/Type/Generic/TemplateTypeTrait.php | 2 +- .../Analyser/NodeScopeResolverTest.php | 1 + .../Rules/Methods/CallMethodsRuleTest.php | 8 +++++ tests/PHPStan/Rules/Methods/data/bug-5562.php | 34 +++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/PHPStan/Rules/Methods/data/bug-5562.php diff --git a/src/Type/Generic/TemplateTypeTrait.php b/src/Type/Generic/TemplateTypeTrait.php index 498c1de77a..d62acb63a5 100644 --- a/src/Type/Generic/TemplateTypeTrait.php +++ b/src/Type/Generic/TemplateTypeTrait.php @@ -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); } diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 7fe63f2d18..de90465d96 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -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'); } /** diff --git a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php index 13f7f0a93b..842737f589 100644 --- a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php @@ -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'], []); + } + } diff --git a/tests/PHPStan/Rules/Methods/data/bug-5562.php b/tests/PHPStan/Rules/Methods/data/bug-5562.php new file mode 100644 index 0000000000..3bcf4b1d4e --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/bug-5562.php @@ -0,0 +1,34 @@ +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; + } + +}