Skip to content

Commit

Permalink
Merge pull request #10274 from tuqqu/enum-case-value-type-from-class-…
Browse files Browse the repository at this point in the history
…constant

Fix inferring enum case value from a class constant
  • Loading branch information
orklah committed Oct 16, 2023
2 parents c9c3067 + d0825b5 commit b432d81
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -338,14 +338,15 @@ public static function infer(
}

if ($existing_class_constants === null
&& $file_source instanceof StatementsAnalyzer
|| $existing_class_constants === []
&& $file_source !== null
) {
try {
$foreign_class_constant = $codebase->classlikes->getClassConstantType(
$const_fq_class_name,
$stmt->name->name,
ReflectionProperty::IS_PRIVATE,
$file_source,
$file_source instanceof StatementsAnalyzer ? $file_source : null,
);

if ($foreign_class_constant) {
Expand Down
50 changes: 50 additions & 0 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,26 @@ function f(Transport $e): void {
'ignored_issues' => [],
'php_version' => '8.1',
],
'backedEnumCaseValueFromClassConstant' => [
'code' => <<<'PHP'
<?php
class FooBar {
public const FOO = 'foo';
public const BAR = 2;
}
enum FooEnum: string {
case FOO = FooBar::FOO;
}
enum BarEnum: int {
case BAR = FooBar::BAR;
}
PHP,
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.1',
],
];
}

Expand Down Expand Up @@ -1030,6 +1050,36 @@ function f(string $state): void {}
'ignored_issues' => [],
'php_version' => '8.1',
],
'stringBackedEnumCaseValueFromClassConstant' => [
'code' => '<?php
class Foo {
const FOO = 1;
}
enum Bar: string
{
case Foo = Foo::FOO;
}
',
'error_message' => 'InvalidEnumCaseValue',
'ignored_issues' => [],
'php_version' => '8.1',
],
'intBackedEnumCaseValueFromClassConstant' => [
'code' => '<?php
class Foo {
const FOO = "foo";
}
enum Bar: int
{
case Foo = Foo::FOO;
}
',
'error_message' => 'InvalidEnumCaseValue',
'ignored_issues' => [],
'php_version' => '8.1',
],
];
}
}

0 comments on commit b432d81

Please sign in to comment.