Skip to content

Commit

Permalink
Merge branch 'driftingly:main' into 147-eloquentorderbytolatestorolde…
Browse files Browse the repository at this point in the history
…strector-configuration
  • Loading branch information
johnbacon authored Dec 7, 2023
2 parents 67c53ce + cf00851 commit 711110f
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 4 deletions.
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "Rector upgrades rules for Laravel Framework",
"require": {
"php": ">=8.1",
"rector/rector": "^0.18.0"
"rector/rector": "^0.18.5"
},
"require-dev": {
"phpunit/phpunit": "^10.0",
Expand All @@ -18,8 +18,7 @@
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-webmozart-assert": "^1.1",
"phpstan/phpstan-strict-rules": "^1.2",
"symplify/vendor-patches": "^11.0",
"rector/rector-debugging": "dev-main"
"symplify/vendor-patches": "^11.0"
},
"autoload": {
"psr-4": {
Expand Down
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 @@ -1050,6 +1050,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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -45,6 +46,7 @@ public function __construct(
private readonly DocBlockUpdater $docBlockUpdater,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly StaticTypeMapper $staticTypeMapper,
) {
}

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);
};

0 comments on commit 711110f

Please sign in to comment.