Skip to content

Place static arrow function body outside of scope on static declaration check (#275) #276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Tests/VariableAnalysisSniff/ArrowFunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function testArrowFunctions()
61,
67,
71,
87,
];
$this->assertSame($expectedWarnings, $lines);
}
Expand Down Expand Up @@ -58,6 +59,7 @@ public function testArrowFunctionsWithoutUnusedBeforeUsed()
63,
67,
71,
87,
];
$this->assertSame($expectedWarnings, $lines);
}
Expand Down
10 changes: 10 additions & 0 deletions Tests/VariableAnalysisSniff/fixtures/ArrowFunctionFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,13 @@ function arrowFunctionWithVariableUsedInsideQuotes($allowed_extensions) {
$data = array_map( fn($extension) => ".{$extension}", $allowed_extensions );
return $data;
}

function staticArrowFunctionAsVariableWithUsedInside($subject) {
$arrowFunc = static fn($foo) => $foo . $subject;
echo $arrowFunc('hello');
}

function staticArrowFunctionAsVariableWithUnusedInside($subject) {
$arrowFunc = static fn($foo) => $subject; // unused variable $foo
echo $arrowFunc('hello');
}
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ protected function processVariableAsStaticDeclaration(File $phpcsFile, $stackPtr
$tokens = $phpcsFile->getTokens();

// Search backwards for a `static` keyword that occurs before the start of the statement.
$startOfStatement = $phpcsFile->findPrevious([T_SEMICOLON, T_OPEN_CURLY_BRACKET], $stackPtr - 1, null, false, null, true);
$startOfStatement = $phpcsFile->findPrevious([T_SEMICOLON, T_OPEN_CURLY_BRACKET, T_FN_ARROW], $stackPtr - 1, null, false, null, true);
$staticPtr = $phpcsFile->findPrevious([T_STATIC], $stackPtr - 1, null, false, null, true);
if (! is_int($startOfStatement)) {
$startOfStatement = 1;
Expand Down