Skip to content

Commit

Permalink
[BUGFIX] Include throw statements to AddErrorCodeToExceptionRector
Browse files Browse the repository at this point in the history
  • Loading branch information
simonschaufi committed Oct 16, 2024
1 parent 7193380 commit 9236944
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
12 changes: 8 additions & 4 deletions rules/CodeQuality/General/AddErrorCodeToExceptionRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Tests\Rector\CodeQuality\General\AddErrorCodeToExceptionRector;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\ValueObject\PhpVersionFeature;

final class AddErrorCodeToExceptionRectorPhp8Test extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
if (PHP_VERSION_ID < PhpVersionFeature::MATCH_EXPRESSION) {
$this->markTestSkipped('Do not execute');
}

$this->doTestFile($filePath);
}

/**
* @return \Iterator<array<string>>
*/
public static function provideData(): \Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/FixturePhp8');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\CodeQuality\General\AddErrorCodeToExceptionRector\FixturePhp8;

class CustomException
{
public function returnMatch()
{
return match (true) {
default => throw new \InvalidArgumentException('Invalid'),
};
}
}
?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\CodeQuality\General\AddErrorCodeToExceptionRector\FixturePhp8;

class CustomException
{
public function returnMatch()
{
return match (true) {
default => throw new \InvalidArgumentException('Invalid', 1729021897),
};
}
}
?>

0 comments on commit 9236944

Please sign in to comment.