Skip to content

Baselined PHPStan errors need to be addressed #86088

Closed

Description

@westonruter

In #66693, PHPStan was integrated into the Gutenberg development tooling (issue #66598).

It is adopting rule level 5, which matches the current rule level in core (see Core-61175). These rules include:

  1. basic checks, unknown classes, unknown functions, unknown methods called on $this, wrong number of arguments passed to those methods and functions, always undefined variables
  2. possibly undefined variables, unknown magic methods and properties on classes with __call and __get
  3. unknown methods checked on all expressions (not just $this), validating PHPDocs
  4. return types, types assigned to properties
  5. basic dead code checking - always false instanceof and other type checks, dead else branches, unreachable code after return; etc.
  6. checking types of arguments passed to methods and functions

To land the PHPStan tooling with a passing GHA job, a PHPStan baseline was needed to ignore the existing errors. The baseline has an initial 226 errors in it.

Pull requests are now needed to fix those issues.

With each error fixed, run composer phpstan:baselines to regenerate the baseline (which is the same command used in core).

Each PR should reference this issue, so it will stay open until the final PR which removes the last error.

Note that some errors may warrant being ignored. See prior art in core's phpstan.neon.dist. Do not use @phpstan-ignore comments to suppress errors unless absolutely neccessary.

Activity

  1. added
    [Type] Bug
    [Type] Code Quality
    on Oct 31, 2026
  2. huzaifaalmesbah commented on Nov 1, 2026

    @huzaifaalmesbah
    Member

    @westonruter A question before I pick up the argument.type errors.

    24 of them come from passing a string to WP_HTML_Tag_Processor::next_tag(), e.g. next_tag( 'FIGURE' ). Core documents $query as array|string|null, and Core's own argument.type baseline has no next_tag entries, but the generated stubs here type it as array{tag_name?: string|null, ...}|null, so the string part seems to get lost when the hash notation is turned into an array shape.

    That makes these false positives rather than code issues. Would you prefer to:

    1. fix the stub generation so string is kept, or
    2. add a targeted ignore for this message in phpstan.neon.dist?

    I'm happy to work on either. In the meantime I'll keep going with the other argument.type errors.

  3. westonruter commented on Nov 1, 2026

    @westonruter
    MemberAuthor

    @huzaifaalmesbah:

    4. 24 of them come from passing a string to WP_HTML_Tag_Processor::next_tag(), e.g. next_tag( 'FIGURE' ). Core documents $query as array|string|null, and Core's own argument.type baseline has no next_tag entries, but the generated stubs here type it as array{tag_name?: string|null, ...}|null, so the string part seems to get lost when the hash notation is turned into an array shape.

    Yes, I see that. The stub gets generated as:

            /**
             * Finds the next tag matching the $query.
             *
             * @since 6.2.0
             * @since 6.5.0 No longer processes incomplete tokens at end of document; pauses the processor at start of token.
             *
             * @param array|string|null $query {
             *     Optional. Which tag name to find, having which class, etc. Default is to find any tag.
             *
             *     @type string|null $tag_name     Which tag to find, or `null` for "any tag."
             *     @type int|null    $match_offset Find the Nth tag matching all search criteria.
             *                                     1 for "first" tag, 3 for "third," etc.
             *                                     Defaults to first tag.
             *     @type string|null $class_name   Tag must contain this whole class name to match.
             *     @type string|null $tag_closers  "visit" or "skip": whether to stop on tag closers, e.g. </div>.
             * }
             * @return bool Whether a tag was matched.
             *
             * @phpstan-impure
             * @phpstan-param null|array{
             *   tag_name?: string|null,
             *   match_offset?: int|null,
             *   class_name?: string|null,
             *   tag_closers?: string|null,
             * } $query
             */
            public function next_tag($query = \null): bool
            {
            }

    The string is getting incorrectly dropped. I think this stems from the stub generator thinking that string here is a query string. This relates to WordPress/wordpress-develop#13433 which seeks to add typing for that function. But here the string is not a query string, but rather just a string which is a shortcut for writing array( 'tag_name' => 'foo' ). So it looks like the stub generator is overly matching that this is a query string. cc @szepeviktor

    So the stub generator should be fixed, but for this one we can also narrow the types in WP_HTML_Tag_Processor with explicit @phpstan-param tags.

    I'll take this.

  4. westonruter commented on Nov 1, 2026

    @westonruter
    MemberAuthor

    9. So the stub generator should be fixed, but for this one we can also narrow the types in WP_HTML_Tag_Processor with explicit @phpstan-param tags.

    See:

  5. huzaifaalmesbah commented on Nov 1, 2026

    @huzaifaalmesbah
    Member

    Two of the remaining errors need a decision before they can be fixed:

    1. lib/experimental/navigation-theme-opt-in.php (8 errors). The block-nav-menus opt-in was disabled in 2021 (Temporarily disable support for theme-opt for block-nav-menus from the Nav Editor #34265) with hard-coded if ( true ) / if ( false ), which causes the dead-code errors. The other 4 are menu items typed as WP_Post with extra properties, which Core also keeps in its baseline. Should this file be removed, now that the Navigation editor is gone, or should these be ignored in phpstan.neon.dist?
    2. WP_REST_Global_Styles_Controller_Gutenberg::prepare_links() (1 error). It takes int $id while the parent takes WP_Post. Core has the same code and baseline entry, and fixing it would change the signature. Should it be ignored, or left in the baseline?
  6. huzaifaalmesbah commented on Nov 1, 2026

    @huzaifaalmesbah
    Member

    For the Tag Cloud error, I've opened a Core PR to document wp_generate_tag_cloud()'s smallest and largest as int|float, since the block passes floats: WordPress/wordpress-develop#15306

    Once that's committed and the stubs are regenerated, the baseline entry for packages/block-library/src/tag-cloud/index.php can be removed.

  7. westonruter commented on Nov 1, 2026

    @westonruter
    MemberAuthor

    🤖 Comment by Claude Opus 5.5 · medium effort

    @huzaifaalmesbah The Tag Cloud fix landed in core as r65161, from WordPress/wordpress-develop#15306. wp_generate_tag_cloud() now documents smallest and largest as int|float, so the baseline entry for packages/block-library/src/tag-cloud/index.php can be removed once the stubs are regenerated from a core version that includes it.

    The commit also fixes a bug that the float type exposed: with equal float font sizes, as the block passes when its smallest and largest sizes match, an aria-label was output on every tag even though it is meant to be omitted when all tags share one size. Since render_block_core_tag_cloud() calls wp_tag_cloud(), the block gets that fix from core with no change needed in Gutenberg.

  8. westonruter commented on Nov 7, 2026

    @westonruter
    MemberAuthor
  9. westonruter commented on Nov 10, 2026

    @westonruter
    MemberAuthor

    With the merging of #86470, the baseline is now empty at PHPStan error level 5!

    parameters:
    ignoreErrors: []

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Type] Bug[Type] Code Quality

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions