1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

allow marking enum cases as @deprecated

This commit is contained in:
Philip Hofstetter 2021-12-20 14:20:11 +01:00
parent cc9111ff48
commit d45cb64827
No known key found for this signature in database
GPG Key ID: 68CE37E7CB4964AF
4 changed files with 44 additions and 1 deletions

View File

@ -218,6 +218,18 @@ class ClassConstFetchAnalyzer
} }
$const_class_storage = $codebase->classlike_storage_provider->get($fq_class_name); $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 if ($fq_class_name === $context->self
|| ( || (

View File

@ -1345,10 +1345,20 @@ class ClassLikeNodeScanner
$case_location = new CodeLocation($this->file_scanner, $stmt); $case_location = new CodeLocation($this->file_scanner, $stmt);
if (!isset($storage->enum_cases[$stmt->name->name])) { if (!isset($storage->enum_cases[$stmt->name->name])) {
$storage->enum_cases[$stmt->name->name] = new EnumCaseStorage( $case = new EnumCaseStorage(
$enum_value, $enum_value,
$case_location $case_location
); );
$comment = $stmt->getDocComment();
if ($comment) {
$comments = DocComment::parsePreservingLength($comment);
if (isset($comments->tags['deprecated'])) {
$case->deprecated = true;
}
}
$storage->enum_cases[$stmt->name->name] = $case;
} else { } else {
if (IssueBuffer::accepts( if (IssueBuffer::accepts(
new DuplicateEnumCase( new DuplicateEnumCase(

View File

@ -14,6 +14,11 @@ class EnumCaseStorage
/** @var CodeLocation */ /** @var CodeLocation */
public $stmt_location; public $stmt_location;
/**
* @var bool
*/
public $deprecated = false;
/** /**
* @param int|string|null $value * @param int|string|null $value
*/ */

View File

@ -251,6 +251,22 @@ class DeprecatedAnnotationTest extends TestCase
', ',
'error_message' => 'DeprecatedProperty', 'error_message' => 'DeprecatedProperty',
], ],
'deprecatedEnumCaseFetch' => [
'<?php
enum Foo {
case A;
/** @deprecated */
case B;
}
Foo::B;
',
'error_message' => 'DeprecatedConstant',
[],
false,
'8.1',
]
]; ];
} }
} }