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 @@ +