Commit 8b2bf86
Add isObject() guard in ImplicitToStringCallHelper for constant scalar intersections
The TypeCombinator narrowing excludes constant scalar values (0, '', '0',
false, 0.0) because TemplateTypeFactory can't create proper template types
for all of them. This means types like `false&T` and `0.0&T` remain as
intersections where MixedType::hasMethod('__toString') returns Yes,
causing false positive purity errors.
Add an early return in ImplicitToStringCallHelper when isObject()->no()
to handle these remaining cases. Since __toString() can only be called on
objects, non-object types always use PHP's built-in string conversion.
Add test cases using empty() + !\is_array() which narrows to constant
scalar falsy values.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>1 parent 13c22db commit 8b2bf86
3 files changed
Lines changed: 34 additions & 0 deletions
File tree
- src/Analyser/ExprHandler/Helper
- tests/PHPStan/Rules/Pure/data
Lines changed: 9 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
31 | 40 | | |
32 | 41 | | |
33 | 42 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
34 | 47 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
0 commit comments