Skip to content

Commit a07d0f5

Browse files
Resolve value of BackEnum
1 parent e52e074 commit a07d0f5

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/Type/ValueOfType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
66
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
77
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
8+
use PHPStan\Type\Generic\TemplateType;
89
use PHPStan\Type\Generic\TemplateTypeVariance;
910
use PHPStan\Type\Traits\LateResolvableTypeTrait;
1011
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
@@ -50,6 +51,13 @@ public function isResolvable(): bool
5051
protected function getResult(): Type
5152
{
5253
if ($this->type->isEnum()->yes()) {
54+
if ($this->type instanceof TemplateType) {
55+
$bound = $this->type->getBound();
56+
if ($bound->equals(new ObjectType('BackedEnum'))) {
57+
return new UnionType([new IntegerType(), new StringType()]);
58+
}
59+
}
60+
5361
$valueTypes = [];
5462
foreach ($this->type->getEnumCases() as $enumCase) {
5563
$valueType = $enumCase->getBackingValueType();

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3666,6 +3666,16 @@ public function testBug3396(): void
36663666
$this->analyse([__DIR__ . '/data/bug-3396.php'], []);
36673667
}
36683668

3669+
#[RequiresPhp('>= 8.1')]
3670+
public function testBug12219(): void
3671+
{
3672+
$this->checkThisOnly = false;
3673+
$this->checkNullables = true;
3674+
$this->checkUnionTypes = true;
3675+
$this->checkExplicitMixed = true;
3676+
$this->analyse([__DIR__ . '/data/bug-12219.php'], []);
3677+
}
3678+
36693679
public function testBug13511(): void
36703680
{
36713681
$this->checkThisOnly = false;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php // lint >= 8.1
2+
3+
namespace Bug12219;
4+
5+
class Bug
6+
{
7+
/**
8+
* @template T of \BackedEnum
9+
* @param class-string<T> $class
10+
* @param value-of<T> $value
11+
*/
12+
public function foo(string $class, mixed $value): void
13+
{
14+
}
15+
16+
/**
17+
* @template Q of \BackedEnum
18+
* @param class-string<Q> $class
19+
* @param value-of<Q> $value
20+
*/
21+
public function bar(string $class, mixed $value): void
22+
{
23+
// Parameter #2 $value of static method Bug::foo() contains unresolvable type.
24+
$this->foo($class, $value);
25+
}
26+
}

0 commit comments

Comments
 (0)