Skip to content

fix: drop invalid generic params from JsonApiResourceCollection return types#83

Open
alies-dev wants to merge 1 commit into
timacdonald:mainfrom
alies-dev:fix/jsonapiresourcecollection-template-params
Open

fix: drop invalid generic params from JsonApiResourceCollection return types#83
alies-dev wants to merge 1 commit into
timacdonald:mainfrom
alies-dev:fix/jsonapiresourcecollection-template-params

Conversation

@alies-dev

Copy link
Copy Markdown

Problem

JsonApiResourceCollection extends Illuminate's AnonymousResourceCollection, which declares no @template params, so JsonApiResourceCollection is non-generic (0 template params). The @return JsonApiResourceCollection<int, mixed> annotations on collection() and newCollection() supply 2 type args to a class that accepts none.

PHPStan (with larastan's generic resource stubs) tolerates this, but Psalm via psalm-plugin-laravel reports against any consumer that calls ::collection():

TooManyTemplateParams: TiMacDonald\JsonApi\JsonApiResourceCollection<int, mixed>
has too many template params, expecting 0 (psalm.dev/184)

Still present in latest v1.0.0-beta.12 and on main HEAD.

Fix

Drop the generic args (src/JsonApiResource.php:154,167) so the annotation matches the class's actual arity. Laravel's AnonymousResourceCollection / ResourceCollection are non-generic, so the bare type is correct; this does not enable any new PHPStan generics check (config does not set checkGenericClassInNonGenericObjectType), so removing the args cannot introduce a PHPStan error.

…n types

JsonApiResourceCollection extends Illuminate's AnonymousResourceCollection,
which declares no @template params, so JsonApiResourceCollection is non-generic
(0 template params). The `@return JsonApiResourceCollection<int, mixed>` on
collection() and newCollection() therefore supplies 2 type args to a class that
accepts none.

PHPStan (with larastan's generic resource stubs) tolerated this, but Psalm via
psalm-plugin-laravel reports:

  TooManyTemplateParams: TiMacDonald\JsonApi\JsonApiResourceCollection<int, mixed>
  has too many template params, expecting 0

Drop the generic args so the annotation matches the class's actual arity.
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.

1 participant