Repository navigation
Do not parenthesize a benevolent union whose members describe the same - #6961
Merged
Merged
Conversation
ondrejmirtes
added a commit
to phpstan/phpstan
that referenced
this pull request
Nov 5, 2026
The "(string) given" description is fixed in phpstan/phpstan-src#6961. Amp-Thread-ID: https://ampcode.com/threads/T-01a19770-5331-74b2-963f-e324ae0f7635
ondrejmirtes
force-pushed
the
benevolent-union-single-description
branch
from
November 5, 2026 21:46
3d11be9 to
c8ed529
Compare
The members of a benevolent union can describe the same at a verbosity level, like decimal-int-string and non-empty-string&uppercase-string as string at the type-only level. The union then described as "(string)", for example in "Parameter #1 $num of function ceil expects float|int, (string) given." for a string cast of a benevolent (float|int). UnionType::describe() now parenthesizes a benevolent union only when more than one distinct name is joined, so a description like array<int|string> of members that describe the same stays unwrapped too. Amp-Thread-ID: https://ampcode.com/threads/T-01a19770-5331-74b2-963f-e324ae0f7635
ondrejmirtes
force-pushed
the
benevolent-union-single-description
branch
from
November 5, 2026 22:02
c8ed529 to
0d22e77
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since 5a3c562 ("Compute the numeric operators on NumberSet"), arithmetic results stay benevolent when an operand is benevolent. That change is intended. Because of it,
(string)of a benevolent(float|int)is now the benevolent union(decimal-int-string|(non-empty-string&uppercase-string)).At the type-only verbosity level both members describe as
string, butBenevolentUnionType::describe()still wrapped the result in parentheses. That produced messages like:This showed up in the Symfony integration baseline in phpstan/phpstan#15668.
The parentheses are now added in
UnionType::describe()'s join step, for a benevolent union, only when more than one distinct name is joined.BenevolentUnionType::describe()is removed. Checking the joined count rather than looking for|in the string keeps members that collapse to a description likearray<int|string>unwrapped as well. The turbo extension'sUnionType/BenevolentUnionTypeget the same change.Tests:
UnionTypeTest::testDescribecovers a real benevolent union ((int|string)at every level), members that only differ at the precise level (Foo), and members that collapse toFoo<int|string>. The last case fails with a string-based check.CallToFunctionParametersRuleTest::testBenevolentUnionDescriptionexpectsstring givenfor the cast and(float|int) givenfor the uncast value.turbo-ext/tests/type-family.phphasbenevolentStringsandbenevolentSameArrayssubjects, so the PHP and native descriptions are compared.