#64896 Fix remaining PHPStan level 10 errors in WP_Hook#12443
#64896 Fix remaining PHPStan level 10 errors in WP_Hook#12443westonruter wants to merge 6 commits into
WP_Hook#12443Conversation
… types. As an `ArrayAccess` object, the offsets of `WP_Hook` are hook priorities, which are always integers (or null when appending via `offsetSet()`), and the values are the `Hook_Callback` groups keyed by unique function ID. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Add `@return void` to the methods lacking return values, document the callback `$args` arrays as `list<mixed>`, and narrow the `$callback` params of `remove_filter()`/`has_filter()` to `callable`, matching `add_filter()`. In `has_filter()`, pass `0` instead of `false` to `_wp_filter_build_unique_id()`, matching its `int $priority` param. In `apply_filters()`, assign the current priority to a local variable before storing it, and bail from the loop in the (impossible) case that `current()` returns `false`, since `$current_priority` only ever holds integers. Co-Authored-By: Claude Fable 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. 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. |
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. |
| * a callback that may or may not exist. | ||
| * @param int $priority The exact priority used when adding the original filter callback. | ||
| * @param string $hook_name The filter hook to which the function to be removed is hooked. | ||
| * @param callable $callback The callback to be removed from running when the filter is applied. |
There was a problem hiding this comment.
I had to widen the $callback type in _wp_filter_build_unique_id() as well.
| * a callback that may or may not exist. | ||
| * @param int $priority The exact priority used when adding the original filter callback. | ||
| * @return bool Whether the callback existed before it was removed. | ||
| * @phpstan-param callable|string|array{ 0: string|object, 1: string, ... } $callback |
There was a problem hiding this comment.
I chose array{ 0: string|object, 1: string, ... } as the array shape for the $callback because this is the array shape required by _wp_filter_build_unique_id() or else it will return null.
This is stacked on #12441 and includes all of its commits. The only new commit here is b0d8b59, which resolves the remaining PHPStan level 10 errors in
WP_Hookon top of the type tightening done in that PR. @johnbillion: feel free to merge this into your branch if you want to fold it in; otherwise I'll rebase ontotrunkonce your PR lands.The new commit:
@return voidto the methods lacking return values.$argsarrays aslist<mixed>and narrows the$callbackparams ofremove_filter()/has_filter()tocallable, matchingadd_filter().0instead offalseto_wp_filter_build_unique_id()inhas_filter(), matching itsint $priorityparam. (The value is unused for the key, so this is behavior-neutral.)apply_filters()loop to assign the current priority to a local variable before storing it in$current_priority, bailing from the loop in the (impossible) case thatcurrent()returnsfalse. This keeps$current_priorityhonestly typed asarray<int, int>; previously, PHPStan sawint|falsebeing stored.With this,
composer phpstan src/wp-includes/class-wp-hook.phpreports no errors at level 10.Local
phpstan.neonoverridesTrac ticket: https://core.trac.wordpress.org/ticket/64896
Trac ticket: https://core.trac.wordpress.org/ticket/64898
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Fable 5
Used for: Diagnosing some of the PHPStan level 10 errors and drafting some of the fixes in the final commit; I reviewed, adjusted, and take responsibility for the changes.
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
https://claude.ai/code/session_01Y1qB3ZWwGV2nBtdb9NgcJa