Skip to content

Commit ea5f4c2

Browse files
committed
fix: use passed to type to compare return types in ClosureReturnTypeRule
1 parent 57e8687 commit ea5f4c2

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

Diff for: src/Analyser/NodeScopeResolver.php

+2
Original file line numberDiff line numberDiff line change
@@ -4311,6 +4311,7 @@ private function processClosureNode(
43114311
$statementResult,
43124312
$executionEnds,
43134313
array_merge($statementResult->getImpurePoints(), $closureImpurePoints),
4314+
$passedToType,
43144315
), $closureScope);
43154316

43164317
return new ProcessClosureResult($scope, $statementResult->getThrowPoints(), $statementResult->getImpurePoints(), $invalidateExpressions);
@@ -4357,6 +4358,7 @@ private function processClosureNode(
43574358
$statementResult,
43584359
$executionEnds,
43594360
array_merge($statementResult->getImpurePoints(), $closureImpurePoints),
4361+
$passedToType,
43604362
), $closureScope);
43614363

43624364
return new ProcessClosureResult($scope->processClosureScope($closureResultScope, null, $byRefUses), $statementResult->getThrowPoints(), $statementResult->getImpurePoints(), $invalidateExpressions);

Diff for: src/Node/ClosureReturnStatementsNode.php

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpParser\NodeAbstract;
1010
use PHPStan\Analyser\ImpurePoint;
1111
use PHPStan\Analyser\StatementResult;
12+
use PHPStan\Type\Type;
1213
use function count;
1314

1415
/**
@@ -33,6 +34,7 @@ public function __construct(
3334
private StatementResult $statementResult,
3435
private array $executionEnds,
3536
private array $impurePoints,
37+
private ?Type $passedToType,
3638
)
3739
{
3840
parent::__construct($closureExpr->getAttributes());
@@ -84,6 +86,11 @@ public function returnsByRef(): bool
8486
return $this->closureExpr->byRef;
8587
}
8688

89+
public function getPassedToType(): ?Type
90+
{
91+
return $this->passedToType;
92+
}
93+
8794
public function getType(): string
8895
{
8996
return 'PHPStan_Node_ClosureReturnStatementsNode';

Diff for: src/Rules/Functions/ClosureReturnTypeRule.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\Node\ClosureReturnStatementsNode;
88
use PHPStan\Rules\FunctionReturnTypeCheck;
99
use PHPStan\Rules\Rule;
10+
use PHPStan\Type\CallableType;
1011
use PHPStan\Type\Type;
1112
use PHPStan\Type\TypeCombinator;
1213

@@ -31,8 +32,13 @@ public function processNode(Node $node, Scope $scope): array
3132
return [];
3233
}
3334

34-
/** @var Type $returnType */
35-
$returnType = $scope->getAnonymousFunctionReturnType();
35+
if ($node->getPassedToType() instanceof CallableType) {
36+
$returnType = $node->getPassedToType()->getReturnType();
37+
} else {
38+
/** @var Type $returnType */
39+
$returnType = $scope->getAnonymousFunctionReturnType();
40+
}
41+
3642
$containsNull = TypeCombinator::containsNull($returnType);
3743
$hasNativeTypehint = $node->getClosureExpr()->returnType !== null;
3844

Diff for: tests/PHPStan/Rules/Functions/WeirdBugTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Rules\Rule;
77
use PHPStan\Rules\RuleLevelHelper;
88
use PHPStan\Testing\RuleTestCase;
9+
use const PHP_VERSION_ID;
910

1011
/**
1112
* @extends RuleTestCase<ClosureReturnTypeRule>

0 commit comments

Comments
 (0)