Skip to content

Commit

Permalink
Fixed bug #2867 : Incorrect scope matching when arrow function used i…
Browse files Browse the repository at this point in the history
…nside IF condition
  • Loading branch information
gsherwood committed Mar 25, 2020
1 parent caddfde commit 27cbb66
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Fixed bug #2853 : Undefined variable error when using Info report
-- Thanks to Juliette Reinders Folmer for the patch
- Fixed bug #2865 : Double arrow tokenized as T_STRING when placed after function named "fn"
- Fixed bug #2867 : Incorrect scope matching when arrow function used inside IF condition
- Fixed bug #2868 : phpcs:ignore annotation doesnt work inside a docblock
- Fixed bug #2878 : PSR12.Files.FileHeader conflicts with Generic.Files.LineEndings
- Fixed bug #2895 : PSR2.Methods.FunctionCallSignature.MultipleArguments false positive with arrow function argument
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,6 @@ $i = 10;
while ($i > 0 && --$i);

for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);

if ($this->valid(fn(): bool => 2 > 1)) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,6 @@ $i = 10;
while ($i > 0 && --$i);

for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);

if ($this->valid(fn(): bool => 2 > 1)) {
}
32 changes: 24 additions & 8 deletions src/Tokenizers/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1314,15 +1314,31 @@ private function recurseScopeMap($stackPtr, $depth=1, &$ignore=0)
}//end if

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

$opener = $i;
}
if (($openerNested === true && $ownerNested === false)
|| ($openerNested === false && $ownerNested === true)
|| ($openerNested === true
&& $this->tokens[$i]['nested_parenthesis'] !== $this->tokens[$stackPtr]['nested_parenthesis'])
) {
// We found the a token that looks like the opener, but it's nested differently.
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $this->tokens[$i]['type'];
echo str_repeat("\t", $depth);
echo "* ignoring possible opener $i:$type as nested parenthesis don't match *".PHP_EOL;
}
} else {
// We found the opening scope token for $currType.
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$type = $this->tokens[$stackPtr]['type'];
echo str_repeat("\t", $depth);
echo "=> Found scope opener for $stackPtr:$type".PHP_EOL;
}

$opener = $i;
}
}//end if
} else if ($tokenType === T_SEMICOLON
&& $opener === null
&& (isset($this->tokens[$stackPtr]['parenthesis_closer']) === false
Expand Down

0 comments on commit 27cbb66

Please sign in to comment.