From 32b19b437b5dbd4b506bc3e54f7dc1d7dc89df72 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 23 Jul 2019 11:07:32 +0200 Subject: [PATCH] [CodeQuality] Add ThrowWithPreviousExceptionRector --- config/set/code-quality/code-quality.yaml | 1 + .../ThrowWithPreviousExceptionRector.php | 102 ++++++++++++++++++ .../Fixture/fixture.php.inc | 35 ++++++ .../ThrowWithPreviousExceptionRectorTest.php | 19 ++++ 4 files changed, 157 insertions(+) create mode 100644 packages/CodeQuality/src/Rector/Catch_/ThrowWithPreviousExceptionRector.php create mode 100644 packages/CodeQuality/tests/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/fixture.php.inc create mode 100644 packages/CodeQuality/tests/Rector/Catch_/ThrowWithPreviousExceptionRector/ThrowWithPreviousExceptionRectorTest.php diff --git a/config/set/code-quality/code-quality.yaml b/config/set/code-quality/code-quality.yaml index 66b98650516d..b72714aad6d9 100644 --- a/config/set/code-quality/code-quality.yaml +++ b/config/set/code-quality/code-quality.yaml @@ -39,3 +39,4 @@ services: Rector\CodeQuality\Rector\FuncCall\IsAWithStringWithThirdArgumentRector: ~ Rector\CodeQuality\Rector\FuncCall\StrlenZeroToIdenticalEmptyStringRector: ~ Rector\CodeQuality\Rector\If_\RemoveAlwaysTrueConditionSetInConstructorRector: ~ + Rector\CodeQuality\Rector\Catch_\ThrowWithPreviousExceptionRector: ~ diff --git a/packages/CodeQuality/src/Rector/Catch_/ThrowWithPreviousExceptionRector.php b/packages/CodeQuality/src/Rector/Catch_/ThrowWithPreviousExceptionRector.php new file mode 100644 index 000000000000..83033111f06e --- /dev/null +++ b/packages/CodeQuality/src/Rector/Catch_/ThrowWithPreviousExceptionRector.php @@ -0,0 +1,102 @@ +getCode(), $throwable); + } + } +} +CODE_SAMPLE + ), + ] + ); + } + + /** + * @return string[] + */ + public function getNodeTypes(): array + { + return [Catch_::class]; + } + + /** + * @param Catch_ $node + */ + public function refactor(Node $node): ?Node + { + $catchedThrowableVariable = $node->var; + + $this->traverseNodesWithCallable($node->stmts, function (Node $node) use ($catchedThrowableVariable): ?int { + if (! $node instanceof Node\Stmt\Throw_) { + return null; + } + + if (! $node->expr instanceof Node\Expr\New_) { + return null; + } + + if (! $node->expr->class instanceof Node\Name) { + return null; + } + + // exception is bundled + if (isset($node->expr->args[2])) { + return null; + } + + if (! isset($node->expr->args[1])) { + // get previous code + $node->expr->args[1] = new Node\Arg(new Node\Expr\MethodCall($catchedThrowableVariable, 'getCode')); + } + + $node->expr->args[2] = new Node\Arg($catchedThrowableVariable); + + // nothing more to add + return NodeTraverser::STOP_TRAVERSAL; + }); + + return $node; + } +} diff --git a/packages/CodeQuality/tests/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/fixture.php.inc b/packages/CodeQuality/tests/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/fixture.php.inc new file mode 100644 index 000000000000..a0ac50486ba1 --- /dev/null +++ b/packages/CodeQuality/tests/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/fixture.php.inc @@ -0,0 +1,35 @@ + +----- +getCode(), $throwable); + } + } +} + +?> diff --git a/packages/CodeQuality/tests/Rector/Catch_/ThrowWithPreviousExceptionRector/ThrowWithPreviousExceptionRectorTest.php b/packages/CodeQuality/tests/Rector/Catch_/ThrowWithPreviousExceptionRector/ThrowWithPreviousExceptionRectorTest.php new file mode 100644 index 000000000000..c70b806ddb55 --- /dev/null +++ b/packages/CodeQuality/tests/Rector/Catch_/ThrowWithPreviousExceptionRector/ThrowWithPreviousExceptionRectorTest.php @@ -0,0 +1,19 @@ +doTestFiles([__DIR__ . '/Fixture/fixture.php.inc']); + } + + protected function getRectorClass(): string + { + return ThrowWithPreviousExceptionRector::class; + } +}