Commit 0b2dc229 authored by Mitch Portier's avatar Mitch Portier Committed by Klaus Purer
Browse files

feat(Array): Add configurable line limit to Array sniff (#3099288 by Arkener)

parent 541558b8
Loading
Loading
Loading
Loading
+12 −3
Original line number Diff line number Diff line
@@ -28,6 +28,14 @@ class ArraySniff implements Sniff
{


    /**
     * The limit that the length of a line should not exceed.
     *
     * @var integer
     */
    public $lineLimit = 80;


    /**
     * Returns an array of tokens this test wants to listen for.
     *
@@ -103,13 +111,14 @@ class ArraySniff implements Sniff
        if ($isInlineArray === true) {
            // Check if this array contains at least 3 elements and exceeds the 80
            // character line length.
            if ($tokens[$tokens[$stackPtr][$parenthesisCloser]]['column'] > 80) {
            if ($tokens[$tokens[$stackPtr][$parenthesisCloser]]['column'] > $this->lineLimit) {
                $comma1 = $phpcsFile->findNext(T_COMMA, ($stackPtr + 1), $tokens[$stackPtr][$parenthesisCloser]);
                if ($comma1 !== false) {
                    $comma2 = $phpcsFile->findNext(T_COMMA, ($comma1 + 1), $tokens[$stackPtr][$parenthesisCloser]);
                    if ($comma2 !== false) {
                        $error = 'If the line declaring an array spans longer than 80 characters, each element should be broken into its own line';
                        $phpcsFile->addError($error, $stackPtr, 'LongLineDeclaration');
                        $error = 'If the line declaring an array spans longer than %s characters, each element should be broken into its own line';
                        $data  = [$this->lineLimit];
                        $phpcsFile->addError($error, $stackPtr, 'LongLineDeclaration', $data);
                    }
                }
            }
+18 −0
Original line number Diff line number Diff line
<?php
/**
 * @file
 * Tests array declarations.
 * phpcs:set Drupal.Arrays.Array lineLimit 100
 */

$array = array(
  'data' => 'my-data',
  'animal' => 'squirrel',
  'inline' => array(),
  'inline1' => array('thisisaverylongstring', 'thisisaverylongstring'),
  'inline2' => array('thisisaverylongstring', 'thisisaverylongstring', 'thisisaverylongstring'),
  'inline3' => array('thisisaverylongstring', 'thisisaverylongstring', 'thisisaverylongstring', 'thisisaverylongstring'),
  'inline4' => array('thisisaverylongstringwithallotoftext', 'thisisaverylongstringwithallotoftext'),
  'inline_long_ok' => array('one', 'two', 'three', 'four', 'five', 'six', 'seven'),
  'inline_long_nok' => array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'),
);
+37 −20
Original line number Diff line number Diff line
@@ -14,10 +14,14 @@ class ArrayUnitTest extends CoderSniffUnitTest
     * The key of the array should represent the line number and the value
     * should represent the number of errors that should occur on that line.
     *
     * @param string $testFile The name of the file being tested.
     *
     * @return array(int => int)
     */
    public function getErrorList()
    public function getErrorList($testFile=null)
    {
        switch ($testFile) {
        case 'ArrayUnitTest.inc':
            return [
                13 => 1,
                33 => 1,
@@ -25,6 +29,12 @@ class ArrayUnitTest extends CoderSniffUnitTest
                88 => 1,
                92 => 1,
            ];
        case 'ArrayUnitTest.1.inc':
            return [
                14 => 1,
                17 => 1,
            ];
        }

    }//end getErrorList()

@@ -35,10 +45,14 @@ class ArrayUnitTest extends CoderSniffUnitTest
     * The key of the array should represent the line number and the value
     * should represent the number of warnings that should occur on that line.
     *
     * @param string $testFile The name of the file being tested.
     *
     * @return array(int => int)
     */
    public function getWarningList()
    public function getWarningList($testFile=null)
    {
        switch ($testFile) {
        case 'ArrayUnitTest.inc':
            return [
                17 => 1,
                22 => 1,
@@ -50,6 +64,9 @@ class ArrayUnitTest extends CoderSniffUnitTest
                59 => 1,
                76 => 1,
            ];
        case 'ArrayUnitTest.1.inc':
            return [];
        }

    }//end getWarningList()