Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array sniffs: ignore short lists #1780

Merged
merged 2 commits into from
Jul 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 64 additions & 0 deletions WordPress/Sniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -3202,4 +3202,68 @@ protected function is_foreach_as( $stackPtr ) {
return ( $stackPtr > $as_ptr );
}

/**
* Determine whether a T_OPEN/CLOSE_SHORT_ARRAY token is a short list() construct.
*
* @internal This function will be introduced in PHPCS upstream in version 3.5.0
* and can be removed from WPCS once WPCS raises the minimum version.
*
* @since 2.2.0
*
* @param int $stackPtr The position of the array bracket token.
*
* @return bool True if the token passed is the open/close bracket of a short list.
* False if the token is a short array bracket or not
* a T_OPEN/CLOSE_SHORT_ARRAY token.
*/
protected function is_short_list( $stackPtr ) {
// Is this one of the tokens this function handles ?
if ( \T_OPEN_SHORT_ARRAY !== $this->tokens[ $stackPtr ]['code']
&& \T_CLOSE_SHORT_ARRAY !== $this->tokens[ $stackPtr ]['code']
) {
return false;
}

switch ( $this->tokens[ $stackPtr ]['code'] ) {
case \T_OPEN_SHORT_ARRAY:
$opener = $stackPtr;
$closer = $this->tokens[ $stackPtr ]['bracket_closer'];
break;

case \T_CLOSE_SHORT_ARRAY:
$opener = $this->tokens[ $stackPtr ]['bracket_opener'];
$closer = $stackPtr;
break;
}

$nextNonEmpty = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $closer + 1 ), null, true, null, true );
if ( false !== $nextNonEmpty && \T_EQUAL === $this->tokens[ $nextNonEmpty ]['code'] ) {
return true;
}

// Check for short list in foreach, i.e. `foreach($array as [$a, $b])`.
if ( $this->is_foreach_as( $stackPtr ) === true ) {
return true;
}

// Maybe this is a short list syntax nested inside another short list syntax ?
$parentOpen = $opener;
do {
$parentOpen = $this->phpcsFile->findPrevious(
\T_OPEN_SHORT_ARRAY,
( $parentOpen - 1 ),
null,
false,
null,
true
);

if ( false === $parentOpen ) {
return false;
}
} while ( $this->tokens[ $parentOpen ]['bracket_closer'] < $opener );

return $this->is_short_list( $parentOpen );
}

}
8 changes: 8 additions & 0 deletions WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ public function register() {
* @return void
*/
public function process_token( $stackPtr ) {

if ( \T_OPEN_SHORT_ARRAY === $this->tokens[ $stackPtr ]['code']
&& $this->is_short_list( $stackPtr )
) {
// Short list, not short array.
return;
}

/*
* Determine the array opener & closer.
*/
Expand Down
7 changes: 7 additions & 0 deletions WordPress/Sniffs/Arrays/ArrayIndentationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ public function process_token( $stackPtr ) {
$this->tab_width = PHPCSHelper::get_tab_width( $this->phpcsFile );
}

if ( \T_OPEN_SHORT_ARRAY === $this->tokens[ $stackPtr ]['code']
&& $this->is_short_list( $stackPtr )
) {
// Short list, not short array.
return;
}

/*
* Determine the array opener & closer.
*/
Expand Down
8 changes: 8 additions & 0 deletions WordPress/Sniffs/Arrays/CommaAfterArrayItemSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ public function register() {
* @return void
*/
public function process_token( $stackPtr ) {

if ( \T_OPEN_SHORT_ARRAY === $this->tokens[ $stackPtr ]['code']
&& $this->is_short_list( $stackPtr )
) {
// Short list, not short array.
return;
}

/*
* Determine the array opener & closer.
*/
Expand Down
8 changes: 8 additions & 0 deletions WordPress/Sniffs/Arrays/MultipleStatementAlignmentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ public function register() {
* normal file processing.
*/
public function process_token( $stackPtr ) {

if ( \T_OPEN_SHORT_ARRAY === $this->tokens[ $stackPtr ]['code']
&& $this->is_short_list( $stackPtr )
) {
// Short list, not short array.
return;
}

/*
* Determine the array opener & closer.
*/
Expand Down
26 changes: 26 additions & 0 deletions WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.2.inc
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,29 @@ $bad = [
end */
'key4' => 'value4'
];

/*
* Issue #1692: Test distinguishing between short array and short list.
*
* Note: these short lists should all contain "violations" against the array
* declaration sniff if they would be recognized as short array to make sure
* they are correctly recognized as short lists.
*/
// Empty list, not allowed since PHP 7.0, but not our concern.
[ ] = $array; // OK.
[, ,] = $array; // OK.

[$var1, $var2] = $array; // OK.

// Keyed list.
[$foo => $bar] = $bar; // OK.
['enabled' => $enabled, 'compression' => $compression] = [ 'enabled' => true, 'compression' => 'gzip' ]; // Bad x 1 - only for the short array, not the short list.

// Nested short lists.
[$var1, , [$var2, $var3,], $var4] = $array; // OK.
[ 'o' => [[ $one, $two, $three ], [ 'what' => $what ]] ] = $x; // OK.

// Destructuring - multiline.
[
'prop1' => $prop1, 'prop2' => $prop2, // OK.
] = $some_var;
29 changes: 29 additions & 0 deletions WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.2.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,32 @@ $bad = [
end */
'key4' => 'value4'
];

/*
* Issue #1692: Test distinguishing between short array and short list.
*
* Note: these short lists should all contain "violations" against the array
* declaration sniff if they would be recognized as short array to make sure
* they are correctly recognized as short lists.
*/
// Empty list, not allowed since PHP 7.0, but not our concern.
[ ] = $array; // OK.
[, ,] = $array; // OK.

[$var1, $var2] = $array; // OK.

// Keyed list.
[$foo => $bar] = $bar; // OK.
['enabled' => $enabled, 'compression' => $compression] = [
'enabled' => true,
'compression' => 'gzip'
]; // Bad x 1 - only for the short array, not the short list.

// Nested short lists.
[$var1, , [$var2, $var3,], $var4] = $array; // OK.
[ 'o' => [[ $one, $two, $three ], [ 'what' => $what ]] ] = $x; // OK.

// Destructuring - multiline.
[
'prop1' => $prop1, 'prop2' => $prop2, // OK.
] = $some_var;
1 change: 1 addition & 0 deletions WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function getErrorList( $testFile = '' ) {
91 => 1,
92 => 1,
104 => 1,
126 => 1,
);

default:
Expand Down
8 changes: 8 additions & 0 deletions WordPress/Tests/Arrays/ArrayIndentationUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,11 @@ $my_array = [
// B
'something_else',
];

/*
* Issue #1692: Test distinguishing between short array and short list.
*/
[
'prop1' => $prop1,
'prop2' => $prop2,
] = $some_var;
8 changes: 8 additions & 0 deletions WordPress/Tests/Arrays/ArrayIndentationUnitTest.1.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,11 @@ $my_array = [
// B
'something_else',
];

/*
* Issue #1692: Test distinguishing between short array and short list.
*/
[
'prop1' => $prop1,
'prop2' => $prop2,
] = $some_var;
8 changes: 8 additions & 0 deletions WordPress/Tests/Arrays/ArrayIndentationUnitTest.2.inc
Original file line number Diff line number Diff line change
Expand Up @@ -429,4 +429,12 @@ $my_array = [
'something_else',
];

/*
* Issue #1692: Test distinguishing between short array and short list.
*/
[
'prop1' => $prop1,
'prop2' => $prop2,
] = $some_var;

// phpcs:set WordPress.Arrays.ArrayIndentation tabIndent true
8 changes: 8 additions & 0 deletions WordPress/Tests/Arrays/ArrayIndentationUnitTest.2.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -429,4 +429,12 @@ $my_array = [
'something_else',
];

/*
* Issue #1692: Test distinguishing between short array and short list.
*/
[
'prop1' => $prop1,
'prop2' => $prop2,
] = $some_var;

// phpcs:set WordPress.Arrays.ArrayIndentation tabIndent true
20 changes: 20 additions & 0 deletions WordPress/Tests/Arrays/CommaAfterArrayItemUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,23 @@ $bad = array(
'second'
// phpcs:enable Standard.Category.Sniff
);

/*
* Issue #1692: Test distinguishing between short array and short list.
*/
// Empty list, not allowed since PHP 7.0, but not our concern.
[,,] = $array; // OK.

[$var1, $var2,] = $array; // OK.

// Keyed list.
['enabled' => $enabled, 'compression' => $compression,] = [ 'enabled' => true, 'compression' => 'gzip', ]; // Bad x 1 - only for the short array, not the short list.

// Nested short lists.
[$var1, , [$var2, $var3,], $var4,] = $array; // OK.

// Destructuring - multiline.
[
'prop1' => $prop1,
'prop2' => $prop2
] = $some_var;
20 changes: 20 additions & 0 deletions WordPress/Tests/Arrays/CommaAfterArrayItemUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,23 @@ $bad = array(
'second',
// phpcs:enable Standard.Category.Sniff
);

/*
* Issue #1692: Test distinguishing between short array and short list.
*/
// Empty list, not allowed since PHP 7.0, but not our concern.
[,,] = $array; // OK.

[$var1, $var2,] = $array; // OK.

// Keyed list.
['enabled' => $enabled, 'compression' => $compression,] = [ 'enabled' => true, 'compression' => 'gzip' ]; // Bad x 1 - only for the short array, not the short list.

// Nested short lists.
[$var1, , [$var2, $var3,], $var4,] = $array; // OK.

// Destructuring - multiline.
[
'prop1' => $prop1,
'prop2' => $prop2
] = $some_var;
1 change: 1 addition & 0 deletions WordPress/Tests/Arrays/CommaAfterArrayItemUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function getErrorList() {
184 => 2,
185 => 2,
190 => 1,
203 => 1,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,4 +571,12 @@ $deprecated_functions = array(
string', // Bad.
);

/*
* This isn't actually testing something, but is the long version of the "short list" test.
*/
list(
'prop1' => $prop1,
'prop2' => $prop2,
) = $some_var;

// phpcs:set WordPress.Arrays.MultipleStatementAlignment alignMultilineItems always
Original file line number Diff line number Diff line change
Expand Up @@ -557,4 +557,12 @@ $deprecated_functions = array(
string', // Bad.
);

/*
* This isn't actually testing something, but is the long version of the "short list" test.
*/
list(
'prop1' => $prop1,
'prop2' => $prop2,
) = $some_var;

// phpcs:set WordPress.Arrays.MultipleStatementAlignment alignMultilineItems always
Original file line number Diff line number Diff line change
Expand Up @@ -571,4 +571,12 @@ $deprecated_functions = [
string', // Bad.
];

/*
* Issue #1692: Test distinguishing between short array and short list.
*/
[
'prop1' => $prop1,
'prop2' => $prop2,
] = $some_var;

// phpcs:set WordPress.Arrays.MultipleStatementAlignment alignMultilineItems always
Original file line number Diff line number Diff line change
Expand Up @@ -557,4 +557,12 @@ $deprecated_functions = [
string', // Bad.
];

/*
* Issue #1692: Test distinguishing between short array and short list.
*/
[
'prop1' => $prop1,
'prop2' => $prop2,
] = $some_var;

// phpcs:set WordPress.Arrays.MultipleStatementAlignment alignMultilineItems always