Repository navigation
Treat a template type of mixed operand like mixed in arithmetic so the result stays a benevolent (float|int) - #6806
Open
phpstan-bot wants to merge 1 commit into
Conversation
…o the result stays a benevolent `(float|int)` - InitializerExprTypeResolver::resolveCommonMath() decided whether an operand is mixed via `TypeCombinator::union($left, $right) instanceof MixedType`. A TemplateMixedType does not absorb the other operand in a union (`T|int`), so `T + int` produced a strict `float|int` while `mixed + int` and `T + U` produced a benevolent `(float|int)`. The check now also looks at each operand directly. Affects +, -, *, /, <<, >> and their compound assignments / ++ / --. - In 2.3 the array_reduce() callback is typed from the callable(TReturn, TIn) parameter, so `$carry` is TReturn while gathering the closure type; `$carry + map($val)` then resolved TReturn to strict float|int, which the `int` return type rejected. - Mirrored the change in turbo-ext/src/InitializerExprTypeResolver.cpp. - Probed unary -/+ (strict float|int for both mixed and T, consistent), ** and % (already identical for mixed and T), and the `T + U` / array `+` paths (already go through `instanceof MixedType` on each operand). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ondrejmirtes
force-pushed
the
create-pull-request/patch-8b4dxl5
branch
from
October 27, 2026 18:45
dadb7da to
b13462d
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.
Summary
Since 2.3.0,
array_reduce($vals, function ($carry, $val) { return $carry + map($val); }, 0)is inferred as strictfloat|int, so returning it from a function declared: intreportsFunction foo() should return int but returns float|int.Before, the result was a benevolent(float|int), and$carry + $valstill gives one.The cause is an inconsistency in arithmetic on a template type of
mixed:T + intgave strictfloat|int, whilemixed + intandT + Ugave benevolent(float|int). This PR makes a template-of-mixed operand behave likemixed.Changes
src/Reflection/InitializerExprTypeResolver.php(resolveCommonMath()): the "operands are mixed" flag now holds when either operand is aMixedType, not only whenTypeCombinator::union($left, $right)collapses toMixedType. The flag controls the integer-range shortcut, the benevolent result for/, and the benevolent result for the other operators.turbo-ext/src/InitializerExprTypeResolver.cpp: the same change in the native mirror.Operators affected:
+,-,*,/,<<,>>, plus their compound assignments and++/--.Checked and already consistent between
mixedandT:**,%, unary-/+(strictfloat|intfor both),T + Uand the array+paths (these already testinstanceof MixedTypeon each operand).Root cause
TypeCombinator::union(T, int)keepsT|intbecause a template type does not absorb other types, so$types instanceof MixedTypewas false forT + int. The result therefore came out as a strict union.In 2.3 the closure passed to
array_reduce()is typed from thecallable(TReturn, TIn): TReturnparameter. While the closure type is gathered for template inference,$carryis the templateTReturn(ofmixed).$carry + map($val)became a strictfloat|int, soTReturnresolved to strictfloat|int, and that strict union reached the return type. In the$carry + $valvariant both operands were templates, which already hit theMixedType && MixedTypebranch, so the result stayed benevolent.Test
tests/PHPStan/Analyser/nsrt/bug-15388.php:array_reduce()with+,-and*callbacks, and arithmetic on a template ofmixed(+,-,*,/,**,<<,%,+=,++) compared withmixed.tests/PHPStan/Rules/Functions/data/bug-15388.php+ReturnTypeRuleTest::testBug15388(): the issue's reproducer, which should report no errors.Both tests fail without the fix.
Fixes phpstan/phpstan#15388
🤖 Generated with Claude Code