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

Fix trailing comma issue in array definition where it choked on closures and multiline functions #59

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 2 additions & 2 deletions Sniffs/Arrays/ArrayDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
*/

// Check each line ends in a comma.
if ($tokens[$index['value']]['code'] !== T_ARRAY) {
$nextComma = $phpcsFile->findNext(array(T_COMMA), ($index['value'] + 1));
if ( ! in_array( $tokens[$index['value']]['code'], array( T_ARRAY, T_CLOSURE ) ) ) {
$nextComma = $phpcsFile->findNext(array(T_COMMA, T_OPEN_PARENTHESIS), ($index['value'] + 1));
if (($nextComma === false) || ($tokens[$nextComma]['line'] !== $tokens[$index['value']]['line'])) {
$error = 'Each line in an array declaration must end in a comma';
$phpcsFile->addError($error, $index['value']);
Expand Down
11 changes: 10 additions & 1 deletion Tests/Arrays/ArrayDeclarationUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ $query_vars = array_merge(
// ...
array(
'post_status' => 'private',
'orderby' => 'title', // Good
'orderby' => 'title', // Good
),
array(
'closure' => function () { // Good, Closures allowed
return array();
},
),
bar( // Good, Functions allowed
1,
2
)
);
$query = new WP_Query( $query_vars );
Expand Down
2 changes: 1 addition & 1 deletion Tests/Arrays/ArrayDeclarationUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getErrorList()
7 => 1,
9 => 1,
16 => 1,
31 => 2,
40 => 2,
);

}//end getErrorList()
Expand Down