Skip to content

Commit

Permalink
allow marking enum cases as @deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
pilif committed Dec 20, 2021
1 parent ce7bd23 commit 9e24c43
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
Expand Up @@ -218,6 +218,18 @@ public static function analyze(
}

$const_class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
if ($const_class_storage->is_enum) {
$case = $const_class_storage->enum_cases[(string)$stmt->name] ?? null;
if ($case && $case->deprecated) {
IssueBuffer::maybeAdd(
new DeprecatedConstant(
"Enum Case $const_id is marked as deprecated",
new CodeLocation($statements_analyzer->getSource(), $stmt),
),
$statements_analyzer->getSuppressedIssues()
);
}
}

if ($fq_class_name === $context->self
|| (
Expand Down
16 changes: 15 additions & 1 deletion src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php
Expand Up @@ -1345,10 +1345,24 @@ private function visitEnumDeclaration(
$case_location = new CodeLocation($this->file_scanner, $stmt);

if (!isset($storage->enum_cases[$stmt->name->name])) {
$storage->enum_cases[$stmt->name->name] = new EnumCaseStorage(
$case = new EnumCaseStorage(
$enum_value,
$case_location
);

$comment = $stmt->getDocComment();
if ($comment) {
$var_comments = CommentAnalyzer::getTypeFromComment(
$comment,
$this->file_scanner,
$this->aliases,
[],
$this->type_aliases
);
$var_comment = array_pop($var_comments);
$case->deprecated = $var_comment ? $var_comment->deprecated : false;
}
$storage->enum_cases[$stmt->name->name] = $case;
} else {
if (IssueBuffer::accepts(
new DuplicateEnumCase(
Expand Down
5 changes: 5 additions & 0 deletions src/Psalm/Storage/EnumCaseStorage.php
Expand Up @@ -14,6 +14,11 @@ class EnumCaseStorage
/** @var CodeLocation */
public $stmt_location;

/**
* @var bool
*/
public $deprecated = false;

/**
* @param int|string|null $value
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/DeprecatedAnnotationTest.php
Expand Up @@ -251,6 +251,22 @@ class Bar
',
'error_message' => 'DeprecatedProperty',
],
'deprecatedEnumCaseFetch' => [
'<?php
enum Foo {
case A;
/** @deprecated */
case B;
}
Foo::B;
',
'error_message' => 'DeprecatedConstant',
[],
false,
'8.1',
]
];
}
}

0 comments on commit 9e24c43

Please sign in to comment.