diff --git a/src/Psalm/Internal/Codebase/ClassLikes.php b/src/Psalm/Internal/Codebase/ClassLikes.php index dc4dce524..9f60c244a 100644 --- a/src/Psalm/Internal/Codebase/ClassLikes.php +++ b/src/Psalm/Internal/Codebase/ClassLikes.php @@ -1627,35 +1627,36 @@ class ClassLikes $storage = $this->classlike_storage_provider->get($class_name); - if (!isset($storage->constants[$constant_name])) { - return null; + if (isset($storage->constants[$constant_name])) { + $constant_storage = $storage->constants[$constant_name]; + + if ($visibility === ReflectionProperty::IS_PUBLIC + && $constant_storage->visibility !== ClassLikeAnalyzer::VISIBILITY_PUBLIC + ) { + return null; + } + + if ($visibility === ReflectionProperty::IS_PROTECTED + && $constant_storage->visibility !== ClassLikeAnalyzer::VISIBILITY_PUBLIC + && $constant_storage->visibility !== ClassLikeAnalyzer::VISIBILITY_PROTECTED + ) { + return null; + } + + if ($constant_storage->unresolved_node) { + $constant_storage->type = new Type\Union([ConstantTypeResolver::resolve( + $this, + $constant_storage->unresolved_node, + $statements_analyzer, + $visited_constant_ids + )]); + } + + return $constant_storage->type; + } elseif (isset($storage->enum_cases[$constant_name])) { + return new Type\Union([new Type\Atomic\TEnumCase($storage->name, $constant_name)]); } - - $constant_storage = $storage->constants[$constant_name]; - - if ($visibility === ReflectionProperty::IS_PUBLIC - && $constant_storage->visibility !== ClassLikeAnalyzer::VISIBILITY_PUBLIC - ) { - return null; - } - - if ($visibility === ReflectionProperty::IS_PROTECTED - && $constant_storage->visibility !== ClassLikeAnalyzer::VISIBILITY_PUBLIC - && $constant_storage->visibility !== ClassLikeAnalyzer::VISIBILITY_PROTECTED - ) { - return null; - } - - if ($constant_storage->unresolved_node) { - $constant_storage->type = new Type\Union([ConstantTypeResolver::resolve( - $this, - $constant_storage->unresolved_node, - $statements_analyzer, - $visited_constant_ids - )]); - } - - return $constant_storage->type; + return null; } private function checkMethodReferences(ClassLikeStorage $classlike_storage, Methods $methods): void diff --git a/tests/ConstantTest.php b/tests/ConstantTest.php index 6daedba4b..516cfdad5 100644 --- a/tests/ConstantTest.php +++ b/tests/ConstantTest.php @@ -7,7 +7,7 @@ class ConstantTest extends TestCase use Traits\ValidCodeAnalysisTestTrait; /** - * @return iterable,error_levels?:string[]}> + * @return iterable,error_levels?:string[], php_version?: string}> */ public function providerValidCodeParse(): iterable { @@ -1180,6 +1180,22 @@ class ConstantTest extends TestCase }', ], + 'classConstantReferencingEnumCase' => [ + ' [ + '$c===' => 'enum(E::Z)' + ], + [], + '8.1' + ], ]; } diff --git a/tests/EnumTest.php b/tests/EnumTest.php index c0c150ffd..4793f7613 100644 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -154,6 +154,26 @@ class EnumTest extends TestCase [], '8.1' ], + 'SKIPPED-wildcardEnum' => [ + ' [], + [], + '8.1', + ], ]; } diff --git a/tests/Traits/ValidCodeAnalysisTestTrait.php b/tests/Traits/ValidCodeAnalysisTestTrait.php index 64be3b4a1..f5db49b50 100644 --- a/tests/Traits/ValidCodeAnalysisTestTrait.php +++ b/tests/Traits/ValidCodeAnalysisTestTrait.php @@ -15,7 +15,7 @@ use const PHP_VERSION; trait ValidCodeAnalysisTestTrait { /** - * @return iterable,error_levels?:string[]}> + * @return iterable,error_levels?:string[],php_version?:string}> */ abstract public function providerValidCodeParse(): iterable;