From 858cd04ec07e39fb6d514e9e37563adac9d399cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dariusz=20Rumi=C5=84ski?= Date: Sat, 30 Dec 2023 03:05:27 +0100 Subject: [PATCH] fix: NoUnneededBracesFixer - improve handling of global namespace (#7639) Co-authored-by: Greg Korba --- .../NoUnneededBracesFixer.php | 12 +++++++++-- .../NoUnneededBracesFixerTest.php | 21 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Fixer/ControlStructure/NoUnneededBracesFixer.php b/src/Fixer/ControlStructure/NoUnneededBracesFixer.php index 281eca1621c..3d1e72bc4c6 100644 --- a/src/Fixer/ControlStructure/NoUnneededBracesFixer.php +++ b/src/Fixer/ControlStructure/NoUnneededBracesFixer.php @@ -134,9 +134,17 @@ private function clearIfIsOverCompleteNamespaceBlock(Tokens $tokens): void $index = $tokens->getNextTokenOfKind(0, [[T_NAMESPACE]]); - do { + $namespaceIsNamed = false; + + $index = $tokens->getNextMeaningfulToken($index); + while ($tokens[$index]->isGivenKind([T_STRING, T_NS_SEPARATOR])) { $index = $tokens->getNextMeaningfulToken($index); - } while ($tokens[$index]->isGivenKind([T_STRING, T_NS_SEPARATOR])); + $namespaceIsNamed = true; + } + + if (!$namespaceIsNamed) { + return; + } if (!$tokens[$index]->equals('{')) { return; // `;` diff --git a/tests/Fixer/ControlStructure/NoUnneededBracesFixerTest.php b/tests/Fixer/ControlStructure/NoUnneededBracesFixerTest.php index e78e1ad77e3..93d5f8b2b68 100644 --- a/tests/Fixer/ControlStructure/NoUnneededBracesFixerTest.php +++ b/tests/Fixer/ControlStructure/NoUnneededBracesFixerTest.php @@ -182,6 +182,27 @@ function Bar(){} } ?>", ['namespaces' => true], ]; + + yield [ + ' true], + ]; + + yield [ + ' true], + ]; } /**