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
2 changes: 2 additions & 0 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,8 @@ public function flipArray(): Type
if ($this->isUnsealed()->yes() && $this->unsealed !== null) {
[$unsealedKey, $unsealedValue] = $this->unsealed;
$builder->makeUnsealed($unsealedValue->toArrayKey(), $unsealedKey);

$builder->setOffsetValueType($unsealedValue->toArrayKey(), $unsealedKey, true);
}

return $builder->getArray();
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Analyser/nsrt/array-fill-keys.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,13 @@ function withNotConstantArray(array $foo, array $bar, array $baz, array $floats,
assertType('non-empty-array<bool|int|string, null>', array_fill_keys($mixed, null));
}
}

/**
* @param array{0: 1, 1: 2, 2: 3} $sealed
* @param array{0: 1, 1: 2, 2: 3, ...<int, bool>} $unsealed
*/
function sealedArrayFillKeys(array $sealed, array $unsealed): void
{
assertType("array{1: 'b', 2: 'b', 3: 'b'}", array_fill_keys($sealed, 'b'));
assertType("array{1: 'b', 2: 'b', 3: 'b', ...<0|1, 'b'>}", array_fill_keys($unsealed, 'b'));
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.

This is correct.

But you might want to improve the array because ...<0|1, 'b'> is kinda complicated for nothing:

  • Since the 1 key already exists, we might want to simplify to
array{1: 'b', 2: 'b', 3: 'b', ...<0, 'b'>}

maybe ?

  • Then it could be just
array{1: 'b', 2: 'b', 3: 'b', 0?: 'b'}

}
11 changes: 11 additions & 0 deletions tests/PHPStan/Analyser/nsrt/array-flip.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,14 @@ function foo10(array $array)
assertType("*NEVER*", array_flip($array)); // this could be array<string, int>&hasOffsetValue(17, 'bar') according to https://3v4l.org/1TAFk
}
}

/**
* @param array{0: 1, 1: 2, 2: 3} $sealed
* @param array{0: 1, 1: 2, 2: 3, ...<int, bool>} $unsealed
*/
function sealedArrayFlip(array $sealed, array $unsealed): void
{
assertType('array{1: 0, 2: 1, 3: 2}', array_flip($sealed));
assertType('array{1: int, 2: 1, 3: 2, 0?: int, ...<0|1, int>}', array_flip($unsealed));
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.

I'm not sure what's expected here, currently the type is

array{1: 0, 2: 1, 3: 2, ...<0|1, int>}

but this gives array_flip($unsealed)[1] to be considered as 0 instead of any integer.

This can be fixed in the ConstantArrayType::flipArray with an extra

$builder->setOffsetValueType($unsealedValue->toArrayKey(), $unsealedKey, true);

but in the same way I dunno if you like the result with the useless ...<0|1, int> since 0 and 1 are already declared in the array.

assertType('int', array_flip($unsealed)[1]);
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/unsealed-derivations.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public function flipPreservesUnsealed(array $arr): void
// `array_flip` swaps keys and values pair-by-pair, so the
// unsealed `<int, string>` becomes `<string, int>` — the value
// type passes through `toArrayKey()` to land in the new key slot.
assertType("array{foo: 'a', bar: 'b', ...<string, int>}", array_flip($arr));
assertType("array{foo: 'a'|int, bar: 'b'|int, ...<string, int>}", array_flip($arr));
}

/**
Expand Down
Loading