Fix phpstan/phpstan#10076: Union of string literal and class-string<T> does not accept that string#5346
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Conversation
…> loses literal members - GenericClassStringType::isSuperTypeOf() returned Yes for non-class-string constants when the generic type was MixedType (including TemplateMixedType) - This caused TypeCombinator::union to absorb string literals like 'object' and 'array' into class-string<T>, losing them from the union - Added a check: when generic type is MixedType but the constant string is not a class-string, return Maybe instead of Yes - New regression test in tests/PHPStan/Rules/Functions/data/bug-10076.php
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.
Summary
When a parameter has type
'object'|'array'|class-string<T>, passing'object'or'array'should be accepted because they match the string literal parts of the union. Instead, PHPStan reported false positive errors like "expects class-string, string given" because the literal string members were silently absorbed intoclass-string<T>during type construction.Changes
GenericClassStringType::isSuperTypeOf()insrc/Type/Generic/GenericClassStringType.phpto returnMaybeinstead ofYeswhen the generic type isMixedType(which includesTemplateMixedType) and the constant string is not a valid class-stringtests/PHPStan/Rules/Functions/data/bug-10076.phpand test method intests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.phpRoot cause
GenericClassStringType::isSuperTypeOf()had a fast path: when the generic type wasinstanceof MixedType, it unconditionally returnedYesfor anyConstantStringType. Template types with no explicit bound areTemplateMixedType(which extendsMixedType), soclass-string<T>claimed to be a supertype of strings like'object'and'array'. This causedTypeCombinator::unionto absorb the constant string members, collapsing'object'|'array'|class-string<T>into justclass-string<T>. After template resolution, the parameter type becameclass-string<object>instead of'object'|'array'|class-string<object>, producing false positive argument type errors.The fix adds a guard: non-class-string constants now get
Maybeinstead ofYes, preserving them as separate union members.Test
Added
tests/PHPStan/Rules/Functions/data/bug-10076.phpwhich reproduces the exact scenario from the issue: a function with@param 'object'|'array'|class-string<T> $typecalled with'object','array', andDateTime::class. No errors should be reported.Fixes phpstan/phpstan#10076