Skip to content

Commit 229a5f9

Browse files
authored
[CodeQuality] Avoid reprint on NotEqual\CommonNotEqualRector to keep format preserving (#7703)
1 parent 82f5181 commit 229a5f9

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\NotEqual\CommonNotEqualRector\Fixture;
4+
5+
final class MultiLineCompare
6+
{
7+
public function run($one, $two)
8+
{
9+
return $one
10+
<> $two;
11+
}
12+
}
13+
14+
?>
15+
-----
16+
<?php
17+
18+
namespace Rector\Tests\CodeQuality\Rector\NotEqual\CommonNotEqualRector\Fixture;
19+
20+
final class MultiLineCompare
21+
{
22+
public function run($one, $two)
23+
{
24+
return $one
25+
!= $two;
26+
}
27+
}
28+
29+
?>

rules/CodeQuality/Rector/NotEqual/CommonNotEqualRector.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr\BinaryOp\NotEqual;
9-
use Rector\NodeTypeResolver\Node\AttributeKey;
109
use Rector\Rector\AbstractRector;
1110
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1211
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -59,29 +58,18 @@ public function getNodeTypes(): array
5958
*/
6059
public function refactor(Node $node): ?NotEqual
6160
{
62-
if (! $this->doesNotEqualContainsShipCompareToken($node)) {
63-
return null;
64-
}
65-
66-
// invoke override to default "!="
67-
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
68-
69-
return $node;
70-
}
71-
72-
private function doesNotEqualContainsShipCompareToken(NotEqual $notEqual): bool
73-
{
74-
$tokenStartPos = $notEqual->getStartTokenPos();
75-
$tokenEndPos = $notEqual->getEndTokenPos();
61+
$tokenStartPos = $node->getStartTokenPos();
62+
$tokenEndPos = $node->getEndTokenPos();
7663

7764
for ($i = $tokenStartPos; $i < $tokenEndPos; ++$i) {
7865
$token = $this->file->getOldTokens()[$i];
7966

8067
if ((string) $token === '<>') {
81-
return true;
68+
$token->text = '!=';
69+
return $node;
8270
}
8371
}
8472

85-
return false;
73+
return null;
8674
}
8775
}

0 commit comments

Comments
 (0)