|
5 | 5 | use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode; |
6 | 6 | use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
7 | 7 | use PHPStan\PhpDocParser\Ast\Type\TypeNode; |
| 8 | +use PHPStan\Reflection\ClassMemberAccessAnswerer; |
| 9 | +use PHPStan\Reflection\ConsistentTemplatesHelper; |
| 10 | +use PHPStan\Reflection\ExtendedMethodReflection; |
| 11 | +use PHPStan\Reflection\ExtendedPropertyReflection; |
| 12 | +use PHPStan\Reflection\Type\UnresolvedMethodPrototypeReflection; |
| 13 | +use PHPStan\Reflection\Type\UnresolvedPropertyPrototypeReflection; |
8 | 14 | use PHPStan\Turbo\ShadowedByTurboExtension; |
9 | 15 | use PHPStan\Type\AcceptsResult; |
10 | 16 | use PHPStan\Type\CompoundType; |
11 | 17 | use PHPStan\Type\IsSuperTypeOfResult; |
12 | 18 | use PHPStan\Type\LateResolvableType; |
| 19 | +use PHPStan\Type\StaticType; |
13 | 20 | use PHPStan\Type\Traits\LateResolvableTypeTrait; |
14 | 21 | use PHPStan\Type\Traits\NonGeneralizableTypeTrait; |
15 | 22 | use PHPStan\Type\Type; |
@@ -48,6 +55,35 @@ public function __construct( |
48 | 55 | { |
49 | 56 | } |
50 | 57 |
|
| 58 | + /** |
| 59 | + * `static<U>` called on a TColl, or on a TColl<…> that is not known yet, is TColl<U> |
| 60 | + * when the class has `@phpstan-consistent-templates`. |
| 61 | + */ |
| 62 | + public static function ofCalledOnType(Type $calledOnType, StaticType $static): ?self |
| 63 | + { |
| 64 | + if (!$static instanceof GenericStaticType) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + if ($calledOnType instanceof self) { |
| 69 | + if ($calledOnType->isResolvable()) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + return new self($calledOnType->constructor, $static, true); |
| 74 | + } |
| 75 | + |
| 76 | + if (!$calledOnType instanceof TemplateType) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + if ((new ConsistentTemplatesHelper())->findConsistentTemplatesClass($static->getClassReflection()) === null) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + return new self($calledOnType, $static, true); |
| 85 | + } |
| 86 | + |
51 | 87 | public function isConsistent(): bool |
52 | 88 | { |
53 | 89 | return $this->consistent; |
@@ -161,6 +197,68 @@ public function inferTemplateTypes(Type $receivedType): TemplateTypeMap |
161 | 197 | ->union($this->application->getStaticObjectType()->inferTemplateTypes($receivedType)); |
162 | 198 | } |
163 | 199 |
|
| 200 | + // the methods and properties of the class TColl will be, where `static` is that class |
| 201 | + |
| 202 | + public function getMethod(string $methodName, ClassMemberAccessAnswerer $scope): ExtendedMethodReflection |
| 203 | + { |
| 204 | + return $this->getUnresolvedMethodPrototype($methodName, $scope)->getTransformedMethod(); |
| 205 | + } |
| 206 | + |
| 207 | + public function getProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection |
| 208 | + { |
| 209 | + return $this->getUnresolvedPropertyPrototype($propertyName, $scope)->getTransformedProperty(); |
| 210 | + } |
| 211 | + |
| 212 | + public function getInstanceProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection |
| 213 | + { |
| 214 | + return $this->getUnresolvedInstancePropertyPrototype($propertyName, $scope)->getTransformedProperty(); |
| 215 | + } |
| 216 | + |
| 217 | + public function getStaticProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection |
| 218 | + { |
| 219 | + return $this->getUnresolvedStaticPropertyPrototype($propertyName, $scope)->getTransformedProperty(); |
| 220 | + } |
| 221 | + |
| 222 | + public function getUnresolvedMethodPrototype(string $methodName, ClassMemberAccessAnswerer $scope): UnresolvedMethodPrototypeReflection |
| 223 | + { |
| 224 | + $prototype = $this->resolve()->getUnresolvedMethodPrototype($methodName, $scope); |
| 225 | + if ($this->isResolvable()) { |
| 226 | + return $prototype; |
| 227 | + } |
| 228 | + |
| 229 | + return $prototype->withCalledOnType($this); |
| 230 | + } |
| 231 | + |
| 232 | + public function getUnresolvedPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection |
| 233 | + { |
| 234 | + $prototype = $this->resolve()->getUnresolvedPropertyPrototype($propertyName, $scope); |
| 235 | + if ($this->isResolvable()) { |
| 236 | + return $prototype; |
| 237 | + } |
| 238 | + |
| 239 | + return $prototype->withFechedOnType($this); |
| 240 | + } |
| 241 | + |
| 242 | + public function getUnresolvedInstancePropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection |
| 243 | + { |
| 244 | + $prototype = $this->resolve()->getUnresolvedInstancePropertyPrototype($propertyName, $scope); |
| 245 | + if ($this->isResolvable()) { |
| 246 | + return $prototype; |
| 247 | + } |
| 248 | + |
| 249 | + return $prototype->withFechedOnType($this); |
| 250 | + } |
| 251 | + |
| 252 | + public function getUnresolvedStaticPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection |
| 253 | + { |
| 254 | + $prototype = $this->resolve()->getUnresolvedStaticPropertyPrototype($propertyName, $scope); |
| 255 | + if ($this->isResolvable()) { |
| 256 | + return $prototype; |
| 257 | + } |
| 258 | + |
| 259 | + return $prototype->withFechedOnType($this); |
| 260 | + } |
| 261 | + |
164 | 262 | /** @phpstan-assert-if-true self $type */ |
165 | 263 | private function isSameUnresolvedClass(Type $type): bool |
166 | 264 | { |
|
0 commit comments