-
-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeQuality] Add SwitchTrueToIfRector (#3535)
- Loading branch information
1 parent
8ff7747
commit 26e570f
Showing
10 changed files
with
258 additions
and
5 deletions.
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
18 changes: 18 additions & 0 deletions
18
rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/Fixture/skip_false.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,18 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Switch_\SwitchTrueToIfRector\Fixture; | ||
|
||
class SkipFalse | ||
{ | ||
public function run() | ||
{ | ||
switch (false) { | ||
case $value === 0: | ||
return 'no'; | ||
case $value === 1: | ||
return 'yes'; | ||
case $value === 2: | ||
return 'maybe'; | ||
}; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/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,42 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Switch_\SwitchTrueToIfRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
switch (true) { | ||
case $value === 0: | ||
return 'no'; | ||
case $value === 1: | ||
return 'yes'; | ||
case $value === 2: | ||
return 'maybe'; | ||
}; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Switch_\SwitchTrueToIfRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
if ($value === 0) { | ||
return 'no'; | ||
} | ||
if ($value === 1) { | ||
return 'yes'; | ||
} | ||
if ($value === 2) { | ||
return 'maybe'; | ||
} | ||
} | ||
} | ||
|
||
?> |
45 changes: 45 additions & 0 deletions
45
rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/Fixture/with_default.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,45 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Switch_\SwitchTrueToIfRector\Fixture; | ||
|
||
class WithDefault | ||
{ | ||
public function run() | ||
{ | ||
switch (true) { | ||
case $value === 0: | ||
return 'no'; | ||
case $value === 1: | ||
return 'yes'; | ||
case $value === 2: | ||
return 'maybe'; | ||
default: | ||
return 'nevermind'; | ||
}; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Switch_\SwitchTrueToIfRector\Fixture; | ||
|
||
class WithDefault | ||
{ | ||
public function run() | ||
{ | ||
if ($value === 0) { | ||
return 'no'; | ||
} | ||
if ($value === 1) { | ||
return 'yes'; | ||
} | ||
if ($value === 2) { | ||
return 'maybe'; | ||
} | ||
return 'nevermind'; | ||
} | ||
} | ||
|
||
?> |
28 changes: 28 additions & 0 deletions
28
rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/SwitchTrueToIfRectorTest.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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Switch_\SwitchTrueToIfRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class SwitchTrueToIfRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\CodeQuality\Rector\Switch_\SwitchTrueToIfRector; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(SwitchTrueToIfRector::class); | ||
}; |
107 changes: 107 additions & 0 deletions
107
rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.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,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Rector\Switch_; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt; | ||
use PhpParser\Node\Stmt\Case_; | ||
use PhpParser\Node\Stmt\If_; | ||
use PhpParser\Node\Stmt\Switch_; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\CodeQuality\Rector\Switch_\SwitchTrueToIfRector\SwitchTrueToIfRectorTest | ||
*/ | ||
final class SwitchTrueToIfRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change switch (true) to if statements', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
switch (true) { | ||
case $value === 0: | ||
return 'no'; | ||
case $value === 1: | ||
return 'yes'; | ||
case $value === 2: | ||
return 'maybe'; | ||
}; | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
if ($value === 0) { | ||
return 'no'; | ||
} | ||
if ($value === 1) { | ||
return 'yes'; | ||
} | ||
if ($value === 2) { | ||
return 'maybe'; | ||
} | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Switch_::class]; | ||
} | ||
|
||
/** | ||
* @param Switch_ $node | ||
* @return Stmt[]|null | ||
*/ | ||
public function refactor(Node $node): ?array | ||
{ | ||
if (! $this->valueResolver->isTrue($node->cond)) { | ||
return null; | ||
} | ||
|
||
$newStmts = []; | ||
|
||
$defaultCase = null; | ||
|
||
foreach ($node->cases as $case) { | ||
if (! $case->cond instanceof Expr) { | ||
$defaultCase = $case; | ||
continue; | ||
} | ||
|
||
$if = new If_($case->cond); | ||
$if->stmts = $case->stmts; | ||
|
||
$newStmts[] = $if; | ||
} | ||
|
||
if ($defaultCase instanceof Case_) { | ||
return array_merge($newStmts, $defaultCase->stmts); | ||
} | ||
|
||
return $newStmts; | ||
} | ||
} |
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
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