mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-19 06:18:07 +01:00
Create SimplifyConditionsRector
This commit is contained in:
parent
45d1810602
commit
ad3df19eb0
@ -1,3 +1,4 @@
|
||||
services:
|
||||
Rector\Rector\CodeQuality\InArrayAndArrayKeysToArrayKeyExistsRector: ~
|
||||
Rector\Rector\CodeQuality\UnnecessaryTernaryExpressionRector: ~
|
||||
Rector\Rector\CodeQuality\SimplifyConditionsRector: ~
|
||||
|
57
src/Rector/CodeQuality/SimplifyConditionsRector.php
Normal file
57
src/Rector/CodeQuality/SimplifyConditionsRector.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector\CodeQuality;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\BinaryOp;
|
||||
use PhpParser\Node\Expr\BooleanNot;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
final class SimplifyConditionsRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $binaryOpClassMap = [
|
||||
BinaryOp\Identical::class => BinaryOp\NotIdentical::class,
|
||||
BinaryOp\NotIdentical::class => BinaryOp\Identical::class,
|
||||
BinaryOp\Equal::class => BinaryOp\NotEqual::class,
|
||||
BinaryOp\NotEqual::class => BinaryOp\Equal::class,
|
||||
BinaryOp\Greater::class => BinaryOp\SmallerOrEqual::class,
|
||||
BinaryOp\Smaller::class => BinaryOp\GreaterOrEqual::class,
|
||||
BinaryOp\GreaterOrEqual::class => BinaryOp\Smaller::class,
|
||||
BinaryOp\SmallerOrEqual::class => BinaryOp\Greater::class,
|
||||
];
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition(
|
||||
'Simplify conditions',
|
||||
[new CodeSample("if (! (\$foo !== 'bar')) {...", "if (\$foo === 'bar') {...")]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [BooleanNot::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BooleanNot $notNode
|
||||
*/
|
||||
public function refactor(Node $notNode): ?Node
|
||||
{
|
||||
if (! $notNode->expr instanceof BinaryOp) {
|
||||
return $notNode;
|
||||
}
|
||||
|
||||
$newBinaryOp = $this->binaryOpClassMap[get_class($notNode->expr)];
|
||||
|
||||
return new $newBinaryOp($notNode->expr->left, $notNode->expr->right);
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
if ($foo === 'bar') {
|
||||
} elseif ($foo == 'bug') {
|
||||
}
|
||||
|
||||
function foo ($bar) {
|
||||
return $bar <= 0;
|
||||
}
|
||||
|
||||
$baz = $foo !== 'baz';
|
||||
|
||||
function bar ($foo) {
|
||||
return $foo < 0;
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\CodeQuality\SimplifyConditionsRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
/**
|
||||
* @covers \Rector\Rector\CodeQuality\SimplifyConditionsRector
|
||||
*/
|
||||
final class SimplifyConditionsRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideWrongToFixedFiles()
|
||||
*/
|
||||
public function test(string $wrong, string $fixed): void
|
||||
{
|
||||
$this->doTestFileMatchesExpectedContent($wrong, $fixed);
|
||||
}
|
||||
|
||||
public function provideWrongToFixedFiles(): Iterator
|
||||
{
|
||||
yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'];
|
||||
}
|
||||
|
||||
protected function provideConfig(): string
|
||||
{
|
||||
return __DIR__ . '/config.yml';
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
if (! ($foo !== 'bar')) {
|
||||
} elseif (! ($foo != 'bug')) {
|
||||
}
|
||||
|
||||
function foo ($bar) {
|
||||
return ! ($bar > 0);
|
||||
}
|
||||
|
||||
$baz = ! ($foo === 'baz');
|
||||
|
||||
function bar ($foo) {
|
||||
return ! ($foo >= 0);
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
services:
|
||||
Rector\Rector\CodeQuality\SimplifyConditionsRector: ~
|
Loading…
x
Reference in New Issue
Block a user