Skip to content

#64896 Fix remaining PHPStan level 10 errors in WP_Hook#12443

Open
westonruter wants to merge 6 commits into
WordPress:trunkfrom
westonruter:fix/wp-hooks-phpstan-level-10-errors
Open

#64896 Fix remaining PHPStan level 10 errors in WP_Hook#12443
westonruter wants to merge 6 commits into
WordPress:trunkfrom
westonruter:fix/wp-hooks-phpstan-level-10-errors

Conversation

@westonruter

@westonruter westonruter commented Jul 7, 2026

Copy link
Copy Markdown
Member

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_Hook on 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 onto trunk once your PR lands.

The new commit:

  • Adds @return void to the methods lacking return values.
  • Documents the callback $args arrays as list<mixed> and narrows the $callback params of remove_filter()/has_filter() to callable, matching add_filter().
  • Passes 0 instead of false to _wp_filter_build_unique_id() in has_filter(), matching its int $priority param. (The value is unused for the key, so this is behavior-neutral.)
  • Restructures the 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 that current() returns false. This keeps $current_priority honestly typed as array<int, int>; previously, PHPStan saw int|false being stored.

With this, composer phpstan src/wp-includes/class-wp-hook.php reports no errors at level 10.

Local phpstan.neon overrides
includes:
	- phpstan.neon.dist
	- vendor/phpstan/phpstan/conf/bleedingEdge.neon

parameters:
	# https://phpstan.org/user-guide/rule-levels
	level: 10

	editorUrl: 'phpstorm://open?file=%%file%%&line=%%line%%'

	# Narrow scanDirectories to source subdirs only. Pointing at plugin roots causes
	# PHPStan to traverse vendor/ and node_modules/ (~9k files), which dominates
	# startup time — excludePaths does not prune the traversal.
	scanDirectories:
		- tests/phpunit

	ignoreErrors:
		- '#^Cannot cast mixed to int\.$#'
		# FTP\Connection is PHP 8.1+; the @var is correct for WP's 7.4+ range, but
		# bleedingEdge's version-aware check flags it against the PHP 7.4 floor.
		-
			identifier: class.notFound
			path: src/wp-admin/includes/class-wp-filesystem-ftpext.php
		-
			message: '#expects resource, FTP\\Connection#'
			path: src/wp-admin/includes/class-wp-filesystem-ftpext.php
		-
			identifier: function.internal

Trac 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

johnbillion and others added 4 commits July 7, 2026 20:13
… 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>
@github-actions

github-actions Bot commented Jul 7, 2026

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 johnbillion, westonruter.

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

@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Comment thread src/wp-includes/class-wp-hook.php Outdated
* 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intentionally wider than callable: 7819ca4

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about bf773c3?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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.

2 participants