Skip to content
Snippets Groups Projects
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
Branches
Tags
No related merge requests found
......@@ -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);
}
}
}
......
<?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'),
);
......@@ -14,17 +14,27 @@ 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)
{
return [
13 => 1,
33 => 1,
83 => 1,
88 => 1,
92 => 1,
];
switch ($testFile) {
case 'ArrayUnitTest.inc':
return [
13 => 1,
33 => 1,
83 => 1,
88 => 1,
92 => 1,
];
case 'ArrayUnitTest.1.inc':
return [
14 => 1,
17 => 1,
];
}
}//end getErrorList()
......@@ -35,21 +45,28 @@ 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)
{
return [
17 => 1,
22 => 1,
23 => 1,
24 => 1,
37 => 1,
42 => 1,
44 => 1,
59 => 1,
76 => 1,
];
switch ($testFile) {
case 'ArrayUnitTest.inc':
return [
17 => 1,
22 => 1,
23 => 1,
24 => 1,
37 => 1,
42 => 1,
44 => 1,
59 => 1,
76 => 1,
];
case 'ArrayUnitTest.1.inc':
return [];
}
}//end getWarningList()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment