Skip to content

Commit 1044f11

Browse files
committed
Fix GetDebugTypeFunctionReturnTypeExtension - should use TypeCombinator instead of new UnionType
1 parent f2acf14 commit 1044f11

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src/Type/Php/GetDebugTypeFunctionReturnTypeExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace PHPStan\Type\Php;
44

5-
use Closure;
65
use PhpParser\Node\Expr\FuncCall;
76
use PHPStan\Analyser\Scope;
87
use PHPStan\Reflection\FunctionReflection;
98
use PHPStan\Type\Constant\ConstantStringType;
109
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
1110
use PHPStan\Type\StringType;
1211
use PHPStan\Type\Type;
12+
use PHPStan\Type\TypeCombinator;
1313
use PHPStan\Type\UnionType;
1414
use function array_key_first;
1515
use function array_map;
@@ -31,7 +31,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3131

3232
$argType = $scope->getType($functionCall->getArgs()[0]->value);
3333
if ($argType instanceof UnionType) {
34-
return new UnionType(array_map(Closure::fromCallable([self::class, 'resolveOneType']), $argType->getTypes()));
34+
return TypeCombinator::union(...array_map(static fn (Type $type) => self::resolveOneType($type), $argType->getTypes()));
3535
}
3636
return self::resolveOneType($argType);
3737
}
@@ -92,7 +92,7 @@ private static function resolveOneType(Type $type): Type
9292
case 1:
9393
return $types[0];
9494
default:
95-
return new UnionType($types);
95+
return TypeCombinator::union(...$types);
9696
}
9797
}
9898

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,16 @@ public function testBug5231Two(): void
448448
$this->assertNotEmpty($errors);
449449
}
450450

451+
public function testBug12512(): void
452+
{
453+
if (PHP_VERSION_ID < 80100) {
454+
$this->markTestSkipped('Test requires PHP 8.1.');
455+
}
456+
457+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-12512.php');
458+
$this->assertNoErrors($errors);
459+
}
460+
451461
public function testBug5529(): void
452462
{
453463
$errors = $this->runAnalyse(__DIR__ . '/nsrt/bug-5529.php');
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php // lint >= 8.1
2+
3+
namespace Bug12512;
4+
5+
enum FooBarEnum: string
6+
{
7+
case CASE_ONE = 'case_one';
8+
case CASE_TWO = 'case_two';
9+
case CASE_THREE = 'case_three';
10+
case CASE_FOUR = 'case_four';
11+
12+
public function matchFunction(): string
13+
{
14+
return match ($this) {
15+
self::CASE_ONE => 'one',
16+
self::CASE_TWO => 'two',
17+
default => throw new \Exception(
18+
sprintf('"%s" is not implemented yet', get_debug_type($this))
19+
)
20+
};
21+
}
22+
}

0 commit comments

Comments
 (0)