Skip to content

Commit 25823a9

Browse files
authored
Support shorthand list assignment (#50)
* Tests: add test for destructuring assignment * Support shorthand list assignment
1 parent 8ff2790 commit 25823a9

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

VariableAnalysis/Lib/Helpers.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@
55
use PHP_CodeSniffer\Files\File;
66

77
class Helpers {
8+
public static function findContainingOpeningSquareBracket(File $phpcsFile, $stackPtr) {
9+
$tokens = $phpcsFile->getTokens();
10+
$previousStatementPtr = self::getPreviousStatementPtr($phpcsFile, $stackPtr);
11+
return $phpcsFile->findPrevious(T_OPEN_SHORT_ARRAY, $stackPtr - 1, $previousStatementPtr);
12+
}
13+
14+
public static function findContainingClosingSquareBracket(File $phpcsFile, $stackPtr) {
15+
$tokens = $phpcsFile->getTokens();
16+
$endOfStatementPtr = $phpcsFile->findNext([T_SEMICOLON], $stackPtr + 1);
17+
if (! $endOfStatementPtr) {
18+
return false;
19+
}
20+
return $phpcsFile->findNext(T_CLOSE_SHORT_ARRAY, $stackPtr + 1, $endOfStatementPtr);
21+
}
22+
23+
public static function getPreviousStatementPtr(File $phpcsFile, $stackPtr) {
24+
return $phpcsFile->findPrevious([T_SEMICOLON, T_CLOSE_CURLY_BRACKET], $stackPtr - 1) ?: 1;
25+
}
26+
827
public static function findContainingOpeningBracket(File $phpcsFile, $stackPtr) {
928
$tokens = $phpcsFile->getTokens();
1029
if (isset($tokens[$stackPtr]['nested_parenthesis'])) {

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,29 @@ protected function checkForAssignment(File $phpcsFile, $stackPtr, $varName, $cur
446446
return true;
447447
}
448448

449+
protected function checkForListShorthandAssignment(File $phpcsFile, $stackPtr, $varName, $currScope) {
450+
$tokens = $phpcsFile->getTokens();
451+
$token = $tokens[$stackPtr];
452+
453+
// OK, are we within a [ ... ] construct?
454+
$openPtr = Helpers::findContainingOpeningSquareBracket($phpcsFile, $stackPtr);
455+
if ($openPtr === false) {
456+
return false;
457+
}
458+
459+
// OK, we're a [ ... ] construct... are we being assigned to?
460+
$closePtr = Helpers::findContainingClosingSquareBracket($phpcsFile, $stackPtr);
461+
$assignPtr = Helpers::isNextThingAnAssign($phpcsFile, $closePtr);
462+
if ($assignPtr === false) {
463+
return false;
464+
}
465+
466+
// Yes, we're being assigned.
467+
$writtenPtr = Helpers::findWhereAssignExecuted($phpcsFile, $assignPtr);
468+
$this->markVariableAssignment($varName, $writtenPtr, $currScope);
469+
return true;
470+
}
471+
449472
protected function checkForListAssignment(File $phpcsFile, $stackPtr, $varName, $currScope) {
450473
$tokens = $phpcsFile->getTokens();
451474
$token = $tokens[$stackPtr];
@@ -745,6 +768,11 @@ protected function processVariable(File $phpcsFile, $stackPtr) {
745768
return;
746769
}
747770

771+
// OK, are we within a [...] = construct?
772+
if ($this->checkForListShorthandAssignment($phpcsFile, $stackPtr, $varName, $currScope)) {
773+
return;
774+
}
775+
748776
// Are we a global declaration?
749777
if ($this->checkForGlobalDeclaration($phpcsFile, $stackPtr, $varName, $currScope)) {
750778
return;

VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,4 +541,16 @@ public function testIgnoreUnusedRegexpIgnoresUnusedVariables() {
541541
];
542542
$this->assertEquals($expectedWarnings, $lines);
543543
}
544+
545+
public function testAllowDestructuringAssignment() {
546+
$fixtureFile = $this->getFixture('DestructuringFixture.php');
547+
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
548+
$phpcsFile->process();
549+
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
550+
$expectedWarnings = [
551+
4,
552+
12,
553+
];
554+
$this->assertEquals($expectedWarnings, $lines);
555+
}
544556
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
function function_with_destructuring_assignment() {
3+
[$a, $b] = [1, 2];
4+
[$c, $d] = [3, 4]; // unused
5+
echo $a;
6+
echo $b;
7+
echo $c;
8+
}
9+
10+
function function_with_destructuring_assignment_using_list() {
11+
list( $a, $b ) = [1, 2];
12+
list( $c, $d ) = [3, 4]; // unused
13+
echo $a;
14+
echo $b;
15+
echo $c;
16+
}

0 commit comments

Comments
 (0)