From d4a32b4e1bbc19a87a71da33745ca4f31d41b0d3 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Fri, 14 Feb 2020 11:17:01 +0100 Subject: [PATCH 1/3] improve create command --- config/set/dead-code/dead-code.yaml | 2 ++ .../src/Configuration/ConfigurationFactory.php | 6 ++++-- packages/rector-generator/src/TemplateVariablesFactory.php | 3 ++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/config/set/dead-code/dead-code.yaml b/config/set/dead-code/dead-code.yaml index 3e7ab49ddc4b..20006969cf70 100644 --- a/config/set/dead-code/dead-code.yaml +++ b/config/set/dead-code/dead-code.yaml @@ -31,3 +31,5 @@ services: Rector\DeadCode\Rector\If_\SimplifyIfElseWithSameContentRector: null Rector\DeadCode\Rector\Ternary\TernaryToBooleanOrFalseToBooleanAndRector: null Rector\PHPUnit\Rector\ClassMethod\RemoveEmptyTestMethodRector: null + Rector\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector: null + Rector\dead-code\Rector\TryCatch\RemoveDeadTryCatchRector: null diff --git a/packages/rector-generator/src/Configuration/ConfigurationFactory.php b/packages/rector-generator/src/Configuration/ConfigurationFactory.php index 5ea2528c6fac..96c750cf48f4 100644 --- a/packages/rector-generator/src/Configuration/ConfigurationFactory.php +++ b/packages/rector-generator/src/Configuration/ConfigurationFactory.php @@ -57,8 +57,10 @@ public function createFromRectorRecipe(array $rectorRecipe): Configuration $rectorRecipe['description'], $this->normalizeCode($rectorRecipe['code_before']), $this->normalizeCode($rectorRecipe['code_after']), - $this->normalizeCode($rectorRecipe['extra_file_content'] ?? ''), - $rectorRecipe['extra_file_name'], + isset($rectorRecipe['extra_file_content']) ? $this->normalizeCode( + $rectorRecipe['extra_file_content'] + ) : null, + $rectorRecipe['extra_file_name'] ?? null, array_filter((array) $rectorRecipe['source']), $this->configResolver->resolveSetConfig($rectorRecipe['set']), $this->detectPhpSnippet($rectorRecipe['code_before']) diff --git a/packages/rector-generator/src/TemplateVariablesFactory.php b/packages/rector-generator/src/TemplateVariablesFactory.php index 5e47ab2ab1d7..8b9765d51702 100644 --- a/packages/rector-generator/src/TemplateVariablesFactory.php +++ b/packages/rector-generator/src/TemplateVariablesFactory.php @@ -10,6 +10,7 @@ use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Name\FullyQualified; use Rector\Core\PhpParser\Printer\BetterStandardPrinter; +use Rector\Core\Util\RectorStrings; use Rector\RectorGenerator\ValueObject\Configuration; final class TemplateVariablesFactory @@ -31,7 +32,7 @@ public function createFromConfiguration(Configuration $configuration): array { $data = [ '_Package_' => $configuration->getPackage(), - '_package_' => strtolower($configuration->getPackage()), + '_package_' => RectorStrings::camelCaseToDashes($configuration->getPackage()), '_Category_' => $configuration->getCategory(), '_Description_' => $configuration->getDescription(), '_Name_' => $configuration->getName(), From af90cba019a48cf88eaee3fd558cdd7e64636e06 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Fri, 14 Feb 2020 11:25:21 +0100 Subject: [PATCH 2/3] [DeadCode] Add RemoveDeadTryCatchRector --- config/set/dead-code/dead-code.yaml | 2 +- docs/AllRectorsOverview.md | 26 +++++- .../TryCatch/RemoveDeadTryCatchRector.php | 93 +++++++++++++++++++ .../Fixture/fixture.php.inc | 33 +++++++ .../RemoveDeadTryCatchRectorTest.php | 30 ++++++ 5 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 rules/dead-code/src/Rector/TryCatch/RemoveDeadTryCatchRector.php create mode 100644 rules/dead-code/tests/Rector/TryCatch/RemoveDeadTryCatchRector/Fixture/fixture.php.inc create mode 100644 rules/dead-code/tests/Rector/TryCatch/RemoveDeadTryCatchRector/RemoveDeadTryCatchRectorTest.php diff --git a/config/set/dead-code/dead-code.yaml b/config/set/dead-code/dead-code.yaml index 20006969cf70..1dfb5b9fbab6 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\dead-code\Rector\TryCatch\RemoveDeadTryCatchRector: null + diff --git a/docs/AllRectorsOverview.md b/docs/AllRectorsOverview.md index 51ad9336a877..1d1b8459562b 100644 --- a/docs/AllRectorsOverview.md +++ b/docs/AllRectorsOverview.md @@ -1,4 +1,4 @@ -# All 449 Rectors Overview +# All 450 Rectors Overview - [Projects](#projects) - [General](#general) @@ -2464,6 +2464,30 @@ Removes dead code statements
+### `RemoveDeadTryCatchRector` + +- class: `Rector\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector` + +Remove dead try/catch + +```diff + class SomeClass + { + public function run() + { +- try { +- // some code +- } +- catch (Throwable $throwable) { +- throw $throwable; +- } ++ // some code + } + } +``` + +
+ ### `RemoveDeadZeroAndOneOperationRector` - class: `Rector\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector` diff --git a/rules/dead-code/src/Rector/TryCatch/RemoveDeadTryCatchRector.php b/rules/dead-code/src/Rector/TryCatch/RemoveDeadTryCatchRector.php new file mode 100644 index 000000000000..a7de24bf3d5e --- /dev/null +++ b/rules/dead-code/src/Rector/TryCatch/RemoveDeadTryCatchRector.php @@ -0,0 +1,93 @@ +catches) !== 1) { + return null; + } + + /** @var Catch_ $onlyCatch */ + $onlyCatch = $node->catches[0]; + if (count($onlyCatch->stmts) !== 1) { + return null; + } + + $onlyCatchStmt = $onlyCatch->stmts[0]; + if (! $onlyCatchStmt instanceof Throw_) { + return null; + } + + if (! $this->areNamesEqual($onlyCatch->var, $onlyCatchStmt->expr)) { + return null; + } + + foreach ($node->stmts as $tryStmt) { + $this->addNodeAfterNode($tryStmt, $node); + } + + $this->removeNode($node); + + return null; + } +} diff --git a/rules/dead-code/tests/Rector/TryCatch/RemoveDeadTryCatchRector/Fixture/fixture.php.inc b/rules/dead-code/tests/Rector/TryCatch/RemoveDeadTryCatchRector/Fixture/fixture.php.inc new file mode 100644 index 000000000000..f6f6634530af --- /dev/null +++ b/rules/dead-code/tests/Rector/TryCatch/RemoveDeadTryCatchRector/Fixture/fixture.php.inc @@ -0,0 +1,33 @@ + +----- + diff --git a/rules/dead-code/tests/Rector/TryCatch/RemoveDeadTryCatchRector/RemoveDeadTryCatchRectorTest.php b/rules/dead-code/tests/Rector/TryCatch/RemoveDeadTryCatchRector/RemoveDeadTryCatchRectorTest.php new file mode 100644 index 000000000000..96098f585eda --- /dev/null +++ b/rules/dead-code/tests/Rector/TryCatch/RemoveDeadTryCatchRector/RemoveDeadTryCatchRectorTest.php @@ -0,0 +1,30 @@ +doTestFile($file); + } + + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + protected function getRectorClass(): string + { + return RemoveDeadTryCatchRector::class; + } +} From 1881f534de54e61668304602392cc4ae09169bd0 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Fri, 14 Feb 2020 15:41:54 +0100 Subject: [PATCH 3/3] fixup! [DeadCode] Add RemoveDeadTryCatchRector --- .../Fixture/multi_lines.php.inc | 40 +++++++++++++++++++ .../Fixture/skip_catch_else.php.inc | 19 +++++++++ 2 files changed, 59 insertions(+) create mode 100644 rules/dead-code/tests/Rector/TryCatch/RemoveDeadTryCatchRector/Fixture/multi_lines.php.inc create mode 100644 rules/dead-code/tests/Rector/TryCatch/RemoveDeadTryCatchRector/Fixture/skip_catch_else.php.inc diff --git a/rules/dead-code/tests/Rector/TryCatch/RemoveDeadTryCatchRector/Fixture/multi_lines.php.inc b/rules/dead-code/tests/Rector/TryCatch/RemoveDeadTryCatchRector/Fixture/multi_lines.php.inc new file mode 100644 index 000000000000..1e7015d065c4 --- /dev/null +++ b/rules/dead-code/tests/Rector/TryCatch/RemoveDeadTryCatchRector/Fixture/multi_lines.php.inc @@ -0,0 +1,40 @@ + 1) { + return 155; + } + } + catch (Throwable $throwable) { + throw $throwable; + } + } +} + +?> +----- + 1) { + return 155; + } + } +} + +?> diff --git a/rules/dead-code/tests/Rector/TryCatch/RemoveDeadTryCatchRector/Fixture/skip_catch_else.php.inc b/rules/dead-code/tests/Rector/TryCatch/RemoveDeadTryCatchRector/Fixture/skip_catch_else.php.inc new file mode 100644 index 000000000000..a14fee408a31 --- /dev/null +++ b/rules/dead-code/tests/Rector/TryCatch/RemoveDeadTryCatchRector/Fixture/skip_catch_else.php.inc @@ -0,0 +1,19 @@ +