Skip to content
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

Merged
merged 3 commits into from
Sep 7, 2022

Conversation

erikgaal
Copy link
Contributor

@erikgaal erikgaal commented Sep 6, 2022

Fixes a bug with static analysis using PHPStan that was introduced in #44012. I've taken the liberty of making the Str and Stringable 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

* @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)
Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

@nbayramberdiyev
Copy link

This makes sense. PHPStan documentation says:

If Collection|Foo[] means “Collection or array” in your case even if Collection implements Traversable, you need to disambiguate the type by using Collection|array<Foo> instead.

@taylorotwell taylorotwell merged commit 3665096 into laravel:9.x Sep 7, 2022
@erikgaal erikgaal deleted the patch-stringable-types branch September 8, 2022 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants