|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Nette\PHPStan\Forms; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr\MethodCall; |
| 7 | +use PhpParser\Node\Expr\Variable; |
| 8 | +use PhpParser\Node\Identifier; |
| 9 | +use PhpParser\Node\Scalar\String_; |
| 10 | +use PhpParser\Node\Stmt; |
| 11 | +use PHPStan\Analyser\Scope; |
| 12 | +use PHPStan\Parser\Parser; |
| 13 | +use PHPStan\Reflection\MethodReflection; |
| 14 | +use PHPStan\Reflection\ReflectionProvider; |
| 15 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 16 | +use PHPStan\Type\ObjectType; |
| 17 | +use PHPStan\Type\Type; |
| 18 | +use PHPStan\Type\TypeCombinator; |
| 19 | +use function count, in_array, is_array, is_string, str_starts_with, ucfirst; |
| 20 | + |
| 21 | + |
| 22 | +/** |
| 23 | + * Narrows return types of Forms\Container::getComponent() and ::offsetGet() |
| 24 | + * by finding the corresponding addXxx() call in the same function body. |
| 25 | + */ |
| 26 | +class FormContainerReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 27 | +{ |
| 28 | + public function __construct( |
| 29 | + private Parser $parser, |
| 30 | + private ReflectionProvider $reflectionProvider, |
| 31 | + ) { |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + public function getClass(): string |
| 36 | + { |
| 37 | + return 'Nette\Forms\Container'; |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 42 | + { |
| 43 | + return in_array($methodReflection->getName(), ['getComponent', 'offsetGet'], true); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + public function getTypeFromMethodCall( |
| 48 | + MethodReflection $methodReflection, |
| 49 | + MethodCall $methodCall, |
| 50 | + Scope $scope, |
| 51 | + ): ?Type |
| 52 | + { |
| 53 | + $args = $methodCall->getArgs(); |
| 54 | + if ($args === []) { |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + $nameType = $scope->getType($args[0]->value); |
| 59 | + $constantStrings = $nameType->getConstantStrings(); |
| 60 | + if (count($constantStrings) !== 1) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + $componentName = $constantStrings[0]->getValue(); |
| 65 | + $type = $this->resolveComponentType($methodCall, $scope, $componentName); |
| 66 | + |
| 67 | + // Respect $throw parameter for getComponent() |
| 68 | + if ($methodReflection->getName() === 'getComponent' && count($args) >= 2) { |
| 69 | + $throwType = $scope->getType($args[1]->value); |
| 70 | + if (!$throwType->isTrue()->yes()) { |
| 71 | + $type = TypeCombinator::addNull($type); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return $type; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + private function resolveComponentType(MethodCall $methodCall, Scope $scope, string $componentName): Type |
| 80 | + { |
| 81 | + // Try to find addXxx() call in the same function body |
| 82 | + if ($methodCall->var instanceof Variable && is_string($methodCall->var->name)) { |
| 83 | + $type = $this->resolveFromAddCall($scope, $methodCall->var->name, $componentName); |
| 84 | + if ($type !== null) { |
| 85 | + return $type; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Fallback: try createComponent*() factory method |
| 90 | + $factoryMethodName = 'createComponent' . ucfirst($componentName); |
| 91 | + $callerType = $scope->getType($methodCall->var); |
| 92 | + if ($callerType->hasMethod($factoryMethodName)->yes()) { |
| 93 | + $factoryMethod = $callerType->getMethod($factoryMethodName, $scope); |
| 94 | + return $factoryMethod->getVariants()[0]->getReturnType(); |
| 95 | + } |
| 96 | + |
| 97 | + // Fallback: when we can't determine the specific control type, |
| 98 | + // return BaseControl (most $form['field'] accesses are controls, not containers) |
| 99 | + return new ObjectType('Nette\Forms\Controls\BaseControl'); |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + private function resolveFromAddCall(Scope $scope, string $variableName, string $componentName): ?Type |
| 104 | + { |
| 105 | + $stmts = $this->parser->parseFile($scope->getFile()); |
| 106 | + $body = $this->findEnclosingBody($stmts, $scope); |
| 107 | + if ($body === null) { |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + $addMethodName = $this->findAddCall($body, $variableName, $componentName); |
| 112 | + if ($addMethodName === null) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + |
| 116 | + $containerClass = $this->reflectionProvider->getClass('Nette\Forms\Container'); |
| 117 | + if (!$containerClass->hasMethod($addMethodName)) { |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + return $containerClass->getNativeMethod($addMethodName)->getVariants()[0]->getReturnType(); |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + /** |
| 126 | + * @param Stmt[] $stmts |
| 127 | + * @return Stmt[]|null |
| 128 | + */ |
| 129 | + private function findEnclosingBody(array $stmts, Scope $scope): ?array |
| 130 | + { |
| 131 | + $function = $scope->getFunction(); |
| 132 | + if ($function === null) { |
| 133 | + return $stmts; |
| 134 | + } |
| 135 | + |
| 136 | + $functionName = $function->getName(); |
| 137 | + $className = $scope->isInClass() ? $scope->getClassReflection()->getName() : null; |
| 138 | + return $this->searchBody($stmts, $functionName, $className); |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | + /** |
| 143 | + * @param Stmt[] $stmts |
| 144 | + * @return Stmt[]|null |
| 145 | + */ |
| 146 | + private function searchBody(array $stmts, string $functionName, ?string $className): ?array |
| 147 | + { |
| 148 | + foreach ($stmts as $stmt) { |
| 149 | + if ($stmt instanceof Stmt\Namespace_) { |
| 150 | + $result = $this->searchBody($stmt->stmts, $functionName, $className); |
| 151 | + if ($result !== null) { |
| 152 | + return $result; |
| 153 | + } |
| 154 | + |
| 155 | + } elseif ( |
| 156 | + $className !== null |
| 157 | + && ($stmt instanceof Stmt\Class_ || $stmt instanceof Stmt\Trait_) |
| 158 | + && $stmt->namespacedName !== null |
| 159 | + && $stmt->namespacedName->toString() === $className |
| 160 | + ) { |
| 161 | + foreach ($stmt->stmts as $member) { |
| 162 | + if ($member instanceof Stmt\ClassMethod && $member->name->toString() === $functionName) { |
| 163 | + return $member->stmts ?? []; |
| 164 | + } |
| 165 | + } |
| 166 | + } elseif ( |
| 167 | + $className === null |
| 168 | + && $stmt instanceof Stmt\Function_ |
| 169 | + && $stmt->namespacedName !== null |
| 170 | + && $stmt->namespacedName->toString() === $functionName |
| 171 | + ) { |
| 172 | + return $stmt->stmts ?? []; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + return null; |
| 177 | + } |
| 178 | + |
| 179 | + |
| 180 | + /** |
| 181 | + * Walks AST to find $variable->addXxx('componentName', ...) call. |
| 182 | + * @param Stmt[] $stmts |
| 183 | + */ |
| 184 | + private function findAddCall(array $stmts, string $variableName, string $componentName): ?string |
| 185 | + { |
| 186 | + foreach ($stmts as $stmt) { |
| 187 | + $result = $this->walkNode($stmt, $variableName, $componentName); |
| 188 | + if ($result !== null) { |
| 189 | + return $result; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + return null; |
| 194 | + } |
| 195 | + |
| 196 | + |
| 197 | + private function walkNode(Node $node, string $variableName, string $componentName): ?string |
| 198 | + { |
| 199 | + if ( |
| 200 | + $node instanceof MethodCall |
| 201 | + && $node->var instanceof Variable |
| 202 | + && $node->var->name === $variableName |
| 203 | + && $node->name instanceof Identifier |
| 204 | + && str_starts_with($node->name->toString(), 'add') |
| 205 | + && $node->getArgs() !== [] |
| 206 | + ) { |
| 207 | + $firstArg = $node->getArgs()[0]->value; |
| 208 | + if ($firstArg instanceof String_ && $firstArg->value === $componentName) { |
| 209 | + return $node->name->toString(); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + foreach ($node->getSubNodeNames() as $name) { |
| 214 | + $subNode = $node->$name; |
| 215 | + if ($subNode instanceof Node) { |
| 216 | + $result = $this->walkNode($subNode, $variableName, $componentName); |
| 217 | + if ($result !== null) { |
| 218 | + return $result; |
| 219 | + } |
| 220 | + } elseif (is_array($subNode)) { |
| 221 | + foreach ($subNode as $item) { |
| 222 | + if ($item instanceof Node) { |
| 223 | + $result = $this->walkNode($item, $variableName, $componentName); |
| 224 | + if ($result !== null) { |
| 225 | + return $result; |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + return null; |
| 233 | + } |
| 234 | +} |
0 commit comments