mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-21 16:02:23 +02:00
Merge pull request #1074 from rectorphp/not-same
[CodeQuality] Add CommonNotEqualRector
This commit is contained in:
commit
62c3f3638b
@ -26,3 +26,5 @@ services:
|
||||
Rector\CodeQuality\Rector\If_\ConsecutiveNullCompareReturnsToNullCoalesceQueueRector: ~
|
||||
Rector\CodeQuality\Rector\If_\SimplifyIfIssetToNullCoalescingRector: ~
|
||||
Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector: ~
|
||||
Rector\CodeQuality\Rector\FuncCall\CommonNotEqualRector: ~
|
||||
Rector\CodeQuality\Rector\NotEqual\CommonNotEqualRector: ~
|
||||
|
@ -0,0 +1,62 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Rector\NotEqual;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\BinaryOp\NotEqual;
|
||||
use Rector\NodeTypeResolver\Node\Attribute;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
/**
|
||||
* @see https://stackoverflow.com/a/4294663/1348344
|
||||
*/
|
||||
final class CommonNotEqualRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Use common != instead of less known <> with same meaning', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run($one, $two)
|
||||
{
|
||||
return $one <> $two;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run($one, $two)
|
||||
{
|
||||
return $one != $two;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [NotEqual::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param NotEqual $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
// invoke override to default "!="
|
||||
$node->setAttribute(Attribute::ORIGINAL_NODE, null);
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\NotEqual\CommonNotEqualRector;
|
||||
|
||||
use Rector\CodeQuality\Rector\NotEqual\CommonNotEqualRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class CommonNotEqualRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc']);
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return CommonNotEqualRector::class;
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\NotEqual\CommonNotEqualRector\Fixture;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run($one, $two)
|
||||
{
|
||||
return $one <> $two;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\NotEqual\CommonNotEqualRector\Fixture;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run($one, $two)
|
||||
{
|
||||
return $one != $two;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user