diff --git a/rules-tests/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector/Fixture/echo_after_if.php.inc b/rules-tests/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector/Fixture/echo_after_if.php.inc
new file mode 100644
index 00000000000..6d90937ab12
--- /dev/null
+++ b/rules-tests/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector/Fixture/echo_after_if.php.inc
@@ -0,0 +1,5 @@
+
+-----
+
diff --git a/rules-tests/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector/Fixture/skip_direct_html.php.inc b/rules-tests/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector/Fixture/skip_direct_html.php.inc
new file mode 100644
index 00000000000..4337de71db6
--- /dev/null
+++ b/rules-tests/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector/Fixture/skip_direct_html.php.inc
@@ -0,0 +1,35 @@
+
+ Hello
+
+
+ Hello
+
+
+
+ Hello
+
+ file->getOldTokens();
- if ($this->isIfConditionFollowedByOpeningCurlyBracket($node, $oldTokens)) {
+ if ($this->shouldSkip($node, $oldTokens)) {
return null;
}
@@ -80,34 +80,18 @@ public function refactor(Node $node): ?Node
/**
* @param mixed[] $oldTokens
*/
- private function isIfConditionFollowedByOpeningCurlyBracket(If_|ElseIf_|Else_ $if, array $oldTokens): bool
+ private function shouldSkip(If_|ElseIf_|Else_ $if, array $oldTokens): bool
{
for ($i = $if->getStartTokenPos(); $i < $if->getEndTokenPos(); ++$i) {
- if ($oldTokens[$i] !== ')') {
- if ($oldTokens[$i] === ';') {
- // all good
- return true;
- }
-
- continue;
- }
-
- // first closing bracket must be followed by curly opening brackets
- // what is next token?
- $nextToken = $oldTokens[$i + 1];
-
- if (is_array($nextToken) && trim((string) $nextToken[1]) === '') {
- // next token is whitespace
- $nextToken = $oldTokens[$i + 2];
- }
-
- if (in_array($nextToken, ['{', ':'], true)) {
+ if ($oldTokens[$i] === ';') {
// all good
return true;
}
}
- return false;
+ $startStmt = current($if->stmts);
+ $lastStmt = end($if->stmts);
+ return $startStmt === false || $lastStmt === false;
}
private function isBareNewNode(If_|ElseIf_|Else_ $if): bool