Repository navigation
Expand file tree
/
Copy pathArrowFunctionHandler.php
More file actions
73 lines (63 loc) · 2.48 KB
/
Copy pathArrowFunctionHandler.php
File metadata and controls
73 lines (63 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php declare(strict_types = 1);
namespace PHPStan\Analyser\ExprHandler;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\ExpressionContext;
use PHPStan\Analyser\ExpressionResult;
use PHPStan\Analyser\ExpressionResultFactory;
use PHPStan\Analyser\ExpressionResultStorage;
use PHPStan\Analyser\ExprHandler;
use PHPStan\Analyser\ExprHandler\Helper\ClosureTypeResolver;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Type\Type;
/**
* @implements ExprHandler<ArrowFunction>
*/
#[AutowiredService]
final class ArrowFunctionHandler implements ExprHandler
{
public function __construct(
private ClosureTypeResolver $closureTypeResolver,
private ExpressionResultFactory $expressionResultFactory,
)
{
}
public function supports(Expr $expr): bool
{
return $expr instanceof ArrowFunction;
}
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
{
// an arrow function nested inside a call argument (array literal,
// ternary) carries the parameter type it is passed to - see
// NodeScopeResolver::annotateNestedClosuresWithPassedToType()
[$passedToType, $nativePassedToType] = $expr->getAttribute(NodeScopeResolver::CLOSURE_PASSED_TO_TYPE_ATTRIBUTE) ?? [null, null];
$arrowFunctionResult = $nodeScopeResolver->processArrowFunctionNode($stmt, $expr, $scope, $storage, $nodeCallback, $passedToType, $nativePassedToType);
$this->closureTypeResolver->seedCacheFromArrowFunctionWalk($scope, $expr, $arrowFunctionResult);
$result = $arrowFunctionResult->getExpressionResult();
return $this->expressionResultFactory->create(
$result->getScope(),
beforeScope: $scope,
expr: $expr,
hasYield: $result->hasYield(),
isAlwaysTerminating: false,
throwPoints: [],
impurePoints: [],
);
}
public function resolveType(MutatingScope $scope, Expr $expr): Type
{
return $this->closureTypeResolver->getClosureType($scope, $expr);
}
public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $expr, TypeSpecifierContext $context): SpecifiedTypes
{
return $typeSpecifier->specifyDefaultTypes($scope, $expr, $context);
}
}