From d4fb908810cfe0e85916c171efee860489c62ed4 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 21 Jul 2020 08:13:31 +0200 Subject: [PATCH] [DeadCode] Skip @api in unused public constastants --- .../RemoveUnusedClassConstantRector.php | 17 ++++++++++++++++- .../Fixture/skip_api.php.inc | 11 +++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/Fixture/skip_api.php.inc diff --git a/rules/dead-code/src/Rector/ClassConst/RemoveUnusedClassConstantRector.php b/rules/dead-code/src/Rector/ClassConst/RemoveUnusedClassConstantRector.php index d60c3e35675..ce03b5c9592 100644 --- a/rules/dead-code/src/Rector/ClassConst/RemoveUnusedClassConstantRector.php +++ b/rules/dead-code/src/Rector/ClassConst/RemoveUnusedClassConstantRector.php @@ -6,6 +6,7 @@ namespace Rector\DeadCode\Rector\ClassConst; use PhpParser\Node; use PhpParser\Node\Stmt\ClassConst; +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; use Rector\Caching\Contract\Rector\ZeroCacheRectorInterface; use Rector\Core\Rector\AbstractRector; use Rector\Core\RectorDefinition\CodeSample; @@ -103,11 +104,25 @@ PHP return null; } + /** + * @param ClassConst $node + */ private function shouldSkip(Node $node): bool { if ($this->isOpenSourceProjectType()) { return true; } - return count($node->consts) !== 1; + + if (count($node->consts) !== 1) { + return true; + } + + /** @var PhpDocInfo|null $phpDocInfo */ + $phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO); + if ($phpDocInfo === null) { + return false; + } + + return $phpDocInfo->hasByName('api'); } } diff --git a/rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/Fixture/skip_api.php.inc b/rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/Fixture/skip_api.php.inc new file mode 100644 index 00000000000..e5c3a169659 --- /dev/null +++ b/rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/Fixture/skip_api.php.inc @@ -0,0 +1,11 @@ +