Merge pull request #679 from rectorphp/simplify-conditions-rector

Create SimplifyConditionsRector
This commit is contained in:
Tomáš Votruba 2018-10-13 11:52:27 +08:00 committed by GitHub
commit 1fe5345a18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 839 additions and 334 deletions

View File

@ -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

View 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);
}
}

View File

@ -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;
}

View File

@ -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';
}
}

View File

@ -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);
}

View File

@ -0,0 +1,2 @@
services:
Rector\Rector\CodeQuality\SimplifyConditionsRector: ~