-
-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
269 additions
and
1 deletion.
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
102 changes: 102 additions & 0 deletions
102
packages/CodeQuality/src/Rector/If_/CombineIfRector.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,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; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/CodeQuality/tests/Rector/If_/CombineIfRector/CombineIfRectorTest.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,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; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/CodeQuality/tests/Rector/If_/CombineIfRector/Fixture/child_else.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,16 @@ | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\If_\CombineIfRector\ChildElse; | ||
|
||
class SomeClass { | ||
public function run() | ||
{ | ||
if ($cond1) { | ||
if ($cond2) { | ||
return 'foo'; | ||
} else { | ||
return 'bar'; | ||
} | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/CodeQuality/tests/Rector/If_/CombineIfRector/Fixture/child_elseif.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,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'; | ||
} | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
packages/CodeQuality/tests/Rector/If_/CombineIfRector/Fixture/fixture.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,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'; | ||
} | ||
} | ||
} | ||
|
||
?> |
17 changes: 17 additions & 0 deletions
17
packages/CodeQuality/tests/Rector/If_/CombineIfRector/Fixture/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,17 @@ | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\If_\CombineIfRector\MoreStatements; | ||
|
||
class SomeClass { | ||
public function run() | ||
{ | ||
if ($cond1) { | ||
if ($cond2) { | ||
return 'foo'; | ||
} | ||
return 'bar'; | ||
} | ||
} | ||
} | ||
|
||
?> |
16 changes: 16 additions & 0 deletions
16
packages/CodeQuality/tests/Rector/If_/CombineIfRector/Fixture/parent_else.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,16 @@ | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\If_\CombineIfRector\ParentElse; | ||
|
||
class SomeClass { | ||
public function run() | ||
{ | ||
if ($cond1) { | ||
if ($cond2) { | ||
return 'foo'; | ||
} | ||
} else { | ||
return 'bar'; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/CodeQuality/tests/Rector/If_/CombineIfRector/Fixture/parent_elseif.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,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'; | ||
} | ||
} | ||
} |