mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 05:48:21 +01:00
[CodeQuality] Add SimplifyTautologyTernaryRector
This commit is contained in:
parent
f238f2a310
commit
1c093821a3
@ -20,3 +20,4 @@ services:
|
||||
Rector\Php\Rector\FuncCall\RemoveExtraParametersRector: ~
|
||||
Rector\CodeQuality\Rector\LogicalOr\LogicalOrToBooleanOrRector: ~
|
||||
Rector\CodeQuality\Rector\BinaryOp\SimplifyDeMorganBinaryRector: ~
|
||||
Rector\CodeQuality\Rector\Ternary\SimplifyTautologyTernaryRector: ~
|
||||
|
@ -0,0 +1,69 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Rector\Ternary;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\BinaryOp\Identical;
|
||||
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
||||
use PhpParser\Node\Expr\Ternary;
|
||||
use Rector\PhpParser\Node\Maintainer\BinaryOpMaintainer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
final class SimplifyTautologyTernaryRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var BinaryOpMaintainer
|
||||
*/
|
||||
private $binaryOpMaintainer;
|
||||
|
||||
public function __construct(BinaryOpMaintainer $binaryOpMaintainer)
|
||||
{
|
||||
$this->binaryOpMaintainer = $binaryOpMaintainer;
|
||||
}
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Simplify tautology ternary to value', [
|
||||
new CodeSample(
|
||||
'$value = ($fullyQualifiedTypeHint !== $typeHint) ? $fullyQualifiedTypeHint : $typeHint;',
|
||||
'$value = $fullyQualifiedTypeHint;'
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Ternary::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Ternary $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $node->cond instanceof NotIdentical && ! $node->cond instanceof Identical) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$isMatch = $this->binaryOpMaintainer->matchFirstAndSecondConditionNode(
|
||||
$node->cond,
|
||||
function (Node $leftNode) use ($node) {
|
||||
return $this->areNodesEqual($leftNode, $node->if);
|
||||
},
|
||||
function (Node $leftNode) use ($node) {
|
||||
return $this->areNodesEqual($leftNode, $node->else);
|
||||
}
|
||||
);
|
||||
|
||||
if ($isMatch === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $node->if;
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Ternary\SimplifyTautologyTernaryRector\Fixture;
|
||||
|
||||
function tautologyTernary()
|
||||
{
|
||||
$fullyQualifiedTypeHint = 1;
|
||||
$typeHint = 2;
|
||||
|
||||
$value = ($fullyQualifiedTypeHint !== $typeHint) ? $fullyQualifiedTypeHint : $typeHint;
|
||||
$value = ($typeHint !== $fullyQualifiedTypeHint) ? $fullyQualifiedTypeHint : $typeHint;
|
||||
|
||||
$value = ($typeHint === $fullyQualifiedTypeHint) ? $fullyQualifiedTypeHint : $typeHint;
|
||||
|
||||
$value = ($typeHint === $fullyQualifiedTypeHint) ? $typeHint : $fullyQualifiedTypeHint;
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Ternary\SimplifyTautologyTernaryRector\Fixture;
|
||||
|
||||
function tautologyTernary()
|
||||
{
|
||||
$fullyQualifiedTypeHint = 1;
|
||||
$typeHint = 2;
|
||||
|
||||
$value = $fullyQualifiedTypeHint;
|
||||
$value = $fullyQualifiedTypeHint;
|
||||
|
||||
$value = $fullyQualifiedTypeHint;
|
||||
|
||||
$value = $typeHint;
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,19 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Ternary\SimplifyTautologyTernaryRector;
|
||||
|
||||
use Rector\CodeQuality\Rector\Ternary\SimplifyTautologyTernaryRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class SimplifyTautologyTernaryRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc']);
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return SimplifyTautologyTernaryRector::class;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user