Merge pull request #1074 from rectorphp/not-same

[CodeQuality] Add CommonNotEqualRector
This commit is contained in:
Tomáš Votruba 2019-02-19 09:40:28 -08:00 committed by GitHub
commit 62c3f3638b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 110 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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