Skip to content

Commit

Permalink
fix: NoUnneededBracesFixer - improve handling of global namespace (PH…
Browse files Browse the repository at this point in the history
…P-CS-Fixer#7639)

Co-authored-by: Greg Korba <[email protected]>
  • Loading branch information
keradus and Wirone authored Dec 30, 2023
1 parent 99ea823 commit 858cd04
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Fixer/ControlStructure/NoUnneededBracesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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; // `;`
Expand Down
21 changes: 21 additions & 0 deletions tests/Fixer/ControlStructure/NoUnneededBracesFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,27 @@ function Bar(){}
} ?>",
['namespaces' => true],
];

yield [
'<?php
namespace A;
class X {}
',
'<?php
namespace A {
class X {}
}',
['namespaces' => true],
];

yield [
'<?php
namespace {
class X {}
}',
null,
['namespaces' => true],
];
}

/**
Expand Down

0 comments on commit 858cd04

Please sign in to comment.