Skip to content

Commit ee69212

Browse files
phpstan-botclaude
andcommitted
Add regression test for phpstan/phpstan#12798
Test is_subclass_of in enum traits with BackedEnum and custom interface checks. Verifies no false positive function.alreadyNarrowedType or function.impossibleType errors are reported. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 44458d8 commit ee69212

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,4 +1230,11 @@ public function testBug13687(): void
12301230
$this->analyse([__DIR__ . '/data/bug-13687.php'], []);
12311231
}
12321232

1233+
#[RequiresPhp('>= 8.1')]
1234+
public function testBug12798(): void
1235+
{
1236+
$this->treatPhpDocTypesAsCertain = true;
1237+
$this->analyse([__DIR__ . '/data/bug-12798.php'], []);
1238+
}
1239+
12331240
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php declare(strict_types = 1); // lint >= 8.1
2+
3+
namespace Bug12798;
4+
5+
interface Colorable
6+
{
7+
public function color(): string;
8+
}
9+
10+
trait HasColors
11+
{
12+
/** @return array<string|int, string> */
13+
public static function colors(): array {
14+
return array_reduce(self::cases(), function (array $colors, self $case) {
15+
$key = is_subclass_of($case, \BackedEnum::class) ? $case->value : $case->name;
16+
$color = is_subclass_of($case, Colorable::class) ? $case->color() : 'gray';
17+
18+
$colors[$key] = $color;
19+
return $colors;
20+
}, []);
21+
}
22+
}
23+
24+
enum AlertLevelBacked: int implements Colorable
25+
{
26+
use HasColors;
27+
28+
case Low = 1;
29+
case Medium = 2;
30+
case Critical = 3;
31+
32+
public function color(): string
33+
{
34+
return match ($this) {
35+
self::Low => 'green',
36+
self::Medium => 'yellow',
37+
self::Critical => 'red',
38+
};
39+
}
40+
}
41+
42+
enum AlertLevel
43+
{
44+
use HasColors;
45+
46+
case Low;
47+
case Medium;
48+
case Critical;
49+
}

0 commit comments

Comments
 (0)