|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Analyser\Generator\NodeHandler; |
| 4 | + |
| 5 | +use Generator; |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\ComplexType; |
| 8 | +use PhpParser\Node\Expr; |
| 9 | +use PhpParser\Node\Identifier; |
| 10 | +use PhpParser\Node\Name; |
| 11 | +use PhpParser\Node\Stmt\Return_; |
| 12 | +use PhpParser\NodeTraverser; |
| 13 | +use PHPStan\Analyser\Generator\GeneratorScope; |
| 14 | +use PHPStan\Analyser\Generator\NodeCallbackRequest; |
| 15 | +use PHPStan\Analyser\Generator\StmtsAnalysisRequest; |
| 16 | +use PHPStan\Analyser\ImpurePoint; |
| 17 | +use PHPStan\Analyser\Scope; |
| 18 | +use PHPStan\Analyser\StatementContext; |
| 19 | +use PHPStan\DependencyInjection\AutowiredService; |
| 20 | +use PHPStan\Node\ExecutionEndNode; |
| 21 | +use PHPStan\Node\InPropertyHookNode; |
| 22 | +use PHPStan\Node\PropertyAssignNode; |
| 23 | +use PHPStan\Node\PropertyHookReturnStatementsNode; |
| 24 | +use PHPStan\Node\ReturnStatement; |
| 25 | +use PHPStan\Parser\LineAttributesVisitor; |
| 26 | +use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection; |
| 27 | +use PHPStan\ShouldNotHappenException; |
| 28 | +use PHPStan\Type\Type; |
| 29 | +use function array_merge; |
| 30 | + |
| 31 | +#[AutowiredService] |
| 32 | +final class PropertyHooksHandler |
| 33 | +{ |
| 34 | + |
| 35 | + public function __construct( |
| 36 | + private AttrGroupsHandler $attrGroupsHandler, |
| 37 | + private StatementPhpDocsHelper $phpDocsHelper, |
| 38 | + private ParamHandler $paramHandler, |
| 39 | + private DeprecatedAttributeHelper $deprecatedAttributeHelper, |
| 40 | + ) |
| 41 | + { |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @param Node\PropertyHook[] $hooks |
| 46 | + * @param (callable(Node, Scope, callable(Node, Scope): void): void)|null $alternativeNodeCallback |
| 47 | + */ |
| 48 | + public function processPropertyHooks( |
| 49 | + Node\Stmt $stmt, |
| 50 | + Identifier|Name|ComplexType|null $nativeTypeNode, |
| 51 | + ?Type $phpDocType, |
| 52 | + string $propertyName, |
| 53 | + array $hooks, |
| 54 | + GeneratorScope $scope, |
| 55 | + ?callable $alternativeNodeCallback, |
| 56 | + ): Generator |
| 57 | + { |
| 58 | + if (!$scope->isInClass()) { |
| 59 | + throw new ShouldNotHappenException(); |
| 60 | + } |
| 61 | + |
| 62 | + $classReflection = $scope->getClassReflection(); |
| 63 | + |
| 64 | + foreach ($hooks as $hook) { |
| 65 | + yield new NodeCallbackRequest($hook, $scope); |
| 66 | + yield from $this->attrGroupsHandler->processAttributeGroups($stmt, $hook->attrGroups, $scope, $alternativeNodeCallback); |
| 67 | + |
| 68 | + [, $phpDocParameterTypes,,,, $phpDocThrowType,,,,,,,, $phpDocComment] = $this->phpDocsHelper->getPhpDocs($scope, $hook); |
| 69 | + |
| 70 | + foreach ($hook->params as $param) { |
| 71 | + yield from $this->paramHandler->processParam($stmt, $param, $scope, $alternativeNodeCallback); |
| 72 | + } |
| 73 | + |
| 74 | + [$isDeprecated, $deprecatedDescription] = $this->deprecatedAttributeHelper->getDeprecatedAttribute($scope, $hook); |
| 75 | + |
| 76 | + $hookScope = $scope->enterPropertyHook( |
| 77 | + $hook, |
| 78 | + $propertyName, |
| 79 | + $nativeTypeNode, |
| 80 | + $phpDocType, |
| 81 | + $phpDocParameterTypes, |
| 82 | + $phpDocThrowType, |
| 83 | + $deprecatedDescription, |
| 84 | + $isDeprecated, |
| 85 | + $phpDocComment, |
| 86 | + ); |
| 87 | + $hookReflection = $hookScope->getFunction(); |
| 88 | + if (!$hookReflection instanceof PhpMethodFromParserNodeReflection) { |
| 89 | + throw new ShouldNotHappenException(); |
| 90 | + } |
| 91 | + |
| 92 | + if (!$classReflection->hasNativeProperty($propertyName)) { |
| 93 | + throw new ShouldNotHappenException(); |
| 94 | + } |
| 95 | + |
| 96 | + $propertyReflection = $classReflection->getNativeProperty($propertyName); |
| 97 | + |
| 98 | + yield new NodeCallbackRequest(new InPropertyHookNode( |
| 99 | + $classReflection, |
| 100 | + $hookReflection, |
| 101 | + $propertyReflection, |
| 102 | + $hook, |
| 103 | + ), $hookScope); |
| 104 | + |
| 105 | + $stmts = $hook->getStmts(); |
| 106 | + if ($stmts === null) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + if ($hook->body instanceof Expr) { |
| 111 | + // enrich attributes of nodes in short hook body statements |
| 112 | + $traverser = new NodeTraverser( |
| 113 | + new LineAttributesVisitor($hook->body->getStartLine(), $hook->body->getEndLine()), |
| 114 | + ); |
| 115 | + $traverser->traverse($stmts); |
| 116 | + } |
| 117 | + |
| 118 | + $gatheredReturnStatements = []; |
| 119 | + $executionEnds = []; |
| 120 | + $methodImpurePoints = []; |
| 121 | + $statementResult = (yield new StmtsAnalysisRequest($stmts, $hookScope, StatementContext::createTopLevel(), static function (Node $node, Scope $scope, $nodeCallback) use ($hookScope, &$gatheredReturnStatements, &$executionEnds, &$hookImpurePoints): void { |
| 122 | + $nodeCallback($node, $scope); |
| 123 | + if ($scope->getFunction() !== $hookScope->getFunction()) { |
| 124 | + return; |
| 125 | + } |
| 126 | + if ($scope->isInAnonymousFunction()) { |
| 127 | + return; |
| 128 | + } |
| 129 | + if ($node instanceof PropertyAssignNode) { |
| 130 | + $hookImpurePoints[] = new ImpurePoint( |
| 131 | + $scope, |
| 132 | + $node, |
| 133 | + 'propertyAssign', |
| 134 | + 'property assignment', |
| 135 | + true, |
| 136 | + ); |
| 137 | + return; |
| 138 | + } |
| 139 | + if ($node instanceof ExecutionEndNode) { |
| 140 | + $executionEnds[] = $node; |
| 141 | + return; |
| 142 | + } |
| 143 | + if (!$node instanceof Return_) { |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + $gatheredReturnStatements[] = new ReturnStatement($scope, $node); |
| 148 | + }))->toPublic(); |
| 149 | + |
| 150 | + yield new NodeCallbackRequest(new PropertyHookReturnStatementsNode( |
| 151 | + $hook, |
| 152 | + $gatheredReturnStatements, |
| 153 | + $statementResult, |
| 154 | + $executionEnds, |
| 155 | + array_merge($statementResult->getImpurePoints(), $methodImpurePoints), |
| 156 | + $classReflection, |
| 157 | + $hookReflection, |
| 158 | + $propertyReflection, |
| 159 | + ), $hookScope); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | +} |
0 commit comments