From fa63dd7309025c9d54e380865fc2b253be9d449b Mon Sep 17 00:00:00 2001 From: Matt Glaman Date: Thu, 30 Jul 2026 09:45:51 -0500 Subject: [PATCH] Include constant name when deprecation has a description The deprecation description was passed to sprintf() as the format string, so the %s placeholder for the constant name only existed in the fallback message. Any constant documented with @deprecated text reported just that text, with no indication of which constant was used. Format the message like the other deprecation checks do: name first, description on the following line. Co-Authored-By: Claude Opus 5 (1M context) --- .../FetchingDeprecatedConstRule.php | 17 ++++++++++++++--- .../FetchingDeprecatedConstRuleTest.php | 18 ++++++++++++++++++ ...cated-const-with-description-definition.php | 8 ++++++++ ...ching-deprecated-const-with-description.php | 6 ++++++ 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 tests/Rules/Deprecations/data/fetching-deprecated-const-with-description-definition.php create mode 100644 tests/Rules/Deprecations/data/fetching-deprecated-const-with-description.php diff --git a/src/Rules/Deprecations/FetchingDeprecatedConstRule.php b/src/Rules/Deprecations/FetchingDeprecatedConstRule.php index 0e5f9d6..2646081 100644 --- a/src/Rules/Deprecations/FetchingDeprecatedConstRule.php +++ b/src/Rules/Deprecations/FetchingDeprecatedConstRule.php @@ -43,16 +43,27 @@ public function processNode(Node $node, Scope $scope): array $constantReflection = $this->reflectionProvider->getConstant($node->name, $scope); - if ($constantReflection->isDeprecated()->yes()) { + if (!$constantReflection->isDeprecated()->yes()) { + return []; + } + + $description = $constantReflection->getDeprecatedDescription(); + if ($description === null) { return [ RuleErrorBuilder::message(sprintf( - $constantReflection->getDeprecatedDescription() ?? 'Use of constant %s is deprecated.', + 'Use of constant %s is deprecated.', $constantReflection->getName(), ))->identifier('constant.deprecated')->build(), ]; } - return []; + return [ + RuleErrorBuilder::message(sprintf( + "Use of constant %s is deprecated:\n%s", + $constantReflection->getName(), + $description, + ))->identifier('constant.deprecated')->build(), + ]; } } diff --git a/tests/Rules/Deprecations/FetchingDeprecatedConstRuleTest.php b/tests/Rules/Deprecations/FetchingDeprecatedConstRuleTest.php index 7f2d451..da8a4a2 100644 --- a/tests/Rules/Deprecations/FetchingDeprecatedConstRuleTest.php +++ b/tests/Rules/Deprecations/FetchingDeprecatedConstRuleTest.php @@ -60,6 +60,24 @@ public function testFetchingDeprecatedConst(): void ); } + public function testFetchingDeprecatedConstWithDescription(): void + { + require_once __DIR__ . '/data/fetching-deprecated-const-with-description-definition.php'; + $this->analyse( + [__DIR__ . '/data/fetching-deprecated-const-with-description.php'], + [ + [ + "Use of constant FetchingDeprecatedConstWithDescription\\DEPRECATED_WITH_DESCRIPTION is deprecated:\nin 1.2.0 and is removed from 2.0.0. Use SomeClass::NEW_ONE instead.", + 5, + ], + [ + "Use of constant FetchingDeprecatedConstWithDescription\\DEPRECATED_WITH_DESCRIPTION is deprecated:\nin 1.2.0 and is removed from 2.0.0. Use SomeClass::NEW_ONE instead.", + 6, + ], + ], + ); + } + public function testEstrictWithVersionGuard(): void { $errors = []; diff --git a/tests/Rules/Deprecations/data/fetching-deprecated-const-with-description-definition.php b/tests/Rules/Deprecations/data/fetching-deprecated-const-with-description-definition.php new file mode 100644 index 0000000..d8fab50 --- /dev/null +++ b/tests/Rules/Deprecations/data/fetching-deprecated-const-with-description-definition.php @@ -0,0 +1,8 @@ +