Skip to content

Commit cdf4b4d

Browse files
authored
More precise array_keys return type
1 parent 3ac64db commit cdf4b4d

18 files changed

+169
-2
lines changed

src/Type/Accessory/AccessoryArrayListType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ public function unsetOffset(Type $offsetType): Type
172172
return new ErrorType();
173173
}
174174

175+
public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type
176+
{
177+
return $this->getKeysArray();
178+
}
179+
175180
public function getKeysArray(): Type
176181
{
177182
return $this;

src/Type/Accessory/HasOffsetType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
352352
return new BooleanType();
353353
}
354354

355+
public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type
356+
{
357+
return $this->getKeysArray();
358+
}
359+
355360
public function getKeysArray(): Type
356361
{
357362
return new NonEmptyArrayType();

src/Type/Accessory/HasOffsetValueType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ public function unsetOffset(Type $offsetType): Type
195195
return $this;
196196
}
197197

198+
public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type
199+
{
200+
return $this->getKeysArray();
201+
}
202+
198203
public function getKeysArray(): Type
199204
{
200205
return new NonEmptyArrayType();

src/Type/Accessory/NonEmptyArrayType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ public function unsetOffset(Type $offsetType): Type
159159
return new ErrorType();
160160
}
161161

162+
public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type
163+
{
164+
return $this->getKeysArray();
165+
}
166+
162167
public function getKeysArray(): Type
163168
{
164169
return $this;

src/Type/Accessory/OversizedArrayType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ public function unsetOffset(Type $offsetType): Type
154154
return new ErrorType();
155155
}
156156

157+
public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type
158+
{
159+
return $this->getKeysArray();
160+
}
161+
157162
public function getKeysArray(): Type
158163
{
159164
return $this;

src/Type/ArrayType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ public function generalizeValues(): self
170170
return new self($this->keyType, $this->itemType->generalize(GeneralizePrecision::lessSpecific()));
171171
}
172172

173+
public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type
174+
{
175+
return $this->getKeysArray();
176+
}
177+
173178
public function getKeysArray(): Type
174179
{
175180
return TypeCombinator::intersect(new self(new IntegerType(), $this->getIterableKeyType()), new AccessoryArrayListType());

src/Type/Constant/ConstantArrayType.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use PHPStan\Type\Generic\TemplateTypeMap;
3636
use PHPStan\Type\Generic\TemplateTypeVariance;
3737
use PHPStan\Type\IntegerRangeType;
38+
use PHPStan\Type\IntegerType;
3839
use PHPStan\Type\IntersectionType;
3940
use PHPStan\Type\IsSuperTypeOfResult;
4041
use PHPStan\Type\MixedType;
@@ -1372,6 +1373,19 @@ private function degradeToGeneralArray(): Type
13721373
return $builder->getArray();
13731374
}
13741375

1376+
public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type
1377+
{
1378+
$keysArray = $this->getKeysOrValuesArray($this->keyTypes);
1379+
1380+
return TypeCombinator::intersect(
1381+
new ArrayType(
1382+
new IntegerType(),
1383+
$keysArray->getIterableValueType(),
1384+
),
1385+
new AccessoryArrayListType(),
1386+
);
1387+
}
1388+
13751389
public function getKeysArray(): self
13761390
{
13771391
return $this->getKeysOrValuesArray($this->keyTypes);

src/Type/IntersectionType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,11 @@ public function unsetOffset(Type $offsetType): Type
842842
return $this->intersectTypes(static fn (Type $type): Type => $type->unsetOffset($offsetType));
843843
}
844844

845+
public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type
846+
{
847+
return $this->intersectTypes(static fn (Type $type): Type => $type->getKeysArrayFiltered($filterValueType, $strict));
848+
}
849+
845850
public function getKeysArray(): Type
846851
{
847852
return $this->intersectTypes(static fn (Type $type): Type => $type->getKeysArray());

src/Type/MixedType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ public function unsetOffset(Type $offsetType): Type
180180
return $this;
181181
}
182182

183+
public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type
184+
{
185+
return $this->getKeysArray();
186+
}
187+
183188
public function getKeysArray(): Type
184189
{
185190
if ($this->isArray()->no()) {

src/Type/NeverType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,11 @@ public function unsetOffset(Type $offsetType): Type
273273
return new NeverType();
274274
}
275275

276+
public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type
277+
{
278+
return $this->getKeysArray();
279+
}
280+
276281
public function getKeysArray(): Type
277282
{
278283
return new NeverType();

0 commit comments

Comments
 (0)