Skip to content

Keep single-key shapes with disjoint values apart in a union - #7207

Merged
ondrejmirtes merged 1 commit into
2.3.xfrom
keep-disjoint-single-key-shapes-apart
Nov 27, 2026
Merged

ondrejmirtes merged 1 commit into
2.3.xfrom
keep-disjoint-single-key-shapes-apart

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Since 0e9fdb5, array{'title'}|array{'body'} is merged into array{'body'|'title'}. That describes the same arrays, but functions and extensions that turn values into keys lose which value belonged to which shape: array_fill_keys($keys, 1) became non-empty-array{body?: 1, title?: 1}, and phpstan-laravel's FormRequest::safe() and phpstan-dba's pdo-union-result.php changed in the integration tests.

ArrayAlgebra::tryUnionConstantArray() now keeps two single-key shapes apart when their values are disjoint and only a union holds both, the rule from before 0e9fdb5. Overlapping values and values that merge into one type (int<0, 5>|int<6, 10>) still merge; shapes with more keys are unchanged. The updated test expectations go back to their values before 0e9fdb5.

Known trade-off: bug-11858 gets its less precise type back, because {A} now merges with {A, B} into array{0: 'A', 1?: 'B'} and appending to a list with optional keys is not exact.

Closes phpstan/phpstan#15864

Amp thread: https://ampcode.com/threads/T-01a21c4b-3106-74b8-add3-927f10530c87

0e9fdb5 merged shapes differing in the value at one key, so
array{'title'}|array{'body'} became array{'body'|'title'}. That holds the
same arrays, but functions and extensions that turn values into keys can
no longer tell which value went with which shape: array_fill_keys() and
array_flip() returned non-empty-array{body?: 1, title?: 1}.

Two single-key shapes now stay a tagged union when their values are
disjoint and only a union holds both, as before 0e9fdb5. Values that
overlap or merge into one type still merge, and shapes with more keys
are unchanged.

bug-11858 gets its less precise type back: {A} now merges with {A, B}
into array{0: 'A', 1?: 'B'} first, and appending to a list with
optional keys is not exact.

Amp-Thread-ID: https://ampcode.com/threads/T-01a21c4b-3106-74b8-add3-927f10530c87
@ondrejmirtes
ondrejmirtes force-pushed the keep-disjoint-single-key-shapes-apart branch from a817b44 to 2b1abef Compare November 27, 2026 09:47
@ondrejmirtes
ondrejmirtes merged commit 7fa98fe into 2.3.x Nov 27, 2026
897 of 917 checks passed
@ondrejmirtes
ondrejmirtes deleted the keep-disjoint-single-key-shapes-apart branch November 27, 2026 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Merging array{'a'}|array{'b'} into array{'a'|'b'} loses precision in array_fill_keys(), array_flip() and extensions (since phpstan-src 0e9fdb53e, #7195)

1 participant