Skip to content

Conversation

@glamorous
Copy link
Contributor

The two docblocks where not correct or not enough for typehinting and static analysis (phpstan).

This piece of code (which is working) had a false static analysis:

 /** * @return LengthAwarePaginator<int, PriceResource> */ protected function getPrices(Article $article): LengthAwarePaginator { return $article->prices() ->paginate(10) ->through(fn(Price $price) => PriceResource::make($price)); } 

It was complaining that it wanted a Price instead of PriceResource, but the paginator can change the items their types (it uses the transform in the background).

These fixes in de docblocks make it possible to write this code and pass the static analysis without to use ignore statements.

If some extra files needed an update or a change should be made, feel free to guide me in the right direction. It's up for discussion.

The issue #46487 already mentioned the problem in the past.

@taylorotwell taylorotwell merged commit d7df269 into laravel:12.x Jun 23, 2025
60 checks passed
@glamorous glamorous deleted the fix-collection-docs branch June 24, 2025 09:10
@glamorous
Copy link
Contributor Author

Thanks!

assertType('Illuminate\Support\Collection<int, User>', $collection->splice(1, 1, [new User]));

assertType('Illuminate\Support\Collection<int, User>', $collection->transform(function ($user, $int) {
assertType('mixed', $collection->transform(function ($user, $int) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should have been an indicator that something was wrong---phpstan should be perfectly capable of determining the correct generics

Comment on lines +1734 to +1735
* @param callable(TValue, TKey): TMapValue $callback
* @return $this<TKey, TMapValue>
Copy link
Contributor

@calebdw calebdw Jul 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both of these methods are incorrect:

Here is what they should be:

Suggested change
* @param callable(TValue, TKey): TMapValue $callback
* @return $this<TKey, TMapValue>
* @param callable(TValue, TKey): TMapValue $callback
* @phpstan-this-out static<TKey, TMapValue>
* @return $this
Copy link
Contributor Author

@glamorous glamorous Jul 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification @calebdw ! Didn't know about @phpstan-this-out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants