Make WordPress Core

Changeset 62292


Ignore:
Timestamp:
05/02/2026 05:16:01 AM (7 months ago)
Author:
westonruter
Message:

Build/Test Tools: Honor @global docblock tags in PHPStan analysis.

This adds a PHPStan extension with a parser-node visitor that bridges WordPress core's @global Type $varname PHPDoc convention to PHPStan's variable type resolution, eliminating 3,784 spurious errors caused by globals resolving as mixed when on rule level 10 (out of 40,069 errors total, so a 9.4% reduction). This avoids the need to add /** @var Type $varname */ annotations with each global variable.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/11692

Props westonruter, apermo, szepeviktor.
See #64898.

Location:
trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/composer.json

    r62271 r62292  
    3333                "lock": false
    3434        },
     35        "autoload-dev": {
     36                "psr-4": {
     37                        "WordPress\\PHPStan\\": "tests/phpstan/"
     38                }
     39        },
    3540        "scripts": {
    3641                "phpstan": "@php ./vendor/bin/phpstan analyse --memory-limit=2G",
  • trunk/tests/phpstan/base.neon

    r61873 r62292  
    44#
    55# https://phpstan.org/config-reference
     6
     7services:
     8        # Bridges WordPress core's `@global Type $varname` function docblock convention to
     9        # PHPStan's variable type resolution. See tests/phpstan/GlobalDocBlockVisitor.php.
     10        -
     11                class: WordPress\PHPStan\GlobalDocBlockVisitor
     12                tags:
     13                        - phpstan.parser.richParserNodeVisitor
    614
    715parameters:
    … …  
    9199                - ../../src/wp-trackback.php
    92100                - ../../src/xmlrpc.php
     101                - GlobalDocBlockVisitor.php
    93102        bootstrapFiles:
    94103                - bootstrap.php
  • trunk/tests/phpstan/bootstrap.php

    r61699 r62292  
    11<?php
    22/**
    3  * Defines default WordPress constants for discovery.
     3 * Defines default WordPress constants for PHPStan discovery.
    44 *
    55 * Mocks the constant initiation that would normally happen in wp-includes/wp-settings.php.
     6 *
     7 * Loaded as a `bootstrapFile` by PHPStan; see `base.neon`.
    68 */
    79
Note: See TracChangeset for help on using the changeset viewer.