diff --git a/rules-tests/CodeQuality/Rector/If_/ShortenElseIfRector/Fixture/skip_alternative_syntax.php.inc b/rules-tests/CodeQuality/Rector/If_/ShortenElseIfRector/Fixture/skip_alternative_syntax.php.inc new file mode 100644 index 00000000000..3edb1782341 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/If_/ShortenElseIfRector/Fixture/skip_alternative_syntax.php.inc @@ -0,0 +1,18 @@ +isAlternativeSyntax($node) || $this->isAlternativeSyntax($if)) { + return null; + } + // Try to shorten the nested if before transforming it to elseif $refactored = $this->shortenElseIf($if); @@ -114,4 +120,32 @@ private function shortenElseIf(If_ $node): ?If_ return $node; } + + private function isAlternativeSyntax(If_ $if): bool + { + $startTokenPos = $if->cond->getEndTokenPos(); + if ($startTokenPos < 0) { + return false; + } + + $oldTokens = $this->getFile() + ->getOldTokens(); + + for ($i = $startTokenPos + 1; isset($oldTokens[$i]); ++$i) { + $token = $oldTokens[$i]; + if (! $token instanceof Token) { + continue; + } + + if ($token->text === ':') { + return true; + } + + if ($token->text === '{') { + return false; + } + } + + return false; + } }