diff --git a/composer.json b/composer.json index 3c3e67f07206..cc2c40159deb 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "jetbrains/phpstorm-stubs": "^2019.3", "nette/robot-loader": "^3.2", "nette/utils": "^3.1", - "nikic/php-parser": "^4.4", + "nikic/php-parser": "^4.5", "ondram/ci-detector": "^3.4", "phpstan/phpdoc-parser": "^0.4.7", "phpstan/phpstan": "^0.12.25", @@ -279,7 +279,5 @@ "branch-alias": { "dev-master": "0.8-dev" } - }, - "minimum-stability": "dev", - "prefer-stable": true + } } diff --git a/config/set/php/php80.yaml b/config/set/php/php80.yaml index 2da08b7774c6..6be9aff08b7b 100644 --- a/config/set/php/php80.yaml +++ b/config/set/php/php80.yaml @@ -8,3 +8,4 @@ services: Rector\Php80\Rector\FuncCall\ClassOnObjectRector: null Rector\Php80\Rector\Ternary\GetDebugTypeRector: null Rector\Php80\Rector\FuncCall\TokenGetAllToObjectRector: null + Rector\Php80\Rector\Catch_\RemoveUnusedVariableInCatchRector: null diff --git a/rules/code-quality/src/Rector/Catch_/ThrowWithPreviousExceptionRector.php b/rules/code-quality/src/Rector/Catch_/ThrowWithPreviousExceptionRector.php index 65b242f4b2ae..0989a949b33a 100644 --- a/rules/code-quality/src/Rector/Catch_/ThrowWithPreviousExceptionRector.php +++ b/rules/code-quality/src/Rector/Catch_/ThrowWithPreviousExceptionRector.php @@ -84,14 +84,17 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $catchedThrowableVariable = $node->var; + $caughtThrowableVariable = $node->var; + if ($caughtThrowableVariable === null) { + return null; + } - $this->traverseNodesWithCallable($node->stmts, function (Node $node) use ($catchedThrowableVariable): ?int { + $this->traverseNodesWithCallable($node->stmts, function (Node $node) use ($caughtThrowableVariable): ?int { if (! $node instanceof Throw_) { return null; } - return $this->refactorThrow($node, $catchedThrowableVariable); + return $this->refactorThrow($node, $caughtThrowableVariable); }); return $node; diff --git a/rules/coding-style/src/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php b/rules/coding-style/src/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php index cbef6f351497..e6de5fe758c2 100644 --- a/rules/coding-style/src/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php +++ b/rules/coding-style/src/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php @@ -70,6 +70,10 @@ public function refactor(Node $node): ?Node $type = $node->types[0]; $typeShortName = $this->getShortName($type); + if ($node->var === null) { + return null; + } + $oldVariableName = $this->getName($node->var); if (! $oldVariableName) { return null; diff --git a/rules/php80/src/Rector/Catch_/RemoveUnusedVariableInCatchRector.php b/rules/php80/src/Rector/Catch_/RemoveUnusedVariableInCatchRector.php new file mode 100644 index 000000000000..fb11fe0dc463 --- /dev/null +++ b/rules/php80/src/Rector/Catch_/RemoveUnusedVariableInCatchRector.php @@ -0,0 +1,93 @@ +var; + if ($caughtVar === null) { + return null; + } + + if ($this->isVariableUsed((array) $node->stmts, $caughtVar)) { + return null; + } + + $node->var = null; + + return $node; + } + + /** + * @param Node[] $nodes + */ + private function isVariableUsed(array $nodes, Variable $variable): bool + { + return (bool) $this->betterNodeFinder->findFirst($nodes, function (Node $node) use ($variable) { + if (! $node instanceof Variable) { + return false; + } + + return $this->areNodesEqual($node, $variable); + }); + } +} diff --git a/rules/php80/tests/Rector/Catch_/RemoveUnusedVariableInCatchRector/Fixture/fixture.php.inc b/rules/php80/tests/Rector/Catch_/RemoveUnusedVariableInCatchRector/Fixture/fixture.php.inc new file mode 100644 index 000000000000..b8979c7d863c --- /dev/null +++ b/rules/php80/tests/Rector/Catch_/RemoveUnusedVariableInCatchRector/Fixture/fixture.php.inc @@ -0,0 +1,31 @@ + +----- + diff --git a/rules/php80/tests/Rector/Catch_/RemoveUnusedVariableInCatchRector/RemoveUnusedVariableInCatchRectorTest.php b/rules/php80/tests/Rector/Catch_/RemoveUnusedVariableInCatchRector/RemoveUnusedVariableInCatchRectorTest.php new file mode 100644 index 000000000000..cd87fb206fe9 --- /dev/null +++ b/rules/php80/tests/Rector/Catch_/RemoveUnusedVariableInCatchRector/RemoveUnusedVariableInCatchRectorTest.php @@ -0,0 +1,30 @@ +doTestFile($file); + } + + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + protected function getRectorClass(): string + { + return RemoveUnusedVariableInCatchRector::class; + } +} diff --git a/rules/phpunit/src/Rector/TryCatchToExpectExceptionRector.php b/rules/phpunit/src/Rector/TryCatchToExpectExceptionRector.php index 5560139810dc..ad9fa602fb2d 100644 --- a/rules/phpunit/src/Rector/TryCatchToExpectExceptionRector.php +++ b/rules/phpunit/src/Rector/TryCatchToExpectExceptionRector.php @@ -101,6 +101,9 @@ private function processTryCatch(TryCatch $tryCatch): ?array $this->newExpressions = []; $exceptionVariable = $tryCatch->catches[0]->var; + if ($exceptionVariable === null) { + return null; + } // we look for: // - instance of $exceptionVariableName