Repository navigation
Code Quality: Remove the global PHPStan type aliases - #14900
westonruter wants to merge 2 commits into
Conversation
The `typeAliases` in `tests/phpstan/base.neon` were global to core's own analysis, but the docblocks in `src/` that used them are read elsewhere too. The stubs generated from core keep the references and lose the definitions, so anyone who analyzes against core, or against those stubs, gets an unknown type where each alias was used, unless they define it themselves. Inline the `Maybe_Callable` union at its uses, which are mostly functions, where a class alias cannot be imported. Declare `Block_Style_Properties` with `@phpstan-type` on `WP_Block_Styles_Registry`, which the stubs keep, and import it into `WP_REST_Block_Types_Controller`. Leave a note in place of the aliases, so that new ones are declared on classes too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
The note in place of the global type aliases says to declare an alias on a class, which leaves a type used by functions to be repeated. Say that this lasts until PHPStan supports declaring an alias with `@phpstan-type` outside a class, and link the issue that tracks it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. 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. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
The `typeAliases` defined in `tests/phpstan/base.neon` only exist for core's own analysis, but the docblocks in `src/` that use them are read elsewhere too. A WordPress release package and the `WordPress/WordPress` repository only include `src/`, and the stubs generated from core (as used by `szepeviktor/phpstan-wordpress`) keep each reference to an alias while losing its definition. Anyone analyzing a plugin or theme against core then gets an unknown type at every use of an alias, unless they define each one themselves. The `Maybe_Callable` union is now written out at each of its uses, which are mostly functions, where an alias declared on a class cannot be imported. The `Block_Style_Properties` shape is declared with `@phpstan-type` on `WP_Block_Styles_Registry`, which the generated stubs keep, and imported into `WP_REST_Block_Types_Controller`. A note in `base.neon` explains why new aliases should be declared on a class as well, at least until PHPStan supports declaring one outside a class. Developed in #14900. Follow-up to r63451, r63524. Props westonruter, marian1. See #65817. git-svn-id: https://develop.svn.wordpress.org/trunk@64756 602fd350-edb4-49c9-b593-d223f7449a82
The `typeAliases` defined in `tests/phpstan/base.neon` only exist for core's own analysis, but the docblocks in `src/` that use them are read elsewhere too. A WordPress release package and the `WordPress/WordPress` repository only include `src/`, and the stubs generated from core (as used by `szepeviktor/phpstan-wordpress`) keep each reference to an alias while losing its definition. Anyone analyzing a plugin or theme against core then gets an unknown type at every use of an alias, unless they define each one themselves. The `Maybe_Callable` union is now written out at each of its uses, which are mostly functions, where an alias declared on a class cannot be imported. The `Block_Style_Properties` shape is declared with `@phpstan-type` on `WP_Block_Styles_Registry`, which the generated stubs keep, and imported into `WP_REST_Block_Types_Controller`. A note in `base.neon` explains why new aliases should be declared on a class as well, at least until PHPStan supports declaring one outside a class. Developed in WordPress/wordpress-develop#14900. Follow-up to r63451, r63524. Props westonruter, marian1. See #65817. Built from https://develop.svn.wordpress.org/trunk@64756 git-svn-id: http://core.svn.wordpress.org/trunk@63906 1a063a9b-81f0-0310-95a4-ce76da25c4cd
✅ Committed in r64756 (ecdec59).
This removes the two global PHPStan type aliases in
tests/phpstan/base.neon,Maybe_Callable(added in r63451) andBlock_Style_Properties(added in r63524), and replaces their uses with types that stay with the docblocks insrc/.Why
A
typeAliasesentry inbase.neonis only defined for core's own analysis, but the docblocks that use it are read elsewhere too. The stubs generated from core (php-stubs/wordpress-stubs) keep each reference to an alias and lose its definition, so PHPStan reports an unknown type at every use for anyone analyzing against those stubs, or against core's source directly, unless they define each alias themselves. The aliases are not present when using a WordPress release package or WordPress/WordPress, since they only includesrc/.This was raised by @IanDelMar in the review thread on
Optional_Callbackin #13890, which proposed a third global alias:and later:
Alternatives discussed there, such as
wordpress-stubsexpanding the aliases when generating the stubs, orphpstan-wordpressshipping their definitions, would each add ongoing maintenance downstream, and neither helps anyone who analyzes against core's source directly. Global aliases can also collide with aliases defined elsewhere, which theWP_prefix suggested in that thread would only reduce.The Gutenberg dependency
This now blocks Gutenberg, which is adding PHPStan in WordPress/gutenberg#66693. Since Gutenberg tracks core trunk, its analysis should use core trunk's types rather than those of the last WordPress release, so the plan there is to generate the stubs from a pinned core trunk commit with the
wordpress-stubsgenerator (untilwordpress-stubsoffers a nightly pre-release from trunk). Generated from trunk, the stubs reference both aliases without defining them, and Gutenberg's analysis reports 106 false errors, mostly where a callback is passed toremove_filter()orremove_action().With this change, stubs generated from this branch contain no reference to an undefined alias, and Gutenberg's analysis reports none of those errors, without Gutenberg defining anything itself. The same applies to the next
wordpress-stubsrelease generated from a version of WordPress that has these docblocks, which would otherwise break the analysis of every project using it.(Stubs generated from trunk also need php-stubs/wordpress-stubs#498, since the function map currently adds a second
@phpstan-returnto 152 docblocks where core trunk now has its own.)Changes
Maybe_Callableis inlined at its seven uses, ascallable|string|array{0: string|object, 1: string}, onremove_filter(),has_filter(),remove_action(),has_action(),_wp_filter_build_unique_id(), and theWP_Hookmethods behind them. Most of the uses are functions, which cannot import an alias declared on a class.Block_Style_Propertiesis declared with@phpstan-typeonWP_Block_Styles_Registry, where four of its five uses are, along with the explanation of its shape that was inbase.neon.WP_REST_Block_Types_Controllerimports it with@phpstan-import-type. An alias declared on a class is kept in the generated stubs, asWP_Post'sData_ArrayandWP_Hook'sHook_Callbackalready are.base.neonhas a note in place of the aliases explaining why there are none, so that new aliases are declared on classes too.The cost is that a type used by functions has to be repeated until PHPStan supports declaring an alias outside a class (phpstan/phpstan#9164). That applies to
Optional_Callbackin #13890 as well.Testing
composer phpstanreports no new errors. The one error it does report, an unmatched baseline entry for$authors_dropdowninclass-wp-posts-list-table.php, is reported on trunk as well.phpstan-diffreports no errors on the changed lines.wordpress-stubsgenerator contain no reference to either alias, and declareBlock_Style_PropertiesonWP_Block_Styles_Registry.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.5
Used for: Replacing the aliases, verifying the change with PHPStan, generating stubs from this branch and analyzing Gutenberg against them, and drafting the commit message and this description. Reviewed by me.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.
🤖 Generated with Claude Code