diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/SimpleTypeInferer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/SimpleTypeInferer.php index ab144621edb..cfd69cb3784 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/SimpleTypeInferer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/SimpleTypeInferer.php @@ -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) { diff --git a/tests/EnumTest.php b/tests/EnumTest.php index 77df3b8d659..12322c66698 100644 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -610,6 +610,26 @@ function f(Transport $e): void { 'ignored_issues' => [], 'php_version' => '8.1', ], + 'backedEnumCaseValueFromClassConstant' => [ + 'code' => <<<'PHP' + [], + 'ignored_issues' => [], + 'php_version' => '8.1', + ], ]; } @@ -1030,6 +1050,36 @@ function f(string $state): void {} 'ignored_issues' => [], 'php_version' => '8.1', ], + 'stringBackedEnumCaseValueFromClassConstant' => [ + 'code' => ' 'InvalidEnumCaseValue', + 'ignored_issues' => [], + 'php_version' => '8.1', + ], + 'intBackedEnumCaseValueFromClassConstant' => [ + 'code' => ' 'InvalidEnumCaseValue', + 'ignored_issues' => [], + 'php_version' => '8.1', + ], ]; } }