Skip to content

Commit 0ab692a

Browse files
committed
Squiz/DisallowMultipleAssignments: bug fix - dynamic property assignment on object stored in array
The sniff would try to find whether the first "relevant" variable found matches the start of the statement and if not, throw an error, but in the case of dynamic property access on objects stored in an array, the first "relevant" variable determination was off and would get stuck on the `]` close bracket of the array access. Fixed now. Includes ample tests. This should also make the sniff slightly more efficient for property access code snippets which the sniff already handled correctly. Fixes 598
1 parent b87dafd commit 0ab692a

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/Standards/Squiz/Sniffs/PHP/DisallowMultipleAssignmentsSniff.php

+15-9
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ public function process(File $phpcsFile, $stackPtr)
105105
}
106106

107107
if ($tokens[$varToken]['code'] === T_VARIABLE) {
108+
$prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($varToken - 1), null, true);
109+
if ($tokens[$prevNonEmpty]['code'] === T_OBJECT_OPERATOR) {
110+
// Dynamic property access, the real "start" variable still needs to be found.
111+
$varToken = $prevNonEmpty;
112+
continue;
113+
}
114+
108115
// We found our variable.
109116
break;
110117
}
@@ -119,15 +126,14 @@ public function process(File $phpcsFile, $stackPtr)
119126

120127
$allowed = Tokens::$emptyTokens;
121128

122-
$allowed[T_STRING] = T_STRING;
123-
$allowed[T_NS_SEPARATOR] = T_NS_SEPARATOR;
124-
$allowed[T_DOUBLE_COLON] = T_DOUBLE_COLON;
125-
$allowed[T_OBJECT_OPERATOR] = T_OBJECT_OPERATOR;
126-
$allowed[T_ASPERAND] = T_ASPERAND;
127-
$allowed[T_DOLLAR] = T_DOLLAR;
128-
$allowed[T_SELF] = T_SELF;
129-
$allowed[T_PARENT] = T_PARENT;
130-
$allowed[T_STATIC] = T_STATIC;
129+
$allowed[T_STRING] = T_STRING;
130+
$allowed[T_NS_SEPARATOR] = T_NS_SEPARATOR;
131+
$allowed[T_DOUBLE_COLON] = T_DOUBLE_COLON;
132+
$allowed[T_ASPERAND] = T_ASPERAND;
133+
$allowed[T_DOLLAR] = T_DOLLAR;
134+
$allowed[T_SELF] = T_SELF;
135+
$allowed[T_PARENT] = T_PARENT;
136+
$allowed[T_STATIC] = T_STATIC;
131137

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

src/Standards/Squiz/Tests/PHP/DisallowMultipleAssignmentsUnitTest.1.inc

+14
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,17 @@ list ($c, $d) = explode(',', '1,2');
134134
<?= $a * $b ?>
135135
<?= [$c, $d] = explode(',', '1,2');
136136
?>
137+
<?php
138+
139+
// Issue #598.
140+
$filtered_results->field = $result;
141+
$filtered_results->$field = $result;
142+
$filtered_results->$row->field = $result;
143+
$filtered_results->$row->$field = $result;
144+
145+
$filtered_results[ $i ]->field = $result;
146+
$filtered_results[ $i ]->$field = $result;
147+
$filtered_results[ $i ]->$row->field = $result;
148+
$filtered_results[ $i ]->$row->$field = $result;
149+
$filtered_results[ $i ]->$row[0]->field = $result;
150+
$filtered_results[ $i ]->$row[$j]->$field = $result;

0 commit comments

Comments
 (0)