From c2b89700a3deefcc169a7eaaca72248e387f1d6f Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Sun, 23 Feb 2020 22:04:20 +0100 Subject: [PATCH] [DeadCode] Add RemoveUnusedClassConstantRector --- config/set/dead-code/dead-code.yaml | 2 +- docs/AllRectorsOverview.md | 35 ++++-- .../RemoveUnusedClassConstantRector.php | 101 ++++++++++++++++++ .../Fixture/external_public.php.inc | 20 ++++ .../Fixture/fixture.php.inc | 27 +++++ .../Fixture/skip_child_used.php.inc | 16 +++ .../Fixture/skip_external_used.php.inc | 16 +++ .../Fixture/skip_multiple_constants.php.inc | 8 ++ .../RemoveUnusedClassConstantRectorTest.php | 30 ++++++ .../PrivatizeLocalClassConstantRector.php | 2 +- 10 files changed, 248 insertions(+), 9 deletions(-) create mode 100644 rules/dead-code/src/Rector/ClassConst/RemoveUnusedClassConstantRector.php create mode 100644 rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/Fixture/external_public.php.inc create mode 100644 rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/Fixture/fixture.php.inc create mode 100644 rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/Fixture/skip_child_used.php.inc create mode 100644 rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/Fixture/skip_external_used.php.inc create mode 100644 rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/Fixture/skip_multiple_constants.php.inc create mode 100644 rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/RemoveUnusedClassConstantRectorTest.php diff --git a/config/set/dead-code/dead-code.yaml b/config/set/dead-code/dead-code.yaml index 1dfb5b9fbab..a7f6e9a93b1 100644 --- a/config/set/dead-code/dead-code.yaml +++ b/config/set/dead-code/dead-code.yaml @@ -32,4 +32,4 @@ services: Rector\DeadCode\Rector\Ternary\TernaryToBooleanOrFalseToBooleanAndRector: null Rector\PHPUnit\Rector\ClassMethod\RemoveEmptyTestMethodRector: null Rector\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector: null - + Rector\DeadCode\Rector\ClassConst\RemoveUnusedClassConstantRector: null diff --git a/docs/AllRectorsOverview.md b/docs/AllRectorsOverview.md index 6d97fb6fe2c..5c6dbf60e42 100644 --- a/docs/AllRectorsOverview.md +++ b/docs/AllRectorsOverview.md @@ -1,4 +1,4 @@ -# All 460 Rectors Overview +# All 461 Rectors Overview - [Projects](#projects) - [General](#general) @@ -351,12 +351,13 @@ Changes method calls based on matching the first parameter value. ```yaml services: Rector\CakePHP\Rector\MethodCall\RenameMethodCallBasedOnParameterRector: - getParam: - match_parameter: paging - replace_with: getAttribute - withParam: - match_parameter: paging - replace_with: withAttribute + $methodNamesByTypes: + getParam: + match_parameter: paging + replace_with: getAttribute + withParam: + match_parameter: paging + replace_with: withAttribute ``` ↓ @@ -2936,6 +2937,26 @@ Remove unreachable statements
+### `RemoveUnusedClassConstantRector` + +- class: [`Rector\DeadCode\Rector\ClassConst\RemoveUnusedClassConstantRector`](/../master/rules/dead-code/src/Rector/ClassConst/RemoveUnusedClassConstantRector.php) +- [test fixtures](/../master/rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/Fixture) + +Remove unused class constants + +```diff + class SomeClass + { +- private const SOME_CONST = 'dead'; +- + public function run() + { + } + } +``` + +
+ ### `RemoveUnusedClassesRector` - class: [`Rector\DeadCode\Rector\Class_\RemoveUnusedClassesRector`](/../master/rules/dead-code/src/Rector/Class_/RemoveUnusedClassesRector.php) diff --git a/rules/dead-code/src/Rector/ClassConst/RemoveUnusedClassConstantRector.php b/rules/dead-code/src/Rector/ClassConst/RemoveUnusedClassConstantRector.php new file mode 100644 index 00000000000..1a312aa1023 --- /dev/null +++ b/rules/dead-code/src/Rector/ClassConst/RemoveUnusedClassConstantRector.php @@ -0,0 +1,101 @@ +classConstParsedNodesFinder = $classConstParsedNodesFinder; + } + + public function getDefinition(): RectorDefinition + { + return new RectorDefinition('Remove unused class constants', [ + new CodeSample( + <<<'PHP' +class SomeClass +{ + private const SOME_CONST = 'dead'; + + public function run() + { + } +} +PHP +, + <<<'PHP' +class SomeClass +{ + public function run() + { + } +} +PHP + + ), + ]); + } + + /** + * @return string[] + */ + public function getNodeTypes(): array + { + return [ClassConst::class]; + } + + /** + * @param ClassConst $node + */ + public function refactor(Node $node): ?Node + { + if (count($node->consts) !== 1) { + return null; + } + + /** @var string $class */ + $class = $node->getAttribute(AttributeKey::CLASS_NAME); + + // 0. constants declared in interfaces have to be public + if ($this->classLikeParsedNodesFinder->findInterface($class) !== null) { + $this->makePublic($node); + return $node; + } + + /** @var string $constant */ + $constant = $this->getName($node); + + $directUseClasses = $this->classConstParsedNodesFinder->findDirectClassConstantFetches($class, $constant); + if ($directUseClasses !== []) { + return null; + } + + $indirectUseClasses = $this->classConstParsedNodesFinder->findIndirectClassConstantFetches($class, $constant); + if ($indirectUseClasses !== []) { + return null; + } + + $this->removeNode($node); + + return null; + } +} diff --git a/rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/Fixture/external_public.php.inc b/rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/Fixture/external_public.php.inc new file mode 100644 index 00000000000..69b0b88f7bc --- /dev/null +++ b/rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/Fixture/external_public.php.inc @@ -0,0 +1,20 @@ + +----- + diff --git a/rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/Fixture/fixture.php.inc b/rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/Fixture/fixture.php.inc new file mode 100644 index 00000000000..6875c6d11be --- /dev/null +++ b/rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/Fixture/fixture.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/Fixture/skip_child_used.php.inc b/rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/Fixture/skip_child_used.php.inc new file mode 100644 index 00000000000..f3343b5653d --- /dev/null +++ b/rules/dead-code/tests/Rector/ClassConst/RemoveUnusedClassConstantRector/Fixture/skip_child_used.php.inc @@ -0,0 +1,16 @@ +doTestFile($file); + } + + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + protected function getRectorClass(): string + { + return RemoveUnusedClassConstantRector::class; + } +} diff --git a/rules/solid/src/Rector/ClassConst/PrivatizeLocalClassConstantRector.php b/rules/solid/src/Rector/ClassConst/PrivatizeLocalClassConstantRector.php index 3ae2f433ba8..de49df53895 100644 --- a/rules/solid/src/Rector/ClassConst/PrivatizeLocalClassConstantRector.php +++ b/rules/solid/src/Rector/ClassConst/PrivatizeLocalClassConstantRector.php @@ -188,7 +188,7 @@ PHP ?ConstantVisibility $constantVisibility, string $class ): void { - // 1. is actually never used (@todo use in "dead-code" set) + // 1. is actually never used if ($directUseClasses === []) { if ($indirectUseClasses !== [] && $constantVisibility !== null) { $this->makePrivateOrWeaker($classConst, $constantVisibility);