Do not re-wrap NeverType as TemplateMixedType in TemplateUnionType::filterTypes()#5500
Merged
ondrejmirtes merged 1 commit intophpstan:2.1.xfrom Apr 21, 2026
Conversation
…pe::filterTypes()` - When TemplateUnionType::filterTypes() filters out all inner types, the parent returns NeverType. The override was re-wrapping it via TemplateTypeFactory::create(), which has no NeverType branch and falls through to TemplateMixedType. - This caused MixedType::hasMethod() to return Yes for __toString, leading to a false "Possibly impure call to method stdClass::__toString()" on pure functions casting template types like T of int|string to string. - Applied the same fix to TemplateBenevolentUnionType::filterTypes(). - The fix covers all callers of filterTypes: filterTypeWithMethod (string cast, concatenation, interpolation, echo, print), filterTypeWithProperty, filterTypeWithConstant, and filterTypeWhenIterable.
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 pure function casts a template type
T of int|stringto string, PHPStan incorrectly reports "Possibly impure call to method stdClass::__toString()". This happens becauseTemplateUnionType::filterTypes()wraps aNeverTyperesult (no matching types) back intoTemplateMixedTypeviaTemplateTypeFactory::create(), andMixedTypereports that it has every method, including__toString.Changes
src/Type/Generic/TemplateUnionType.php: ReturnNeverTypedirectly fromfilterTypes()instead of re-wrapping it throughTemplateTypeFactory::create()(which falls through toTemplateMixedType)src/Type/Generic/TemplateBenevolentUnionType.php: Same fix applied to the benevolent union variantRoot cause
TemplateUnionType::filterTypes()callsparent::filterTypes()which returnsNeverTypewhen all union members are filtered out. The override then passes thisNeverTypetoTemplateTypeFactory::create(), which has no branch forNeverTypeand falls through to the catch-all:new TemplateMixedType(...). SinceMixedType::hasMethod()returnsYesfor any method name,filterTypeWithMethod()finds a__toStringmethod on the resulting type and creates aDummyMethodReflectionwithstdClassas its declaring class — producing the false positive purity error.The fix is at the
filterTypes()level, so it covers all callers:filterTypeWithMethod(string cast, concatenation, interpolation, echo, print,.=)filterTypeWithProperty,filterTypeWithConstant,filterTypeWhenIterableAnalogous cases probed:
TemplateBenevolentUnionType— same bug pattern, fixed with same approachImplicitToStringCallHelperpath)T of int|float|bool) — confirmed fixedTest
tests/PHPStan/Rules/Pure/data/bug-14504.php— tests(string)cast, string concatenation, string interpolation, and non-string scalar union bounds (int|float|bool) in pure functions with template typestests/PHPStan/Rules/Pure/data/bug-14504-method.php— tests the same pattern in a pure method contextFixes phpstan/phpstan#14504