Skip to content

Commit 0b7f9af

Browse files
authored
feat(Array): Allow array definition lines up to 120 characters for better readability (#3185082)
1 parent 7a35274 commit 0b7f9af

File tree

6 files changed

+62
-8
lines changed

6 files changed

+62
-8
lines changed

coder_sniffer/Drupal/Sniffs/Arrays/ArraySniff.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ class ArraySniff implements Sniff
3131
/**
3232
* The limit that the length of a line should not exceed.
3333
*
34-
* This can be configured to have a different value but the default is 80.
34+
* This can be configured to have a different value but the default is 120.
35+
* We don't enforce 80 characters by default because that can make array
36+
* definitions in nested arrays or function calls less readable.
3537
*
3638
* @var integer
3739
*/
38-
public $lineLimit = 80;
40+
public $lineLimit = 120;
3941

4042

4143
/**

tests/Drupal/Arrays/ArrayUnitTest.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ $array = [
1010
'inline' => [],
1111
'inline3' => ['one', 'two', 'three'],
1212
'inline_long_ok' => ['one', 'two', 'three', 'four', 'five'],
13-
'inline_long_nok' => ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'],
13+
'inline_long_nok' => ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'],
1414
'nested' => [
1515
],
1616
'nested2' => [
@@ -30,7 +30,7 @@ $array = array(
3030
'inline' => array(),
3131
'inline3' => array('one', 'two', 'three'),
3232
'inline_long_ok' => array('one', 'two', 'three', 'four', 'five'),
33-
'inline_long_nok' => array('one', 'two', 'three', 'four', 'five', 'six', 'seven'),
33+
'inline_long_nok' => array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'),
3434
'nested' => array(
3535
),
3636
'nested2' => array(

tests/Drupal/Arrays/ArrayUnitTest.inc.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ $array = [
1111
'inline' => [],
1212
'inline3' => ['one', 'two', 'three'],
1313
'inline_long_ok' => ['one', 'two', 'three', 'four', 'five'],
14-
'inline_long_nok' => ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'],
14+
'inline_long_nok' => ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'],
1515
'nested' => [],
1616
'nested2' => [
1717
'a',
@@ -30,7 +30,7 @@ $array = array(
3030
'inline' => array(),
3131
'inline3' => array('one', 'two', 'three'),
3232
'inline_long_ok' => array('one', 'two', 'three', 'four', 'five'),
33-
'inline_long_nok' => array('one', 'two', 'three', 'four', 'five', 'six', 'seven'),
33+
'inline_long_nok' => array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'),
3434
'nested' => array(),
3535
'nested2' => array(
3636
'a',

tests/Drupal/bad/bad.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@
172172
);
173173

174174
// Single line array declaration is too long.
175-
$page_options = array('home' => t('Front Page'), 'all' => t('All Pages'), 'list' => t('List Pages'));
175+
$page_options = array('home' => t('Front Page'), 'all' => t('All Pages'), 'list' => t('List Pages'), 'delete' => t('Delete'));
176176

177177
// Item assignment operators must be prefixed and followed by a space
178178
$a = array('one'=>'1');

tests/Drupal/bad/bad.php.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ $a = array(
174174
);
175175

176176
// Single line array declaration is too long.
177-
$page_options = array('home' => t('Front Page'), 'all' => t('All Pages'), 'list' => t('List Pages'));
177+
$page_options = array('home' => t('Front Page'), 'all' => t('All Pages'), 'list' => t('List Pages'), 'delete' => t('Delete'));
178178

179179
// Item assignment operators must be prefixed and followed by a space.
180180
$a = array('one' => '1');

tests/Drupal/good/good.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,3 +1827,55 @@ enum MyWellNamedEnum: int {
18271827
case One = 1;
18281828
case Two = 2;
18291829
}
1830+
1831+
// Allow longer than 80 character array definitions on one line (up to 120).
1832+
if (!in_array($form_object->getOperation(), ['add', 'edit', 'delete', 'clone', 'default'], TRUE)) {
1833+
return;
1834+
}
1835+
if ($content_type_facet_field && in_array($entity_type, ['entity:node', 'entity:media'])) {
1836+
$foo->setContentTypeFacetValues($item, $entity_type, $content_type_facet_field);
1837+
}
1838+
1839+
/**
1840+
* Testing nested array definitions going over 80 characters.
1841+
*/
1842+
class LongNestedArrayLine {
1843+
1844+
/**
1845+
* Test method.
1846+
*/
1847+
public function foo() {
1848+
foreach ($x as $y) {
1849+
foreach ($a as $b) {
1850+
$form[$policyTypeKey]['directives'][$directiveName]['options']['flags_wrapper']['flags'] = [
1851+
'#type' => 'checkboxes',
1852+
'#parents' => [$policyTypeKey, 'directives', $directiveName, 'flags', 'humans'],
1853+
'#options' => [
1854+
'unsafe-inline' => "'unsafe-inline'",
1855+
'unsafe-eval' => "'unsafe-eval'",
1856+
'unsafe-hashes' => "'unsafe-hashes'",
1857+
'unsafe-allow-redirects' => "'unsafe-allow-redirects'",
1858+
'strict-dynamic' => "'strict-dynamic'",
1859+
'report-sample' => "'report-sample'",
1860+
],
1861+
'#default_value' => $config->get($policyTypeKey . '.directives.' . $directiveName . '.flags') ?: [],
1862+
];
1863+
}
1864+
}
1865+
}
1866+
1867+
}
1868+
1869+
// Allow array definition statements over 80 but under 120 characters.
1870+
$form['strings'] = [
1871+
'#type' => 'table',
1872+
'#tree' => TRUE,
1873+
'#language' => $langname,
1874+
'#header' => [
1875+
$this->t('Source string'),
1876+
$this->t('Translation for @language on this longer line', ['@language' => $langname]),
1877+
$this->t('Delete'),
1878+
],
1879+
'#empty' => $this->t('No strings available.'),
1880+
'#attributes' => ['class' => ['locale-translate-edit-table']],
1881+
];

0 commit comments

Comments
 (0)