|
2 | 2 |
|
3 | 3 | namespace PHPStan\Type; |
4 | 4 |
|
| 5 | +use PHPStan\DependencyInjection\ReportUnsafeArrayStringKeyCastingToggle; |
5 | 6 | use PHPStan\Php\PhpVersion; |
6 | 7 | use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode; |
7 | 8 | use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
|
12 | 13 | use PHPStan\ShouldNotHappenException; |
13 | 14 | use PHPStan\TrinaryLogic; |
14 | 15 | use PHPStan\Type\Accessory\AccessoryArrayListType; |
| 16 | +use PHPStan\Type\Accessory\AccessoryDecimalIntegerStringType; |
15 | 17 | use PHPStan\Type\Accessory\HasOffsetValueType; |
16 | 18 | use PHPStan\Type\Accessory\NonEmptyArrayType; |
17 | 19 | use PHPStan\Type\Constant\ConstantArrayType; |
@@ -47,6 +49,8 @@ class ArrayType implements Type |
47 | 49 |
|
48 | 50 | private Type $keyType; |
49 | 51 |
|
| 52 | + private ?Type $cachedIterableKeyType = null; |
| 53 | + |
50 | 54 | /** @api */ |
51 | 55 | public function __construct(Type $keyType, private Type $itemType) |
52 | 56 | { |
@@ -198,15 +202,44 @@ public function getArraySize(): Type |
198 | 202 |
|
199 | 203 | public function getIterableKeyType(): Type |
200 | 204 | { |
| 205 | + if ($this->cachedIterableKeyType !== null) { |
| 206 | + return $this->cachedIterableKeyType; |
| 207 | + } |
201 | 208 | $keyType = $this->keyType; |
202 | 209 | if ($keyType instanceof MixedType && !$keyType instanceof TemplateMixedType) { |
203 | | - return new BenevolentUnionType([new IntegerType(), new StringType()]); |
| 210 | + $keyType = new BenevolentUnionType([new IntegerType(), new StringType()]); |
204 | 211 | } |
205 | 212 | if ($keyType instanceof StrictMixedType) { |
206 | | - return new BenevolentUnionType([new IntegerType(), new StringType()]); |
| 213 | + $keyType = new BenevolentUnionType([new IntegerType(), new StringType()]); |
| 214 | + } |
| 215 | + |
| 216 | + $level = ReportUnsafeArrayStringKeyCastingToggle::getLevel(); |
| 217 | + if ($level === null) { |
| 218 | + return $this->cachedIterableKeyType = $keyType; |
| 219 | + } |
| 220 | + |
| 221 | + if ($level === ReportUnsafeArrayStringKeyCastingToggle::PREVENT) { |
| 222 | + return $this->cachedIterableKeyType = $keyType; |
| 223 | + } |
| 224 | + |
| 225 | + if ($level !== ReportUnsafeArrayStringKeyCastingToggle::DETECT) { // @phpstan-ignore notIdentical.alwaysFalse |
| 226 | + throw new ShouldNotHappenException(); |
207 | 227 | } |
208 | 228 |
|
209 | | - return $keyType; |
| 229 | + return $this->cachedIterableKeyType = TypeTraverser::map($keyType, static function (Type $type, callable $traverse): Type { |
| 230 | + if ($type instanceof UnionType) { |
| 231 | + return $traverse($type); |
| 232 | + } |
| 233 | + |
| 234 | + if ($type->isString()->yes() && !$type->isDecimalIntegerString()->no()) { |
| 235 | + return TypeCombinator::union( |
| 236 | + new IntegerType(), |
| 237 | + TypeCombinator::intersect($type, new AccessoryDecimalIntegerStringType(inverse: true)), |
| 238 | + ); |
| 239 | + } |
| 240 | + |
| 241 | + return $type; |
| 242 | + }); |
210 | 243 | } |
211 | 244 |
|
212 | 245 | public function getFirstIterableKeyType(): Type |
|
0 commit comments