Add array assertions about unsealed arrays#5722
Conversation
|
You've opened the pull request against the latest branch 2.2.x. PHPStan 2.2 is not going to be released for months. If your code is relevant on 2.1.x and you want it to be released sooner, please rebase your pull request and change its target to 2.1.x. |
| 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')); |
There was a problem hiding this comment.
This is correct.
But you might want to improve the array because ...<0|1, 'b'> is kinda complicated for nothing:
- Since the
1key 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'}
| 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)); |
There was a problem hiding this comment.
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.
|
This pull request has been marked as ready for review. |
I'd like you point of view of the behavior expected here