Skip to content

Commit 22b8abb

Browse files
committed
Fixed bug #592 : USE statement tokenising can sometimes result in mismatched scopes
1 parent 980ce82 commit 22b8abb

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

CodeSniffer/File.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,6 +1837,14 @@ private static function _createScopeMap(&$tokens, $tokenizer, $eolChar)
18371837
echo "\tStart scope map at $i:$type => $content".PHP_EOL;
18381838
}
18391839

1840+
if (isset($tokens[$i]['scope_condition']) === true) {
1841+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1842+
echo "\t* already processed, skipping *".PHP_EOL;
1843+
}
1844+
1845+
continue;
1846+
}
1847+
18401848
$i = self::_recurseScopeMap(
18411849
$tokens,
18421850
$numTokens,

CodeSniffer/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,18 @@ switch (true) {
8888
return null;
8989
}
9090
}
91-
?>
91+
92+
use Vendor\Test\FooBar;
93+
94+
function test()
95+
{
96+
switch ($val) {
97+
case 'foo':
98+
echo 'foo';
99+
break;
100+
default:
101+
echo 'foo';
102+
}
103+
104+
exit;
105+
}

CodeSniffer/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc.fixed

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,18 @@ switch (true) {
8888
return null;
8989
}
9090
}
91-
?>
91+
92+
use Vendor\Test\FooBar;
93+
94+
function test()
95+
{
96+
switch ($val) {
97+
case 'foo':
98+
echo 'foo';
99+
break;
100+
default:
101+
echo 'foo';
102+
}
103+
104+
exit;
105+
}

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
3333
- Added more guard code for syntax errors to various sniffs
3434
- Squiz CommentedOutCodeSniff is now better at ignoring URLs inside comments
3535
- Fixed bug #584 : Squiz.Arrays.ArrayDeclaration sniff gives incorrect NoComma error for multiline string values
36+
- Fixed bug #592 : USE statement tokenising can sometimes result in mismatched scopes
3637
- Fixed bug #594 : Tokenizer issue on closure that returns by reference
3738
</notes>
3839
<contents>

0 commit comments

Comments
 (0)