Add custom rector rule for yoda conditions
This commit is contained in:
44
utils/rector/src/Rector/YodaConditionsRector.php
Normal file
44
utils/rector/src/Rector/YodaConditionsRector.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Utils\Rector\Rector;
|
||||
|
||||
use PhpParser\Node;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use PhpParser\Node\Expr\BinaryOp\Equal;
|
||||
use PhpParser\Node\Expr\BinaryOp\Identical;
|
||||
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
||||
use PhpParser\Node\Expr\BinaryOp\NotEqual;
|
||||
use PhpParser\Node\Scalar;
|
||||
use PhpParser\Node\Expr\ConstFetch;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
|
||||
/**
|
||||
* @see \Rector\Tests\TypeDeclaration\Rector\YodaConditionsRector\YodaConditionsRectorTest
|
||||
*/
|
||||
final class YodaConditionsRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Equal::class, NotEqual::class, Identical::class, NotIdentical::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \PhpParser\Node\Stmt\Class_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
// Ensure the left operand is not a constant
|
||||
if ($node->left instanceof Node\Expr\Variable && (! $node->right instanceof Node\Expr\Variable)) {
|
||||
// Swap the left and right operands
|
||||
$this->mirrorComments($node->right, $node->left);
|
||||
[$node->left, $node->right] = [$node->right, $node->left];
|
||||
}
|
||||
return $node;
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\TypeDeclaration\Rector\YodaConditionsRector\Fixture;
|
||||
const A_CONST = 'x';
|
||||
$a = 'x';
|
||||
if ($a === 'x') {
|
||||
$a = 'xx';
|
||||
}
|
||||
if ($a === null) {
|
||||
$a = 'xx';
|
||||
}
|
||||
if ($a == false) {
|
||||
$a = 'xx';
|
||||
}
|
||||
if ($a === A_CONST) {
|
||||
$a = 'xx';
|
||||
}
|
||||
if (strlen($a) === 1) {
|
||||
$a = 'xx';
|
||||
}
|
||||
if ($a === 0 || $a === rand()) {
|
||||
return $a;
|
||||
}
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\TypeDeclaration\Rector\YodaConditionsRector\Fixture;
|
||||
const A_CONST = 'x';
|
||||
$a = 'x';
|
||||
if ('x' === $a) {
|
||||
$a = 'xx';
|
||||
}
|
||||
if (null === $a) {
|
||||
$a = 'xx';
|
||||
}
|
||||
if (false == $a) {
|
||||
$a = 'xx';
|
||||
}
|
||||
if (A_CONST === $a) {
|
||||
$a = 'xx';
|
||||
}
|
||||
if (strlen($a) === 1) {
|
||||
$a = 'xx';
|
||||
}
|
||||
if (0 === $a || rand() === $a) {
|
||||
return $a;
|
||||
}
|
||||
?>
|
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\TypeDeclaration\Rector\YodaConditionsRector;
|
||||
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class YodaConditionsRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(string $filePath): void
|
||||
{
|
||||
$this->doTestFile($filePath);
|
||||
}
|
||||
|
||||
public static function provideData(): \Iterator
|
||||
{
|
||||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
|
||||
return static function (RectorConfig $rectorConfig): void {
|
||||
$rectorConfig->rule(\Utils\Rector\Rector\YodaConditionsRector::class);
|
||||
};
|
Reference in New Issue
Block a user