Skip to content

Commit

Permalink
[CodeQuality] Make CommonNotEqualRector skip if not <> (#5292)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 27, 2023
1 parent e66754a commit b5f434b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\NotEqual\CommonNotEqualRector\Fixture;

final class SkipAlready
{
public function run($one, $two)
{
return $one != $two;
}
}
25 changes: 24 additions & 1 deletion rules/CodeQuality/Rector/NotEqual/CommonNotEqualRector.php
Expand Up @@ -58,11 +58,34 @@ public function getNodeTypes(): array
/**
* @param NotEqual $node
*/
public function refactor(Node $node): NotEqual
public function refactor(Node $node): ?NotEqual
{
if (! $this->doesNotEqualContainsShipCompareToken($node)) {
return null;
}

// invoke override to default "!="
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);

return $node;
}

private function doesNotEqualContainsShipCompareToken(NotEqual $notEqual): bool
{
$tokenStartPos = $notEqual->getStartTokenPos();
$tokenEndPos = $notEqual->getEndTokenPos();

for ($i = $tokenStartPos; $i < $tokenEndPos; ++$i) {
$token = $this->file->getOldTokens()[$i];
if (! isset($token[1])) {
continue;
}

if ($token[1] === '<>') {
return true;
}
}

return false;
}
}

0 comments on commit b5f434b

Please sign in to comment.