From e8f01cafec62c65f70a498aac4e6295e0e8710b4 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 27 Nov 2023 16:47:00 +0100 Subject: [PATCH] [CodeQuality] Make CommonNotEqualRector skip if not <> --- .../Fixture/skip_already.php.inc | 11 ++++++++ .../Rector/NotEqual/CommonNotEqualRector.php | 25 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 rules-tests/CodeQuality/Rector/NotEqual/CommonNotEqualRector/Fixture/skip_already.php.inc diff --git a/rules-tests/CodeQuality/Rector/NotEqual/CommonNotEqualRector/Fixture/skip_already.php.inc b/rules-tests/CodeQuality/Rector/NotEqual/CommonNotEqualRector/Fixture/skip_already.php.inc new file mode 100644 index 00000000000..407662c6ffa --- /dev/null +++ b/rules-tests/CodeQuality/Rector/NotEqual/CommonNotEqualRector/Fixture/skip_already.php.inc @@ -0,0 +1,11 @@ +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; + } }