From 632c69bae6c8e28bc020c74491ed6abc70c7a773 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Wed, 5 Jun 2019 13:57:11 -0500 Subject: [PATCH] [Bug] ForeachArrayMergeSniff throws an error for inline control structure - fixed magento-coding-standard#110 --- Magento2/Sniffs/Performance/ForeachArrayMergeSniff.php | 4 ++++ Magento2/Tests/Performance/ForeachArrayMergeUnitTest.inc | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/Magento2/Sniffs/Performance/ForeachArrayMergeSniff.php b/Magento2/Sniffs/Performance/ForeachArrayMergeSniff.php index eae75006..e478638d 100644 --- a/Magento2/Sniffs/Performance/ForeachArrayMergeSniff.php +++ b/Magento2/Sniffs/Performance/ForeachArrayMergeSniff.php @@ -47,6 +47,10 @@ public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); + // If it's inline control structure we do nothing. PSR2 issue will be raised. + if (!array_key_exists('scope_opener', $tokens[$stackPtr])) { + return; + } $scopeOpener = $tokens[$stackPtr]['scope_opener']; $scopeCloser = $tokens[$stackPtr]['scope_closer']; diff --git a/Magento2/Tests/Performance/ForeachArrayMergeUnitTest.inc b/Magento2/Tests/Performance/ForeachArrayMergeUnitTest.inc index 02aa5093..b55690b5 100644 --- a/Magento2/Tests/Performance/ForeachArrayMergeUnitTest.inc +++ b/Magento2/Tests/Performance/ForeachArrayMergeUnitTest.inc @@ -41,3 +41,11 @@ foreach ([] as $collection) { $selectBuilder->setColumns(array_merge($selectBuilder->getColumns(), $source)); } } + +foreach ([] as $item) + // inline foreach + $a = array_merge([], []); + +foreach ([] as $item) +// array_merge after inline foreach +$a = array_merge([], []);