Skip to content

Keep subclasses of string, int and float apart in unions - #7215

Merged
ondrejmirtes merged 1 commit into
2.3.xfrom
scalar-subclass-unions
Nov 27, 2026
Merged

ondrejmirtes merged 1 commit into
2.3.xfrom
scalar-subclass-unions

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Since 47ef9f6, TypeCombinator merges strings by getStringRefinement() and numbers by toExactNumberSet(). Subclasses inherited those from StringType, IntegerType and FloatType, so two unrelated subclasses defined by an extension looked identical and the first one won: model property of User|model property of Account became model property of User, depending on operand order (Larastan, phpstan-laravel and librenms integration tests).

The three base methods now describe only the exact class; any other subclass returns null and is unioned through isSuperTypeOf(). ConstantIntegerType, ConstantFloatType and FloatRangeType override toExactNumberSet() to keep their exact numbers. The native mirrors in turbo-ext do the same.

New TypeCombinatorTest cases fail on 2.3.x and pass here. The full suite passes with and without the extension.

Closes phpstan/phpstan#15859

Amp thread: https://ampcode.com/threads/T-01a21c4a-e25b-74f0-9147-bfb3ae88286a

StringType::getStringRefinement() and IntegerType/FloatType::toExactNumberSet()
now describe only the exact class. A subclass defined by an extension, as
Larastan's model property of X, may hold fewer values than its parent, so
ScalarUnion merged two of them by their cells or numbers and kept only the
first: model property of User|model property of Account became model property
of User. They now get null and are unioned by isSuperTypeOf().
ConstantIntegerType, ConstantFloatType and FloatRangeType, whose numbers are
exact, override toExactNumberSet() to keep theirs.

Amp-Thread-ID: https://ampcode.com/threads/T-01a21c4a-e25b-74f0-9147-bfb3ae88286a
@ondrejmirtes
ondrejmirtes merged commit b50f182 into 2.3.x Nov 27, 2026
394 of 398 checks passed
@ondrejmirtes
ondrejmirtes deleted the scalar-subclass-unions branch November 27, 2026 11:54
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.

Union of two StringType subclasses keeps only one of them, order-dependent (since phpstan-src 47ef9f6dc, #7195)

1 participant