From 92369441f1e2818f19212a1a6ae3172be1d446d1 Mon Sep 17 00:00:00 2001 From: Simon Schaufelberger Date: Wed, 16 Oct 2024 12:08:30 +0200 Subject: [PATCH] [BUGFIX] Include throw statements to AddErrorCodeToExceptionRector --- .../General/AddErrorCodeToExceptionRector.php | 12 ++++--- .../AddErrorCodeToExceptionRectorPhp8Test.php | 36 +++++++++++++++++++ .../FixturePhp8/fixture.php.inc | 29 +++++++++++++++ 3 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 tests/Rector/CodeQuality/General/AddErrorCodeToExceptionRector/AddErrorCodeToExceptionRectorPhp8Test.php create mode 100644 tests/Rector/CodeQuality/General/AddErrorCodeToExceptionRector/FixturePhp8/fixture.php.inc diff --git a/rules/CodeQuality/General/AddErrorCodeToExceptionRector.php b/rules/CodeQuality/General/AddErrorCodeToExceptionRector.php index 314b79cf5..d8ea32889 100644 --- a/rules/CodeQuality/General/AddErrorCodeToExceptionRector.php +++ b/rules/CodeQuality/General/AddErrorCodeToExceptionRector.php @@ -6,7 +6,8 @@ use PhpParser\Node; use PhpParser\Node\Expr\New_; -use PhpParser\Node\Stmt\Throw_; +use PhpParser\Node\Expr\Throw_ as ThrowExpression; +use PhpParser\Node\Stmt\Throw_ as ThrowStatement; use Rector\Rector\AbstractRector; use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment; use Ssch\TYPO3Rector\Contract\NoChangelogRequiredInterface; @@ -36,11 +37,11 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [Throw_::class]; + return [ThrowStatement::class, ThrowExpression::class]; } /** - * @param Throw_ $node + * @param ThrowStatement|ThrowExpression $node */ public function refactor(Node $node): ?Node { @@ -62,7 +63,10 @@ public function refactor(Node $node): ?Node return $node; } - private function shouldSkip(Throw_ $node): bool + /** + * @param ThrowStatement|ThrowExpression $node + */ + private function shouldSkip($node): bool { if (! $node->expr instanceof New_) { return true; diff --git a/tests/Rector/CodeQuality/General/AddErrorCodeToExceptionRector/AddErrorCodeToExceptionRectorPhp8Test.php b/tests/Rector/CodeQuality/General/AddErrorCodeToExceptionRector/AddErrorCodeToExceptionRectorPhp8Test.php new file mode 100644 index 000000000..e06f05289 --- /dev/null +++ b/tests/Rector/CodeQuality/General/AddErrorCodeToExceptionRector/AddErrorCodeToExceptionRectorPhp8Test.php @@ -0,0 +1,36 @@ +markTestSkipped('Do not execute'); + } + + $this->doTestFile($filePath); + } + + /** + * @return \Iterator> + */ + public static function provideData(): \Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/FixturePhp8'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Rector/CodeQuality/General/AddErrorCodeToExceptionRector/FixturePhp8/fixture.php.inc b/tests/Rector/CodeQuality/General/AddErrorCodeToExceptionRector/FixturePhp8/fixture.php.inc new file mode 100644 index 000000000..43836e0d0 --- /dev/null +++ b/tests/Rector/CodeQuality/General/AddErrorCodeToExceptionRector/FixturePhp8/fixture.php.inc @@ -0,0 +1,29 @@ + throw new \InvalidArgumentException('Invalid'), + }; + } +} +?> +----- + throw new \InvalidArgumentException('Invalid', 1729021897), + }; + } +} +?>