|
9 | 9 | use PHPStan\Reflection\ConsistentTemplatesHelper; |
10 | 10 | use PHPStan\Reflection\ExtendedMethodReflection; |
11 | 11 | use PHPStan\Reflection\ExtendedPropertyReflection; |
| 12 | +use PHPStan\Reflection\ReflectionProviderStaticAccessor; |
12 | 13 | use PHPStan\Reflection\Type\UnresolvedMethodPrototypeReflection; |
13 | 14 | use PHPStan\Reflection\Type\UnresolvedPropertyPrototypeReflection; |
14 | 15 | use PHPStan\Turbo\ShadowedByTurboExtension; |
|
24 | 25 | use PHPStan\Type\VerbosityLevel; |
25 | 26 | use function array_map; |
26 | 27 | use function array_merge; |
| 28 | +use function count; |
27 | 29 | use function implode; |
28 | 30 | use function sprintf; |
29 | 31 |
|
@@ -84,6 +86,38 @@ public static function ofCalledOnType(Type $calledOnType, StaticType $static): ? |
84 | 86 | return new self($calledOnType, $static, true); |
85 | 87 | } |
86 | 88 |
|
| 89 | + /** |
| 90 | + * `new $class(…)` on a class-string<TColl> is a TColl<…> when the class TColl is bound by |
| 91 | + * has `@phpstan-consistent-templates`: this application of TColl to that class's own |
| 92 | + * template types, for the caller to resolve from the constructor's arguments. |
| 93 | + */ |
| 94 | + public static function ofInstantiation(Type $objectType): ?self |
| 95 | + { |
| 96 | + if (!$objectType instanceof TemplateType) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + $classReflections = $objectType->getBound()->getObjectClassReflections(); |
| 101 | + if (count($classReflections) !== 1) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + $classReflection = ReflectionProviderStaticAccessor::getInstance()->getClass($classReflections[0]->getName()); |
| 106 | + if (!$classReflection->isGeneric()) { |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + if ((new ConsistentTemplatesHelper())->findConsistentTemplatesClass($classReflection) === null) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + return new self( |
| 115 | + $objectType, |
| 116 | + new GenericStaticType($classReflection, $classReflection->typeMapToList($classReflection->getTemplateTypeMap()), null, []), |
| 117 | + true, |
| 118 | + ); |
| 119 | + } |
| 120 | + |
87 | 121 | public function isConsistent(): bool |
88 | 122 | { |
89 | 123 | return $this->consistent; |
@@ -189,6 +223,35 @@ public function accepts(Type $type, bool $strictTypes): AcceptsResult |
189 | 223 | return $accepts; |
190 | 224 | } |
191 | 225 |
|
| 226 | + public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult |
| 227 | + { |
| 228 | + // a TColl<X> is a TColl |
| 229 | + if (!$this->isResolvable() && $otherType->isSuperTypeOf($this->constructor)->yes()) { |
| 230 | + return IsSuperTypeOfResult::createYes(); |
| 231 | + } |
| 232 | + |
| 233 | + $result = $this->resolve(); |
| 234 | + if ($result instanceof CompoundType) { |
| 235 | + return $result->isSubTypeOf($otherType); |
| 236 | + } |
| 237 | + |
| 238 | + return $otherType->isSuperTypeOf($result); |
| 239 | + } |
| 240 | + |
| 241 | + public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult |
| 242 | + { |
| 243 | + if (!$this->isResolvable() && $acceptingType->accepts($this->constructor, $strictTypes)->yes()) { |
| 244 | + return AcceptsResult::createYes(); |
| 245 | + } |
| 246 | + |
| 247 | + $result = $this->resolve(); |
| 248 | + if ($result instanceof CompoundType) { |
| 249 | + return $result->isAcceptedBy($acceptingType, $strictTypes); |
| 250 | + } |
| 251 | + |
| 252 | + return $acceptingType->accepts($result, $strictTypes); |
| 253 | + } |
| 254 | + |
192 | 255 | public function inferTemplateTypes(Type $receivedType): TemplateTypeMap |
193 | 256 | { |
194 | 257 | // the received object is the class the constructor stands for, |
|
0 commit comments