Skip to content

Commit c70325b

Browse files
committed
Fix FunctionReturnStatementsNode to not contain statements from nested closures and anonymous functions
1 parent a81a8c3 commit c70325b

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,14 @@ private function processStmtNode(
419419
$nodeCallback(new InFunctionNode($stmt), $functionScope);
420420

421421
$gatheredReturnStatements = [];
422-
$statementResult = $this->processStmtNodes($stmt, $stmt->stmts, $functionScope, static function (\PhpParser\Node $node, Scope $scope) use ($nodeCallback, &$gatheredReturnStatements): void {
422+
$statementResult = $this->processStmtNodes($stmt, $stmt->stmts, $functionScope, static function (\PhpParser\Node $node, Scope $scope) use ($nodeCallback, $functionScope, &$gatheredReturnStatements): void {
423423
$nodeCallback($node, $scope);
424+
if ($scope->getFunction() !== $functionScope->getFunction()) {
425+
return;
426+
}
427+
if ($scope->isInAnonymousFunction()) {
428+
return;
429+
}
424430
if (!$node instanceof Return_) {
425431
return;
426432
}

tests/PHPStan/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public function testRule(): void
2828
'Function TooWideFunctionReturnType\baz() never returns null so it can be removed from the return typehint.',
2929
15,
3030
],
31+
[
32+
'Function TooWideFunctionReturnType\ipsum() never returns null so it can be removed from the return typehint.',
33+
27,
34+
],
3135
]);
3236
}
3337

tests/PHPStan/Rules/TooWideTypehints/data/tooWideFunctionReturnType.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,17 @@ function lorem(): ?string {
2323

2424
return null;
2525
}
26+
27+
function ipsum(): ?string {
28+
$f = function () {
29+
return null;
30+
};
31+
32+
$c = new class () {
33+
public function doFoo() {
34+
return null;
35+
}
36+
};
37+
38+
return 'str';
39+
}

0 commit comments

Comments
 (0)