Skip to content

Build/Test Tools: Treat request-scoped constants as dynamic in PHPStan - #13071

Open
tstokes8040 wants to merge 1 commit into
WordPress:trunkfrom
tstokes8040:phpstan/dynamic-constants
Open

Build/Test Tools: Treat request-scoped constants as dynamic in PHPStan#13071
tstokes8040 wants to merge 1 commit into
WordPress:trunkfrom
tstokes8040:phpstan/dynamic-constants

Conversation

@tstokes8040

@tstokes8040 tstokes8040 commented Aug 16, 2026

Copy link
Copy Markdown

DOING_AJAX, DOING_CRON, REST_REQUEST, WP_INSTALLING, WP_INSTALLING_NETWORK, WP_REPAIRING and XMLRPC_REQUEST are each defined only as the literal true within the analyzed tree, so PHPStan concluded that the right side of the defined( 'X' ) && X idiom can never be falsy:

Constant Sole definition
DOING_AJAX wp-admin/admin-ajax.php:16
DOING_CRON wp-cron.php:42
REST_REQUEST wp-includes/rest-api.php:478
WP_INSTALLING wp-admin/install.php:32
WP_INSTALLING_NETWORK wp-admin/network.php:13
WP_REPAIRING wp-admin/maint/repair.php:8
XMLRPC_REQUEST xmlrpc.php:13

That conclusion is correct about the code PHPStan can see and wrong about reality. Every one of these is request-scoped: absent on most requests, defined only by the front controller handling that particular kind of request, and definable by a plugin or by wp-config.php before load. The defined( 'X' ) && X guard is doing real work, and the constants belong in dynamicConstantNames alongside the ones already listed there.

This resolves 11 booleanAnd.rightAlwaysTrue occurrences, leaving 5 in the baseline:

File Line(s)
wp-admin/includes/schema.php 46
wp-includes/functions.php 3857
wp-includes/l10n.php 962, 967, 971
wp-includes/load.php 643, 1638, 1753, 1789
wp-includes/user.php 387, 3827

The baseline is generated, not hand-edited; it was regenerated with composer phpstan:baselines -- --identifier=booleanAnd.rightAlwaysTrue.

No source file is touched, so there is no runtime behaviour to change. Follow-up to [63191], which added DB_COLLATE and WP_DEVELOPMENT_MODE for the same reason.

Testing instructions

  1. npm run typecheck:php on trunk reports [OK] No errors, because the occurrences are baselined.
  2. With this branch applied, npm run typecheck:php reports [OK] No errors across 1289 files, with tests/phpstan/baselines/booleanAnd.rightAlwaysTrue.neon down from 9 entries / 16 occurrences to 5 entries / 5 occurrences.
  3. To confirm the change is surgical, run the analysis with all baselines suppressed both before and after and diff the results: exactly 11 errors disappear, all of them booleanAnd.rightAlwaysTrue, and no new error of any identifier appears anywhere in the codebase.
  4. npm run test:php -- --group html-api and the wider suite are unaffected, since no source file changes.

Trac ticket: https://core.trac.wordpress.org/ticket/65817

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: grouping the remaining boolean PHPStan baselines by root cause, identifying this cluster and the constants behind it, measuring the before/after error diff across the whole codebase, regenerating the baseline, and drafting this description. The change itself and the verification runs were reviewed and confirmed by me in a local development environment.

Count of removed errors: 1

`DOING_AJAX`, `DOING_CRON`, `REST_REQUEST`, `WP_INSTALLING`, `WP_INSTALLING_NETWORK`, `WP_REPAIRING` and `XMLRPC_REQUEST` are each defined only as the literal `true` within the analyzed tree, so PHPStan concluded that the right side of the `defined( 'X' ) && X` idiom can never be falsy. Every one of them is request-scoped: absent on most requests, and definable by a plugin or by `wp-config.php` before load. Listing them in `dynamicConstantNames` stops PHPStan assuming a value it cannot know.

This resolves 11 `booleanAnd.rightAlwaysTrue` occurrences, in `wp-admin/includes/schema.php`, `wp-includes/functions.php`, `wp-includes/l10n.php`, `wp-includes/load.php` and `wp-includes/user.php`, leaving 5 in the baseline. It was regenerated with:
{{{
composer phpstan:baselines -- --identifier=booleanAnd.rightAlwaysTrue
}}}

Follow-up to [63191].

Props tstokes8040.
See #65817.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props tstokes8040.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

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