Skip to content

Commit 33306db

Browse files
committed
Fixed bug #527 : Closure inside IF statement is not tokenized correctly
1 parent f307928 commit 33306db

File tree

6 files changed

+82
-35
lines changed

6 files changed

+82
-35
lines changed

CodeSniffer/File.php

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,32 +2154,45 @@ private static function _recurseScopeMap(
21542154
&& $opener === null
21552155
) {
21562156
if ($tokenType === T_OPEN_CURLY_BRACKET) {
2157-
// Make sure this is actually an opener and not a
2158-
// string offset (e.g., $var{0}).
2159-
for ($x = ($i - 1); $x > 0; $x--) {
2160-
if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$x]['code']]) === true) {
2161-
continue;
2162-
} else {
2163-
// If the first non-whitespace/comment token is a
2164-
// variable or object operator then this is an opener
2165-
// for a string offset and not a scope.
2166-
if ($tokens[$x]['code'] === T_VARIABLE
2167-
|| $tokens[$x]['code'] === T_OBJECT_OPERATOR
2168-
) {
2169-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
2170-
echo str_repeat("\t", $depth);
2171-
echo '* ignoring curly brace *'.PHP_EOL;
2172-
}
2157+
if (isset($tokens[$stackPtr]['parenthesis_closer']) === true
2158+
&& $i < $tokens[$stackPtr]['parenthesis_closer']
2159+
) {
2160+
// We found a curly brace inside the condition of the
2161+
// current scope opener, so it must be a string offset.
2162+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
2163+
echo str_repeat("\t", $depth);
2164+
echo '* ignoring curly brace *'.PHP_EOL;
2165+
}
21732166

2174-
$ignore++;
2175-
}//end if
2167+
$ignore++;
2168+
} else {
2169+
// Make sure this is actually an opener and not a
2170+
// string offset (e.g., $var{0}).
2171+
for ($x = ($i - 1); $x > 0; $x--) {
2172+
if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$x]['code']]) === true) {
2173+
continue;
2174+
} else {
2175+
// If the first non-whitespace/comment token is a
2176+
// variable or object operator then this is an opener
2177+
// for a string offset and not a scope.
2178+
if ($tokens[$x]['code'] === T_VARIABLE
2179+
|| $tokens[$x]['code'] === T_OBJECT_OPERATOR
2180+
) {
2181+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
2182+
echo str_repeat("\t", $depth);
2183+
echo '* ignoring curly brace *'.PHP_EOL;
2184+
}
21762185

2177-
break;
2178-
}//end if
2179-
}//end for
2186+
$ignore++;
2187+
}//end if
2188+
2189+
break;
2190+
}//end if
2191+
}//end for
2192+
}//end if
21802193
}//end if
21812194

2182-
if ($ignore === 0) {
2195+
if ($ignore === 0 || $tokenType !== T_OPEN_CURLY_BRACKET) {
21832196
// We found the opening scope token for $currType.
21842197
if (PHP_CODESNIFFER_VERBOSITY > 1) {
21852198
$type = $tokens[$stackPtr]['type'];
@@ -2196,22 +2209,13 @@ private static function _recurseScopeMap(
21962209
&& isset($tokens[$i]['parenthesis_closer']) === true
21972210
) {
21982211
// If we get into here, then we opened a parenthesis for
2199-
// a scope (eg. an if or else if). We can just skip to
2200-
// the closing parenthesis.
2201-
$i = $tokens[$i]['parenthesis_closer'];
2202-
2203-
// Update the start of the line so that when we check to see
2212+
// a scope (eg. an if or else if) so we need to update the
2213+
// start of the line so that when we check to see
22042214
// if the closing parenthesis is more than 3 lines away from
22052215
// the statement, we check from the closing parenthesis.
2206-
$startLine
2207-
= $tokens[$tokens[$i]['parenthesis_closer']]['line'];
2208-
2209-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
2210-
echo str_repeat("\t", $depth);
2211-
echo '* skipping parenthesis *'.PHP_EOL;
2212-
}
2216+
$startLine = $tokens[$tokens[$i]['parenthesis_closer']]['line'];
22132217
}
2214-
}//end if
2218+
}
22152219
} else if ($tokenType === T_OPEN_CURLY_BRACKET && $opener !== null) {
22162220
// We opened something that we don't have a scope opener for.
22172221
// Examples of this are curly brackets for string offsets etc.
@@ -2223,6 +2227,14 @@ private static function _recurseScopeMap(
22232227
}
22242228

22252229
$ignore++;
2230+
} else if ($tokenType === T_CLOSE_CURLY_BRACKET && $ignore > 0) {
2231+
// We found the end token for the opener we were ignoring.
2232+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
2233+
echo str_repeat("\t", $depth);
2234+
echo '* finished ignoring curly brace *'.PHP_EOL;
2235+
}
2236+
2237+
$ignore--;
22262238
} else if ($opener === null
22272239
&& isset($tokenizer->scopeOpeners[$currType]) === true
22282240
) {

CodeSniffer/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,12 @@ Foo
8989
END
9090
,
9191
'bar');
92+
93+
if (array_filter(
94+
$commands,
95+
function ($cmd) use ($commandName) {
96+
return ($cmd['name'] == $commandName);
97+
}
98+
)) {
99+
// Do something
100+
}

CodeSniffer/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.inc.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,12 @@ END
9696
,
9797
'bar'
9898
);
99+
100+
if (array_filter(
101+
$commands,
102+
function ($cmd) use ($commandName) {
103+
return ($cmd['name'] == $commandName);
104+
}
105+
)) {
106+
// Do something
107+
}

CodeSniffer/Standards/Squiz/Tests/Functions/MultiLineFunctionDeclarationUnitTest.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,12 @@ function myFunction(
102102
$firstParameter,$secondParameter=[1,2,3],$third=null
103103
) {
104104
}
105+
106+
if (array_filter(
107+
$commands,
108+
function ($cmd) use ($commandName) {
109+
return ($cmd['name'] == $commandName);
110+
}
111+
)) {
112+
// Do something
113+
}

CodeSniffer/Standards/Squiz/Tests/WhiteSpace/ScopeClosingBraceUnitTest.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ foreach ($elements as $element) {
4545
}
4646
}
4747

48+
if ($token['code'] === T_COMMENT
49+
&& $multiLineComment === false
50+
&& (substr($token['content'], 0, 2) === '//'
51+
|| $token['content']{0} === '#')
52+
) {
53+
}
54+
4855
switch ($blah) {
4956
case 'one':
5057
echo 'one';

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
4141
- Fixed bug #479 : Yielded values are not recognised as returned values in Squiz FunctionComment sniff
4242
- Fixed bug #512 : Endless loop whilst parsing mixture of control structure styles
4343
- Fixed bug #515 : Spaces in JS block incorrectly flagged as indentation error
44+
- Fixed bug #527 : Closure inside IF statement is not tokenized correctly
4445
</notes>
4546
<contents>
4647
<dir name="/">

0 commit comments

Comments
 (0)