diff --git a/CHANGELOG.md b/CHANGELOG.md index 78060241..0f8d38de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,7 +44,7 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed -- Nothing. +- [#139](https://github.com/webimpress/coding-standard/pull/139) fixes calculating spaces before double arrow in arrays - `Arrays\DoubleArrow` sniff. ## 1.2.0 - 2020-11-27 diff --git a/src/WebimpressCodingStandard/Sniffs/Arrays/DoubleArrowSniff.php b/src/WebimpressCodingStandard/Sniffs/Arrays/DoubleArrowSniff.php index b65c2528..583f754b 100644 --- a/src/WebimpressCodingStandard/Sniffs/Arrays/DoubleArrowSniff.php +++ b/src/WebimpressCodingStandard/Sniffs/Arrays/DoubleArrowSniff.php @@ -161,29 +161,29 @@ private function calculateExpectedSpaces(File $phpcsFile, array $indices) : arra $res[$i] = $current; $min = $length; $current = $length; - } - - if ($k > 0) { - $valueEnd = $phpcsFile->findPrevious( - Tokens::$emptyTokens, - $indices[$k]['index_start'] - 1, - null, - true - ); + } else { + if ($k > 0) { + $valueEnd = $phpcsFile->findPrevious( + Tokens::$emptyTokens, + $indices[$k]['index_start'] - 1, + null, + true + ); + + if ($valueEnd && $tokens[$valueEnd]['line'] !== $tokens[$indices[$k]['index_start']]['line'] - 1) { + $res[$i] = $current; + $min = $length; + $current = $length; + } + } - if ($valueEnd && $tokens[$valueEnd]['line'] < $tokens[$indices[$k]['index_start']]['line'] - 1) { - $res[$i] = $current; + if ($length < $min) { $min = $length; - $current = $length; } - } - if ($length < $min) { - $min = $length; - } - - if ($length > $current) { - $current = $length; + if ($length > $current) { + $current = $length; + } } if (! isset($chars[$k + 1])) { diff --git a/test/Sniffs/Arrays/DoubleArrowUnitTest.1.inc b/test/Sniffs/Arrays/DoubleArrowUnitTest.1.inc index 1eed88a8..55b7be9e 100644 --- a/test/Sniffs/Arrays/DoubleArrowUnitTest.1.inc +++ b/test/Sniffs/Arrays/DoubleArrowUnitTest.1.inc @@ -62,3 +62,9 @@ $x = [ // comment '8_____' => 'comment breaks alignment group', ]; + +$charMap = [ + 'a' => 'A', 'b' => 'B', + // Comment + 'c' => 'C', 'd' => 'D', +]; diff --git a/test/Sniffs/Arrays/DoubleArrowUnitTest.1.inc.fixed b/test/Sniffs/Arrays/DoubleArrowUnitTest.1.inc.fixed index 48f9fdec..602e7f4f 100644 --- a/test/Sniffs/Arrays/DoubleArrowUnitTest.1.inc.fixed +++ b/test/Sniffs/Arrays/DoubleArrowUnitTest.1.inc.fixed @@ -62,3 +62,9 @@ $x = [ // comment '8_____' => 'comment breaks alignment group', ]; + +$charMap = [ + 'a' => 'A', 'b' => 'B', + // Comment + 'c' => 'C', 'd' => 'D', +];