Skip to content

Commit

Permalink
Merge pull request #610 from PHPCSStandards/feature/598-squiz-disallo…
Browse files Browse the repository at this point in the history
…wmultipleassignments-bug-fix

Squiz/DisallowMultipleAssignments: bug fix - dynamic property assignment on object stored in array
  • Loading branch information
jrfnl authored Sep 14, 2024
2 parents b87dafd + 0ab692a commit 16b6f35
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ public function process(File $phpcsFile, $stackPtr)
}

if ($tokens[$varToken]['code'] === T_VARIABLE) {
$prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($varToken - 1), null, true);
if ($tokens[$prevNonEmpty]['code'] === T_OBJECT_OPERATOR) {
// Dynamic property access, the real "start" variable still needs to be found.
$varToken = $prevNonEmpty;
continue;
}

// We found our variable.
break;
}
Expand All @@ -119,15 +126,14 @@ public function process(File $phpcsFile, $stackPtr)

$allowed = Tokens::$emptyTokens;

$allowed[T_STRING] = T_STRING;
$allowed[T_NS_SEPARATOR] = T_NS_SEPARATOR;
$allowed[T_DOUBLE_COLON] = T_DOUBLE_COLON;
$allowed[T_OBJECT_OPERATOR] = T_OBJECT_OPERATOR;
$allowed[T_ASPERAND] = T_ASPERAND;
$allowed[T_DOLLAR] = T_DOLLAR;
$allowed[T_SELF] = T_SELF;
$allowed[T_PARENT] = T_PARENT;
$allowed[T_STATIC] = T_STATIC;
$allowed[T_STRING] = T_STRING;
$allowed[T_NS_SEPARATOR] = T_NS_SEPARATOR;
$allowed[T_DOUBLE_COLON] = T_DOUBLE_COLON;
$allowed[T_ASPERAND] = T_ASPERAND;
$allowed[T_DOLLAR] = T_DOLLAR;
$allowed[T_SELF] = T_SELF;
$allowed[T_PARENT] = T_PARENT;
$allowed[T_STATIC] = T_STATIC;

$varToken = $phpcsFile->findPrevious($allowed, ($varToken - 1), null, true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,17 @@ list ($c, $d) = explode(',', '1,2');
<?= $a * $b ?>
<?= [$c, $d] = explode(',', '1,2');
?>
<?php

// Issue #598.
$filtered_results->field = $result;
$filtered_results->$field = $result;
$filtered_results->$row->field = $result;
$filtered_results->$row->$field = $result;

$filtered_results[ $i ]->field = $result;
$filtered_results[ $i ]->$field = $result;
$filtered_results[ $i ]->$row->field = $result;
$filtered_results[ $i ]->$row->$field = $result;
$filtered_results[ $i ]->$row[0]->field = $result;
$filtered_results[ $i ]->$row[$j]->$field = $result;

0 comments on commit 16b6f35

Please sign in to comment.