Skip to content

Commit 27cbb66

Browse files
committed
Fixed bug #2867 : Incorrect scope matching when arrow function used inside IF condition
1 parent caddfde commit 27cbb66

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
3939
- Fixed bug #2853 : Undefined variable error when using Info report
4040
-- Thanks to Juliette Reinders Folmer for the patch
4141
- Fixed bug #2865 : Double arrow tokenized as T_STRING when placed after function named "fn"
42+
- Fixed bug #2867 : Incorrect scope matching when arrow function used inside IF condition
4243
- Fixed bug #2868 : phpcs:ignore annotation doesnt work inside a docblock
4344
- Fixed bug #2878 : PSR12.Files.FileHeader conflicts with Generic.Files.LineEndings
4445
- Fixed bug #2895 : PSR2.Methods.FunctionCallSignature.MultipleArguments false positive with arrow function argument

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,6 @@ $i = 10;
250250
while ($i > 0 && --$i);
251251

252252
for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
253+
254+
if ($this->valid(fn(): bool => 2 > 1)) {
255+
}

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,6 @@ $i = 10;
283283
while ($i > 0 && --$i);
284284

285285
for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
286+
287+
if ($this->valid(fn(): bool => 2 > 1)) {
288+
}

src/Tokenizers/Tokenizer.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,15 +1314,31 @@ private function recurseScopeMap($stackPtr, $depth=1, &$ignore=0)
13141314
}//end if
13151315

13161316
if ($ignore === 0 || $tokenType !== T_OPEN_CURLY_BRACKET) {
1317-
// We found the opening scope token for $currType.
1318-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1319-
$type = $this->tokens[$stackPtr]['type'];
1320-
echo str_repeat("\t", $depth);
1321-
echo "=> Found scope opener for $stackPtr:$type".PHP_EOL;
1322-
}
1317+
$openerNested = isset($this->tokens[$i]['nested_parenthesis']);
1318+
$ownerNested = isset($this->tokens[$stackPtr]['nested_parenthesis']);
13231319

1324-
$opener = $i;
1325-
}
1320+
if (($openerNested === true && $ownerNested === false)
1321+
|| ($openerNested === false && $ownerNested === true)
1322+
|| ($openerNested === true
1323+
&& $this->tokens[$i]['nested_parenthesis'] !== $this->tokens[$stackPtr]['nested_parenthesis'])
1324+
) {
1325+
// We found the a token that looks like the opener, but it's nested differently.
1326+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1327+
$type = $this->tokens[$i]['type'];
1328+
echo str_repeat("\t", $depth);
1329+
echo "* ignoring possible opener $i:$type as nested parenthesis don't match *".PHP_EOL;
1330+
}
1331+
} else {
1332+
// We found the opening scope token for $currType.
1333+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1334+
$type = $this->tokens[$stackPtr]['type'];
1335+
echo str_repeat("\t", $depth);
1336+
echo "=> Found scope opener for $stackPtr:$type".PHP_EOL;
1337+
}
1338+
1339+
$opener = $i;
1340+
}
1341+
}//end if
13261342
} else if ($tokenType === T_SEMICOLON
13271343
&& $opener === null
13281344
&& (isset($this->tokens[$stackPtr]['parenthesis_closer']) === false

0 commit comments

Comments
 (0)