Skip to content
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
1 change: 1 addition & 0 deletions config/set/solid/solid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ services:
Rector\SOLID\Rector\Class_\MakeUnusedClassesWithChildrenAbstractRector: null
Rector\SOLID\Rector\Property\ChangeReadOnlyPropertyWithDefaultValueToConstantRector: null
Rector\SOLID\Rector\ClassMethod\ChangeReadOnlyVariableWithDefaultValueToConstantRector: null
Rector\SOLID\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector: null
36 changes: 35 additions & 1 deletion docs/AllRectorsOverview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# All 456 Rectors Overview
# All 457 Rectors Overview

- [Projects](#projects)
- [General](#general)
Expand Down Expand Up @@ -8034,6 +8034,40 @@ Change if/else value to early return

<br>

### `ChangeNestedForeachIfsToEarlyContinueRector`

- class: [`Rector\SOLID\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector`](/../master/rules/solid/src/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector.php)
- [test fixtures](/../master/rules/solid/tests/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector/Fixture)

Change nested ifs to foreach with continue

```diff
class SomeClass
{
public function run()
{
$items = [];

foreach ($values as $value) {
- if ($value === 5) {
- if ($value2 === 10) {
- $items[] = 'maybe';
- }
+ if ($value !== 5) {
+ continue;
}
+ if ($value2 !== 10) {
+ continue;
+ }
+
+ $items[] = 'maybe';
}
}
}
```

<br>

### `ChangeNestedIfsToEarlyReturnRector`

- class: [`Rector\SOLID\Rector\If_\ChangeNestedIfsToEarlyReturnRector`](/../master/rules/solid/src/Rector/If_/ChangeNestedIfsToEarlyReturnRector.php)
Expand Down
38 changes: 38 additions & 0 deletions rules/solid/src/NodeTransformer/ConditionInverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Rector\SOLID\NodeTransformer;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BooleanNot;
use Rector\Core\PhpParser\Node\Manipulator\BinaryOpManipulator;

final class ConditionInverter
{
/**
* @var BinaryOpManipulator
*/
private $binaryOpManipulator;

public function __construct(BinaryOpManipulator $binaryOpManipulator)
{
$this->binaryOpManipulator = $binaryOpManipulator;
}

public function createInvertedCondition(Expr $expr): Expr
{
// inverse condition
if ($expr instanceof BinaryOp) {
$inversedCondition = $this->binaryOpManipulator->invertCondition($expr);
if ($inversedCondition === null) {
return new BooleanNot($expr);
}

return $inversedCondition;
}

return new BooleanNot($expr);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

declare(strict_types=1);

namespace Rector\SOLID\Rector\Foreach_;

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Stmt\Continue_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use Rector\Core\PhpParser\Node\Manipulator\IfManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\SOLID\NodeTransformer\ConditionInverter;

/**
* @see \Rector\SOLID\Tests\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector\ChangeNestedForeachIfsToEarlyContinueRectorTest
*/
final class ChangeNestedForeachIfsToEarlyContinueRector extends AbstractRector
{
/**
* @var IfManipulator
*/
private $ifManipulator;

/**
* @var ConditionInverter
*/
private $conditionInverter;

public function __construct(IfManipulator $ifManipulator, ConditionInverter $conditionInverter)
{
$this->ifManipulator = $ifManipulator;
$this->conditionInverter = $conditionInverter;
}

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Change nested ifs to foreach with continue', [
new CodeSample(
<<<'PHP'
class SomeClass
{
public function run()
{
$items = [];

foreach ($values as $value) {
if ($value === 5) {
if ($value2 === 10) {
$items[] = 'maybe';
}
}
}
}
}
PHP
,
<<<'PHP'
class SomeClass
{
public function run()
{
$items = [];

foreach ($values as $value) {
if ($value !== 5) {
continue;
}
if ($value2 !== 10) {
continue;
}

$items[] = 'maybe';
}
}
}
PHP

),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Foreach_::class];
}

/**
* @param Foreach_ $node
*/
public function refactor(Node $node): ?Node
{
$nestedIfsWithOnlyNonReturn = $this->ifManipulator->collectNestedIfsWithNonBreaking($node);
if ($nestedIfsWithOnlyNonReturn === []) {
return null;
}

$this->processNestedIfsWithNonBreaking($node, $nestedIfsWithOnlyNonReturn);

return $node;
}

/**
* @param If_[] $nestedIfsWithOnlyReturn
*/
private function processNestedIfsWithNonBreaking(Foreach_ $foreach, array $nestedIfsWithOnlyReturn): void
{
// add nested if openly after this
$nestedIfsWithOnlyReturnCount = count($nestedIfsWithOnlyReturn);

// clear
$foreach->stmts = [];

foreach ($nestedIfsWithOnlyReturn as $key => $nestedIfWithOnlyReturn) {
// last item → the return node
if ($nestedIfsWithOnlyReturnCount === $key + 1) {
$finalReturn = clone $nestedIfWithOnlyReturn;

$this->addInvertedIfStmtWithContinue($nestedIfWithOnlyReturn, $foreach);

$foreach->stmts = array_merge($foreach->stmts, $finalReturn->stmts);
} else {
$this->addInvertedIfStmtWithContinue($nestedIfWithOnlyReturn, $foreach);
}
}
}

private function addInvertedIfStmtWithContinue(If_ $nestedIfWithOnlyReturn, Foreach_ $foreach): void
{
$invertedCondition = $this->conditionInverter->createInvertedCondition($nestedIfWithOnlyReturn->cond);

// special case
if ($invertedCondition instanceof BooleanNot && $invertedCondition->expr instanceof BooleanAnd) {
$booleanNotPartIf = new If_(new BooleanNot($invertedCondition->expr->left));
$foreach->stmts[] = $booleanNotPartIf;

$booleanNotPartIf = new If_(new BooleanNot($invertedCondition->expr->right));
$foreach->stmts[] = $booleanNotPartIf;

return;
}

$nestedIfWithOnlyReturn->cond = $invertedCondition;
$nestedIfWithOnlyReturn->stmts = [new Continue_()];

$foreach->stmts[] = $nestedIfWithOnlyReturn;
}
}
33 changes: 8 additions & 25 deletions rules/solid/src/Rector/If_/ChangeNestedIfsToEarlyReturnRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@
namespace Rector\SOLID\Rector\If_;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\PhpParser\Node\Manipulator\BinaryOpManipulator;
use Rector\Core\PhpParser\Node\Manipulator\IfManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\SOLID\NodeTransformer\ConditionInverter;

/**
* @see \Rector\SOLID\Tests\Rector\If_\ChangeNestedIfsToEarlyReturnRector\ChangeNestedIfsToEarlyReturnRectorTest
Expand All @@ -29,14 +27,14 @@ final class ChangeNestedIfsToEarlyReturnRector extends AbstractRector
private $ifManipulator;

/**
* @var BinaryOpManipulator
* @var ConditionInverter
*/
private $binaryOpManipulator;
private $conditionInverter;

public function __construct(IfManipulator $ifManipulator, BinaryOpManipulator $binaryOpManipulator)
public function __construct(IfManipulator $ifManipulator, ConditionInverter $conditionInverter)
{
$this->ifManipulator = $ifManipulator;
$this->binaryOpManipulator = $binaryOpManipulator;
$this->conditionInverter = $conditionInverter;
}

public function getDefinition(): RectorDefinition
Expand Down Expand Up @@ -105,7 +103,7 @@ public function refactor(Node $node): ?Node
return null;
}

$this->processNestedIfsWIthOnlyReturn($node, $nestedIfsWithOnlyReturn, $nextNode);
$this->processNestedIfsWithOnlyReturn($node, $nestedIfsWithOnlyReturn, $nextNode);
$this->removeNode($node);

return null;
Expand All @@ -114,7 +112,7 @@ public function refactor(Node $node): ?Node
/**
* @param If_[] $nestedIfsWithOnlyReturn
*/
private function processNestedIfsWIthOnlyReturn(If_ $if, array $nestedIfsWithOnlyReturn, Return_ $nextReturn): void
private function processNestedIfsWithOnlyReturn(If_ $if, array $nestedIfsWithOnlyReturn, Return_ $nextReturn): void
{
// add nested if openly after this
$nestedIfsWithOnlyReturnCount = count($nestedIfsWithOnlyReturn);
Expand All @@ -134,7 +132,7 @@ private function addStandaloneIfsWithReturn(If_ $nestedIfWithOnlyReturn, If_ $if
{
$return = clone $return;

$invertedCondition = $this->createInvertedCondition($nestedIfWithOnlyReturn->cond);
$invertedCondition = $this->conditionInverter->createInvertedCondition($nestedIfWithOnlyReturn->cond);

// special case
if ($invertedCondition instanceof BooleanNot && $invertedCondition->expr instanceof BooleanAnd) {
Expand All @@ -152,19 +150,4 @@ private function addStandaloneIfsWithReturn(If_ $nestedIfWithOnlyReturn, If_ $if

$this->addNodeAfterNode($nestedIfWithOnlyReturn, $if);
}

private function createInvertedCondition(Expr $expr): Expr
{
// inverse condition
if ($expr instanceof BinaryOp) {
$inversedCondition = $this->binaryOpManipulator->invertCondition($expr);
if ($inversedCondition === null) {
return new BooleanNot($expr);
}

return $inversedCondition;
}

return new BooleanNot($expr);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Rector\SOLID\Tests\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector;

use Iterator;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\SOLID\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector;

final class ChangeNestedForeachIfsToEarlyContinueRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $file): void
{
$this->doTestFile($file);
}

public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

protected function getRectorClass(): string
{
return ChangeNestedForeachIfsToEarlyContinueRector::class;
}
}
Loading