Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeQuality] Add TernaryFalseExpressionToIfRector
- Loading branch information
1 parent
407ad5f
commit 2fdd940
Showing
8 changed files
with
230 additions
and
0 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
29 changes: 29 additions & 0 deletions
29
...r/Expression/TernaryFalseExpressionToIfRector/Fixture/include_no_side_effect_expr.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,29 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector\Fixture; | ||
|
||
final class IncludeNoSideEffectExpr | ||
{ | ||
public function run($value, $someMethod) | ||
{ | ||
$value ? $someMethod->call($value) : true; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector\Fixture; | ||
|
||
final class IncludeNoSideEffectExpr | ||
{ | ||
public function run($value, $someMethod) | ||
{ | ||
if ($value) { | ||
$someMethod->call($value); | ||
} | ||
} | ||
} | ||
|
||
?> |
29 changes: 29 additions & 0 deletions
29
...eQuality/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/negated_value.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,29 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector\Fixture; | ||
|
||
final class NegatedValue | ||
{ | ||
public function run($value) | ||
{ | ||
! $value ? 'execute' : false; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector\Fixture; | ||
|
||
final class NegatedValue | ||
{ | ||
public function run($value) | ||
{ | ||
if (! $value) { | ||
'execute'; | ||
} | ||
} | ||
} | ||
|
||
?> |
11 changes: 11 additions & 0 deletions
11
...lity/Rector/Expression/TernaryFalseExpressionToIfRector/Fixture/skip_null_ternary.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,11 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector\Fixture; | ||
|
||
final class SkipNullTernary | ||
{ | ||
public function run($value, $someMethod) | ||
{ | ||
$value ?: $someMethod->call($value); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/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,29 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
public function run($value, $someMethod) | ||
{ | ||
$value ? $someMethod->call($value) : false; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
public function run($value, $someMethod) | ||
{ | ||
if ($value) { | ||
$someMethod->call($value); | ||
} | ||
} | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...ctor/Expression/TernaryFalseExpressionToIfRector/TernaryFalseExpressionToIfRectorTest.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\Expression\TernaryFalseExpressionToIfRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class TernaryFalseExpressionToIfRectorTest 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'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector/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\Expression\TernaryFalseExpressionToIfRector; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(TernaryFalseExpressionToIfRector::class); | ||
}; |
87 changes: 87 additions & 0 deletions
87
rules/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector.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,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Rector\Expression; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\Ternary; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PhpParser\Node\Stmt\If_; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\DeadCode\SideEffect\SideEffectNodeDetector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector\TernaryFalseExpressionToIfRectorTest | ||
*/ | ||
final class TernaryFalseExpressionToIfRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly SideEffectNodeDetector $sideEffectNodeDetector | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change ternary with false to if and explicit call', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
public function run($value, $someMethod) | ||
{ | ||
$value ? $someMethod->call($value) : false; | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
public function run($value, $someMethod) | ||
{ | ||
if ($value) { | ||
$someMethod->call($value); | ||
} | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Expression::class]; | ||
} | ||
|
||
/** | ||
* @param Expression $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $node->expr instanceof Ternary) { | ||
return null; | ||
} | ||
|
||
$ternary = $node->expr; | ||
if (! $ternary->if instanceof Expr) { | ||
return null; | ||
} | ||
|
||
if ($this->sideEffectNodeDetector->detect($ternary->else)) { | ||
return null; | ||
} | ||
|
||
return new If_($ternary->cond, [ | ||
'stmts' => [new Expression($ternary->if)], | ||
]); | ||
} | ||
} |