Repository navigation
Expand file tree
/
Copy pathClassMethodHandler.php
More file actions
302 lines (272 loc) · 10.8 KB
/
Copy pathClassMethodHandler.php
File metadata and controls
302 lines (272 loc) · 10.8 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php declare(strict_types = 1);
namespace PHPStan\Analyser\StmtHandler;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassMethod;
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\PropertyHooksProcessor;
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\ClassPropertyNode;
use PHPStan\Node\ExecutionEndNode;
use PHPStan\Node\Expr\PropertyInitializationExpr;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Node\MethodReturnStatementsNode;
use PHPStan\Node\PropertyAssignNode;
use PHPStan\Node\ReturnAfterFinallyNode;
use PHPStan\Node\ReturnStatement;
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\Turbo\ShadowedByTurboExtension;
use PHPStan\Type\MixedType;
use PHPStan\Type\ParserNodeTypeToPHPStanType;
use PHPStan\Type\TypeUtils;
use function array_merge;
use function is_string;
/**
* @implements StmtHandler<ClassMethod>
*/
#[AutowiredService]
#[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/ClassMethodHandler.cpp')]
final class ClassMethodHandler implements StmtHandler
{
public function __construct(
private DeprecatedAttributeResolver $deprecatedAttributeResolver,
private PhpDocsResolver $phpDocsResolver,
private PropertyHooksProcessor $propertyHooksProcessor,
private AttributesHandler $attributesHandler,
private ParametersProcessor $parametersProcessor,
)
{
}
public function supports(Stmt $stmt): bool
{
return $stmt instanceof ClassMethod;
}
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, $isFinal, $isPure, $acceptsNamedArguments, $isReadOnly, $phpDocComment, $asserts, $selfOutType, $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);
}
$isFromTrait = $stmt->getAttribute('originalTraitMethodName') === '__construct';
$isConstructor = $isFromTrait || $stmt->name->toLowerString() === '__construct';
$methodScope = $scope->enterClassMethod(
$stmt,
$templateTypeMap,
$phpDocParameterTypes,
$phpDocReturnType,
$phpDocThrowType,
$deprecatedDescription,
$isDeprecated,
$isInternal,
$isFinal,
$isPure,
$acceptsNamedArguments,
$asserts,
$selfOutType,
$phpDocComment,
$phpDocParameterOutTypes,
$phpDocImmediatelyInvokedCallableParameters,
$phpDocClosureThisTypeParameters,
$isConstructor,
null,
$pureUnlessCallableIsImpureParameters,
$pureUnlessParameterPassedParameters,
);
if (!$scope->isInClass()) {
throw new ShouldNotHappenException();
}
$classReflection = $scope->getClassReflection();
if ($isConstructor) {
foreach ($stmt->params as $param) {
if ($param->flags === 0 && $param->hooks === []) {
continue;
}
if (!$param->var instanceof Variable || !is_string($param->var->name) || $param->var->name === '') {
throw new ShouldNotHappenException();
}
$phpDoc = null;
if ($param->getDocComment() !== null) {
$phpDoc = $param->getDocComment()->getText();
}
$promotedNativeType = $param->type !== null ? ParserNodeTypeToPHPStanType::resolve($param->type, $classReflection) : null;
$dependencies[] = Dependencies::create($methodScope->getFile(), [$promotedNativeType, $phpDocParameterTypes[$param->var->name] ?? null]);
$nodeScopeResolver->callNodeCallback($nodeCallback, new ClassPropertyNode(
$param->var->name,
$param->flags,
$promotedNativeType,
null,
$phpDoc,
$phpDocParameterTypes[$param->var->name] ?? null,
true,
$isFromTrait,
$param,
$isReadOnly,
$scope->isInTrait(),
$classReflection->isReadOnly(),
false,
$classReflection,
), $methodScope, $storage);
$dependencies[] = $this->propertyHooksProcessor->processPropertyHooks(
$nodeScopeResolver,
$stmt,
$param->type,
$phpDocParameterTypes[$param->var->name] ?? null,
$param->var->name,
$param->hooks,
$scope,
$storage,
$nodeCallback,
);
$methodScope = $methodScope->assignExpression(new PropertyInitializationExpr($param->var->name), new MixedType(), new MixedType());
}
}
if ($stmt->getAttribute('virtual', false) === false) {
$methodReflection = $methodScope->getFunction();
if (!$methodReflection instanceof PhpMethodFromParserNodeReflection) {
throw new ShouldNotHappenException();
}
$dependencies[] = Dependencies::create($methodScope->getFile(), [...DependencyTypes::ofDeclaration($methodReflection, true), $methodReflection->getSelfOutType()]);
$nodeScopeResolver->callNodeCallback($nodeCallback, new InClassMethodNode($classReflection, $methodReflection, $stmt), $methodScope, $storage);
}
if ($stmt->stmts !== null) {
$gatheredReturnStatements = [];
$gatheredReturnStatementsAfterFinally = [];
$gatheredYieldStatements = [];
$executionEnds = [];
$methodImpurePoints = [];
// the body's results live in a per-body storage released right
// after the MethodReturnStatementsNode rules ran: later asks about
// body expressions (e.g. class-level rules pricing gathered nodes)
// go through the on-demand bridge, so keeping the results for the
// rest of the file would only pin the body's whole result graph
// (callbacks, scopes, types) at no benefit
$bodyStorage = $storage->duplicate();
$scope->pushExpressionResultStorage($bodyStorage);
try {
$nodeScopeResolver->pushNodeGatherer(static function (Node $node, Scope $scope) use ($nodeScopeResolver, $methodScope, &$gatheredReturnStatements, &$gatheredReturnStatementsAfterFinally, &$gatheredYieldStatements, &$executionEnds, &$methodImpurePoints): void {
if ($scope->getFunction() !== $methodScope->getFunction()) {
return;
}
if ($scope->isInAnonymousFunction()) {
return;
}
if ($node instanceof PropertyAssignNode) {
if (
$node->getPropertyFetch() instanceof Expr\PropertyFetch
&& $scope->getFunction() instanceof PhpMethodFromParserNodeReflection
&& $scope->getFunction()->getDeclaringClass()->hasConstructor()
&& $scope->getFunction()->getDeclaringClass()->getConstructor()->getName() === $scope->getFunction()->getName()
&& TypeUtils::findThisType($nodeScopeResolver->readScopeStateOrSyntheticType($node->getPropertyFetch()->var, $scope->toWalkScope())) !== null
) {
return;
}
$methodImpurePoints[] = 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[] = $node;
}
if (!$node instanceof Return_) {
return;
}
$gatheredReturnStatements[] = new ReturnStatement($scope, $node);
});
try {
$internalStatementResult = $nodeScopeResolver->processStmtNodesInternal($stmt, $stmt->stmts, $methodScope, $bodyStorage, $nodeCallback, StatementContext::createTopLevel($context->shouldResolveTemplateArguments()));
$dependencies[] = $internalStatementResult->getDependencies();
$statementResult = $internalStatementResult->toPublic();
} finally {
$nodeScopeResolver->popNodeGatherer();
}
$methodReflection = $methodScope->getFunction();
if (!$methodReflection instanceof PhpMethodFromParserNodeReflection) {
throw new ShouldNotHappenException();
}
$nodeScopeResolver->callNodeCallback($nodeCallback, new MethodReturnStatementsNode(
$stmt,
$gatheredReturnStatements,
$gatheredReturnStatementsAfterFinally,
$gatheredYieldStatements,
$statementResult,
$executionEnds,
array_merge($statementResult->getImpurePoints(), $methodImpurePoints),
$classReflection,
$methodReflection,
), $methodScope, $bodyStorage);
$nodeScopeResolver->callNodeCallback($nodeCallback, VariableLivenessResolver::resolve($stmt, $internalStatementResult->getVariableFlow()), $methodScope, $bodyStorage);
} finally {
$scope->popExpressionResultStorage();
}
if ($isConstructor) {
$finalScope = null;
foreach ($executionEnds as $executionEnd) {
if ($executionEnd->getStatementResult()->isAlwaysTerminating()) {
continue;
}
$endScope = $executionEnd->getStatementResult()->getScope();
if ($finalScope === null) {
$finalScope = $endScope;
continue;
}
$finalScope = $finalScope->mergeWith($endScope);
}
foreach ($gatheredReturnStatements as $statement) {
if ($finalScope === null) {
$finalScope = $statement->getScope()->toWalkScope();
continue;
}
$finalScope = $finalScope->mergeWith($statement->getScope()->toWalkScope());
}
if ($finalScope !== null) {
$scope = $finalScope->rememberConstructorScope();
}
}
}
return new InternalStatementResult($scope, hasYield: false, isAlwaysTerminating: false, exitPoints: [], throwPoints: [], impurePoints: [], dependencies: Dependencies::merge(...$dependencies));
}
}