Make WordPress Core

Changeset 65808


Ignore:
Timestamp:
11/27/2026 08:15:25 PM (15 hours ago)
Author:
westonruter
Message:

Build/Test Tools: Add tests for the PHPStan extensions.

The extensions in tests/phpstan change what PHPStan reads out of core, deriving types from hash notation and @global tags, typing filtered values, and checking hook documentation, so a defect in one surfaces only as a quiet change in what the analysis reports. They are now covered by a PHPUnit suite built on PHPStan's test harness, which runs them against fixtures rather than against core, with hook reference comments resolving against a small fixture tree instead of src. It is run with composer phpstan:test or npm run typecheck:php:test.

Since the harness parses every file as an analyzed one, it cannot see whether the redefinition of the simple parser in base.neon still applies, which is what gives a file that is only scanned the same derived types. One test therefore runs a real analysis with base.neon to cover it, so that a PHPStan upgrade renaming the service it redefines fails a test rather than quietly undoing it.

In CI, the suite runs in the analysis of the whole codebase, after its results are reported, so that a failing test does not also leave a pull request without its annotations. It is skipped on branches that do not have it.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/15850.
Follow-up to r62292, r62939, r63420, r63462.

Props johnbillion, lucatume, westonruter.
Fixes #66634.

Location:
trunk
Files:
38 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/.github/workflows/phpstan-static-analysis.yml

    r65693 r65808  
    2222      - 'tests/phpstan/baselines/**'
    2323      - 'tests/phpstan/baselines-strict/**'
     24      # These files configure the tests of the PHPStan extensions, and hold their fixtures.
     25      - 'tests/phpstan/phpunit.xml.dist'
     26      - 'tests/phpstan/Tests/**'
    2427      # Confirm any changes to relevant workflow files.
    2528      - '.github/workflows/phpstan-static-analysis.yml'
  • trunk/.github/workflows/reusable-phpstan-static-analysis-v2.yml

    r65693 r65808  
    3939  # - Reports the results as annotations.
    4040  # - Uploads the results as an artifact (pull requests only).
     41  # - Runs the tests of the WordPress-specific PHPStan extensions (default analysis only).
    4142  # - Checks for errors that cannot be reported as warnings, such as out-of-date baselines.
    4243  # - Saves the PHPStan result cache.
    … …  
    278279          path: ${{ runner.temp }}/phpstan-results/
    279280
     281      # The extensions in `tests/phpstan` are tested against fixtures rather than against
     282      # core, so a defect in one is reported here as a failing test rather than as a
     283      # change in what the analysis above reports.
     284      #
     285      # This runs after the results are reported and uploaded, so that a failing test
     286      # does not also keep the pull request from being annotated and commented on. It
     287      # runs even when the analysis failed, since the two are independent, but not when
     288      # the analysis was skipped: that means a step setting up the job, such as installing
     289      # the Composer dependencies, failed, and the tests could only fail along with it.
     290      # Both analyses load the same extensions, so the tests run in only one of them.
     291      - name: Run the tests of the PHPStan extensions
     292        if: ${{ !cancelled() && steps.phpstan.outcome != 'skipped' && matrix.analysis == 'default' && hashFiles('tests/phpstan/phpunit.xml.dist') != '' }}
     293        run: composer run phpstan:test
     294
    280295      # Some errors cannot be ignored, so they cannot be reported as warnings either. Most often
    281296      # this is an ignored error that no longer occurs, or occurs a different number of times,
  • trunk/composer.json

    r65769 r65808  
    8181                "phpstan:baselines": [ "Composer\\Config::disableProcessTimeout", "@php ./tests/phpstan/generate-baselines.php" ],
    8282                "phpstan:fix": [ "Composer\\Config::disableProcessTimeout", "@php ./tests/phpstan/analyse.php --memory-limit=2G --fix" ],
     83                "phpstan:test": "@php ./vendor/phpunit/phpunit/phpunit -c tests/phpstan/phpunit.xml.dist",
    8384                "compat": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --standard=phpcompat.xml.dist --report=summary,source",
    8485                "format": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf --report=summary,source",
  • trunk/package.json

    r65764 r65808  
    156156                "typecheck:php:baselines": "node ./tools/local-env/scripts/docker.js run --rm php composer phpstan:baselines --",
    157157                "typecheck:php:fix": "node ./tools/local-env/scripts/docker.js run --rm php composer phpstan:fix --",
     158                "typecheck:php:test": "node ./tools/local-env/scripts/docker.js run --rm php composer phpstan:test --",
    158159                "gutenberg:copy": "node tools/gutenberg/copy.js",
    159160                "gutenberg:verify": "node tools/gutenberg/utils.js",
  • trunk/tests/phpstan/README.md

    r65693 r65808  
    210210| `wordpress.hookParamCountMismatch` | The call passes a different number of arguments than the docblock documents `@param` tags for. Passing fewer risks an `ArgumentCountError` in a callback registered for the documented count; passing more silently drops the extra argument and leaves the documentation misleading. |
    211211
     212### Testing the extensions
     213
     214The extensions have tests of their own under [`Tests/`](Tests), built on PHPStan's test harness. The two rules are run over fixture files and the errors they report are compared with the ones expected. The visitors and the return type extension are checked through the types PHPStan infers for the `assertType()` calls in their fixtures. Reference comments resolve against a small WordPress tree of fixtures in [`Tests/data/wordpress`](Tests/data/wordpress) rather than against `src`, so a broken reference can be tested without one existing in core.
     215
     216The harness parses every file the way an analyzed file is parsed, so one test, `ReflectionParserTest`, runs a real analysis with `base.neon` instead. It checks that a function declared in a file that is only scanned is reflected with the shape its hash notation describes, which depends on the redefinition of `currentPhpVersionSimpleParser` there.
     217
     218Run them with:
     219
     220```bash
     221npm run typecheck:php:test
     222```
     223
     224or, outside the Docker environment:
     225
     226```bash
     227composer phpstan:test
     228```
     229
     230They also run in CI, after the analysis of the whole codebase, so that a failing test does not keep its results from being reported.
     231
    212232## Ignoring and baselining errors
    213233
  • trunk/tests/phpstan/base.neon

    r65310 r65808  
    7676        # the per-file reflection cache can be keyed on the file's contents alone.
    7777        #
    78         # HashNotationVisitor breaks that agreement. It rewrites `@param`, `@return` and
    79         # `@var` docblocks, which are exactly what reflection exposes, so a file read
     78        # HashNotationVisitor breaks that agreement. It rewrites `@param` and `@return`
     79        # docblocks, which are exactly what reflection exposes, so a file read
    8080        # through the simple parser reflects the documented `array` where the same file
    8181        # analyzed reflects the shape its hash notation describes. Analyzing a subset of
    … …  
    9292        # into anything and the behavior above returns; the errors it prevents are all of
    9393        # the quiet sort, so check this first when a hash notation type looks wrong.
     94        # Tests/ReflectionParserTest.php fails when that happens.
    9495        #
    9596        # `arguments!` replaces the inherited arguments rather than merging with them.
    … …  
    306307                - HookDocumentationRule.php
    307308                - HookParamCountRule.php
     309                - Tests
    308310        bootstrapFiles:
    309311                - bootstrap.php
    … …  
    313315        excludePaths:
    314316                analyseAndScan:
     317                        # Fixtures for the tests of the extensions above, which carry the errors those tests expect.
     318                        - Tests/data
    315319                        # Locally installed npm dependencies, such as in bundled themes, which may include PHP files.
    316320                        - */node_modules/*
Note: See TracChangeset for help on using the changeset viewer.