mirror of
https://github.com/rectorphp/rector.git
synced 2025-03-14 04:19:44 +01:00
Merge pull request #679 from rectorphp/simplify-conditions-rector
Create SimplifyConditionsRector
This commit is contained in:
commit
1fe5345a18
@ -1,3 +1,4 @@
|
||||
services:
|
||||
Rector\Rector\CodeQuality\InArrayAndArrayKeysToArrayKeyExistsRector: ~
|
||||
Rector\Rector\CodeQuality\UnnecessaryTernaryExpressionRector: ~
|
||||
Rector\Rector\CodeQuality\SimplifyConditionsRector: ~
|
||||
|
File diff suppressed because it is too large
Load Diff
71
src/Rector/CodeQuality/SimplifyConditionsRector.php
Normal file
71
src/Rector/CodeQuality/SimplifyConditionsRector.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector\CodeQuality;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\BinaryOp;
|
||||
use PhpParser\Node\Expr\BinaryOp\Identical;
|
||||
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
||||
use PhpParser\Node\Expr\BinaryOp\Equal;
|
||||
use PhpParser\Node\Expr\BinaryOp\NotEqual;
|
||||
use PhpParser\Node\Expr\BinaryOp\Greater;
|
||||
use PhpParser\Node\Expr\BinaryOp\Smaller;
|
||||
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
|
||||
use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
|
||||
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 = [
|
||||
Identical::class => NotIdentical::class,
|
||||
NotIdentical::class => Identical::class,
|
||||
Equal::class => NotEqual::class,
|
||||
NotEqual::class => Equal::class,
|
||||
Greater::class => SmallerOrEqual::class,
|
||||
Smaller::class => GreaterOrEqual::class,
|
||||
GreaterOrEqual::class => Smaller::class,
|
||||
SmallerOrEqual::class => 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;
|
||||
}
|
||||
|
||||
$binaryOpType = get_class($notNode->expr);
|
||||
|
||||
if (! isset($this->binaryOpClassMap[$binaryOpType])) {
|
||||
return $notNode;
|
||||
}
|
||||
|
||||
$newBinaryOp = $this->binaryOpClassMap[$binaryOpType];
|
||||
|
||||
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