diff --git a/PSR2R/Sniffs/Namespaces/UseInAlphabeticalOrderSniff.php b/PSR2R/Sniffs/Namespaces/UseInAlphabeticalOrderSniff.php deleted file mode 100644 index 1627c01..0000000 --- a/PSR2R/Sniffs/Namespaces/UseInAlphabeticalOrderSniff.php +++ /dev/null @@ -1,165 +0,0 @@ -_processed[$phpcsFile->getFilename()])) { - return; - } - - $this->_uses = []; - $next = $stackPtr; - - while ($next !== false) { - $this->checkUseToken($phpcsFile, $next); - $next = $phpcsFile->findNext(T_USE, $next + 1); - } - - // Prevent multiple uses in the same file from entering - $this->_processed[$phpcsFile->getFilename()] = true; - - foreach ($this->_uses as $scope => $used) { - $defined = $sorted = array_keys($used); - - natcasesort($sorted); - $sorted = array_values($sorted); - if ($sorted === $defined) { - continue; - } - - $wrongName = null; - foreach ($defined as $i => $name) { - if ($name !== $sorted[$i]) { - $wrongName = $name; - - break; - } - } - - $error = 'Use classes must be in alphabetical order.'; - $fix = $phpcsFile->addFixableError($error, $used[$wrongName], 'Order', []); - if ($fix) { - $map = []; - foreach ($sorted as $name) { - $tokenIndex = array_shift($used); - $tokenIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $tokenIndex + 1, null, true); - $map[$tokenIndex] = $name; - } - - $phpcsFile->fixer->beginChangeset(); - - foreach ($map as $index => $name) { - $phpcsFile->fixer->replaceToken($index, $name); - $endIndex = $phpcsFile->findNext([T_SEMICOLON, T_OPEN_CURLY_BRACKET], $index + 1); - for ($i = $index + 1; $i < $endIndex; $i++) { - $phpcsFile->fixer->replaceToken($i, ''); - } - } - - $phpcsFile->fixer->endChangeset(); - } - } - } - - /** - * Check all the use tokens in a file. - * - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file to check. - * @param int $stackPtr The index of the first use token. -* - * @return void - */ - protected function checkUseToken(File $phpcsFile, $stackPtr) { - // If the use token is for a closure we want to ignore it. - $isClosure = $this->isClosure($phpcsFile, $stackPtr); - if ($isClosure) { - return; - } - - $tokens = $phpcsFile->getTokens(); - - $content = ''; - $startIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true); - $endIndex = $phpcsFile->findNext([T_SEMICOLON, T_OPEN_CURLY_BRACKET], $startIndex + 1); - - for ($i = $startIndex; $i < $endIndex; $i++) { - $content .= $tokens[$i]['content']; - } - - // Check for class scoping on use. Traits should be - // ordered independently. - $scope = 0; - if (!empty($tokens[$i]['conditions'])) { - $scope = key($tokens[$i]['conditions']); - } - $this->_uses[$scope][$content] = $stackPtr; - } - - /** - * Check if the current stackPtr is a use token that is for a closure. - * - * @param \PHP_CodeSniffer\Files\File $phpcsFile - * @param int $stackPtr -* - * @return bool - */ - protected function isClosure(File $phpcsFile, $stackPtr) { - return $phpcsFile->findPrevious( - [T_CLOSURE], - $stackPtr - 1, - null, - false, - null, - true - ); - } - -} diff --git a/PSR2R/ruleset.xml b/PSR2R/ruleset.xml index 888eb20..4d7db2a 100644 --- a/PSR2R/ruleset.xml +++ b/PSR2R/ruleset.xml @@ -77,7 +77,6 @@ - @@ -162,6 +161,7 @@ +