Skip to content

Commit

Permalink
feat(Array): Allow array definition lines up to 120 characters for be…
Browse files Browse the repository at this point in the history
…tter readability (#3185082)
  • Loading branch information
klausi authored Oct 9, 2023
1 parent 7a35274 commit 0b7f9af
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 8 deletions.
6 changes: 4 additions & 2 deletions coder_sniffer/Drupal/Sniffs/Arrays/ArraySniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ class ArraySniff implements Sniff
/**
* The limit that the length of a line should not exceed.
*
* This can be configured to have a different value but the default is 80.
* This can be configured to have a different value but the default is 120.
* We don't enforce 80 characters by default because that can make array
* definitions in nested arrays or function calls less readable.
*
* @var integer
*/
public $lineLimit = 80;
public $lineLimit = 120;


/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Drupal/Arrays/ArrayUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ $array = [
'inline' => [],
'inline3' => ['one', 'two', 'three'],
'inline_long_ok' => ['one', 'two', 'three', 'four', 'five'],
'inline_long_nok' => ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'],
'inline_long_nok' => ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'],
'nested' => [
],
'nested2' => [
Expand All @@ -30,7 +30,7 @@ $array = array(
'inline' => array(),
'inline3' => array('one', 'two', 'three'),
'inline_long_ok' => array('one', 'two', 'three', 'four', 'five'),
'inline_long_nok' => array('one', 'two', 'three', 'four', 'five', 'six', 'seven'),
'inline_long_nok' => array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'),
'nested' => array(
),
'nested2' => array(
Expand Down
4 changes: 2 additions & 2 deletions tests/Drupal/Arrays/ArrayUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ $array = [
'inline' => [],
'inline3' => ['one', 'two', 'three'],
'inline_long_ok' => ['one', 'two', 'three', 'four', 'five'],
'inline_long_nok' => ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'],
'inline_long_nok' => ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'],
'nested' => [],
'nested2' => [
'a',
Expand All @@ -30,7 +30,7 @@ $array = array(
'inline' => array(),
'inline3' => array('one', 'two', 'three'),
'inline_long_ok' => array('one', 'two', 'three', 'four', 'five'),
'inline_long_nok' => array('one', 'two', 'three', 'four', 'five', 'six', 'seven'),
'inline_long_nok' => array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'),
'nested' => array(),
'nested2' => array(
'a',
Expand Down
2 changes: 1 addition & 1 deletion tests/Drupal/bad/bad.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
);

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

// Item assignment operators must be prefixed and followed by a space
$a = array('one'=>'1');
Expand Down
2 changes: 1 addition & 1 deletion tests/Drupal/bad/bad.php.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ $a = array(
);

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

// Item assignment operators must be prefixed and followed by a space.
$a = array('one' => '1');
Expand Down
52 changes: 52 additions & 0 deletions tests/Drupal/good/good.php
Original file line number Diff line number Diff line change
Expand Up @@ -1827,3 +1827,55 @@ enum MyWellNamedEnum: int {
case One = 1;
case Two = 2;
}

// Allow longer than 80 character array definitions on one line (up to 120).
if (!in_array($form_object->getOperation(), ['add', 'edit', 'delete', 'clone', 'default'], TRUE)) {
return;
}
if ($content_type_facet_field && in_array($entity_type, ['entity:node', 'entity:media'])) {
$foo->setContentTypeFacetValues($item, $entity_type, $content_type_facet_field);
}

/**
* Testing nested array definitions going over 80 characters.
*/
class LongNestedArrayLine {

/**
* Test method.
*/
public function foo() {
foreach ($x as $y) {
foreach ($a as $b) {
$form[$policyTypeKey]['directives'][$directiveName]['options']['flags_wrapper']['flags'] = [
'#type' => 'checkboxes',
'#parents' => [$policyTypeKey, 'directives', $directiveName, 'flags', 'humans'],
'#options' => [
'unsafe-inline' => "'unsafe-inline'",
'unsafe-eval' => "'unsafe-eval'",
'unsafe-hashes' => "'unsafe-hashes'",
'unsafe-allow-redirects' => "'unsafe-allow-redirects'",
'strict-dynamic' => "'strict-dynamic'",
'report-sample' => "'report-sample'",
],
'#default_value' => $config->get($policyTypeKey . '.directives.' . $directiveName . '.flags') ?: [],
];
}
}
}

}

// Allow array definition statements over 80 but under 120 characters.
$form['strings'] = [
'#type' => 'table',
'#tree' => TRUE,
'#language' => $langname,
'#header' => [
$this->t('Source string'),
$this->t('Translation for @language on this longer line', ['@language' => $langname]),
$this->t('Delete'),
],
'#empty' => $this->t('No strings available.'),
'#attributes' => ['class' => ['locale-translate-edit-table']],
];

0 comments on commit 0b7f9af

Please sign in to comment.