Skip to content

Commit

Permalink
Use custom logic for finding arrow function scope (#296)
Browse files Browse the repository at this point in the history
* Add test for arrow function as argument

* Do not rely on phpcs to find scope for arrow functions
  • Loading branch information
sirbrillig committed Mar 30, 2023
1 parent 11b9433 commit 4cca2aa
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
5 changes: 5 additions & 0 deletions Tests/VariableAnalysisSniff/fixtures/ArrowFunctionFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,8 @@ function staticArrowFunctionAsVariableWithUnusedInside($subject) {
$arrowFunc = static fn($foo) => $subject; // unused variable $foo
echo $arrowFunc('hello');
}

function arrowFunctionAsExpressionInArgumentWithArray() {
$type = do_something(fn($array, $needle) => $array[2] === $needle);
echo $type;
}
6 changes: 0 additions & 6 deletions VariableAnalysis/Lib/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,12 +628,6 @@ public static function isArrowFunction(File $phpcsFile, $stackPtr)
public static function getArrowFunctionOpenClose(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if (defined('T_FN') && $tokens[$stackPtr]['code'] === T_FN) {
return [
'scope_opener' => $tokens[$stackPtr]['scope_opener'],
'scope_closer' => $tokens[$stackPtr]['scope_closer'],
];
}
if ($tokens[$stackPtr]['content'] !== 'fn') {
return null;
}
Expand Down

0 comments on commit 4cca2aa

Please sign in to comment.