Skip to content

Add default value to WP_REST_Request generic template to avoid missingType.generics errors #408

@Nikschavan

Description

@Nikschavan

Problem

Since #66, WP_REST_Request has a generic template:

@phpstan-template T of array

This works great when users explicitly specify the type:

@phpstan-param WP_REST_Request<array{post?: int, orderby?: string}> $request

However, at PHPStan level 9, any method with a WP_REST_Request parameter that doesn't specify the generic type triggers:

Method MyController::handle_request() has parameter $request with
generic class WP_REST_Request but does not specify its types: T

Reproduction: https://phpstan.org/r/fc6325e9-8b23-43b7-b42d-7ec37c8a2187

Impact

This affects any WordPress plugin/theme using PHPStan level 9 with REST controllers. Common workarounds:

  • Adding @phpstan-ignore-next-line to every method (verbose)
  • Adding a global ignore rule in phpstan.neon
  • Adding to baseline (not ideal for new code)

Proposed Solution

Add a default value to the template:

@phpstan-template T of array = array<string, mixed>

Playground with the fix: https://phpstan.org/r/80a8af41-1a34-400f-a3ad-e0bb00cc80d4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions