Skip to content
Closed
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 @@ -1523,6 +1523,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'));
}
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));
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