mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 11:44:14 +01:00
Added CombineIfRector
This commit is contained in:
parent
ed868e61fc
commit
e856fb8ddb
@ -1,4 +1,4 @@
|
||||
services:
|
||||
services:
|
||||
Rector\CodeQuality\Rector\Assign\CombinedAssignRector: null
|
||||
Rector\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector: null
|
||||
Rector\CodeQuality\Rector\Foreach_\ForeachToInArrayRector: null
|
||||
@ -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
|
||||
|
102
packages/CodeQuality/src/Rector/If_/CombineIfRector.php
Normal file
102
packages/CodeQuality/src/Rector/If_/CombineIfRector.php
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -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';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -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';
|
||||
}
|
||||
}
|
||||
}
|
@ -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';
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user