File tree Expand file tree Collapse file tree 4 files changed +56
-2
lines changed Expand file tree Collapse file tree 4 files changed +56
-2
lines changed Original file line number Diff line number Diff line change 1616use PHPStan \Type \Generic \TemplateType ;
1717use PHPStan \Type \Generic \TemplateTypeMap ;
1818use PHPStan \Type \Generic \TemplateTypeVariance ;
19+ use PHPStan \Type \Generic \TemplateUnionType ;
1920
2021/** @api */
2122class 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 );
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments