|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type; |
| 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\Type\Generic\TemplateTypeHelper; |
| 9 | +use PHPStan\Type\Generic\TemplateTypeMap; |
| 10 | +use PHPStan\Type\Generic\TemplateTypeVariance; |
| 11 | +use PHPStan\Type\Generic\TemplateTypeVarianceMap; |
| 12 | +use PHPStan\Type\Traits\LateResolvableTypeTrait; |
| 13 | +use PHPStan\Type\Traits\NonGeneralizableTypeTrait; |
| 14 | +use function array_map; |
| 15 | +use function array_merge; |
| 16 | +use function array_unique; |
| 17 | +use function count; |
| 18 | +use function implode; |
| 19 | +use function sprintf; |
| 20 | + |
| 21 | +/** |
| 22 | + * Represents a generic @phpstan-type alias applied to concrete (or partially-resolved) |
| 23 | + * type arguments. For example, {@code Filter<int>} where {@code @phpstan-type Filter<TItem>} is |
| 24 | + * declared expands lazily to the alias body with TItem substituted. |
| 25 | + * |
| 26 | + * Mirrors the role of GenericObjectType for classes: GenericObjectType is a class constructor |
| 27 | + * applied to type args; GenericTypeAliasType is a type alias applied to type args. |
| 28 | + * |
| 29 | + * Implements LateResolvableType so TypeUtils::resolveLateResolvableTypes() expands it at the |
| 30 | + * right moment without leaking TemplateType placeholders to the rest of the type system. |
| 31 | + */ |
| 32 | +final class GenericTypeAliasType implements CompoundType, LateResolvableType |
| 33 | +{ |
| 34 | + |
| 35 | + use LateResolvableTypeTrait; |
| 36 | + use NonGeneralizableTypeTrait; |
| 37 | + |
| 38 | + /** |
| 39 | + * @param list<string> $paramNames Ordered parameter names from the alias declaration. |
| 40 | + * @param list<Type> $args Supplied type arguments (may be shorter than paramNames |
| 41 | + * when trailing params are covered by defaults). |
| 42 | + * @param list<Type|null> $defaults Per-param declared default type; null when the param has no default. |
| 43 | + * @param list<Type> $boundFallbacks Per-param bound type used when both arg and default are absent. |
| 44 | + */ |
| 45 | + public function __construct( |
| 46 | + private readonly string $aliasName, |
| 47 | + private readonly Type $resolvedBody, |
| 48 | + private readonly array $paramNames, |
| 49 | + private readonly array $args, |
| 50 | + private readonly array $defaults, |
| 51 | + private readonly array $boundFallbacks, |
| 52 | + ) |
| 53 | + { |
| 54 | + } |
| 55 | + |
| 56 | + public function getAliasName(): string |
| 57 | + { |
| 58 | + return $this->aliasName; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Returns the names of required params (no declared default) that were not supplied as args. |
| 63 | + * A non-empty list means this is a "raw" usage of a generic alias that should be reported. |
| 64 | + * |
| 65 | + * @return list<string> |
| 66 | + */ |
| 67 | + public function getMissingRequiredParamNames(): array |
| 68 | + { |
| 69 | + $missing = []; |
| 70 | + foreach ($this->paramNames as $i => $name) { |
| 71 | + if (!isset($this->args[$i]) && $this->defaults[$i] === null) { |
| 72 | + $missing[] = $name; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return $missing; |
| 77 | + } |
| 78 | + |
| 79 | + public function getReferencedClasses(): array |
| 80 | + { |
| 81 | + $classes = $this->resolvedBody->getReferencedClasses(); |
| 82 | + foreach ($this->args as $arg) { |
| 83 | + $classes = array_merge($classes, $arg->getReferencedClasses()); |
| 84 | + } |
| 85 | + |
| 86 | + return array_unique($classes); |
| 87 | + } |
| 88 | + |
| 89 | + public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array |
| 90 | + { |
| 91 | + $refs = []; |
| 92 | + foreach ($this->args as $arg) { |
| 93 | + $refs = array_merge($refs, $arg->getReferencedTemplateTypes($positionVariance)); |
| 94 | + } |
| 95 | + |
| 96 | + return $refs; |
| 97 | + } |
| 98 | + |
| 99 | + public function equals(Type $type): bool |
| 100 | + { |
| 101 | + if (!$type instanceof self) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + if ($this->aliasName !== $type->aliasName || count($this->args) !== count($type->args)) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + foreach ($this->args as $i => $arg) { |
| 110 | + if (!$arg->equals($type->args[$i])) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + public function describe(VerbosityLevel $level): string |
| 119 | + { |
| 120 | + if ($this->args === []) { |
| 121 | + return $this->aliasName; |
| 122 | + } |
| 123 | + |
| 124 | + return sprintf( |
| 125 | + '%s<%s>', |
| 126 | + $this->aliasName, |
| 127 | + implode(', ', array_map(static fn (Type $t) => $t->describe($level), $this->args)), |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + public function isResolvable(): bool |
| 132 | + { |
| 133 | + foreach ($this->args as $arg) { |
| 134 | + if (TypeUtils::containsTemplateType($arg)) { |
| 135 | + return false; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + foreach ($this->paramNames as $i => $name) { |
| 140 | + if (!isset($this->args[$i]) && $this->defaults[$i] === null) { |
| 141 | + return false; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return true; |
| 146 | + } |
| 147 | + |
| 148 | + protected function getResult(): Type |
| 149 | + { |
| 150 | + $map = []; |
| 151 | + foreach ($this->paramNames as $i => $name) { |
| 152 | + $map[$name] = $this->args[$i] ?? $this->defaults[$i] ?? $this->boundFallbacks[$i]; |
| 153 | + } |
| 154 | + |
| 155 | + return TemplateTypeHelper::resolveTemplateTypes( |
| 156 | + $this->resolvedBody, |
| 157 | + new TemplateTypeMap($map), |
| 158 | + TemplateTypeVarianceMap::createEmpty(), |
| 159 | + TemplateTypeVariance::createInvariant(), |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * @param callable(Type): Type $cb |
| 165 | + */ |
| 166 | + public function traverse(callable $cb): Type |
| 167 | + { |
| 168 | + $newArgs = array_map($cb, $this->args); |
| 169 | + |
| 170 | + foreach ($this->args as $i => $arg) { |
| 171 | + if ($arg !== $newArgs[$i]) { |
| 172 | + return new self( |
| 173 | + $this->aliasName, |
| 174 | + $this->resolvedBody, |
| 175 | + $this->paramNames, |
| 176 | + $newArgs, |
| 177 | + $this->defaults, |
| 178 | + $this->boundFallbacks, |
| 179 | + ); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + return $this; |
| 184 | + } |
| 185 | + |
| 186 | + public function traverseSimultaneously(Type $right, callable $cb): Type |
| 187 | + { |
| 188 | + if (!$right instanceof self) { |
| 189 | + return $this; |
| 190 | + } |
| 191 | + |
| 192 | + $newArgs = []; |
| 193 | + $changed = false; |
| 194 | + foreach ($this->args as $i => $arg) { |
| 195 | + $newArg = isset($right->args[$i]) ? $cb($arg, $right->args[$i]) : $arg; |
| 196 | + if ($newArg !== $arg) { |
| 197 | + $changed = true; |
| 198 | + } |
| 199 | + |
| 200 | + $newArgs[] = $newArg; |
| 201 | + } |
| 202 | + |
| 203 | + if (!$changed) { |
| 204 | + return $this; |
| 205 | + } |
| 206 | + |
| 207 | + return new self( |
| 208 | + $this->aliasName, |
| 209 | + $this->resolvedBody, |
| 210 | + $this->paramNames, |
| 211 | + $newArgs, |
| 212 | + $this->defaults, |
| 213 | + $this->boundFallbacks, |
| 214 | + ); |
| 215 | + } |
| 216 | + |
| 217 | + public function toPhpDocNode(): TypeNode |
| 218 | + { |
| 219 | + if ($this->args === []) { |
| 220 | + return new IdentifierTypeNode($this->aliasName); |
| 221 | + } |
| 222 | + |
| 223 | + return new GenericTypeNode( |
| 224 | + new IdentifierTypeNode($this->aliasName), |
| 225 | + array_map(static fn (Type $t) => $t->toPhpDocNode(), $this->args), |
| 226 | + ); |
| 227 | + } |
| 228 | + |
| 229 | +} |
| 230 | + |
0 commit comments