|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Generic; |
| 4 | + |
| 5 | +use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode; |
| 6 | +use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
| 7 | +use PHPStan\PhpDocParser\Ast\Type\TypeNode; |
| 8 | +use PHPStan\Turbo\ShadowedByTurboExtension; |
| 9 | +use PHPStan\Type\AcceptsResult; |
| 10 | +use PHPStan\Type\CompoundType; |
| 11 | +use PHPStan\Type\IsSuperTypeOfResult; |
| 12 | +use PHPStan\Type\LateResolvableType; |
| 13 | +use PHPStan\Type\Traits\LateResolvableTypeTrait; |
| 14 | +use PHPStan\Type\Traits\NonGeneralizableTypeTrait; |
| 15 | +use PHPStan\Type\Type; |
| 16 | +use PHPStan\Type\TypeCombinator; |
| 17 | +use PHPStan\Type\VerbosityLevel; |
| 18 | +use function array_map; |
| 19 | +use function array_merge; |
| 20 | +use function implode; |
| 21 | +use function sprintf; |
| 22 | + |
| 23 | +/** |
| 24 | + * `TColl<string>` for `@template TColl of Coll` where Coll has `@phpstan-consistent-templates`: |
| 25 | + * the class TColl is with other type arguments. Once TColl is known, like `Vec<int>`, the type |
| 26 | + * is that class with these arguments, `Vec<string>`. Until then it is a `Coll<string>`, and |
| 27 | + * only another `TColl<…>` is known to be the same class. |
| 28 | + * |
| 29 | + * The application is a `static<…>` of the bound's class, so a class below it gets the type |
| 30 | + * arguments the way `static<…>` called on it does. |
| 31 | + */ |
| 32 | +#[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/TemplateTypeApplication.cpp')] |
| 33 | +final class TemplateTypeApplication implements CompoundType, LateResolvableType |
| 34 | +{ |
| 35 | + |
| 36 | + use LateResolvableTypeTrait; |
| 37 | + use NonGeneralizableTypeTrait; |
| 38 | + |
| 39 | + /** |
| 40 | + * @param bool $consistent Whether the bound's class has `@phpstan-consistent-templates`; |
| 41 | + * without it, the type is the bound with the type arguments. |
| 42 | + */ |
| 43 | + public function __construct( |
| 44 | + private Type $constructor, |
| 45 | + private GenericStaticType $application, |
| 46 | + private bool $consistent, |
| 47 | + ) |
| 48 | + { |
| 49 | + } |
| 50 | + |
| 51 | + public function isConsistent(): bool |
| 52 | + { |
| 53 | + return $this->consistent; |
| 54 | + } |
| 55 | + |
| 56 | + public function getConstructor(): Type |
| 57 | + { |
| 58 | + return $this->constructor; |
| 59 | + } |
| 60 | + |
| 61 | + public function getApplication(): GenericStaticType |
| 62 | + { |
| 63 | + return $this->application; |
| 64 | + } |
| 65 | + |
| 66 | + public function getReferencedClasses(): array |
| 67 | + { |
| 68 | + return array_merge($this->constructor->getReferencedClasses(), $this->application->getReferencedClasses()); |
| 69 | + } |
| 70 | + |
| 71 | + public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array |
| 72 | + { |
| 73 | + return array_merge( |
| 74 | + $this->constructor->getReferencedTemplateTypes($positionVariance), |
| 75 | + $this->application->getReferencedTemplateTypes($positionVariance), |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + public function equals(Type $type): bool |
| 80 | + { |
| 81 | + return $type instanceof self |
| 82 | + && $this->constructor->equals($type->constructor) |
| 83 | + && $this->application->equals($type->application); |
| 84 | + } |
| 85 | + |
| 86 | + public function describe(VerbosityLevel $level): string |
| 87 | + { |
| 88 | + if ($this->isResolvable() || !$this->constructor instanceof TemplateType) { |
| 89 | + return $this->resolve()->describe($level); |
| 90 | + } |
| 91 | + |
| 92 | + $constructor = $this->constructor; |
| 93 | + $arguments = implode(', ', array_map(static fn (Type $type): string => $type->describe($level), $this->application->getTypes())); |
| 94 | + |
| 95 | + // written as in the PHPDoc, but the cache keys tell apart the templates of the same name |
| 96 | + return $level->handle( |
| 97 | + static fn (): string => sprintf('%s<%s>', $constructor->getName(), $arguments), |
| 98 | + static fn (): string => sprintf('%s<%s>', $constructor->getName(), $arguments), |
| 99 | + static fn (): string => sprintf('%s<%s>', $constructor->getName(), $arguments), |
| 100 | + static fn (): string => sprintf('%s<%s>', $constructor->describe(VerbosityLevel::cache()), $arguments), |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + public function isResolvable(): bool |
| 105 | + { |
| 106 | + return !$this->consistent || !$this->constructor instanceof TemplateType; |
| 107 | + } |
| 108 | + |
| 109 | + protected function getResult(): Type |
| 110 | + { |
| 111 | + if (!$this->consistent) { |
| 112 | + return $this->application->getStaticObjectType(); |
| 113 | + } |
| 114 | + |
| 115 | + $applicationClassReflection = $this->application->getClassReflection(); |
| 116 | + $results = []; |
| 117 | + foreach ($this->constructor->getObjectClassReflections() as $classReflection) { |
| 118 | + if ($classReflection->getName() !== $applicationClassReflection->getName() && !$classReflection->isSubclassOfClass($applicationClassReflection)) { |
| 119 | + continue; |
| 120 | + } |
| 121 | + |
| 122 | + $results[] = $this->application->changeBaseClass($classReflection)->getStaticObjectType(); |
| 123 | + } |
| 124 | + |
| 125 | + if ($results === []) { |
| 126 | + return $this->application->getStaticObjectType(); |
| 127 | + } |
| 128 | + |
| 129 | + return TypeCombinator::union(...$results); |
| 130 | + } |
| 131 | + |
| 132 | + public function isSuperTypeOf(Type $type): IsSuperTypeOfResult |
| 133 | + { |
| 134 | + if ($this->isSameUnresolvedClass($type)) { |
| 135 | + return $this->application->getStaticObjectType()->isSuperTypeOf($type->application->getStaticObjectType()); |
| 136 | + } |
| 137 | + |
| 138 | + return $this->isSuperTypeOfDefault($type); |
| 139 | + } |
| 140 | + |
| 141 | + public function accepts(Type $type, bool $strictTypes): AcceptsResult |
| 142 | + { |
| 143 | + if ($this->isSameUnresolvedClass($type)) { |
| 144 | + return $this->application->getStaticObjectType()->accepts($type->application->getStaticObjectType(), $strictTypes); |
| 145 | + } |
| 146 | + |
| 147 | + $accepts = $this->resolve()->accepts($type, $strictTypes); |
| 148 | + if (!$this->isResolvable()) { |
| 149 | + // like a template, an unknown class is the same as another one only maybe |
| 150 | + return $accepts->and(AcceptsResult::createMaybe()); |
| 151 | + } |
| 152 | + |
| 153 | + return $accepts; |
| 154 | + } |
| 155 | + |
| 156 | + public function inferTemplateTypes(Type $receivedType): TemplateTypeMap |
| 157 | + { |
| 158 | + // the received object is the class the constructor stands for, |
| 159 | + // and its type arguments are what the application has |
| 160 | + return $this->constructor->inferTemplateTypes($receivedType) |
| 161 | + ->union($this->application->getStaticObjectType()->inferTemplateTypes($receivedType)); |
| 162 | + } |
| 163 | + |
| 164 | + /** @phpstan-assert-if-true self $type */ |
| 165 | + private function isSameUnresolvedClass(Type $type): bool |
| 166 | + { |
| 167 | + return $type instanceof self |
| 168 | + && !$this->isResolvable() |
| 169 | + && $this->constructor->equals($type->constructor); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * @param callable(Type): Type $cb |
| 174 | + */ |
| 175 | + public function traverse(callable $cb): Type |
| 176 | + { |
| 177 | + $constructor = $cb($this->constructor); |
| 178 | + $application = $this->application->traverse($cb); |
| 179 | + |
| 180 | + if ($this->constructor === $constructor && $this->application === $application) { |
| 181 | + return $this; |
| 182 | + } |
| 183 | + |
| 184 | + return $this->create($constructor, $application); |
| 185 | + } |
| 186 | + |
| 187 | + public function traverseSimultaneously(Type $right, callable $cb): Type |
| 188 | + { |
| 189 | + if (!$right instanceof self) { |
| 190 | + return $this; |
| 191 | + } |
| 192 | + |
| 193 | + $constructor = $cb($this->constructor, $right->constructor); |
| 194 | + $application = $this->application->traverseSimultaneously($right->application, $cb); |
| 195 | + |
| 196 | + if ($this->constructor === $constructor && $this->application === $application) { |
| 197 | + return $this; |
| 198 | + } |
| 199 | + |
| 200 | + return $this->create($constructor, $application); |
| 201 | + } |
| 202 | + |
| 203 | + /** A traversal may turn the application into anything, which then stands on its own. */ |
| 204 | + private function create(Type $constructor, Type $application): Type |
| 205 | + { |
| 206 | + if (!$application instanceof GenericStaticType) { |
| 207 | + return $application; |
| 208 | + } |
| 209 | + |
| 210 | + return new self($constructor, $application, $this->consistent); |
| 211 | + } |
| 212 | + |
| 213 | + public function toPhpDocNode(): TypeNode |
| 214 | + { |
| 215 | + $constructor = $this->constructor->toPhpDocNode(); |
| 216 | + if (!$constructor instanceof IdentifierTypeNode) { |
| 217 | + return $this->resolve()->toPhpDocNode(); |
| 218 | + } |
| 219 | + |
| 220 | + return new GenericTypeNode( |
| 221 | + $constructor, |
| 222 | + array_map(static fn (Type $type): TypeNode => $type->toPhpDocNode(), $this->application->getTypes()), |
| 223 | + ); |
| 224 | + } |
| 225 | + |
| 226 | +} |
0 commit comments