Skip to content

Commit 18f98cc

Browse files
committed
Fixed bug #558 : PHPCBF adds brace for ELSE IF split over multiple lines
1 parent 0f06788 commit 18f98cc

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,11 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
8787
}
8888

8989
// Ignore the ELSE in ELSE IF. We'll process the IF part later.
90-
if (($tokens[$stackPtr]['code'] === T_ELSE) && ($tokens[($stackPtr + 2)]['code'] === T_IF)) {
91-
return;
90+
if ($tokens[$stackPtr]['code'] === T_ELSE) {
91+
$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
92+
if ($tokens[$next]['code'] === T_IF) {
93+
return;
94+
}
9295
}
9396

9497
if ($tokens[$stackPtr]['code'] === T_WHILE) {

CodeSniffer/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,10 @@ if ($this->allowShopping !== true):
9898
else:
9999
echo 'foo';
100100
endif;
101+
102+
// ELSE IF split over multiple lines (not inline)
103+
if ($test) {
104+
} else
105+
if ($test) {
106+
} else {
107+
}

CodeSniffer/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.inc.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,10 @@ if ($this->allowShopping !== true):
9999
else:
100100
echo 'foo';
101101
endif;
102+
103+
// ELSE IF split over multiple lines (not inline)
104+
if ($test) {
105+
} else
106+
if ($test) {
107+
} else {
108+
}

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
4747
- Fixed bug #527 : Closure inside IF statement is not tokenized correctly
4848
- Fixed bug #551 : Multiple catch blocks not checked in Squiz.ControlStructures.ControlSignature sniff
4949
- Fixed bug #554 : ScopeIndentSniff causes errors when encountering an unmatched parenthesis
50+
- Fixed bug #558 : PHPCBF adds brace for ELSE IF split over multiple lines
5051
</notes>
5152
<contents>
5253
<dir name="/">

0 commit comments

Comments
 (0)