Skip to content

Commit

Permalink
Fix for inferring enum case value from a class constant
Browse files Browse the repository at this point in the history
  • Loading branch information
tuqqu committed Oct 10, 2023
1 parent f1fc9c4 commit 6039e2b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -337,15 +337,13 @@ public static function infer(
return Type::getLiteralClassString($const_fq_class_name, true);
}

if ($existing_class_constants === null
&& $file_source instanceof StatementsAnalyzer
) {
if ($existing_class_constants === null || $existing_class_constants === []) {
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 6039e2b

Please sign in to comment.