mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 08:20:53 +00:00
[CodeQuality] Add TernaryFalseExpressionToIfRector (#2590)
This commit is contained in:
parent
407ad5fc5e
commit
7c8b23b02d
|
@ -1,4 +1,4 @@
|
|||
# 410 Rules Overview
|
||||
# 411 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
|||
|
||||
- [Arguments](#arguments) (5)
|
||||
|
||||
- [CodeQuality](#codequality) (72)
|
||||
- [CodeQuality](#codequality) (73)
|
||||
|
||||
- [CodingStyle](#codingstyle) (35)
|
||||
|
||||
|
@ -1589,6 +1589,27 @@ Switch negated ternary condition rector
|
|||
|
||||
<br>
|
||||
|
||||
### TernaryFalseExpressionToIfRector
|
||||
|
||||
Change ternary with false to if and explicit call
|
||||
|
||||
- class: [`Rector\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector`](../rules/CodeQuality/Rector/Expression/TernaryFalseExpressionToIfRector.php)
|
||||
|
||||
```diff
|
||||
final class SomeClass
|
||||
{
|
||||
public function run($value, $someMethod)
|
||||
{
|
||||
- $value ? $someMethod->call($value) : false;
|
||||
+ if ($value) {
|
||||
+ $someMethod->call($value);
|
||||
+ }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### ThrowWithPreviousExceptionRector
|
||||
|
||||
When throwing into a catch block, checks that the previous exception is passed to the new throw clause
|
||||
|
@ -1954,7 +1975,7 @@ return static function (RectorConfig $rectorConfig): void {
|
|||
- /**
|
||||
- * @var DateTime[]
|
||||
- */
|
||||
+ /** @var DateTime[]|null */
|
||||
+ /** @var DateTime[] */
|
||||
private ?array $dateTimes;
|
||||
}
|
||||
```
|
||||
|
|
|
@ -17,6 +17,7 @@ use Rector\CodeQuality\Rector\Concat\JoinStringConcatRector;
|
|||
use Rector\CodeQuality\Rector\Do_\DoWhileBreakFalseToIfElseRector;
|
||||
use Rector\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector;
|
||||
use Rector\CodeQuality\Rector\Expression\InlineIfToExplicitIfRector;
|
||||
use Rector\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector;
|
||||
use Rector\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector;
|
||||
use Rector\CodeQuality\Rector\For_\ForToForeachRector;
|
||||
use Rector\CodeQuality\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector;
|
||||
|
@ -182,5 +183,6 @@ return static function (RectorConfig $rectorConfig): void {
|
|||
DoWhileBreakFalseToIfElseRector::class,
|
||||
InlineArrayReturnAssignRector::class,
|
||||
InlineIsAInstanceOfRector::class,
|
||||
TernaryFalseExpressionToIfRector::class,
|
||||
]);
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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';
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
};
|
|
@ -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)],
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -58,7 +58,7 @@ final class SomeClass
|
|||
/** @phpstan-var string */
|
||||
private const TEXT = 'text';
|
||||
|
||||
/** @var DateTime[]|null */
|
||||
/** @var DateTime[] */
|
||||
private ?array $dateTimes;
|
||||
}
|
||||
CODE_SAMPLE
|
||||
|
|
Loading…
Reference in New Issue
Block a user