Skip to content

Commit

Permalink
ForbiddenEmptyListAssignment: Report on empty lists using short list …
Browse files Browse the repository at this point in the history
…syntax

Includes unit tests.
  • Loading branch information
jrfnl committed Jun 11, 2018
1 parent 2362a4e commit 44e28df
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
50 changes: 33 additions & 17 deletions PHPCompatibility/Sniffs/PHP/ForbiddenEmptyListAssignmentSniff.php
Expand Up @@ -52,7 +52,10 @@ public function register()
$this->ignoreTokens[T_OPEN_PARENTHESIS] = T_OPEN_PARENTHESIS;
$this->ignoreTokens[T_CLOSE_PARENTHESIS] = T_CLOSE_PARENTHESIS;

return array(T_LIST);
return array(
T_LIST,
T_OPEN_SHORT_ARRAY,
);
}

/**
Expand All @@ -66,32 +69,45 @@ public function register()
*/
public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
if ($this->supportsAbove('7.0')) {
$tokens = $phpcsFile->getTokens();
if ($this->supportsAbove('7.0') === false) {
return;
}

$tokens = $phpcsFile->getTokens();

if ($tokens[$stackPtr]['code'] === T_OPEN_SHORT_ARRAY) {
if ($this->isShortList($phpcsFile, $stackPtr) === false) {
return;
}

$open = $stackPtr;
$close = $tokens[$stackPtr]['bracket_closer'];
} else {
// T_LIST.
$open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr, null, false, null, true);
if ($open === false || isset($tokens[$open]['parenthesis_closer']) === false) {
return;
}

$close = $tokens[$open]['parenthesis_closer'];
$error = true;
if (($close - $open) > 1) {
for ($cnt = $open + 1; $cnt < $close; $cnt++) {
if (isset($this->ignoreTokens[$tokens[$cnt]['code']]) === false) {
$error = false;
break;
}
}

$error = true;
if (($close - $open) > 1) {
for ($cnt = $open + 1; $cnt < $close; $cnt++) {
if (isset($this->ignoreTokens[$tokens[$cnt]['code']]) === false) {
$error = false;
break;
}
}
}

if ($error === true) {
$phpcsFile->addError(
'Empty list() assignments are not allowed since PHP 7.0',
$stackPtr,
'Found'
);
}
if ($error === true) {
$phpcsFile->addError(
'Empty list() assignments are not allowed since PHP 7.0',
$stackPtr,
'Found'
);
}
}
}
Expand Up @@ -55,6 +55,13 @@ public function dataEmptyListAssignment()
array(5),
array(6),
array(7),
array(8),
array(20),
array(21),
array(22),
array(23),
array(24),
array(25),
);
}

Expand Down Expand Up @@ -89,7 +96,7 @@ public function dataNoFalsePositives()
array(15),
array(16),
array(17),
array(20),
array(28),
);
}

Expand Down
Expand Up @@ -5,7 +5,7 @@
list(/*comment*/) = 5;
list( /*comment*/ /*another comment*/ ) = 5;
list(,(),) = 5;

list($x, list(), $y) = $a;

/*
* The below list assignments are all valid.
Expand All @@ -16,5 +16,13 @@
list($a[0], $a[1], $a[2]) = $infoArray;
list( ${$drink} ) = $infoArray;

// Invalid with short list syntax.
[] = 5;
[, ,] = 5;
[/*comment*/] = 5;
[ /*comment*/ /*another comment*/ ] = 5;
[,(),] = 5;
[$x, [], $y] = $a;

// Don't trigger on unfinished code during live code review.
list(

0 comments on commit 44e28df

Please sign in to comment.