Unverified Commit dcdf6578 authored by Jonathan Smith's avatar Jonathan Smith Committed by GitHub
Browse files

fix(Array): Use $parenthesisCloser column instead of full line length (#3169452 by jonathan1055)

parent 5e23744d
Loading
Loading
Loading
Loading
+5 −10
Original line number Diff line number Diff line
@@ -113,18 +113,13 @@ class ArraySniff implements Sniff
        if ($isInlineArray === true) {
            // Check if this array has more than one element and exceeds the
            // line length defined by $this->lineLimit.
            $currentLine = $tokens[$stackPtr]['line'];
            $tokenCount  = $stackPtr;
            while ($tokenCount < ($phpcsFile->numTokens - 1) && $tokens[($tokenCount + 1)]['line'] === $currentLine) {
                $tokenCount++;
            };
            $lineLength = ($tokens[$tokenCount]['column'] + $tokens[$tokenCount]['length'] - 1);

            if ($lineLength > $this->lineLimit) {
            $arrayEnding = $tokens[$tokens[$stackPtr][$parenthesisCloser]]['column'];

            if ($arrayEnding > $this->lineLimit) {
                $comma1 = $phpcsFile->findNext(T_COMMA, ($stackPtr + 1), $tokens[$stackPtr][$parenthesisCloser]);
                if ($comma1 !== false) {
                    $error = 'The array declaration line has %s characters (the limit is %s). The array content should be split up over multiple lines';
                    $phpcsFile->addError($error, $stackPtr, 'LongLineDeclaration', [$lineLength, $this->lineLimit]);
                    $error = 'The array declaration extends to column %s (the limit is %s). The array content should be split up over multiple lines';
                    $phpcsFile->addError($error, $stackPtr, 'LongLineDeclaration', [$arrayEnding, $this->lineLimit]);
                }
            }

+4 −0
Original line number Diff line number Diff line
@@ -18,4 +18,8 @@ $array = array(
  'inline_two_elements_ok' => array('one-two-three', 'the-2nd-element-is-within-the-limit'),
  'inline_two_elements_ok2' => array('one-two-three-four', 'the-2nd-element-is-right-on-the-limit'),
  'inline_two_elements_not_ok' => array('one-two-three-four-five', 'the-2nd-element-extends-beyond-the-limit'),
  'inline_two_elements_ok3' => func(['one-two-three-four', 'five'], 'other text which goes past the limit'),
  'inline_two_elements_ok4' => func(['one-two-three-four', 'this-2nd-element-is-right-on-the-limit'], 'other text'),
  'inline_two_elements_ok5' => func(['one-two-three-four'], ['second_array' => 'this-is-ok'], 'other text'),
  'inline_two_elements_not_ok' => func(['one-two'], ['second_array' => 'three', 'four-five' => 'six'], 'other text'),
);
+1 −1
Original line number Diff line number Diff line
@@ -32,9 +32,9 @@ class ArrayUnitTest extends CoderSniffUnitTest
        case 'ArrayUnitTest.1.inc':
            return [
                14 => 1,
                15 => 1,
                17 => 1,
                20 => 1,
                24 => 1,
            ];
        }