diff --git a/CHANGELOG.md b/CHANGELOG.md index a2ead2c3..a5ed5d1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -78,6 +78,8 @@ All notable changes to this project will be documented in this file, in reverse - [#40](https://github.com/webimpress/coding-standard/pull/40) fixes `PHP\RedundantSemicolon` sniff to remove redundant semicolon after colon and goto label +- [#55](https://github.com/webimpress/coding-standard/pull/55) fixes `Namespaces\AlphabeticallySortedUses` sniff to work with files without namespaces + ## 1.0.5 - 2019-10-06 ### Added diff --git a/src/WebimpressCodingStandard/Sniffs/Namespaces/AlphabeticallySortedUsesSniff.php b/src/WebimpressCodingStandard/Sniffs/Namespaces/AlphabeticallySortedUsesSniff.php index 51ac62da..ca8f5af5 100644 --- a/src/WebimpressCodingStandard/Sniffs/Namespaces/AlphabeticallySortedUsesSniff.php +++ b/src/WebimpressCodingStandard/Sniffs/Namespaces/AlphabeticallySortedUsesSniff.php @@ -20,6 +20,7 @@ use const T_NAMESPACE; use const T_NS_SEPARATOR; +use const T_OPEN_TAG; use const T_SEMICOLON; use const T_STRING; use const T_USE; @@ -31,17 +32,26 @@ class AlphabeticallySortedUsesSniff implements Sniff */ public function register() : array { - return [T_NAMESPACE]; + return [T_OPEN_TAG, T_NAMESPACE]; } /** * @param int $stackPtr + * @return int */ public function process(File $phpcsFile, $stackPtr) { - $uses = $this->getUseStatements($phpcsFile, $stackPtr); $tokens = $phpcsFile->getTokens(); + if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) { + $namespace = $phpcsFile->findNext(T_NAMESPACE, $stackPtr + 1); + if ($namespace) { + return $namespace; + } + } + + $uses = $this->getUseStatements($phpcsFile, $stackPtr); + $lastUse = null; foreach ($uses as $use) { if (! $lastUse) { @@ -61,7 +71,7 @@ public function process(File $phpcsFile, $stackPtr) $this->fixAlphabeticalOrder($phpcsFile, $uses); } - return; + return $stackPtr + 1; } // Check empty lines between use statements. @@ -130,6 +140,10 @@ public function process(File $phpcsFile, $stackPtr) $lastUse = $use; } + + return $tokens[$stackPtr]['code'] === T_OPEN_TAG + ? $phpcsFile->numTokens + 1 + : $stackPtr + 1; } /** diff --git a/test/Sniffs/Namespaces/AlphabeticallySortedUsesUnitTest.2.inc b/test/Sniffs/Namespaces/AlphabeticallySortedUsesUnitTest.2.inc new file mode 100644 index 00000000..fb192fb9 --- /dev/null +++ b/test/Sniffs/Namespaces/AlphabeticallySortedUsesUnitTest.2.inc @@ -0,0 +1,31 @@ + $foo; + }; + } + + public function anonym() + { + return new class() { + use AnotherTrait; + }; + } +} diff --git a/test/Sniffs/Namespaces/AlphabeticallySortedUsesUnitTest.2.inc.fixed b/test/Sniffs/Namespaces/AlphabeticallySortedUsesUnitTest.2.inc.fixed new file mode 100644 index 00000000..4224c3ad --- /dev/null +++ b/test/Sniffs/Namespaces/AlphabeticallySortedUsesUnitTest.2.inc.fixed @@ -0,0 +1,33 @@ + $foo; + }; + } + + public function anonym() + { + return new class() { + use AnotherTrait; + }; + } +} diff --git a/test/Sniffs/Namespaces/AlphabeticallySortedUsesUnitTest.php b/test/Sniffs/Namespaces/AlphabeticallySortedUsesUnitTest.php index d26d1daf..b3c1b021 100644 --- a/test/Sniffs/Namespaces/AlphabeticallySortedUsesUnitTest.php +++ b/test/Sniffs/Namespaces/AlphabeticallySortedUsesUnitTest.php @@ -15,6 +15,10 @@ protected function getErrorList(string $testFile = '') : array return [ 6 => 1, ]; + case 'AlphabeticallySortedUsesUnitTest.2.inc': + return [ + 4 => 1, + ]; } return [