Repository navigation
Check that array values passed to strtr(), substr_replace(), preg_grep() and similar functions are castable to string - #7150
Conversation
VincentLanglet
left a comment
There was a problem hiding this comment.
How ParameterCastableToStringRule is different than using __stringAndStringable (and similar) in functionMap ?
|
The reviewer asked a question rather than requesting a change, so I made no code changes and no commits. Below is the answer, based on a side-by-side run. The working tree is clean. How I compared them: I temporarily changed Where they behave the same. On direct calls, both report the same calls at the same levels, because both go through
Both use the identifier Where they differ:
For the functions this PR adds (
I can move these to the function map if the reviewer prefers that. A better option might be to first make |
we likely cannot update the type printing because lets go with this rule |
staabm
left a comment
There was a problem hiding this comment.
will this new errors only be emitted in bleeding edge?
|
The new errors now appear only with bleeding edge. Before this change they would have shown up in every stable release at level 5. That's committed as 7203cd7 and not pushed.
|
…g_grep()` and similar functions are castable to string - ParameterCastableToStringRule: replace the "check first arg" list with a function => parameter indices map and add strtr (array form only, $replace_pairs), substr_replace ($string, $replace), preg_grep ($array), preg_replace_callback_array ($subject), setlocale (array $locales), proc_open ($command, $env_vars) and pcntl_exec ($args, $env_vars) - Check every array argument except the trailing callable of array_diff_uassoc() and array_intersect_uassoc() - Only array arguments are checked, so the string forms (e.g. the three-argument strtr()) are unaffected - array_multisort() with SORT_STRING was left out, it depends on flags
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7203cd7 to
0b4d5e0
Compare
Summary
PHP converts each value of the
$replace_pairsarray ofstrtr()to a string. Objects that are notStringablethrow anError, and arrays produce an "Array to string conversion" warning. PHPStan didn't report either case. This PR extendsParameterCastableToStringRule, the rule that already coversarray_diff(),natsort(),array_fill_keys()and others, tostrtr()and to the sibling functions listed in the issue comments.Changes
src/Rules/Functions/ParameterCastableToStringRule.php:$checkFirstArgFunctionslist is now a function → parameter-indices map.strtr→$replace_pairs, only for the two-argument formsubstr_replace→$string,$replacepreg_grep→$arraypreg_replace_callback_array→$subjectsetlocale→ the array$localesformproc_open→ array$command,$env_varspcntl_exec→$args,$env_varsarray_diff_uassoc()/array_intersect_uassoc(): every argument except the trailing comparison callable is checked.strtr($s, 'a', 'b'),setlocale(LC_ALL, 'C')) are unaffected.array_multisort()withSORT_STRINGdepends on the flags, and fits better inSortParameterCastableToStringRule.Root cause
The function map declares these parameters as plain
array(orstring|array), and no castable-to-string rule knew about them, so the values were never checked.Test
tests/PHPStan/Rules/Functions/data/bug-15800.php(inParameterCastableToStringRuleTest::testBug15800) contains the reproducer from the issue (stdClassandarray<int>values forstrtr()). It also has positive and negative cases for each newly covered function, including scalar/Stringablevalues and the three-argumentstrtr()form, which must not be reported. The test fails without the change.Fixes phpstan/phpstan#15800