Add custom rector rule for yoda conditions

This commit is contained in:
David Stone
2025-02-09 00:21:40 -07:00
parent be0ab98895
commit 07a69d33e5
7 changed files with 167 additions and 29 deletions

View File

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

View File

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

View File

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