Skip to content

Commit aed6fc3

Browse files
committed
Introduce Type->getConstantArrayKeys()
1 parent 8ae740c commit aed6fc3

37 files changed

+227
-4
lines changed

src/Analyser/MutatingScope.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4229,29 +4229,41 @@ public function specifyExpressionType(Expr $expr, Type $type, Type $nativeType,
42294229

42304230
$scope = $this;
42314231
if ($expr instanceof Expr\ArrayDimFetch && $expr->dim !== null) {
4232-
$dimType = $scope->getType($expr->dim)->toArrayKey();
4233-
if ($dimType instanceof ConstantIntegerType || $dimType instanceof ConstantStringType) {
4232+
$dimTypes = $scope->getType($expr->dim)->getConstantArrayKeys();
4233+
4234+
if ($dimTypes !== []) {
42344235
$exprVarType = $scope->getType($expr->var);
42354236
if (!$exprVarType instanceof MixedType && !$exprVarType->isArray()->no()) {
4237+
$hasOffsets = [];
4238+
$containsInteger = false;
4239+
foreach($dimTypes as $dimType) {
4240+
$hasOffsets[] = new HasOffsetValueType($dimType, $type);
4241+
4242+
if ($dimType instanceof ConstantIntegerType) {
4243+
$containsInteger = true;
4244+
}
4245+
}
4246+
42364247
$types = [
42374248
new ArrayType(new MixedType(), new MixedType()),
42384249
new ObjectType(ArrayAccess::class),
42394250
new NullType(),
42404251
];
4241-
if ($dimType instanceof ConstantIntegerType) {
4252+
if ($containsInteger) {
42424253
$types[] = new StringType();
42434254
}
42444255

42454256
$scope = $scope->specifyExpressionType(
42464257
$expr->var,
42474258
TypeCombinator::intersect(
42484259
TypeCombinator::intersect($exprVarType, TypeCombinator::union(...$types)),
4249-
new HasOffsetValueType($dimType, $type),
4260+
...$hasOffsets,
42504261
),
42514262
$scope->getNativeType($expr->var),
42524263
$certainty,
42534264
);
42544265
}
4266+
42554267
}
42564268
}
42574269

src/Type/Accessory/AccessoryArrayListType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public function getConstantArrays(): array
7070
return [];
7171
}
7272

73+
public function getConstantArrayKeys(): array
74+
{
75+
return [];
76+
}
77+
7378
public function getConstantStrings(): array
7479
{
7580
return [];

src/Type/Accessory/AccessoryLiteralStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ public function getObjectClassReflections(): array
6464
return [];
6565
}
6666

67+
public function getConstantArrayKeys(): array
68+
{
69+
return [];
70+
}
71+
6772
public function getConstantStrings(): array
6873
{
6974
return [];

src/Type/Accessory/AccessoryLowercaseStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ public function getObjectClassReflections(): array
6464
return [];
6565
}
6666

67+
public function getConstantArrayKeys(): array
68+
{
69+
return [];
70+
}
71+
6772
public function getConstantStrings(): array
6873
{
6974
return [];

src/Type/Accessory/AccessoryNonEmptyStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ public function getObjectClassReflections(): array
6565
return [];
6666
}
6767

68+
public function getConstantArrayKeys(): array
69+
{
70+
return [];
71+
}
72+
6873
public function getConstantStrings(): array
6974
{
7075
return [];

src/Type/Accessory/AccessoryNonFalsyStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public function getObjectClassReflections(): array
6767
return [];
6868
}
6969

70+
public function getConstantArrayKeys(): array
71+
{
72+
return [];
73+
}
74+
7075
public function getConstantStrings(): array
7176
{
7277
return [];

src/Type/Accessory/AccessoryNumericStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ public function getObjectClassReflections(): array
6464
return [];
6565
}
6666

67+
public function getConstantArrayKeys(): array
68+
{
69+
return [];
70+
}
71+
6772
public function getConstantStrings(): array
6873
{
6974
return [];

src/Type/Accessory/AccessoryUppercaseStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ public function getObjectClassReflections(): array
6464
return [];
6565
}
6666

67+
public function getConstantArrayKeys(): array
68+
{
69+
return [];
70+
}
71+
6772
public function getConstantStrings(): array
6873
{
6974
return [];

src/Type/Accessory/HasOffsetType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public function getObjectClassReflections(): array
7575
return [];
7676
}
7777

78+
public function getConstantArrayKeys(): array
79+
{
80+
return [];
81+
}
82+
7883
public function getConstantStrings(): array
7984
{
8085
return [];

src/Type/Accessory/HasOffsetValueType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public function getObjectClassReflections(): array
7878
return [];
7979
}
8080

81+
public function getConstantArrayKeys(): array
82+
{
83+
return [];
84+
}
85+
8186
public function getConstantStrings(): array
8287
{
8388
return [];

0 commit comments

Comments
 (0)