|
2 | 2 |
|
3 | 3 | namespace PHPStan\Analyser; |
4 | 4 |
|
5 | | -use PhpParser\Node; |
6 | | -use PHPStan\DependencyInjection\AutowiredParameter; |
| 5 | +use PhpParser\Node\Expr\PropertyFetch; |
| 6 | +use PhpParser\Node\Identifier; |
7 | 7 | use PHPStan\DependencyInjection\AutowiredService; |
8 | | -use PHPStan\File\FileHelper; |
9 | 8 | use PHPStan\Node\MethodReturnStatementsNode; |
10 | | -use PHPStan\Parser\Parser; |
11 | | -use PHPStan\Reflection\MethodReflection; |
12 | | -use PHPStan\Turbo\ShadowedByTurboExtension; |
| 9 | +use PHPStan\Node\PropertyAssignNode; |
| 10 | +use PHPStan\Reflection\ClassReflection; |
13 | 11 | use PHPStan\Type\NeverType; |
| 12 | +use PHPStan\Type\Type; |
| 13 | +use PHPStan\Type\TypeCombinator; |
| 14 | +use PHPStan\Type\TypeUtils; |
14 | 15 | use function array_key_exists; |
15 | | -use function count; |
16 | | -use function is_array; |
17 | | -use function sprintf; |
| 16 | +use function strtolower; |
18 | 17 |
|
| 18 | +/** |
| 19 | + * The walks of the methods of the classes being walked: the scope a method's walk ends |
| 20 | + * with is what calling the method does to $this. ClassLikeHandler walks the methods |
| 21 | + * called on $this before their callers. |
| 22 | + */ |
19 | 23 | #[AutowiredService] |
20 | | -#[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/CalledMethodProcessor.cpp')] |
21 | 24 | final class CalledMethodProcessor |
22 | 25 | { |
23 | 26 |
|
24 | | - /** @var array<string, true> */ |
25 | | - private array $calledMethodStack = []; |
| 27 | + /** @var array<string, array<string, MethodReturnStatementsNode>> by class name and lowercase method name */ |
| 28 | + private array $walkedMethods = []; |
26 | 29 |
|
27 | | - /** @var array<string, MutatingScope|null> */ |
28 | | - private array $calledMethodResults = []; |
| 30 | + /** @var array<string, array<string, MutatingScope|null>> by class name and lowercase method name */ |
| 31 | + private array $exitScopes = []; |
29 | 32 |
|
30 | | - public function __construct( |
31 | | - private FileHelper $fileHelper, |
32 | | - #[AutowiredParameter(ref: '@defaultAnalysisParser')] |
33 | | - private Parser $parser, |
34 | | - private ScopeFactory $scopeFactory, |
35 | | - ) |
| 33 | + /** @var array<string, array<string, array<string, array{Type, Type}|null>>> by class name and lowercase method name */ |
| 34 | + private array $assignedTypes = []; |
| 35 | + |
| 36 | + public function rememberWalkedMethod(MethodReturnStatementsNode $node): void |
36 | 37 | { |
| 38 | + $className = $node->getClassReflection()->getName(); |
| 39 | + $methodName = strtolower($node->getMethodName()); |
| 40 | + $this->walkedMethods[$className][$methodName] = $node; |
| 41 | + unset($this->exitScopes[$className][$methodName], $this->assignedTypes[$className][$methodName]); |
37 | 42 | } |
38 | 43 |
|
39 | | - public function processCalledMethod(NodeScopeResolver $nodeScopeResolver, MethodReflection $methodReflection): ?MutatingScope |
| 44 | + /** |
| 45 | + * The merged scope at the ends of the method's walk, null when it was not walked |
| 46 | + * yet or never returns. |
| 47 | + */ |
| 48 | + public function getWalkedMethodExitScope(ClassReflection $classReflection, string $methodName): ?MutatingScope |
40 | 49 | { |
41 | | - $declaringClass = $methodReflection->getDeclaringClass(); |
42 | | - if ($declaringClass->isAnonymous()) { |
43 | | - return null; |
44 | | - } |
45 | | - if ($declaringClass->getFileName() === null) { |
| 50 | + $className = $classReflection->getName(); |
| 51 | + $methodName = strtolower($methodName); |
| 52 | + if (!isset($this->walkedMethods[$className][$methodName])) { |
46 | 53 | return null; |
47 | 54 | } |
48 | | - |
49 | | - $stackName = sprintf('%s::%s', $declaringClass->getName(), $methodReflection->getName()); |
50 | | - if (array_key_exists($stackName, $this->calledMethodResults)) { |
51 | | - return $this->calledMethodResults[$stackName]; |
| 55 | + if (!array_key_exists($methodName, $this->exitScopes[$className] ?? [])) { |
| 56 | + $this->exitScopes[$className][$methodName] = $this->getExitScope($this->walkedMethods[$className][$methodName]); |
52 | 57 | } |
53 | 58 |
|
54 | | - if (array_key_exists($stackName, $this->calledMethodStack)) { |
55 | | - return null; |
56 | | - } |
| 59 | + return $this->exitScopes[$className][$methodName]; |
| 60 | + } |
57 | 61 |
|
58 | | - if (count($this->calledMethodStack) > 0) { |
| 62 | + /** |
| 63 | + * The types the method's walk saw assigned to the property of any object with that name, |
| 64 | + * with their native types, by property name; a property with an assigned type that names |
| 65 | + * the method's template types is null. Null when the method was not walked yet. |
| 66 | + * |
| 67 | + * @return array<string, array{Type, Type}|null>|null |
| 68 | + */ |
| 69 | + public function getWalkedMethodAssignedTypes(ClassReflection $classReflection, string $methodName): ?array |
| 70 | + { |
| 71 | + $className = $classReflection->getName(); |
| 72 | + $methodName = strtolower($methodName); |
| 73 | + if (!isset($this->walkedMethods[$className][$methodName])) { |
59 | 74 | return null; |
60 | 75 | } |
61 | | - |
62 | | - $this->calledMethodStack[$stackName] = true; |
63 | | - |
64 | | - $fileName = $this->fileHelper->normalizePath($declaringClass->getFileName()); |
65 | | - if (!$nodeScopeResolver->isAnalysedFile($fileName)) { |
66 | | - unset($this->calledMethodStack[$stackName]); |
67 | | - return null; |
| 76 | + if (isset($this->assignedTypes[$className][$methodName])) { |
| 77 | + return $this->assignedTypes[$className][$methodName]; |
68 | 78 | } |
69 | | - $parserNodes = $this->parser->parseFile($fileName); |
70 | 79 |
|
71 | | - $returnStatement = null; |
72 | | - $this->processNodesForCalledMethod($nodeScopeResolver, $parserNodes, new ExpressionResultStorage(), $fileName, $methodReflection, static function (Node $node, Scope $scope) use ($methodReflection, &$returnStatement): void { |
73 | | - if (!$node instanceof MethodReturnStatementsNode) { |
74 | | - return; |
| 80 | + $assignedTypes = []; |
| 81 | + foreach ($this->walkedMethods[$className][$methodName]->getImpurePoints() as $impurePoint) { |
| 82 | + $node = $impurePoint->getNode(); |
| 83 | + if (!$node instanceof PropertyAssignNode) { |
| 84 | + continue; |
75 | 85 | } |
76 | | - |
77 | | - if ($node->getClassReflection()->getName() !== $methodReflection->getDeclaringClass()->getName()) { |
78 | | - return; |
| 86 | + $propertyFetch = $node->getPropertyFetch(); |
| 87 | + if (!$propertyFetch instanceof PropertyFetch || !$propertyFetch->name instanceof Identifier) { |
| 88 | + continue; |
79 | 89 | } |
80 | | - |
81 | | - if ($returnStatement !== null) { |
82 | | - return; |
| 90 | + $propertyName = $propertyFetch->name->toString(); |
| 91 | + if (array_key_exists($propertyName, $assignedTypes) && $assignedTypes[$propertyName] === null) { |
| 92 | + continue; |
83 | 93 | } |
84 | | - |
85 | | - $returnStatement = $node; |
86 | | - }); |
87 | | - |
88 | | - $calledMethodEndScope = null; |
89 | | - if ($returnStatement instanceof MethodReturnStatementsNode) { |
90 | | - foreach ($returnStatement->getExecutionEnds() as $executionEnd) { |
91 | | - $statementResult = $executionEnd->getStatementResult(); |
92 | | - $endExprResult = $executionEnd->getExprResult(); |
93 | | - if ($endExprResult !== null) { |
94 | | - $walkScope = $statementResult->getScope()->toWalkScope(); |
95 | | - $exprType = $endExprResult->getTypeOnScope($walkScope, $walkScope->nativeTypesPromoted); |
96 | | - if ($exprType instanceof NeverType && $exprType->isExplicit()) { |
97 | | - continue; |
98 | | - } |
99 | | - } |
100 | | - if ($calledMethodEndScope === null) { |
101 | | - $calledMethodEndScope = $statementResult->getScope()->toWalkScope(); |
102 | | - continue; |
103 | | - } |
104 | | - |
105 | | - $calledMethodEndScope = $calledMethodEndScope->mergeWith($statementResult->getScope()->toWalkScope()); |
106 | | - } |
107 | | - foreach ($returnStatement->getReturnStatements() as $statement) { |
108 | | - if ($calledMethodEndScope === null) { |
109 | | - $calledMethodEndScope = $statement->getScope()->toWalkScope(); |
110 | | - continue; |
111 | | - } |
112 | | - |
113 | | - $calledMethodEndScope = $calledMethodEndScope->mergeWith($statement->getScope()->toWalkScope()); |
| 94 | + $scope = $impurePoint->getScope(); |
| 95 | + $type = $scope->getType($node->getAssignedExpr()); |
| 96 | + if (TypeUtils::containsTemplateType($type)) { |
| 97 | + // the method's own template types mean nothing after the call |
| 98 | + $assignedTypes[$propertyName] = null; |
| 99 | + continue; |
114 | 100 | } |
| 101 | + $nativeType = $scope->getNativeType($node->getAssignedExpr()); |
| 102 | + $assignedTypes[$propertyName] = isset($assignedTypes[$propertyName]) |
| 103 | + ? [TypeCombinator::union($assignedTypes[$propertyName][0], $type), TypeCombinator::union($assignedTypes[$propertyName][1], $nativeType)] |
| 104 | + : [$type, $nativeType]; |
115 | 105 | } |
116 | 106 |
|
117 | | - unset($this->calledMethodStack[$stackName]); |
118 | | - |
119 | | - $this->calledMethodResults[$stackName] = $calledMethodEndScope; |
120 | | - |
121 | | - return $calledMethodEndScope; |
| 107 | + return $this->assignedTypes[$className][$methodName] = $assignedTypes; |
122 | 108 | } |
123 | 109 |
|
124 | | - public function clearCalledMethodResults(): void |
| 110 | + public function clearCalledMethodResults(ClassReflection $classReflection): void |
125 | 111 | { |
126 | | - $this->calledMethodResults = []; |
| 112 | + $className = $classReflection->getName(); |
| 113 | + unset($this->walkedMethods[$className], $this->exitScopes[$className], $this->assignedTypes[$className]); |
127 | 114 | } |
128 | 115 |
|
129 | | - /** |
130 | | - * @param Node[]|Node|scalar|null $node |
131 | | - * @param callable(Node $node, Scope $scope): void $nodeCallback |
132 | | - */ |
133 | | - private function processNodesForCalledMethod(NodeScopeResolver $nodeScopeResolver, $node, ExpressionResultStorage $storage, string $fileName, MethodReflection $methodReflection, callable $nodeCallback): void |
| 116 | + private function getExitScope(MethodReturnStatementsNode $returnStatement): ?MutatingScope |
134 | 117 | { |
135 | | - if ($node instanceof Node) { |
136 | | - $declaringClass = $methodReflection->getDeclaringClass(); |
137 | | - if ( |
138 | | - $node instanceof Node\Stmt\Class_ |
139 | | - && isset($node->namespacedName) |
140 | | - && $declaringClass->getName() === (string) $node->namespacedName |
141 | | - && $declaringClass->getNativeReflection()->getStartLine() === $node->getStartLine() |
142 | | - ) { |
143 | | - |
144 | | - $stmts = $node->stmts; |
145 | | - foreach ($stmts as $stmt) { |
146 | | - if (!$stmt instanceof Node\Stmt\ClassMethod) { |
147 | | - continue; |
148 | | - } |
149 | | - |
150 | | - if ($stmt->name->toString() !== $methodReflection->getName()) { |
151 | | - continue; |
152 | | - } |
153 | | - |
154 | | - if ($stmt->getEndLine() - $stmt->getStartLine() > 50) { |
155 | | - continue; |
156 | | - } |
157 | | - |
158 | | - $scope = $this->scopeFactory->create(ScopeContext::create($fileName))->enterClass($declaringClass); |
159 | | - $nodeScopeResolver->processStmtNode($stmt, $scope, $storage, $nodeCallback, StatementContext::createTopLevel()); |
| 118 | + $exitScope = null; |
| 119 | + foreach ($returnStatement->getExecutionEnds() as $executionEnd) { |
| 120 | + $statementResult = $executionEnd->getStatementResult(); |
| 121 | + $endExprResult = $executionEnd->getExprResult(); |
| 122 | + if ($endExprResult !== null) { |
| 123 | + $walkScope = $statementResult->getScope()->toWalkScope(); |
| 124 | + $exprType = $endExprResult->getTypeOnScope($walkScope, $walkScope->nativeTypesPromoted); |
| 125 | + if ($exprType instanceof NeverType && $exprType->isExplicit()) { |
| 126 | + continue; |
160 | 127 | } |
161 | | - return; |
162 | | - } |
163 | | - if ($node instanceof Node\Stmt\ClassLike) { |
164 | | - return; |
165 | | - } |
166 | | - if ($node instanceof Node\FunctionLike) { |
167 | | - return; |
168 | 128 | } |
169 | | - foreach ($node->getSubNodeNames() as $subNodeName) { |
170 | | - $subNode = $node->{$subNodeName}; |
171 | | - $this->processNodesForCalledMethod($nodeScopeResolver, $subNode, $storage, $fileName, $methodReflection, $nodeCallback); |
| 129 | + if ($exitScope === null) { |
| 130 | + $exitScope = $statementResult->getScope()->toWalkScope(); |
| 131 | + continue; |
172 | 132 | } |
173 | | - } elseif (is_array($node)) { |
174 | | - foreach ($node as $subNode) { |
175 | | - $this->processNodesForCalledMethod($nodeScopeResolver, $subNode, $storage, $fileName, $methodReflection, $nodeCallback); |
| 133 | + |
| 134 | + $exitScope = $exitScope->mergeWith($statementResult->getScope()->toWalkScope()); |
| 135 | + } |
| 136 | + foreach ($returnStatement->getReturnStatements() as $statement) { |
| 137 | + if ($exitScope === null) { |
| 138 | + $exitScope = $statement->getScope()->toWalkScope(); |
| 139 | + continue; |
176 | 140 | } |
| 141 | + |
| 142 | + $exitScope = $exitScope->mergeWith($statement->getScope()->toWalkScope()); |
177 | 143 | } |
| 144 | + |
| 145 | + return $exitScope; |
178 | 146 | } |
179 | 147 |
|
180 | 148 | } |
0 commit comments