-
Notifications
You must be signed in to change notification settings - Fork 11.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[9.x] Fix Stringable typehints with Enumerable #44030
Conversation
* @param string|string[]|Enumerable<array-key, string> $subject | ||
* @param string|array<string>|Enumerable<array-key, string> $search | ||
* @param string|array<string>|Enumerable<array-key, string> $replace | ||
* @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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
Str
andStringable
helpers 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