|
124 | 124 | use PHPStan\Parser\ArrowFunctionArgVisitor;
|
125 | 125 | use PHPStan\Parser\ClosureArgVisitor;
|
126 | 126 | use PHPStan\Parser\ImmediatelyInvokedClosureVisitor;
|
| 127 | +use PHPStan\Parser\LineAttributesVisitor; |
127 | 128 | use PHPStan\Parser\Parser;
|
128 | 129 | use PHPStan\Parser\PropertyHookNameVisitor;
|
129 | 130 | use PHPStan\Php\PhpVersion;
|
@@ -4830,54 +4831,61 @@ private function processPropertyHooks(
|
4830 | 4831 | $hook,
|
4831 | 4832 | ), $hookScope);
|
4832 | 4833 |
|
| 4834 | + $stmts = $hook->getStmts(); |
| 4835 | + if ($stmts === null) { |
| 4836 | + return; |
| 4837 | + } |
| 4838 | + |
4833 | 4839 | if ($hook->body instanceof Expr) {
|
4834 |
| - $this->processExprNode($stmt, $hook->body, $hookScope, $nodeCallback, ExpressionContext::createTopLevel()); |
4835 |
| - $nodeCallback(new PropertyAssignNode(new PropertyFetch(new Variable('this'), $propertyName, $hook->body->getAttributes()), $hook->body, false), $hookScope); |
4836 |
| - } elseif (is_array($hook->body)) { |
4837 |
| - $gatheredReturnStatements = []; |
4838 |
| - $executionEnds = []; |
4839 |
| - $methodImpurePoints = []; |
4840 |
| - $statementResult = $this->processStmtNodes(new PropertyHookStatementNode($hook), $hook->body, $hookScope, static function (Node $node, Scope $scope) use ($nodeCallback, $hookScope, &$gatheredReturnStatements, &$executionEnds, &$hookImpurePoints): void { |
4841 |
| - $nodeCallback($node, $scope); |
4842 |
| - if ($scope->getFunction() !== $hookScope->getFunction()) { |
4843 |
| - return; |
4844 |
| - } |
4845 |
| - if ($scope->isInAnonymousFunction()) { |
4846 |
| - return; |
4847 |
| - } |
4848 |
| - if ($node instanceof PropertyAssignNode) { |
4849 |
| - $hookImpurePoints[] = new ImpurePoint( |
4850 |
| - $scope, |
4851 |
| - $node, |
4852 |
| - 'propertyAssign', |
4853 |
| - 'property assignment', |
4854 |
| - true, |
4855 |
| - ); |
4856 |
| - return; |
4857 |
| - } |
4858 |
| - if ($node instanceof ExecutionEndNode) { |
4859 |
| - $executionEnds[] = $node; |
4860 |
| - return; |
4861 |
| - } |
4862 |
| - if (!$node instanceof Return_) { |
4863 |
| - return; |
4864 |
| - } |
| 4840 | + // enrich attributes of nodes in short hook body statements |
| 4841 | + $traverser = new NodeTraverser( |
| 4842 | + new LineAttributesVisitor($hook->body->getStartLine(), $hook->body->getEndLine()), |
| 4843 | + ); |
| 4844 | + $traverser->traverse($stmts); |
| 4845 | + } |
4865 | 4846 |
|
4866 |
| - $gatheredReturnStatements[] = new ReturnStatement($scope, $node); |
4867 |
| - }, StatementContext::createTopLevel()); |
| 4847 | + $gatheredReturnStatements = []; |
| 4848 | + $executionEnds = []; |
| 4849 | + $methodImpurePoints = []; |
| 4850 | + $statementResult = $this->processStmtNodes(new PropertyHookStatementNode($hook), $stmts, $hookScope, static function (Node $node, Scope $scope) use ($nodeCallback, $hookScope, &$gatheredReturnStatements, &$executionEnds, &$hookImpurePoints): void { |
| 4851 | + $nodeCallback($node, $scope); |
| 4852 | + if ($scope->getFunction() !== $hookScope->getFunction()) { |
| 4853 | + return; |
| 4854 | + } |
| 4855 | + if ($scope->isInAnonymousFunction()) { |
| 4856 | + return; |
| 4857 | + } |
| 4858 | + if ($node instanceof PropertyAssignNode) { |
| 4859 | + $hookImpurePoints[] = new ImpurePoint( |
| 4860 | + $scope, |
| 4861 | + $node, |
| 4862 | + 'propertyAssign', |
| 4863 | + 'property assignment', |
| 4864 | + true, |
| 4865 | + ); |
| 4866 | + return; |
| 4867 | + } |
| 4868 | + if ($node instanceof ExecutionEndNode) { |
| 4869 | + $executionEnds[] = $node; |
| 4870 | + return; |
| 4871 | + } |
| 4872 | + if (!$node instanceof Return_) { |
| 4873 | + return; |
| 4874 | + } |
4868 | 4875 |
|
4869 |
| - $nodeCallback(new PropertyHookReturnStatementsNode( |
4870 |
| - $hook, |
4871 |
| - $gatheredReturnStatements, |
4872 |
| - $statementResult, |
4873 |
| - $executionEnds, |
4874 |
| - array_merge($statementResult->getImpurePoints(), $methodImpurePoints), |
4875 |
| - $classReflection, |
4876 |
| - $hookReflection, |
4877 |
| - $propertyReflection, |
4878 |
| - ), $hookScope); |
4879 |
| - } |
| 4876 | + $gatheredReturnStatements[] = new ReturnStatement($scope, $node); |
| 4877 | + }, StatementContext::createTopLevel()); |
4880 | 4878 |
|
| 4879 | + $nodeCallback(new PropertyHookReturnStatementsNode( |
| 4880 | + $hook, |
| 4881 | + $gatheredReturnStatements, |
| 4882 | + $statementResult, |
| 4883 | + $executionEnds, |
| 4884 | + array_merge($statementResult->getImpurePoints(), $methodImpurePoints), |
| 4885 | + $classReflection, |
| 4886 | + $hookReflection, |
| 4887 | + $propertyReflection, |
| 4888 | + ), $hookScope); |
4881 | 4889 | }
|
4882 | 4890 | }
|
4883 | 4891 |
|
|
0 commit comments