Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 11 additions & 21 deletions MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ public function process(File $phpcsFile, $stackPtr): void

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

if (true === $this->checkIsNonImportUse($phpcsFile, $stackPtr)) {
return;
}

if ($this->currentFile !== $phpcsFile->getFilename()) {
$this->lastLine = -1;
$this->lastImport = '';
Expand All @@ -115,13 +119,6 @@ public function process(File $phpcsFile, $stackPtr): void
$tokens = $phpcsFile->getTokens();
$line = $tokens[$stackPtr]['line'];

// Ignore function () use () {...}.
$isNonImportUse = $this->checkIsNonImportUse($phpcsFile, $stackPtr);

if (true === $isNonImportUse) {
return;
}

$currentImportArr = $this->getUseImport($phpcsFile, $stackPtr);

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

$prev = $phpcsFile->findPrevious(
PHP_CodeSniffer_Tokens::EMPTY_TOKENS,
($stackPtr - 1),
0,
true,
null,
true
);

if (false !== $prev) {
$prevToken = $tokens[$prev];
// Ignore USE keywords for closures.
if (true === isset($tokens[$stackPtr]['parenthesis_owner'])) {
return true;
}

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

return false;
Expand Down
19 changes: 19 additions & 0 deletions MO4/Tests/Formatting/AlphabeticalUseStatementsUnitTest.pass.2.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use A;
use B;

class Foo
{
use ZebraTrait;
use AlphaTrait;
}

class Bar
{
/** @use GenericTrait<string> */
use GenericTrait;
/** @use AnotherTrait<int> */
use AnotherTrait;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use A;
use B;

$closure = function () use ($b, $a) {
return $a + $b;
};
2 changes: 2 additions & 0 deletions MO4/Tests/Formatting/AlphabeticalUseStatementsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ final class AlphabeticalUseStatementsUnitTest extends AbstractMo4SniffUnitTest
protected $expectedErrorList = [
'AlphabeticalUseStatementsUnitTest.pass.inc' => [],
'AlphabeticalUseStatementsUnitTest.pass.1.inc' => [],
'AlphabeticalUseStatementsUnitTest.pass.2.inc' => [],
'AlphabeticalUseStatementsUnitTest.pass.3.inc' => [],
'AlphabeticalUseStatementsUnitTest.fail.1.inc' => [
4 => 1,
5 => 1,
Expand Down
Loading