|
8 | 8 | use PhpParser\Node\Expr\Closure; |
9 | 9 | use PhpParser\Node\Stmt\ClassMethod; |
10 | 10 | use PhpParser\Node\Stmt\Function_; |
| 11 | +use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode; |
11 | 12 | use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode; |
| 13 | +use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode; |
| 14 | +use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeForParameterNode; |
| 15 | +use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeNode; |
12 | 16 | use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode; |
13 | 17 | use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
| 18 | +use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode; |
| 19 | +use PHPStan\PhpDocParser\Ast\Type\NullableTypeNode; |
| 20 | +use PHPStan\PhpDocParser\Ast\Type\ObjectShapeNode; |
| 21 | +use PHPStan\PhpDocParser\Ast\Type\OffsetAccessTypeNode; |
14 | 22 | use PHPStan\PhpDocParser\Ast\Type\TypeNode; |
| 23 | +use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; |
15 | 24 |
|
16 | 25 | /** |
17 | 26 | * Enforces LSP safety rules and runtime state invariants to determine |
@@ -78,11 +87,8 @@ public static function isReturnEliminationSafe( |
78 | 87 | return false; |
79 | 88 | } |
80 | 89 |
|
81 | | - if ($returnTypeNode instanceof GenericTypeNode) { |
82 | | - $baseName = strtolower((string) $returnTypeNode->type); |
83 | | - if (! isset(self::PERMISSIBLE_NON_OBJECT_GENERICS[$baseName])) { |
84 | | - return false; |
85 | | - } |
| 90 | + if (self::containsObjectGeneric($returnTypeNode)) { |
| 91 | + return false; |
86 | 92 | } |
87 | 93 |
|
88 | 94 | if ($classContext === null || $functionNode instanceof Function_ || $functionNode instanceof Closure || $functionNode instanceof ArrowFunction) { |
@@ -156,4 +162,93 @@ public static function isGeneratorEliminationSafe( |
156 | 162 |
|
157 | 163 | return false; |
158 | 164 | } |
| 165 | + |
| 166 | + /** |
| 167 | + * Recursively checks whether a TypeNode contains any object GenericTypeNode |
| 168 | + * (e.g. Collection<Dog>, ?Collection<Dog>, Collection<Dog>|null, list<Collection<Dog>>, |
| 169 | + * array{c: Collection<Dog>}) whose runtime WeakMap template bindings must be preserved. |
| 170 | + */ |
| 171 | + public static function containsObjectGeneric(TypeNode $typeNode): bool |
| 172 | + { |
| 173 | + while ($typeNode instanceof NullableTypeNode || $typeNode instanceof ArrayTypeNode) { |
| 174 | + $typeNode = $typeNode->type; |
| 175 | + } |
| 176 | + |
| 177 | + if ($typeNode instanceof GenericTypeNode) { |
| 178 | + $baseName = strtolower((string) $typeNode->type); |
| 179 | + if (! isset(self::PERMISSIBLE_NON_OBJECT_GENERICS[$baseName])) { |
| 180 | + return true; |
| 181 | + } |
| 182 | + |
| 183 | + foreach ($typeNode->genericTypes as $genericArg) { |
| 184 | + if (self::containsObjectGeneric($genericArg)) { |
| 185 | + return true; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + return false; |
| 190 | + } |
| 191 | + |
| 192 | + if ($typeNode instanceof UnionTypeNode || $typeNode instanceof IntersectionTypeNode) { |
| 193 | + foreach ($typeNode->types as $member) { |
| 194 | + if (self::containsObjectGeneric($member)) { |
| 195 | + return true; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + return false; |
| 200 | + } |
| 201 | + |
| 202 | + if ($typeNode instanceof ArrayShapeNode) { |
| 203 | + foreach ($typeNode->items as $item) { |
| 204 | + if (self::containsObjectGeneric($item->valueType)) { |
| 205 | + return true; |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + if ($typeNode->unsealedType !== null) { |
| 210 | + if (self::containsObjectGeneric($typeNode->unsealedType->valueType)) { |
| 211 | + return true; |
| 212 | + } |
| 213 | + |
| 214 | + if ($typeNode->unsealedType->keyType !== null && self::containsObjectGeneric($typeNode->unsealedType->keyType)) { |
| 215 | + return true; |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + return false; |
| 220 | + } |
| 221 | + |
| 222 | + if ($typeNode instanceof ObjectShapeNode) { |
| 223 | + foreach ($typeNode->items as $item) { |
| 224 | + if (self::containsObjectGeneric($item->valueType)) { |
| 225 | + return true; |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + return false; |
| 230 | + } |
| 231 | + |
| 232 | + if ($typeNode instanceof CallableTypeNode) { |
| 233 | + foreach ($typeNode->parameters as $param) { |
| 234 | + if (self::containsObjectGeneric($param->type)) { |
| 235 | + return true; |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + return self::containsObjectGeneric($typeNode->returnType); |
| 240 | + } |
| 241 | + |
| 242 | + if ($typeNode instanceof ConditionalTypeNode || $typeNode instanceof ConditionalTypeForParameterNode) { |
| 243 | + return self::containsObjectGeneric($typeNode->if) |
| 244 | + || self::containsObjectGeneric($typeNode->else); |
| 245 | + } |
| 246 | + |
| 247 | + if ($typeNode instanceof OffsetAccessTypeNode) { |
| 248 | + return self::containsObjectGeneric($typeNode->type) |
| 249 | + || self::containsObjectGeneric($typeNode->offset); |
| 250 | + } |
| 251 | + |
| 252 | + return false; |
| 253 | + } |
159 | 254 | } |
0 commit comments