From 197aba544f3624c7c50de345de104fcdde85173c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Thu, 7 Nov 2019 14:02:59 +0000 Subject: [PATCH 1/2] Hotfix: Namespaces\AlphabeticallySortedUses - sort uses also in files without namespace Before this sniff worked only on files with namespaces. We'd like to sort imports also in files without namespace defined. --- .../AlphabeticallySortedUsesSniff.php | 20 +++++++++-- .../AlphabeticallySortedUsesUnitTest.2.inc | 31 +++++++++++++++++ ...phabeticallySortedUsesUnitTest.2.inc.fixed | 33 +++++++++++++++++++ .../AlphabeticallySortedUsesUnitTest.php | 4 +++ 4 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 test/Sniffs/Namespaces/AlphabeticallySortedUsesUnitTest.2.inc create mode 100644 test/Sniffs/Namespaces/AlphabeticallySortedUsesUnitTest.2.inc.fixed 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 [ From dfa6682d89137f5f2c793673752073e5b5f0b687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Mon, 11 Nov 2019 13:20:22 +0000 Subject: [PATCH 2/2] Adds CHANGELOG entry for #55 --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb993811..24b789c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,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