Skip to content

Commit

Permalink
Drop special handling of constant references on enums
Browse files Browse the repository at this point in the history
Internally enum cases are constants, and they should be resolved as
such.

Fixes #6994
  • Loading branch information
weirdan committed Nov 26, 2021
1 parent 2fbad1b commit f215ed2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,53 +210,43 @@ public static function analyze(

$const_class_storage = $codebase->classlike_storage_provider->get($fq_class_name);

if ($const_class_storage->is_enum) {
if (isset($const_class_storage->enum_cases[$stmt->name->name])) {
$class_constant_type = new Type\Union([
new Type\Atomic\TEnumCase($fq_class_name, $stmt->name->name)
]);
} else {
$class_constant_type = null;
}
if ($fq_class_name === $context->self
|| (
$statements_analyzer->getSource()->getSource() instanceof TraitAnalyzer &&
$fq_class_name === $statements_analyzer->getSource()->getFQCLN()
)
) {
$class_visibility = \ReflectionProperty::IS_PRIVATE;
} elseif ($context->self &&
($codebase->classlikes->classExtends($context->self, $fq_class_name)
|| $codebase->classlikes->classExtends($fq_class_name, $context->self))
) {
$class_visibility = \ReflectionProperty::IS_PROTECTED;
} else {
if ($fq_class_name === $context->self
|| (
$statements_analyzer->getSource()->getSource() instanceof TraitAnalyzer &&
$fq_class_name === $statements_analyzer->getSource()->getFQCLN()
)
) {
$class_visibility = \ReflectionProperty::IS_PRIVATE;
} elseif ($context->self &&
($codebase->classlikes->classExtends($context->self, $fq_class_name)
|| $codebase->classlikes->classExtends($fq_class_name, $context->self))
) {
$class_visibility = \ReflectionProperty::IS_PROTECTED;
} else {
$class_visibility = \ReflectionProperty::IS_PUBLIC;
}

try {
$class_constant_type = $codebase->classlikes->getClassConstantType(
$fq_class_name,
$stmt->name->name,
$class_visibility,
$statements_analyzer
);
} catch (\InvalidArgumentException $_) {
return true;
} catch (\Psalm\Exception\CircularReferenceException $e) {
if (IssueBuffer::accepts(
new CircularReference(
'Constant ' . $const_id . ' contains a circular reference',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
$class_visibility = \ReflectionProperty::IS_PUBLIC;
}

return true;
try {
$class_constant_type = $codebase->classlikes->getClassConstantType(
$fq_class_name,
$stmt->name->name,
$class_visibility,
$statements_analyzer
);
} catch (\InvalidArgumentException $_) {
return true;
} catch (\Psalm\Exception\CircularReferenceException $e) {
if (IssueBuffer::accepts(
new CircularReference(
'Constant ' . $const_id . ' contains a circular reference',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}

return true;
}

if (!$class_constant_type) {
Expand Down
20 changes: 20 additions & 0 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,26 @@ public static function foo(self $i) : void {}
[],
'8.1',
],
'wildcardConstantsOnEnum' => [
'<?php
enum A {
const C_1 = 1;
const C_2 = 2;
const C_3 = 3;
/**
* @param self::C_* $i
*/
public static function foo(int $i) : void {}
}
A::foo(A::C_1);
A::foo(A::C_2);
A::foo(A::C_3);',
'assertions' => [],
[],
'8.1',
],
'EnumCaseInAttribute' => [
'<?php
class CreateController {
Expand Down

0 comments on commit f215ed2

Please sign in to comment.