Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,25 @@
* `allow_single_item_single_line_associative_arrays` property.
* @since 3.0.0 Removed various whitespace related checks and fixers in favor of the PHPCSExtra
* `NormalizedArrays.Arrays.ArrayBraceSpacing` sniff.
* @since 3.4.0 The `allow_single_item_single_line_associative_arrays` property has been
* deprecated in favor of `allow_single_item_single_line_explicit_key_arrays`.
*/
final class ArrayDeclarationSpacingSniff extends Sniff {

/**
* Whether to allow single item arrays with explicit keys to be single line.
*
* @since 3.4.0
*
* @var bool Defaults to true.
*/
public $allow_single_item_single_line_explicit_key_arrays = true;

/**
* Whether or not to allow single item arrays with explicit keys to be single line.
*
* @since 0.14.0
* @deprecated 3.4.0 Use $allow_single_item_single_line_explicit_key_arrays instead.
*
* @var bool Defaults to true.
*/
Expand Down Expand Up @@ -114,10 +126,19 @@ public function process_token( $stackPtr ) {
* @return void
*/
protected function process_single_line_array( $stackPtr, $opener, $closer ) {
// For now, if the new property has not been changed from its default value and the deprecated property has, use
// the deprecated property's value.
$allow_single_item = $this->allow_single_item_single_line_explicit_key_arrays;
if ( true === $allow_single_item
&& true !== $this->allow_single_item_single_line_associative_arrays
) {
$allow_single_item = $this->allow_single_item_single_line_associative_arrays;
}

$array_items = PassedParameters::getParameters( $this->phpcsFile, $stackPtr );
if ( ( false === $this->allow_single_item_single_line_associative_arrays
if ( ( false === $allow_single_item
&& empty( $array_items ) )
|| ( true === $this->allow_single_item_single_line_associative_arrays
|| ( true === $allow_single_item
&& \count( $array_items ) === 1 )
) {
return;
Expand All @@ -138,7 +159,7 @@ protected function process_single_line_array( $stackPtr, $opener, $closer ) {
return;
}
$error = 'When an array is declared with explicit keys, each value should start on %s.';
if ( true === $this->allow_single_item_single_line_associative_arrays ) {
if ( true === $allow_single_item ) {
$error = 'When a multi-item array is declared with explicit keys, each value should start on %s.';
}

Expand Down
9 changes: 6 additions & 3 deletions WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ $bad = array(
// Don't confuse list arrows with array arrows.
$okay = array( $item1, list( 'key1' => $a, 'key2' => $b ) = $array, $item3 );

// Live coding/parse error.
// This must be the last test in the file!
$ignore = array( $item1, 'key' => 'value',
// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_explicit_key_arrays false

$bad = array( 'key' => 'value' ); // Bad.
$bad = array( 'key1' => 'value1', 'key2' => 'value2' ); // Bad.

// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_explicit_key_arrays true
14 changes: 11 additions & 3 deletions WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.1.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ $bad = array(
// Don't confuse list arrows with array arrows.
$okay = array( $item1, list( 'key1' => $a, 'key2' => $b ) = $array, $item3 );

// Live coding/parse error.
// This must be the last test in the file!
$ignore = array( $item1, 'key' => 'value',
// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_explicit_key_arrays false

$bad = array(
'key' => 'value'
); // Bad.
$bad = array(
'key1' => 'value1',
'key2' => 'value2'
); // Bad.

// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_explicit_key_arrays true
7 changes: 7 additions & 0 deletions WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.2.inc
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,10 @@ $bad = [

// Don't confuse list arrows with array arrows.
$okay = [ $item1, [ 'key1' => $a, 'key2' => $b ] = $array, $item3 ];

// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_explicit_key_arrays false

$bad = [ 'key' => 'value' ]; // Bad.
$bad = [ 'key1' => 'value1', 'key2' => 'value2' ]; // Bad.

// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_explicit_key_arrays true
12 changes: 12 additions & 0 deletions WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.2.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,15 @@ $bad = [

// Don't confuse list arrows with array arrows.
$okay = [ $item1, [ 'key1' => $a, 'key2' => $b ] = $array, $item3 ];

// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_explicit_key_arrays false

$bad = [
'key' => 'value'
]; // Bad.
$bad = [
'key1' => 'value1',
'key2' => 'value2'
]; // Bad.

// phpcs:set WordPress.Arrays.ArrayDeclarationSpacing allow_single_item_single_line_explicit_key_arrays true
8 changes: 8 additions & 0 deletions WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.3.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/*
* Intentional parse error (unclosed array).
* This should be the only test in this file.
*/

$ignore = array( $item1, 'key' => 'value',
40 changes: 22 additions & 18 deletions WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,33 @@ public function getErrorList( $testFile = '' ) {
62 => 1,
63 => 1,
75 => 1,
87 => 1,
88 => 1,
);

// Short arrays.
case 'ArrayDeclarationSpacingUnitTest.2.inc':
return array(
9 => 4,
13 => 2,
15 => 1,
19 => 1,
22 => 1,
25 => 1,
44 => 1,
45 => 1,
48 => 1,
49 => 1,
52 => 2,
54 => 1,
57 => 1,
58 => 1,
62 => 1,
63 => 1,
75 => 1,
97 => 1,
9 => 4,
13 => 2,
15 => 1,
19 => 1,
22 => 1,
25 => 1,
44 => 1,
45 => 1,
48 => 1,
49 => 1,
52 => 2,
54 => 1,
57 => 1,
58 => 1,
62 => 1,
63 => 1,
75 => 1,
97 => 1,
113 => 1,
114 => 1,
);

default:
Expand Down
Loading