Skip to content

Commit dddb318

Browse files
committed
Squiz/DisallowMultipleAssignments: split errorcode
As per the feature request in 2083, the errorcode of the sniff has now been split into two: * `FoundInControlStructure` when the assignment is within a control structure (with the exception of `while` statements and the first leaf of a `for` statement as those won't be reported anyway). * `Found` for all other cases. Fixes 2083 Fixes 1679
1 parent 49a3e26 commit dddb318

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,28 @@ public function process(File $phpcsFile, $stackPtr)
153153
return;
154154
}
155155

156-
$error = 'Assignments must be the first block of code on a line';
157-
$phpcsFile->addError($error, $stackPtr, 'Found');
156+
$error = 'Assignments must be the first block of code on a line';
157+
$errorCode = 'Found';
158+
159+
if (isset($nested) === true) {
160+
$controlStructures = [
161+
T_IF => T_IF,
162+
T_ELSEIF => T_ELSEIF,
163+
T_SWITCH => T_SWITCH,
164+
T_CASE => T_CASE,
165+
T_FOR => T_FOR,
166+
];
167+
foreach ($nested as $opener => $closer) {
168+
if (isset($tokens[$opener]['parenthesis_owner']) === true
169+
&& isset($controlStructures[$tokens[$tokens[$opener]['parenthesis_owner']]['code']]) === true
170+
) {
171+
$errorCode .= 'InControlStructure';
172+
break;
173+
}
174+
}
175+
}
176+
177+
$phpcsFile->addError($error, $stackPtr, $errorCode);
158178

159179
}//end process()
160180

0 commit comments

Comments
 (0)