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

[Fix] Static analysis of DataCollection::map(...) and DataCollection::through(...) methods #640

Merged

Conversation

joelbutcher
Copy link
Contributor

The problem

The static analysis type hinting for DataCollection::map(...) and DataCollection::through(...) expect the return type of the passed closures to match the same type of each item within the collection. Typically the map method of arrays, or Laravel collections, is used to transform a collection of items into something different (i.e. a different collection of items, who's types may not match the initial collections types).

Currently, this expectation can lead to static analysis errors such as:

Parameter #1 $map of method Spatie\LaravelData\DataCollection<int, App\Data\Foo>::map() expects callable(App\Data\Foo, int): App\Data\Foo, Closure(App\Data\Foo, int): App\Data\Bar given

The fix

The fix here is is to add another template to both these methods TMapValue to use as the return type, which may or may not be the same type as items in the collection

@rubenvanassche
Copy link
Member

Thanks!

@rubenvanassche rubenvanassche merged commit 8550a10 into spatie:main Feb 9, 2024
7 of 8 checks passed
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.

2 participants