Skip to content

Commit 3d6a421

Browse files
committed
Change UnionType description if it contains TemplateUnionType
1 parent 0fcd93b commit 3d6a421

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

src/Type/UnionType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPStan\Type\Generic\TemplateType;
1717
use PHPStan\Type\Generic\TemplateTypeMap;
1818
use PHPStan\Type\Generic\TemplateTypeVariance;
19+
use PHPStan\Type\Generic\TemplateUnionType;
1920

2021
/** @api */
2122
class UnionType implements CompoundType
@@ -153,7 +154,7 @@ public function describe(VerbosityLevel $level): string
153154
$joinTypes = static function (array $types) use ($level): string {
154155
$typeNames = [];
155156
foreach ($types as $type) {
156-
if ($type instanceof ClosureType || $type instanceof CallableType) {
157+
if ($type instanceof ClosureType || $type instanceof CallableType || $type instanceof TemplateUnionType) {
157158
$typeNames[] = sprintf('(%s)', $type->describe($level));
158159
} elseif ($type instanceof IntersectionType) {
159160
$intersectionDescription = $type->describe($level);

tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,4 +512,19 @@ public function testBug3151(): void
512512
$this->analyse([__DIR__ . '/data/bug-3151.php'], []);
513513
}
514514

515+
public function testTemplateUnion(): void
516+
{
517+
$this->analyse([__DIR__ . '/data/return-template-union.php'], [
518+
[
519+
'Method ReturnTemplateUnion\Foo::doFoo2() should return T of bool|float|int|string but returns (T of bool|float|int|string)|null.',
520+
25,
521+
],
522+
[
523+
// should not be reported
524+
'Method ReturnTemplateUnion\Foo::doFoo3() should return (T of bool|float|int|string)|null but returns (T of bool|float|int|string)|null.',
525+
35,
526+
],
527+
]);
528+
}
529+
515530
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace ReturnTemplateUnion;
4+
5+
class Foo
6+
{
7+
8+
/**
9+
* @template T of bool|string|int|float
10+
* @param T $p
11+
* @return T
12+
*/
13+
public function doFoo($p)
14+
{
15+
return $p;
16+
}
17+
18+
/**
19+
* @template T of bool|string|int|float
20+
* @param T|null $p
21+
* @return T
22+
*/
23+
public function doFoo2($p)
24+
{
25+
return $p;
26+
}
27+
28+
/**
29+
* @template T of bool|string|int|float
30+
* @param T|null $p
31+
* @return T|null
32+
*/
33+
public function doFoo3($p)
34+
{
35+
return $p;
36+
}
37+
38+
}

tests/PHPStan/Type/TypeCombinatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1820,7 +1820,7 @@ public function dataUnion(): array
18201820
new NullType(),
18211821
],
18221822
UnionType::class,
1823-
'T of bool|float|int|string (function doFoo(), parameter)|null',
1823+
'(T of bool|float|int|string (function doFoo(), parameter))|null',
18241824
],
18251825
];
18261826
}

0 commit comments

Comments
 (0)