From 44e28dfea67423208b682e16893dfe8b1ffc5bb4 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 27 Mar 2018 04:49:23 +0200 Subject: [PATCH] ForbiddenEmptyListAssignment: Report on empty lists using short list syntax Includes unit tests. --- .../PHP/ForbiddenEmptyListAssignmentSniff.php | 50 ++++++++++++------- .../ForbiddenEmptyListAssignmentSniffTest.php | 9 +++- .../forbidden_empty_list_assignment.php | 10 +++- 3 files changed, 50 insertions(+), 19 deletions(-) diff --git a/PHPCompatibility/Sniffs/PHP/ForbiddenEmptyListAssignmentSniff.php b/PHPCompatibility/Sniffs/PHP/ForbiddenEmptyListAssignmentSniff.php index b7941a5b3..899c0aa83 100644 --- a/PHPCompatibility/Sniffs/PHP/ForbiddenEmptyListAssignmentSniff.php +++ b/PHPCompatibility/Sniffs/PHP/ForbiddenEmptyListAssignmentSniff.php @@ -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, + ); } /** @@ -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' + ); } } } diff --git a/PHPCompatibility/Tests/Sniffs/PHP/ForbiddenEmptyListAssignmentSniffTest.php b/PHPCompatibility/Tests/Sniffs/PHP/ForbiddenEmptyListAssignmentSniffTest.php index de0a2c1f1..fd0d886d0 100644 --- a/PHPCompatibility/Tests/Sniffs/PHP/ForbiddenEmptyListAssignmentSniffTest.php +++ b/PHPCompatibility/Tests/Sniffs/PHP/ForbiddenEmptyListAssignmentSniffTest.php @@ -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), ); } @@ -89,7 +96,7 @@ public function dataNoFalsePositives() array(15), array(16), array(17), - array(20), + array(28), ); } diff --git a/PHPCompatibility/Tests/sniff-examples/forbidden_empty_list_assignment.php b/PHPCompatibility/Tests/sniff-examples/forbidden_empty_list_assignment.php index 8bbb4c5ca..cf5f83b55 100644 --- a/PHPCompatibility/Tests/sniff-examples/forbidden_empty_list_assignment.php +++ b/PHPCompatibility/Tests/sniff-examples/forbidden_empty_list_assignment.php @@ -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. @@ -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(