-
-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeQuality] Add DoWhileBreakFalseToIfElseRector
- Loading branch information
1 parent
305c524
commit aaf1a43
Showing
7 changed files
with
270 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
...uality/Rector/Do_/DoWhileBreakFalseToIfElseRector/DoWhileBreakFalseToIfElseRectorTest.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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Do_\DoWhileBreakFalseToIfElseRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class DoWhileBreakFalseToIfElseRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
/** | ||
* @return Iterator<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...tests/CodeQuality/Rector/Do_/DoWhileBreakFalseToIfElseRector/Fixture/double_break.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,35 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Do_\DoWhileBreakFalseToIfElseRector\Fixture; | ||
|
||
do { | ||
if (mt_rand(0, 1)) { | ||
$value = 5; | ||
break; | ||
} | ||
|
||
if (mt_rand(0, 2)) { | ||
$value = 10; | ||
break; | ||
} | ||
|
||
$value = 25; | ||
} while (false); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Do_\DoWhileBreakFalseToIfElseRector\Fixture; | ||
|
||
if (mt_rand(0, 1)) { | ||
$value = 5; | ||
} else { | ||
if (mt_rand(0, 2)) { | ||
$value = 10; | ||
} else { | ||
$value = 25; | ||
} | ||
} | ||
|
||
?> |
26 changes: 26 additions & 0 deletions
26
rules-tests/CodeQuality/Rector/Do_/DoWhileBreakFalseToIfElseRector/Fixture/if_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,26 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Do_\DoWhileBreakFalseToIfElseRector\Fixture; | ||
|
||
do { | ||
if (mt_rand(0, 1)) { | ||
$value = 5; | ||
break; | ||
} else { | ||
$value = 10; | ||
} | ||
} while (false); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Do_\DoWhileBreakFalseToIfElseRector\Fixture; | ||
|
||
if (mt_rand(0, 1)) { | ||
$value = 5; | ||
} else { | ||
$value = 10; | ||
} | ||
|
||
?> |
22 changes: 22 additions & 0 deletions
22
...tests/CodeQuality/Rector/Do_/DoWhileBreakFalseToIfElseRector/Fixture/single_break.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,22 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Do_\DoWhileBreakFalseToIfElseRector\Fixture; | ||
|
||
do { | ||
if (mt_rand(0, 1)) { | ||
$value = 5; | ||
break; | ||
} | ||
} while (false); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Do_\DoWhileBreakFalseToIfElseRector\Fixture; | ||
|
||
if (mt_rand(0, 1)) { | ||
$value = 5; | ||
} | ||
|
||
?> |
26 changes: 26 additions & 0 deletions
26
...s-tests/CodeQuality/Rector/Do_/DoWhileBreakFalseToIfElseRector/Fixture/some_class.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,26 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Do_\DoWhileBreakFalseToIfElseRector\Fixture; | ||
|
||
do { | ||
if (mt_rand(0, 1)) { | ||
$value = 5; | ||
break; | ||
} | ||
|
||
$value = 10; | ||
} while (false); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Do_\DoWhileBreakFalseToIfElseRector\Fixture; | ||
|
||
if (mt_rand(0, 1)) { | ||
$value = 5; | ||
} else { | ||
$value = 10; | ||
} | ||
|
||
?> |
11 changes: 11 additions & 0 deletions
11
...s-tests/CodeQuality/Rector/Do_/DoWhileBreakFalseToIfElseRector/config/configured_rule.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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\CodeQuality\Rector\Do_\DoWhileBreakFalseToIfElseRector; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(DoWhileBreakFalseToIfElseRector::class); | ||
}; |
117 changes: 117 additions & 0 deletions
117
rules/CodeQuality/Rector/Do_/DoWhileBreakFalseToIfElseRector.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,117 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Rector\Do_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt; | ||
use PhpParser\Node\Stmt\Break_; | ||
use PhpParser\Node\Stmt\Do_; | ||
use PhpParser\Node\Stmt\Else_; | ||
use PhpParser\Node\Stmt\If_; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\CodeQuality\Rector\Do_\DoWhileBreakFalseToIfElseRector\DoWhileBreakFalseToIfElseRectorTest | ||
*/ | ||
final class DoWhileBreakFalseToIfElseRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Replace do (...} while (false); with more readable if/else conditions', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
do { | ||
if (mt_rand(0, 1)) { | ||
$value = 5; | ||
break; | ||
} | ||
$value = 10; | ||
} while (false); | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
if (mt_rand(0, 1)) { | ||
$value = 5; | ||
} else { | ||
$value = 10; | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Do_::class]; | ||
} | ||
|
||
/** | ||
* @param Do_ $node | ||
*/ | ||
public function refactor(Node $node) | ||
{ | ||
if (! $this->valueResolver->isFalse($node->cond)) { | ||
return null; | ||
} | ||
|
||
$currentStmts = $node->stmts; | ||
return $this->resolveNewStmts($currentStmts); | ||
} | ||
|
||
/** | ||
* @param Stmt[] $currentStmts | ||
* @return Stmt[] | ||
*/ | ||
private function resolveNewStmts(array $currentStmts): array | ||
{ | ||
$foundBreak = $this->betterNodeFinder->findFirstInstanceOf($currentStmts, Break_::class); | ||
if (! $foundBreak instanceof Break_) { | ||
return $currentStmts; | ||
} | ||
|
||
$newStmts = []; | ||
|
||
foreach ($currentStmts as $key => $currentStmt) { | ||
$foundBreak = $this->betterNodeFinder->findFirstInstanceOf($currentStmt, Break_::class); | ||
if (! $foundBreak instanceof Break_) { | ||
continue; | ||
} | ||
|
||
$this->removeNode($foundBreak); | ||
|
||
// collect rest of nodes | ||
$restOfStmts = array_slice($currentStmts, $key + 1, count($currentStmts)); | ||
|
||
$currentIf = $currentStmt instanceof If_ | ||
? $currentStmt | ||
: $this->betterNodeFinder->findInstanceOf($currentStmt, If_::class); | ||
|
||
if (! $currentIf instanceof If_) { | ||
continue; | ||
} | ||
|
||
// reprint new tokens | ||
$currentIf->setAttribute(AttributeKey::ORIGINAL_NODE, null); | ||
|
||
if ($restOfStmts !== []) { | ||
$restOfStmts = $this->resolveNewStmts($restOfStmts); | ||
$currentIf->else = new Else_($restOfStmts); | ||
} | ||
|
||
$newStmts[] = $currentStmt; | ||
break; | ||
} | ||
|
||
return $newStmts; | ||
} | ||
} |