Skip to content

Commit 8b1110b

Browse files
herndlmondrejmirtes
authored andcommitted
Consistently return ErrorType for invalid keys
1 parent 4b61506 commit 8b1110b

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

src/Type/ArrayType.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,14 @@ public function unsetOffset(Type $offsetType): Type
369369

370370
public function fillKeysArray(Type $valueType): Type
371371
{
372-
$itemType = $this->getIterableValueType();
372+
$itemType = $this->getItemType();
373+
if ((new IntegerType())->isSuperTypeOf($itemType)->no()) {
374+
$stringKeyType = $itemType->toString();
375+
if ($stringKeyType instanceof ErrorType) {
376+
return $stringKeyType;
377+
}
373378

374-
if ((new IntegerType())->isSuperTypeOf($itemType)->no() && !$itemType->toString() instanceof ErrorType) {
375-
return new ArrayType($itemType->toString(), $valueType);
379+
return new ArrayType($stringKeyType, $valueType);
376380
}
377381

378382
return new ArrayType($itemType, $valueType);

src/Type/Constant/ConstantArrayType.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -677,11 +677,12 @@ public function fillKeysArray(Type $valueType): Type
677677

678678
foreach ($this->valueTypes as $i => $keyType) {
679679
if ((new IntegerType())->isSuperTypeOf($keyType)->no()) {
680-
if ($keyType->toString() instanceof ErrorType) {
681-
return new NeverType();
680+
$stringKeyType = $keyType->toString();
681+
if ($stringKeyType instanceof ErrorType) {
682+
return $stringKeyType;
682683
}
683684

684-
$builder->setOffsetValueType($keyType->toString(), $valueType, $this->isOptionalKey($i));
685+
$builder->setOffsetValueType($stringKeyType, $valueType, $this->isOptionalKey($i));
685686
} else {
686687
$builder->setOffsetValueType($keyType, $valueType, $this->isOptionalKey($i));
687688
}

tests/PHPStan/Analyser/data/array-fill-keys.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function withObjectKey() : array
4848
{
4949
assertType("array{foo: 'b'}", array_fill_keys([new Foo()], 'b'));
5050
assertType("non-empty-array<string, 'b'>", array_fill_keys([new Bar()], 'b'));
51-
assertType("*NEVER*", array_fill_keys([new Baz()], 'b'));
51+
assertType("*ERROR*", array_fill_keys([new Baz()], 'b'));
5252
}
5353

5454
function withUnionKeys(): void
@@ -94,7 +94,7 @@ function withNotConstantArray(array $foo, array $bar, array $baz, array $floats,
9494
assertType("array<numeric-string, null>", array_fill_keys($floats, null));
9595
assertType("array<bool|int|string, null>", array_fill_keys($mixed, null));
9696
assertType('array<string, null>', array_fill_keys($list, null));
97-
assertType('array<ArrayFillKeys\Baz, null>', array_fill_keys($objectsWithoutToString, null)); // should be *NEVER* or *ERROR* according to https://3v4l.org/gGpic?
97+
assertType('*ERROR*', array_fill_keys($objectsWithoutToString, null));
9898

9999
if (array_key_exists(17, $mixed)) {
100100
assertType('non-empty-array<bool|int|string, null>', array_fill_keys($mixed, null));

0 commit comments

Comments
 (0)