Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ThrowIfRector rule #154

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 45 Rules Overview
# 46 Rules Overview

## AddArgumentDefaultValueRector

Expand Down Expand Up @@ -1019,6 +1019,25 @@ Use `Str::startsWith()` or `Str::endsWith()` instead of `substr()` === `$str`

<br>

## ThrowIfRector

Change if throw to throw_if

- class: [`RectorLaravel\Rector\If_\ThrowIfRector`](../src/Rector/If_/ThrowIfRector.php)

```diff
-if ($condition) {
- throw new Exception();
-}
-if (!$condition) {
- throw new Exception();
-}
+throw_if($condition, new Exception());
+throw_unless($condition, new Exception());
```

<br>

## UnifyModelDatesWithCastsRector

Unify Model `$dates` property with `$casts`
Expand Down
76 changes: 76 additions & 0 deletions src/Rector/If_/ThrowIfRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace RectorLaravel\Rector\If_;

use PhpParser\Node;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Throw_;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \RectorLaravel\Tests\Rector\If_\ThrowIfRector\ThrowIfRectorTest
*/
class ThrowIfRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change if throw to throw_if', [
new CodeSample(
<<<'CODE_SAMPLE'
if ($condition) {
throw new Exception();
}
if (!$condition) {
throw new Exception();
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
throw_if($condition, new Exception());
throw_unless($condition, new Exception());
CODE_SAMPLE
),
]);
}

public function getNodeTypes(): array
{
return [If_::class];
}

public function refactor(Node $node): ?Node
{
if (!$node instanceof If_) {
return null;
}

$ifStmts = $node->stmts;

// Check if there's a single throw statement inside the if
if (count($ifStmts) === 1 && $ifStmts[0] instanceof Throw_) {
$condition = $node->cond;
$throwExpr = $ifStmts[0]->expr;

// Check if the condition is a negation
if ($condition instanceof BooleanNot) {
// Create a new throw_unless function call
return new Node\Stmt\Expression(new FuncCall(new Node\Name('throw_unless'), [
new Node\Arg($condition->expr),
new Node\Arg($throwExpr),
]));
} else {
// Create a new throw_if function call
return new Node\Stmt\Expression(new FuncCall(new Node\Name('throw_if'), [
new Node\Arg($condition),
new Node\Arg($throwExpr),
]));
}
}

return null;
}
}
33 changes: 33 additions & 0 deletions tests/Rector/If_/ThrowIfRector/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace RectorLaravel\Tests\Rector\If_\ThrowIfRector\Fixture;

class Fixture
{
public function handle($condition)
{
if ($condition) {
throw new Exception();
}
if (!$condition) {
throw new Exception();
}
}
}

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\If_\ThrowIfRector\Fixture;

class Fixture
{
public function handle($condition)
{
throw_if($condition, new Exception());
throw_unless($condition, new Exception());
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace RectorLaravel\Tests\Rector\If_\ThrowIfRector\Fixture;

class SkipIfMoreStatements
{
public function run($condition)
{
if ($condition) {
echo 'hello';
throw new Exception();
}
if ($condition) {
throw new Exception();
echo 'world';
}
if ($condition) {
}
}
}

?>
28 changes: 28 additions & 0 deletions tests/Rector/If_/ThrowIfRector/ThrowIfRectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace RectorLaravel\Tests\Rector\If_\ThrowIfRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ThrowIfRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
11 changes: 11 additions & 0 deletions tests/Rector/If_/ThrowIfRector/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');

$rectorConfig->rule(\RectorLaravel\Rector\If_\ThrowIfRector::class);
};