-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'driftingly:main' into 147-eloquentorderbytolatestorolde…
…strector-configuration
- Loading branch information
Showing
8 changed files
with
194 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
|
||
?> |
22 changes: 22 additions & 0 deletions
22
tests/Rector/If_/ThrowIfRector/Fixture/skip_if_more_statements.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |