Build/Test Tools: Treat DB_COLLATE and WP_DEVELOPMENT_MODE as dynamic constants. - #12958
Open
CallumBW95 wants to merge 1 commit into
Open
Conversation
… constants. Both constants vary per installation, but PHPStan resolved each to the empty string it is given during analysis: `DB_COLLATE` from `wp-config-sample.php`, pulled in via `scanFiles`, and `WP_DEVELOPMENT_MODE` from `tests/phpstan/bootstrap.php`. Conditionals guarding either were therefore reported as having a constant result. `WP_DEVELOPMENT_MODE` accepts 'core', 'plugin', 'theme', 'all', or an empty string, and `DB_COLLATE` is set per site in `wp-config.php`, so both belong in `dynamicConstantNames` alongside comparable entries such as `WP_DEBUG` and `SCRIPT_DEBUG`. This resolves three baselined errors and empties two baselines, which are removed along with their `includes` entries in `phpstan.neon.dist`: * `ternary.alwaysFalse`: two occurrences in `class-wp-debug-data.php`. * `booleanAnd.rightAlwaysFalse`: one occurrence in `class-wpdb.php`. No source changes are needed; the conditionals were already correct and only the analysis was wrong. Props CallumBW95. See #65817.
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DB_COLLATEandWP_DEVELOPMENT_MODEboth vary per installation, but neither is listed indynamicConstantNames. PHPStan therefore resolves each to the empty string it happens to be given during analysis:DB_COLLATEcomes fromwp-config-sample.php, pulled in viascanFiles, where it isdefine( 'DB_COLLATE', '' );.WP_DEVELOPMENT_MODEcomes fromtests/phpstan/bootstrap.php, where it isdefine( 'WP_DEVELOPMENT_MODE', '' );.Any conditional guarding either constant is then analysed as having a constant result, which accounts for the three baselined errors below. The conditionals are correct as written, so this change touches configuration only and leaves every source file alone.
Adding both constants to
dynamicConstantNamesresolves the three errors and empties two baselines. Those files are deleted along with theirincludesentries inphpstan.neon.dist.ternary.alwaysFalseclass-wp-debug-data.php:1548DB_COLLATE ? DB_COLLATE : __( 'Empty value' )ternary.alwaysFalseclass-wp-debug-data.php:1634WP_DEVELOPMENT_MODE ? WP_DEVELOPMENT_MODE : __( 'Disabled' )booleanAnd.rightAlwaysFalseclass-wpdb.php:846defined( 'DB_COLLATE' ) && DB_COLLATETaking these errors at face value would suggest deleting the branch PHPStan considers unreachable. That would be a regression. It would drop the reported value on any site that sets either constant, and in
wpdb::init_charset()it would forceutf8_general_cion multisite whatever collation the site has configured.Why these two belong on the list
WP_DEVELOPMENT_MODEaccepts'core','plugin','theme','all', or an empty string to disable, as documented onwp_get_development_mode()inwp-includes/load.php. It is defined intests/phpstan/bootstrap.phpimmediately alongsideWP_DEBUG,WP_DEBUG_DISPLAY,WP_DEBUG_LOG,WP_CACHE,SCRIPT_DEBUG,MEDIA_TRASHandSHORTINIT, and all seven of those are already indynamicConstantNames.Core's own development environment also disagrees with the value the analysis assumes.
tools/local-env/scripts/install.jswritesWP_DEVELOPMENT_MODEfromLOCAL_WP_DEVELOPMENT_MODE, which.env.examplesets tocore. A checkout installed withnpm run env:installhas the constant set to a non-empty value while PHPStan reads it as''.DB_COLLATEis set per site inwp-config.phpand is empty only in the shipped sample file.DB_CHARSETsits next to it and is read the same way.Background
dynamicConstantNamesarrived in [61699] (2026-02-20), the changeset that integrated PHPStan into the core development workflow.git log -Sagainstbase.neonshows that neither constant has appeared on the list at any point, so these errors have been baselined since the baselines were first generated.Testing instructions
trunk,npm run typecheck:phpreports[OK] No errors, because all three occurrences are baselined.tests/phpstan/baselines/ternary.alwaysFalse.neonandtests/phpstan/baselines/booleanAnd.rightAlwaysFalse.neonalong with theirincludesentries, then run it again. PHPStan reports:Ternary operator condition is always false.twice insrc/wp-admin/includes/class-wp-debug-data.phpRight side of && is always false.insrc/wp-includes/class-wpdb.phpnpm run typecheck:phpreports[OK] No errorswith both baselines gone and no new errors elsewhere.npm run test:phpis unchanged: 30774 tests, 4559286 assertions, 86 warnings, 44 skipped, identical before and after.The usual risk when adding to
dynamicConstantNamesruns the other way. Making a value unknown can surface new errors in code that relied on PHPStan narrowing it. The full run above found none.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: tracing the three baselined errors back to the constant values supplied by
scanFilesandbootstrap.php,git log -Sarchaeology againstbase.neon, and drafting this description. The diagnosis that this is a configuration issue rather than a source defect, the configuration change itself, the baseline regeneration, and verification against full PHPStan and PHPUnit runs were reviewed and confirmed by me in a local development environment.