From c9c072fdb244190c42abcfa36274aa3be9792300 Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Fri, 20 Aug 2021 12:44:19 +0200 Subject: [PATCH 1/2] Prevent ImportsFromTestNamespaceSniff from hanging when group use declaration has trailing coma. If a grouped use statement has a trailing coma like this: ``` use Magento\Something{ Foo, Bar, }; ``` `findNext` on line 59 was returning false and next call was starting to search again from line 1 (`$next + 1`) and this function was looping indefinitely through input tokens. --- .../Namespaces/ImportsFromTestNamespaceSniff.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php b/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php index 3e5dbce7..1266c0dc 100644 --- a/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php +++ b/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php @@ -57,11 +57,13 @@ public function process(File $phpcsFile, $stackPtr) $closingCurly = $phpcsFile->findNext(T_CLOSE_USE_GROUP, ($next + 1)); do { $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), $closingCurly, true); - $groupedAsContent = $baseUse. $tokens[$next]['content']; - $next = $phpcsFile->findNext(T_COMMA, ($next + 1), $closingCurly); - if (strpos($groupedAsContent, $this->prohibitNamespace) !== false) { - $phpcsFile->addWarning($this->warningMessage, $stackPtr, $this->warningCode); - return; + if ($next !== false) { + $groupedAsContent = $baseUse. $tokens[$next]['content']; + $next = $phpcsFile->findNext(T_COMMA, ($next + 1), $closingCurly); + if (strpos($groupedAsContent, $this->prohibitNamespace) !== false) { + $phpcsFile->addWarning($this->warningMessage, $stackPtr, $this->warningCode); + return; + } } } while ($next !== false); } From 696c3aae04492499c249c4c0340bd0d64021d742 Mon Sep 17 00:00:00 2001 From: Franciszek Szczepan Wawrzak Date: Fri, 20 Aug 2021 14:26:06 +0200 Subject: [PATCH 2/2] add test case for #220 --- Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.inc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.inc b/Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.inc index 50d339a1..b3ca9957 100644 --- a/Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.inc +++ b/Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.inc @@ -3,4 +3,5 @@ use Magento\Tests; use Magento\Foo\Tests as FakeTest; use Magento\Real\Classes; use \Magento\{Tests\String, Tests\Int}; -use \Magento\{Foo\string, Bar\float}; \ No newline at end of file +use \Magento\{Foo\string, Bar\float}; +use \Foo\{Trailing, Space,}; \ No newline at end of file