Skip to content

Commit

Permalink
Change UnionType description if it contains TemplateUnionType
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 1, 2021
1 parent 0fcd93b commit 3d6a421
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Type/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
]);
}

}
38 changes: 38 additions & 0 deletions tests/PHPStan/Rules/Methods/data/return-template-union.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace ReturnTemplateUnion;

class Foo
{

/**
* @template T of bool|string|int|float
* @param T $p
* @return T
*/
public function doFoo($p)
{
return $p;
}

/**
* @template T of bool|string|int|float
* @param T|null $p
* @return T
*/
public function doFoo2($p)
{
return $p;
}

/**
* @template T of bool|string|int|float
* @param T|null $p
* @return T|null
*/
public function doFoo3($p)
{
return $p;
}

}
2 changes: 1 addition & 1 deletion tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1820,7 +1820,7 @@ public function dataUnion(): array
new NullType(),
],
UnionType::class,
'T of bool|float|int|string (function doFoo(), parameter)|null',
'(T of bool|float|int|string (function doFoo(), parameter))|null',
],
];
}
Expand Down

0 comments on commit 3d6a421

Please sign in to comment.