Skip to content

Commit

Permalink
Generic/InlineControlStructure: fix error when handling else if
Browse files Browse the repository at this point in the history
This commit fixes a bug in this sniff where it would not handle
correctly `else if` statements when there is a comment between the
`else` and `if` tokens.

The affected code was added in
squizlabs/PHP_CodeSniffer@18f98cc
to fix a similar bug when there is more than one whitespace between `else`
and `if`, but it failed to consider that there might be comment tokens
between `else` and `if`.
  • Loading branch information
rodrigoprimo committed May 17, 2024
1 parent 72b50b9 commit f83ba7e
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function process(File $phpcsFile, $stackPtr)

// Ignore the ELSE in ELSE IF. We'll process the IF part later.
if ($tokens[$stackPtr]['code'] === T_ELSE) {
$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if ($tokens[$next]['code'] === T_IF) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,7 @@ function testFinally()
} finally {
}
}

if ($something) {
echo 'hello';
} else /* comment */ if ($somethingElse) echo 'hi';
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,8 @@ function testFinally()
}
}
}

if ($something) {
echo 'hello';
} else /* comment */ if ($somethingElse) { echo 'hi';
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ if ($("#myid").rotationDegrees()=='90')

if ($("#myid").rotationDegrees()=='90')
$foo = {'transform': 'rotate(90deg)'};

if (something) {
alert('hello');
} else /* comment */ if (somethingElse) alert('hi');
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ if ($("#myid").rotationDegrees()=='90') {
if ($("#myid").rotationDegrees()=='90') {
$foo = {'transform': 'rotate(90deg)'};
}

if (something) {
alert('hello');
} else /* comment */ if (somethingElse) { alert('hi');
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function getErrorList($testFile='')
242 => 1,
260 => 1,
269 => 1,
278 => 1,
];

case 'InlineControlStructureUnitTest.js':
Expand All @@ -90,6 +91,7 @@ public function getErrorList($testFile='')
21 => 1,
27 => 1,
30 => 1,
35 => 1,
];

default:
Expand Down

0 comments on commit f83ba7e

Please sign in to comment.