From 3d6a42179375a49e31888da3a645a3a94a65e44d Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Wed, 1 Sep 2021 11:59:32 +0200 Subject: [PATCH] Change UnionType description if it contains TemplateUnionType --- src/Type/UnionType.php | 3 +- .../Rules/Methods/ReturnTypeRuleTest.php | 15 ++++++++ .../Methods/data/return-template-union.php | 38 +++++++++++++++++++ tests/PHPStan/Type/TypeCombinatorTest.php | 2 +- 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 tests/PHPStan/Rules/Methods/data/return-template-union.php diff --git a/src/Type/UnionType.php b/src/Type/UnionType.php index f1c2ee2aae..d24baf3bbc 100644 --- a/src/Type/UnionType.php +++ b/src/Type/UnionType.php @@ -16,6 +16,7 @@ use PHPStan\Type\Generic\TemplateType; use PHPStan\Type\Generic\TemplateTypeMap; use PHPStan\Type\Generic\TemplateTypeVariance; +use PHPStan\Type\Generic\TemplateUnionType; /** @api */ class UnionType implements CompoundType @@ -153,7 +154,7 @@ public function describe(VerbosityLevel $level): string $joinTypes = static function (array $types) use ($level): string { $typeNames = []; foreach ($types as $type) { - if ($type instanceof ClosureType || $type instanceof CallableType) { + if ($type instanceof ClosureType || $type instanceof CallableType || $type instanceof TemplateUnionType) { $typeNames[] = sprintf('(%s)', $type->describe($level)); } elseif ($type instanceof IntersectionType) { $intersectionDescription = $type->describe($level); diff --git a/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php b/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php index 2a59ec2ed7..cdc7ad1b48 100644 --- a/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php +++ b/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php @@ -512,4 +512,19 @@ public function testBug3151(): void $this->analyse([__DIR__ . '/data/bug-3151.php'], []); } + public function testTemplateUnion(): void + { + $this->analyse([__DIR__ . '/data/return-template-union.php'], [ + [ + 'Method ReturnTemplateUnion\Foo::doFoo2() should return T of bool|float|int|string but returns (T of bool|float|int|string)|null.', + 25, + ], + [ + // should not be reported + 'Method ReturnTemplateUnion\Foo::doFoo3() should return (T of bool|float|int|string)|null but returns (T of bool|float|int|string)|null.', + 35, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Methods/data/return-template-union.php b/tests/PHPStan/Rules/Methods/data/return-template-union.php new file mode 100644 index 0000000000..a154bfd03d --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/return-template-union.php @@ -0,0 +1,38 @@ +