Skip to content

Commit f83ba7e

Browse files
committed
Generic/InlineControlStructure: fix error when handling else if
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`.
1 parent 72b50b9 commit f83ba7e

File tree

6 files changed

+21
-1
lines changed

6 files changed

+21
-1
lines changed

src/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function process(File $phpcsFile, $stackPtr)
7575

7676
// Ignore the ELSE in ELSE IF. We'll process the IF part later.
7777
if ($tokens[$stackPtr]['code'] === T_ELSE) {
78-
$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
78+
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
7979
if ($tokens[$next]['code'] === T_IF) {
8080
return;
8181
}

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,7 @@ function testFinally()
272272
} finally {
273273
}
274274
}
275+
276+
if ($something) {
277+
echo 'hello';
278+
} else /* comment */ if ($somethingElse) echo 'hi';

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,8 @@ function testFinally()
307307
}
308308
}
309309
}
310+
311+
if ($something) {
312+
echo 'hello';
313+
} else /* comment */ if ($somethingElse) { echo 'hi';
314+
}

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ if ($("#myid").rotationDegrees()=='90')
2929

3030
if ($("#myid").rotationDegrees()=='90')
3131
$foo = {'transform': 'rotate(90deg)'};
32+
33+
if (something) {
34+
alert('hello');
35+
} else /* comment */ if (somethingElse) alert('hi');

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.js.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,8 @@ if ($("#myid").rotationDegrees()=='90') {
3737
if ($("#myid").rotationDegrees()=='90') {
3838
$foo = {'transform': 'rotate(90deg)'};
3939
}
40+
41+
if (something) {
42+
alert('hello');
43+
} else /* comment */ if (somethingElse) { alert('hi');
44+
}

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public function getErrorList($testFile='')
7878
242 => 1,
7979
260 => 1,
8080
269 => 1,
81+
278 => 1,
8182
];
8283

8384
case 'InlineControlStructureUnitTest.js':
@@ -90,6 +91,7 @@ public function getErrorList($testFile='')
9091
21 => 1,
9192
27 => 1,
9293
30 => 1,
94+
35 => 1,
9395
];
9496

9597
default:

0 commit comments

Comments
 (0)