[9.x] Fix Stringable typehints with Enumerable - #44030
Conversation
| * @param string|array<string>|Enumerable<array-key, string> $subject | ||
| * @return string | ||
| */ | ||
| public static function replace($search, $replace, $subject) |
There was a problem hiding this comment.
We could also make this work with iterable, but then we need to call iterator_to_array, which might not be as efficient? Not sure if that's true though.
There was a problem hiding this comment.
Made it work with iterable now. Did some benchmarks and tried to keep the same performance by not over-naively passing everything to collect.
|
This makes sense. PHPStan documentation says:
|
|
Code style: In if (! is_iterable($needles)) {
$needles = (array) $needles;
}Whereas in if (! is_iterable($needles)) {
$needles = [$needles];
}Additionally, in if (! is_iterable($pattern)) {
$pattern = [$pattern];
}Suggestions:
|
|
Note that, provided In the case at hand, for converting non-iterable to array, I would definitely pick
|
Fixes a bug with static analysis using PHPStan that was introduced in #44012. I've taken the liberty of making the
StrandStringablehelpers work with iterators where possible, and broadened the types as well.Below is an example of the errors and how they are fixed now.
https://phpstan.org/r/a8f703b0-5d8c-470e-8b7f-2ce3adbcdf1a