Repository navigation
Expand file tree
/
Copy pathFunctionHandler.php
More file actions
185 lines (168 loc) · 6.96 KB
/
Copy pathFunctionHandler.php
File metadata and controls
185 lines (168 loc) · 6.96 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php declare(strict_types = 1);
namespace PHPStan\Analyser\StmtHandler;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Analyser\AttributesHandler;
use PHPStan\Analyser\DeprecatedAttributeResolver;
use PHPStan\Analyser\ExpressionResultStorage;
use PHPStan\Analyser\ImpurePoint;
use PHPStan\Analyser\InternalStatementResult;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\ParametersProcessor;
use PHPStan\Analyser\PhpDocsResolver;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\StatementContext;
use PHPStan\Analyser\StmtHandler;
use PHPStan\Analyser\VariableLivenessResolver;
use PHPStan\Dependency\Dependencies;
use PHPStan\Dependency\DependencyTypes;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\ExecutionEndNode;
use PHPStan\Node\FunctionReturnStatementsNode;
use PHPStan\Node\InFunctionNode;
use PHPStan\Node\PropertyAssignNode;
use PHPStan\Node\ReturnAfterFinallyNode;
use PHPStan\Node\ReturnStatement;
use PHPStan\Node\YieldStatement;
use PHPStan\Reflection\Php\PhpFunctionFromParserNodeReflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\Turbo\ShadowedByTurboExtension;
use function array_merge;
/**
* @implements StmtHandler<Function_>
*/
#[AutowiredService]
#[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/FunctionHandler.cpp')]
final class FunctionHandler implements StmtHandler
{
public function __construct(
private DeprecatedAttributeResolver $deprecatedAttributeResolver,
private PhpDocsResolver $phpDocsResolver,
private AttributesHandler $attributesHandler,
private ParametersProcessor $parametersProcessor,
)
{
}
public function supports(Stmt $stmt): bool
{
return $stmt instanceof Function_;
}
public function processStmt(
NodeScopeResolver $nodeScopeResolver,
Stmt $stmt,
MutatingScope $scope,
ExpressionResultStorage $storage,
callable $nodeCallback,
StatementContext $context,
): InternalStatementResult
{
$dependencies = [];
$dependencies[] = $this->attributesHandler->processAttributeGroups($nodeScopeResolver, $stmt, $stmt->attrGroups, $scope, $storage, $nodeCallback);
[$templateTypeMap, $phpDocParameterTypes, $phpDocImmediatelyInvokedCallableParameters, $phpDocClosureThisTypeParameters, $phpDocReturnType, $phpDocThrowType, $deprecatedDescription, $isDeprecated, $isInternal, , $isPure, $acceptsNamedArguments, , $phpDocComment, $asserts,, $phpDocParameterOutTypes, , , , $pureUnlessCallableIsImpureParameters, $pureUnlessParameterPassedParameters] = $this->phpDocsResolver->getPhpDocs($scope, $stmt);
$dependencies[] = $this->parametersProcessor->processParams($nodeScopeResolver, $stmt, $stmt->params, $scope, $storage, $nodeCallback);
if ($stmt->returnType !== null) {
$nodeScopeResolver->callNodeCallback($nodeCallback, $stmt->returnType, $scope, $storage);
}
if (!$isDeprecated) {
[$isDeprecated, $deprecatedDescription] = $this->deprecatedAttributeResolver->getDeprecatedAttribute($scope, $stmt);
}
$functionScope = $scope->enterFunction(
$stmt,
$templateTypeMap,
$phpDocParameterTypes,
$phpDocReturnType,
$phpDocThrowType,
$deprecatedDescription,
$isDeprecated,
$isInternal,
$isPure,
$acceptsNamedArguments,
$asserts,
$phpDocComment,
$phpDocParameterOutTypes,
$phpDocImmediatelyInvokedCallableParameters,
$phpDocClosureThisTypeParameters,
$pureUnlessCallableIsImpureParameters,
$pureUnlessParameterPassedParameters,
);
$functionReflection = $functionScope->getFunction();
if (!$functionReflection instanceof PhpFunctionFromParserNodeReflection) {
throw new ShouldNotHappenException();
}
$dependencies[] = Dependencies::create($scope->getFile(), DependencyTypes::ofDeclaration($functionReflection, true));
$nodeScopeResolver->callNodeCallback($nodeCallback, new InFunctionNode($functionReflection, $stmt), $functionScope, $storage);
$gatheredReturnStatements = [];
$gatheredReturnStatementsAfterFinally = [];
$gatheredYieldStatements = [];
$executionEnds = [];
$functionImpurePoints = [];
// the body's results live in a per-body storage released right after
// the FunctionReturnStatementsNode rules ran - see the ClassMethod
// branch for the reasoning
$bodyStorage = $storage->duplicate();
$scope->pushExpressionResultStorage($bodyStorage);
try {
$nodeScopeResolver->pushNodeGatherer(static function (Node $node, Scope $scope) use ($functionScope, &$gatheredReturnStatements, &$gatheredReturnStatementsAfterFinally, &$gatheredYieldStatements, &$executionEnds, &$functionImpurePoints): void {
if ($scope->getFunction() !== $functionScope->getFunction()) {
return;
}
if ($scope->isInAnonymousFunction()) {
return;
}
if ($node instanceof PropertyAssignNode) {
$functionImpurePoints[] = new ImpurePoint(
$scope,
$node,
'propertyAssign',
'property assignment',
true,
);
return;
}
if ($node instanceof ExecutionEndNode) {
$executionEnds[] = $node;
return;
}
if ($node instanceof ReturnAfterFinallyNode) {
$gatheredReturnStatementsAfterFinally[] = new ReturnStatement($scope, $node->getReturnNode());
return;
}
if ($node instanceof Expr\Yield_ || $node instanceof Expr\YieldFrom) {
$gatheredYieldStatements[] = new YieldStatement($scope, $node);
}
if (!$node instanceof Return_) {
return;
}
$gatheredReturnStatements[] = new ReturnStatement($scope, $node);
});
try {
$internalStatementResult = $nodeScopeResolver->processStmtNodesInternal($stmt, $stmt->stmts, $functionScope, $bodyStorage, $nodeCallback, StatementContext::createTopLevel($context->shouldResolveTemplateArguments()));
$dependencies[] = $internalStatementResult->getDependencies();
$statementResult = $internalStatementResult->toPublic();
} finally {
$nodeScopeResolver->popNodeGatherer();
}
$nodeScopeResolver->callNodeCallback($nodeCallback, new FunctionReturnStatementsNode(
$stmt,
$gatheredReturnStatements,
$gatheredReturnStatementsAfterFinally,
$gatheredYieldStatements,
$statementResult,
$executionEnds,
array_merge($statementResult->getImpurePoints(), $functionImpurePoints),
$functionReflection,
), $functionScope, $bodyStorage);
$nodeScopeResolver->callNodeCallback($nodeCallback, VariableLivenessResolver::resolve($stmt, $internalStatementResult->getVariableFlow()), $functionScope, $bodyStorage);
} finally {
$scope->popExpressionResultStorage();
}
// declaring the function defines it in global state, so a negative
// function_exists() narrowing that may refer to that function must be forgotten
$scope = $scope->invalidateExistenceCheckExpressions(['function_exists'], $functionReflection->getName());
return new InternalStatementResult($scope, hasYield: false, isAlwaysTerminating: false, exitPoints: [], throwPoints: [], impurePoints: [], dependencies: Dependencies::merge(...$dependencies));
}
}