Skip to content

Commit

Permalink
Merge ad89be4 into ed868e6
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroensmit committed Jan 15, 2020
2 parents ed868e6 + ad89be4 commit 5091344
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/set/code-quality/code-quality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ services:
Rector\CodeQuality\Rector\If_\ConsecutiveNullCompareReturnsToNullCoalesceQueueRector: null
Rector\CodeQuality\Rector\If_\SimplifyIfIssetToNullCoalescingRector: null
Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector: null
Rector\CodeQuality\Rector\If_\CombineIfRector: null
Rector\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector: null
Rector\CodeQuality\Rector\Ternary\SimplifyDuplicatedTernaryRector: null
Rector\CodeQuality\Rector\Identical\SimplifyBoolIdenticalTrueRector: null
Expand Down
25 changes: 24 additions & 1 deletion docs/AllRectorsOverview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# All 429 Rectors Overview
# All 430 Rectors Overview

- [Projects](#projects)
- [General](#general)
Expand Down Expand Up @@ -514,6 +514,29 @@ Change array_push() to direct variable assign

<br>

### `CombineIfRector`

- class: `Rector\CodeQuality\Rector\If_\CombineIfRector`

Merges nested if statements

```diff
class SomeClass {
public function run()
{
- if ($cond1) {
- if ($cond2) {
- return 'foo';
- }
+ if ($cond1 && $cond2) {
+ return 'foo';
}
}
}
```

<br>

### `CombinedAssignRector`

- class: `Rector\CodeQuality\Rector\Assign\CombinedAssignRector`
Expand Down
102 changes: 102 additions & 0 deletions packages/CodeQuality/src/Rector/If_/CombineIfRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

namespace Rector\CodeQuality\Rector\If_;

use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node;
use PhpParser\Node\Stmt\If_;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

/**
* @see \Rector\CodeQuality\Tests\Rector\If_\CombineIfRector\CombineIfRectorTest
*/
final class CombineIfRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Merges nested if statements', [
new CodeSample(
<<<'PHP'
class SomeClass {
public function run()
{
if ($cond1) {
if ($cond2) {
return 'foo';
}
}
}
}
PHP
,
<<<'PHP'
class SomeClass {
public function run()
{
if ($cond1 && $cond2) {
return 'foo';
}
}
}
PHP

),
]);
}

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

/**
* @param If_ $node
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
return null;
}

/** @var Node\Stmt\If_ $subIf */
$subIf = $node->stmts[0];
$node->cond = new BooleanAnd($node->cond, $subIf->cond);
$node->stmts = $subIf->stmts;
return $node;
}

private function shouldSkip(If_ $node): bool
{
if ($node->else !== null) {
return true;
}

if (count($node->stmts) !== 1) {
return true;
}

if ($node->elseifs) {
return true;
}

if (! $node->stmts[0] instanceof If_) {
return true;
}

if ($node->stmts[0]->else !== null) {
return true;
}

if ($node->stmts[0]->elseifs) {
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Rector\CodeQuality\Tests\Rector\If_\CombineIfRector;

use Iterator;
use Rector\CodeQuality\Rector\If_\CombineIfRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

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

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

protected function getRectorClass(): string
{
return CombineIfRector::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\CodeQuality\Tests\Rector\If_\CombineIfRector\ChildElse;

class SomeClass {
public function run()
{
if ($cond1) {
if ($cond2) {
return 'foo';
} else {
return 'bar';
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\CodeQuality\Tests\Rector\If_\CombineIfRector\ChildElseIf;

class SomeClass {
public function run()
{
if ($cond1) {
if ($cond2) {
return 'foo';
} elseif($cond3) {
return 'bar';
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\CodeQuality\Tests\Rector\If_\CombineIfRector\Fixture;

class SomeClass {
public function run()
{
if ($cond1) {
if ($cond2) {
return 'foo';
}
}
}
}

?>
-----
<?php

namespace Rector\CodeQuality\Tests\Rector\If_\CombineIfRector\Fixture;

class SomeClass {
public function run()
{
if ($cond1 && $cond2) {
return 'foo';
}
}
}

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

namespace Rector\CodeQuality\Tests\Rector\If_\CombineIfRector\MoreStatements;

class SomeClass {
public function run()
{
if ($cond1) {
if ($cond2) {
return 'foo';
}
return 'bar';
}
}
}

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

namespace Rector\CodeQuality\Tests\Rector\If_\CombineIfRector\ParentElse;

class SomeClass {
public function run()
{
if ($cond1) {
if ($cond2) {
return 'foo';
}
} else {
return 'bar';
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\CodeQuality\Tests\Rector\If_\CombineIfRector\ParentElseIf;

class SomeClass {
public function run()
{
if ($cond1) {
if ($cond2) {
return 'foo';
}
} elseif($cond3) {
return 'bar';
}
}
}

0 comments on commit 5091344

Please sign in to comment.