Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,12 @@ public function unsetOffset(Type $offsetType): Type
public function fillKeysArray(Type $valueType): Type
{
$itemType = $this->getItemType();
$arrayKeyType = $itemType->toArrayKey();

if ($arrayKeyType->isInteger()->yes()) {
return new ArrayType($arrayKeyType, $valueType);
}

if ($itemType->isInteger()->no()) {
$stringKeyType = $itemType->toString();
if ($stringKeyType instanceof ErrorType) {
Expand Down
8 changes: 6 additions & 2 deletions tests/PHPStan/Analyser/nsrt/array-fill-keys.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,20 @@ function withOptionalKeys(): void
* @param array<int, int|string|bool> $mixed
* @param list<string> $list
* @param Baz[] $objectsWithoutToString
* @param array<int|decimal-int-string> $decimalIntStringOrInt
* @param array<decimal-int-string> $decimalIntString
*/
function withNotConstantArray(array $foo, array $bar, array $baz, array $floats, array $mixed, array $list, array $objectsWithoutToString): void
function withNotConstantArray(array $foo, array $bar, array $baz, array $floats, array $mixed, array $list, array $objectsWithoutToString, array $decimalIntStringOrInt, array $decimalIntString): void
{
assertType("array<string, null>", array_fill_keys($foo, null));
assertType("array<int, null>", array_fill_keys($bar, null));
assertType("array<'foo', null>", array_fill_keys($baz, null));
assertType("array<numeric-string&uppercase-string, null>", array_fill_keys($floats, null));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix is in line with actual PHP behaviour, but i'm not sure if this result is intentional for BC break purposes.

assertType("array<int, null>", array_fill_keys($floats, null));
assertType("array<bool|int|string, null>", array_fill_keys($mixed, null));
assertType('array<string, null>', array_fill_keys($list, null));
assertType('*ERROR*', array_fill_keys($objectsWithoutToString, null));
assertType('array<int, null>', array_fill_keys($decimalIntStringOrInt, null));
assertType('array<int, null>', array_fill_keys($decimalIntString, null));

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