Skip to content

[TypeDeclaration] Skip string param inference in ScalarParamTypeByMethodCallTypeRector to avoid unsafe scalar coercion - #8514

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-scalar-param-string-coercion
Sep 24, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
fix-scalar-param-string-coercion

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

ScalarParamTypeByMethodCallTypeRector inferred a param type by copying the declared param type of the called method (CallerParamMatcher::matchCallParamType returns the callee param type node verbatim). For a string callee param this is unsafe: PHP scalar coercion lets a caller legitimately pass int/float/bool.

Real case: a method passes $value to a vendor set(string $key, string $value). The rule added string $value, but the caller actually passes an int and relied on coercion. Under declare(strict_types=1) this then breaks callers with a TypeError.

There is no local signal to tell the safe case from the unsafe one - both are an untyped variable passed to a string param - so the rule now bails out when the inferred type is a plain string. Non-string scalars and unions (int, bool, float, int|bool, bool|string|null) still get inferred.

  • guard isMatchingParamType() against pure string
  • repoint transform fixtures to an int callee, swap the code sample to int
  • add skip_string_caller_param fixture covering the coercion case

…hodCallTypeRector to avoid unsafe scalar coercion
@TomasVotruba
TomasVotruba marked this pull request as ready for review September 24, 2026 08:50
@TomasVotruba
TomasVotruba merged commit f839588 into main Sep 24, 2026
45 checks passed
@TomasVotruba
TomasVotruba deleted the fix-scalar-param-string-coercion branch September 24, 2026 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant