|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type; |
| 4 | + |
| 5 | +use PHPStan\Type\Constant\ConstantArrayType; |
| 6 | +use PHPStan\Type\Constant\ConstantIntegerType; |
| 7 | +use PHPStan\Type\Constant\ConstantStringType; |
| 8 | +use function count; |
| 9 | +use function implode; |
| 10 | +use function in_array; |
| 11 | +use function sort; |
| 12 | +use function spl_object_id; |
| 13 | + |
| 14 | +/** |
| 15 | + * The shapes nested loops nest in each other, by their keys, for |
| 16 | + * RecursiveType::fromNestedShapes(): a shape with the keys of one at the top |
| 17 | + * of the value folds into the value, keeping the values where the nesting ends. |
| 18 | + * |
| 19 | + * @internal |
| 20 | + */ |
| 21 | +final class NestedShapeFolder |
| 22 | +{ |
| 23 | + |
| 24 | + /** @var array<string, true> the keys the shapes nest each other under */ |
| 25 | + private array $nestingKeys = []; |
| 26 | + |
| 27 | + /** |
| 28 | + * @param array<string, true> $keySets |
| 29 | + */ |
| 30 | + public function __construct(private array $keySets) |
| 31 | + { |
| 32 | + } |
| 33 | + |
| 34 | + public static function getKeySet(Type $type): ?string |
| 35 | + { |
| 36 | + $type = self::getShape($type); |
| 37 | + if ($type === null) { |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + $keys = []; |
| 42 | + foreach ($type->getKeyTypes() as $keyType) { |
| 43 | + $keys[] = self::getKey($keyType); |
| 44 | + } |
| 45 | + sort($keys); |
| 46 | + |
| 47 | + return implode("\0", $keys); |
| 48 | + } |
| 49 | + |
| 50 | + /** Whether the type is one of the shapes, or a recursive type of them an inner loop made. */ |
| 51 | + public function folds(Type $type): bool |
| 52 | + { |
| 53 | + if ($type instanceof RecursiveType) { |
| 54 | + $body = $type->getBody(); |
| 55 | + $found = false; |
| 56 | + foreach ($body instanceof UnionType ? $body->getTypes() : [$body] as $member) { |
| 57 | + $keySet = self::getKeySet($member); |
| 58 | + if ($keySet === null) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + if (!isset($this->keySets[$keySet])) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + $found = true; |
| 65 | + } |
| 66 | + |
| 67 | + return $found; |
| 68 | + } |
| 69 | + |
| 70 | + $keySet = self::getKeySet($type); |
| 71 | + |
| 72 | + return $keySet !== null && isset($this->keySets[$keySet]); |
| 73 | + } |
| 74 | + |
| 75 | + public function collectNestingKeys(Type $type): void |
| 76 | + { |
| 77 | + $this->walk($type, function (string $key, Type $value): void { |
| 78 | + // a reference is a shape the value nests in itself |
| 79 | + if (!$value instanceof RecursiveTypeReference && !$this->folds($value)) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + $this->nestingKeys[$key] = true; |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * The values under the nesting keys that are not shapes, at every depth. |
| 89 | + * |
| 90 | + * @return list<Type> |
| 91 | + */ |
| 92 | + public function getEnds(Type $type): array |
| 93 | + { |
| 94 | + $ends = []; |
| 95 | + if ($type instanceof RecursiveType) { |
| 96 | + $body = $type->getBody(); |
| 97 | + foreach ($body instanceof UnionType ? $body->getTypes() : [$body] as $member) { |
| 98 | + if ($member instanceof RecursiveTypeReference || $this->folds($member) || in_array($member, $ends, true)) { |
| 99 | + continue; |
| 100 | + } |
| 101 | + $ends[] = $member; |
| 102 | + } |
| 103 | + } |
| 104 | + $this->walk($type, function (string $key, Type $value) use (&$ends): void { |
| 105 | + if ( |
| 106 | + !isset($this->nestingKeys[$key]) |
| 107 | + || $value instanceof RecursiveTypeReference |
| 108 | + || $this->folds($value) |
| 109 | + || in_array($value, $ends, true) |
| 110 | + ) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + $ends[] = $value; |
| 115 | + }); |
| 116 | + |
| 117 | + return $ends; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * The shapes in the values of the shapes in the type, at every depth, with |
| 122 | + * a recursive type an inner loop made unfolded, so that its references stay bound. |
| 123 | + * |
| 124 | + * @return list<Type> |
| 125 | + */ |
| 126 | + public function getNestedShapes(Type $type): array |
| 127 | + { |
| 128 | + $shapes = []; |
| 129 | + $visited = []; |
| 130 | + $this->collectShapes($type, false, $shapes, $visited); |
| 131 | + |
| 132 | + return $shapes; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @param list<Type> $shapes |
| 137 | + * @param array<int, true> $visited the recursive types already unfolded |
| 138 | + */ |
| 139 | + private function collectShapes(Type $type, bool $nested, array &$shapes, array &$visited): void |
| 140 | + { |
| 141 | + if ($type instanceof RecursiveType) { |
| 142 | + if (isset($visited[spl_object_id($type)])) { |
| 143 | + return; |
| 144 | + } |
| 145 | + $visited[spl_object_id($type)] = true; |
| 146 | + $type = $type->unfold(); |
| 147 | + } |
| 148 | + if ($type instanceof UnionType) { |
| 149 | + foreach ($type->getTypes() as $member) { |
| 150 | + $this->collectShapes($member, $nested, $shapes, $visited); |
| 151 | + } |
| 152 | + return; |
| 153 | + } |
| 154 | + $shape = self::getShape($type); |
| 155 | + if ($shape === null || !$this->folds($type)) { |
| 156 | + return; |
| 157 | + } |
| 158 | + if ($nested && !in_array($type, $shapes, true)) { |
| 159 | + $shapes[] = $type; |
| 160 | + } |
| 161 | + foreach ($shape->getValueTypes() as $valueType) { |
| 162 | + $this->collectShapes($valueType, true, $shapes, $visited); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Calls $visit with each key and member of a value of the shapes in the type, |
| 168 | + * the shapes in their values too, and the members of a recursive type there. |
| 169 | + * |
| 170 | + * @param callable(string, Type): void $visit |
| 171 | + */ |
| 172 | + private function walk(Type $type, callable $visit): void |
| 173 | + { |
| 174 | + $visited = []; |
| 175 | + $this->walkShapes($type, $visit, $visited); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * @param callable(string, Type): void $visit |
| 180 | + * @param array<int, true> $visited the recursive types already walked |
| 181 | + */ |
| 182 | + private function walkShapes(Type $type, callable $visit, array &$visited): void |
| 183 | + { |
| 184 | + if ($type instanceof RecursiveType) { |
| 185 | + if (isset($visited[spl_object_id($type)])) { |
| 186 | + return; |
| 187 | + } |
| 188 | + $visited[spl_object_id($type)] = true; |
| 189 | + $type = $type->getBody(); |
| 190 | + } |
| 191 | + foreach ($type instanceof UnionType ? $type->getTypes() : [$type] as $member) { |
| 192 | + $shape = self::getShape($member); |
| 193 | + if ($shape === null || !$this->folds($member)) { |
| 194 | + continue; |
| 195 | + } |
| 196 | + foreach ($shape->getKeyTypes() as $i => $keyType) { |
| 197 | + $this->walkValue(self::getKey($keyType), $shape->getValueTypes()[$i], $visit, $visited); |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * @param callable(string, Type): void $visit |
| 204 | + * @param array<int, true> $visited the recursive types already walked |
| 205 | + */ |
| 206 | + private function walkValue(string $key, Type $value, callable $visit, array &$visited): void |
| 207 | + { |
| 208 | + foreach ($value instanceof UnionType ? $value->getTypes() : [$value] as $member) { |
| 209 | + $visit($key, $member); |
| 210 | + if ($member instanceof RecursiveType) { |
| 211 | + if (isset($visited[spl_object_id($member)])) { |
| 212 | + continue; |
| 213 | + } |
| 214 | + $visited[spl_object_id($member)] = true; |
| 215 | + // what the recursive type holds besides its shapes is there too, like null |
| 216 | + $this->walkValue($key, $member->getBody(), $visit, $visited); |
| 217 | + continue; |
| 218 | + } |
| 219 | + $this->walkShapes($member, $visit, $visited); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + /** The shape the type is, but not a recursive type or its reference, which only unfold to one. */ |
| 224 | + private static function getShape(Type $type): ?ConstantArrayType |
| 225 | + { |
| 226 | + if ($type instanceof RecursiveType || $type instanceof RecursiveTypeReference) { |
| 227 | + return null; |
| 228 | + } |
| 229 | + |
| 230 | + $constantArrays = $type->getConstantArrays(); |
| 231 | + if (count($constantArrays) !== 1 || !$type->isConstantArray()->yes()) { |
| 232 | + return null; |
| 233 | + } |
| 234 | + |
| 235 | + return $constantArrays[0]; |
| 236 | + } |
| 237 | + |
| 238 | + private static function getKey(ConstantIntegerType|ConstantStringType $keyType): string |
| 239 | + { |
| 240 | + return ($keyType instanceof ConstantIntegerType ? 'i' : 's') . $keyType->getValue(); |
| 241 | + } |
| 242 | + |
| 243 | +} |
0 commit comments