Skip to content

Commit

Permalink
Fixed bug #543 : SWITCH with closure in condition generates inline co…
Browse files Browse the repository at this point in the history
…ntrol structure error
  • Loading branch information
gsherwood committed Apr 21, 2015
1 parent 18f98cc commit 30c618e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
23 changes: 22 additions & 1 deletion CodeSniffer/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2067,6 +2067,27 @@ private static function _recurseScopeMap(
continue;
}

if ($tokenType === T_FUNCTION) {
// Probably a closure, so process it manually.
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $tokens[$stackPtr]['type'];
echo str_repeat("\t", $depth);
echo "=> Found function before scope opener for $stackPtr:$type, processing manually".PHP_EOL;
}

$i = self::_recurseScopeMap(
$tokens,
$numTokens,
$tokenizer,
$eolChar,
$i,
($depth + 1),
$ignore
);

continue;
}//end if

// Found another opening condition but still haven't
// found our opener, so we are never going to find one.
if (PHP_CODESNIFFER_VERBOSITY > 1) {
Expand All @@ -2076,7 +2097,7 @@ private static function _recurseScopeMap(
}

return $stackPtr;
}
}//end if

if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo str_repeat("\t", $depth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,13 @@ if ($test) {
if ($test) {
} else {
}

switch($response = \Bar::baz('bat', function ($foo) {
return 'bar';
})) {
case 1:
return 'test';

case 2:
return 'other';
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,13 @@ if ($test) {
if ($test) {
} else {
}

switch($response = \Bar::baz('bat', function ($foo) {
return 'bar';
})) {
case 1:
return 'test';

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

0 comments on commit 30c618e

Please sign in to comment.