Skip to content

Commit 30c618e

Browse files
committed
Fixed bug #543 : SWITCH with closure in condition generates inline control structure error
1 parent 18f98cc commit 30c618e

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

CodeSniffer/File.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2067,6 +2067,27 @@ private static function _recurseScopeMap(
20672067
continue;
20682068
}
20692069

2070+
if ($tokenType === T_FUNCTION) {
2071+
// Probably a closure, so process it manually.
2072+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
2073+
$type = $tokens[$stackPtr]['type'];
2074+
echo str_repeat("\t", $depth);
2075+
echo "=> Found function before scope opener for $stackPtr:$type, processing manually".PHP_EOL;
2076+
}
2077+
2078+
$i = self::_recurseScopeMap(
2079+
$tokens,
2080+
$numTokens,
2081+
$tokenizer,
2082+
$eolChar,
2083+
$i,
2084+
($depth + 1),
2085+
$ignore
2086+
);
2087+
2088+
continue;
2089+
}//end if
2090+
20702091
// Found another opening condition but still haven't
20712092
// found our opener, so we are never going to find one.
20722093
if (PHP_CODESNIFFER_VERBOSITY > 1) {
@@ -2076,7 +2097,7 @@ private static function _recurseScopeMap(
20762097
}
20772098

20782099
return $stackPtr;
2079-
}
2100+
}//end if
20802101

20812102
if (PHP_CODESNIFFER_VERBOSITY > 1) {
20822103
echo str_repeat("\t", $depth);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,13 @@ if ($test) {
105105
if ($test) {
106106
} else {
107107
}
108+
109+
switch($response = \Bar::baz('bat', function ($foo) {
110+
return 'bar';
111+
})) {
112+
case 1:
113+
return 'test';
114+
115+
case 2:
116+
return 'other';
117+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,13 @@ if ($test) {
106106
if ($test) {
107107
} else {
108108
}
109+
110+
switch($response = \Bar::baz('bat', function ($foo) {
111+
return 'bar';
112+
})) {
113+
case 1:
114+
return 'test';
115+
116+
case 2:
117+
return 'other';
118+
}

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
4545
- Fixed bug #515 : Spaces in JS block incorrectly flagged as indentation error
4646
- Fixed bug #523 : Generic ScopeIndent errors for IF in FINALLY
4747
- Fixed bug #527 : Closure inside IF statement is not tokenized correctly
48+
- Fixed bug #543 : SWITCH with closure in condition generates inline control structure error
4849
- Fixed bug #551 : Multiple catch blocks not checked in Squiz.ControlStructures.ControlSignature sniff
4950
- Fixed bug #554 : ScopeIndentSniff causes errors when encountering an unmatched parenthesis
5051
- Fixed bug #558 : PHPCBF adds brace for ELSE IF split over multiple lines

0 commit comments

Comments
 (0)