Skip to content

Commit dffc68a

Browse files
Khartirclaude
authored andcommitted
Fix AlphabeticalUseStatements sorting trait use statements
The sniff was incorrectly processing trait use statements inside class/trait/enum bodies. The parent class's private shouldIgnoreUse() filtered them out, but the child class continued executing its own sorting logic. Add a hasCondition() check to also ignore trait use. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 741e2b8 commit dffc68a

4 files changed

Lines changed: 40 additions & 21 deletions

File tree

MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ public function process(File $phpcsFile, $stackPtr): void
106106

107107
parent::process($phpcsFile, $stackPtr);
108108

109+
if (true === $this->checkIsNonImportUse($phpcsFile, $stackPtr)) {
110+
return;
111+
}
112+
109113
if ($this->currentFile !== $phpcsFile->getFilename()) {
110114
$this->lastLine = -1;
111115
$this->lastImport = '';
@@ -115,13 +119,6 @@ public function process(File $phpcsFile, $stackPtr): void
115119
$tokens = $phpcsFile->getTokens();
116120
$line = $tokens[$stackPtr]['line'];
117121

118-
// Ignore function () use () {...}.
119-
$isNonImportUse = $this->checkIsNonImportUse($phpcsFile, $stackPtr);
120-
121-
if (true === $isNonImportUse) {
122-
return;
123-
}
124-
125122
$currentImportArr = $this->getUseImport($phpcsFile, $stackPtr);
126123

127124
if (false === $currentImportArr) {
@@ -235,21 +232,14 @@ private function checkIsNonImportUse(File $phpcsFile, int $stackPtr): bool
235232
{
236233
$tokens = $phpcsFile->getTokens();
237234

238-
$prev = $phpcsFile->findPrevious(
239-
PHP_CodeSniffer_Tokens::EMPTY_TOKENS,
240-
($stackPtr - 1),
241-
0,
242-
true,
243-
null,
244-
true
245-
);
246-
247-
if (false !== $prev) {
248-
$prevToken = $tokens[$prev];
235+
// Ignore USE keywords for closures.
236+
if (true === isset($tokens[$stackPtr]['parenthesis_owner'])) {
237+
return true;
238+
}
249239

250-
if (T_CLOSE_PARENTHESIS === $prevToken['code']) {
251-
return true;
252-
}
240+
// Ignore USE keywords for traits (inside class/trait/enum bodies).
241+
if (true === $phpcsFile->hasCondition($stackPtr, [T_CLASS, T_TRAIT, T_ENUM])) {
242+
return true;
253243
}
254244

255245
return false;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use A;
4+
use B;
5+
6+
class Foo
7+
{
8+
use ZebraTrait;
9+
use AlphaTrait;
10+
}
11+
12+
class Bar
13+
{
14+
/** @use GenericTrait<string> */
15+
use GenericTrait;
16+
/** @use AnotherTrait<int> */
17+
use AnotherTrait;
18+
}
19+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
use A;
4+
use B;
5+
6+
$closure = function () use ($b, $a) {
7+
return $a + $b;
8+
};

MO4/Tests/Formatting/AlphabeticalUseStatementsUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ final class AlphabeticalUseStatementsUnitTest extends AbstractMo4SniffUnitTest
3535
protected $expectedErrorList = [
3636
'AlphabeticalUseStatementsUnitTest.pass.inc' => [],
3737
'AlphabeticalUseStatementsUnitTest.pass.1.inc' => [],
38+
'AlphabeticalUseStatementsUnitTest.pass.2.inc' => [],
39+
'AlphabeticalUseStatementsUnitTest.pass.3.inc' => [],
3840
'AlphabeticalUseStatementsUnitTest.fail.1.inc' => [
3941
4 => 1,
4042
5 => 1,

0 commit comments

Comments
 (0)