Skip to content

Commit 8c31253

Browse files
committed
Fixed bug #2529 : Generic.Formatting.MultipleStatementAlignment wrong error for assign in string concat
Decided is was easiest to ignore assignment that are in parenthesis on the same line
1 parent 0278329 commit 8c31253

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
106106
-- Thanks to Juliette Reinders Folmer for the patch
107107
- Fixed bug #2526 : XML report format has bad syntax on Windows
108108
-- Thanks to Juliette Reinders Folmer for the patch
109+
- Fixed bug #2529 : Generic.Formatting.MultipleStatementAlignment wrong error for assign in string concat
109110
- Fixed bug #2549 : Searching for a phpcs.xml file can throw warnings due to open_basedir restrictions
110111
-- Thanks to Matthew Peveler for the patch
111112
- Fixed bug #2558 : PHP 7.4 throwing offset syntax with curly braces is deprecated message

src/Standards/Generic/Sniffs/Formatting/MultipleStatementAlignmentSniff.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ public function process(File $phpcsFile, $stackPtr)
7777

7878
// Ignore assignments used in a condition, like an IF or FOR.
7979
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
80+
// If the parenthesis is on the same line as the assignment,
81+
// then it should be ignored as it is specifically being grouped.
82+
$parens = $tokens[$stackPtr]['nested_parenthesis'];
83+
$lastParen = array_pop($parens);
84+
if ($tokens[$lastParen]['line'] === $tokens[$stackPtr]['line']) {
85+
return;
86+
}
87+
8088
foreach ($tokens[$stackPtr]['nested_parenthesis'] as $start => $end) {
8189
if (isset($tokens[$start]['parenthesis_owner']) === true) {
8290
return;
@@ -217,6 +225,14 @@ public function checkAlignment($phpcsFile, $stackPtr, $end=null)
217225

218226
// Make sure it is not assigned inside a condition (eg. IF, FOR).
219227
if (isset($tokens[$assign]['nested_parenthesis']) === true) {
228+
// If the parenthesis is on the same line as the assignment,
229+
// then it should be ignored as it is specifically being grouped.
230+
$parens = $tokens[$assign]['nested_parenthesis'];
231+
$lastParen = array_pop($parens);
232+
if ($tokens[$lastParen]['line'] === $tokens[$assign]['line']) {
233+
break;
234+
}
235+
220236
foreach ($tokens[$assign]['nested_parenthesis'] as $start => $end) {
221237
if (isset($tokens[$start]['parenthesis_owner']) === true) {
222238
break(2);

src/Standards/Generic/Tests/Formatting/MultipleStatementAlignmentUnitTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,6 @@ $foofoo = new Foo([
403403
$b = new Bar(),
404404
$c = new Bar(),
405405
]);
406+
407+
$i = 0;
408+
echo "TEST: ".($i += 1)."\n";

src/Standards/Generic/Tests/Formatting/MultipleStatementAlignmentUnitTest.inc.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,6 @@ $foofoo = new Foo([
403403
$b = new Bar(),
404404
$c = new Bar(),
405405
]);
406+
407+
$i = 0;
408+
echo "TEST: ".($i += 1)."\n";

0 commit comments

Comments
 (0)