diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/SimpleTypeInferer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/SimpleTypeInferer.php index ab144621edb..6d94391ffd2 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/SimpleTypeInferer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/SimpleTypeInferer.php @@ -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) { 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', + ], ]; } }