Skip to content

#64896 Tighten the documented types in WP_Hook#12441

Open
johnbillion wants to merge 3 commits into
WordPress:trunkfrom
johnbillion:64896-wp_hook-docs
Open

#64896 Tighten the documented types in WP_Hook#12441
johnbillion wants to merge 3 commits into
WordPress:trunkfrom
johnbillion:64896-wp_hook-docs

Conversation

@johnbillion

@johnbillion johnbillion commented Jul 7, 2026

Copy link
Copy Markdown
Member

This adds a Hook_Callback PHPStan type to represent the shape of a callback used internally in WP_Hook and used in return values of array access methods.

Also narrows a few other documented types.

Trac ticket: https://core.trac.wordpress.org/ticket/64896
Trac ticket: https://core.trac.wordpress.org/ticket/64898

@johnbillion johnbillion requested a review from westonruter July 7, 2026 19:15
@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
@westonruter

Copy link
Copy Markdown
Member

I was looking into the other PHPStan rule level 10 issues in this file, and I found something related to the change you made here. It seems the offsetSet() method is lacking the use of the new type. I tried adding it:

--- a/src/wp-includes/class-wp-hook.php
+++ b/src/wp-includes/class-wp-hook.php
 	 *
 	 * @param mixed $offset The offset to assign the value to.
 	 * @param mixed $value The value to set.
+	 *
+	 * @phpstan-param array-key|null $offset
+	 * @phpstan-param array<string, Hook_Callback> $value
 	 */
 	#[ReturnTypeWillChange]
 	public function offsetSet( $offset, $value ) {

But there are two errors remaining here:

public function offsetSet( $offset, $value ) {
if ( is_null( $offset ) ) {
$this->callbacks[] = $value;
} else {
$this->callbacks[ $offset ] = $value;
}
$this->priorities = array_keys( $this->callbacks );
}

At:

$this->callbacks[ $offset ] = $value;

phpstan: Property WP_Hook::$callbacks (array<int, array<string, array{function: callable(): mixed, accepted_args: int}>>) does not accept non-empty-array<int|string, array<string, array{function: callable(): mixed, accepted_args: int}>>.

And at:

$this->priorities = array_keys( $this->callbacks );

phpstan: Property WP_Hook::$priorities (list<int>) does not accept non-empty-list<int|string>.

… 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>
@westonruter

Copy link
Copy Markdown
Member

Addressed in 67f3a9b.

@westonruter

Copy link
Copy Markdown
Member

I've followed up with #12443 to add b0d8b59 which fixes the remaining PHPStan level 10 errors in class-wp-hooks.php. If you don't want to include those fixes in your commit here, I can commit it separately.

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