Skip to content

PHPStan: Ignore return.void errors for action callbacks - #86108

Merged
westonruter merged 1 commit into
WordPress:trunkfrom
noruzzamans:fix/phpstan-media-delete-attachment-void
Nov 4, 2026
Merged

westonruter merged 1 commit into
WordPress:trunkfrom
noruzzamans:fix/phpstan-media-delete-attachment-void

Conversation

@noruzzamans

@noruzzamans noruzzamans commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

What?

See #86088 and discussion in review.

Adds an ignoreErrors entry in tools/phpstan/base.neon for action callback return.void errors and removes the baselined occurrence for lib/media/load.php from tools/phpstan/baseline.neon. Reverts the signature and test changes to gutenberg_delete_heic_companion_file() so it retains its bool return type.

Why?

gutenberg_delete_heic_companion_file() returns bool to signal to callers and unit tests whether a companion file was deleted. While action callbacks hooked via add_action() have their return values discarded by do_action(), returning a status is harmless and common across WordPress core (e.g. _wp_cron(), wp_set_up_cross_origin_isolation(), wp_save_post_revision()).

Changing the function to return void would weaken tests by preventing them from asserting whether the function took action versus bailed out. As noted by @westonruter, ignoring this specific message and identifier in base.neon preserves both static analysis cleanliness and test fidelity.

How?

  • Added ignoreErrors in tools/phpstan/base.neon matching #^Action callback returns .+ but should not return anything\.$# with identifier return.void.
  • Removed the baselined return.void entry for lib/media/load.php in tools/phpstan/baseline.neon.
  • Kept lib/media/load.php and its unit tests intact with bool return value assertions.

Testing Instructions

  1. Run composer phpstan and confirm it passes with no unmatched ignored errors.
  2. Run composer phpstan:baselines and confirm tools/phpstan/baseline.neon has no further changes.

@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: noruzzamans <noruzzaman@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: huzaifaalmesbah <huzaifaalmesbah@git.wordpress.org>

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

@huzaifaalmesbah huzaifaalmesbah added [Type] Code Quality Issues or PRs that relate to code quality No Core Sync Required Indicates that any changes do not need to be synced to WordPress Core labels Nov 1, 2026
@huzaifaalmesbah

Copy link
Copy Markdown
Member

Makes sense for an action callback. One small thing: the no-op test (test_noop_when_metadata_source_image_is_missing) no longer asserts anything about the call itself, now that the assertFalse() is gone. Could it assert that the companion path (or the attached file) still exists afterwards, so the test still checks that nothing was deleted?

@noruzzamans

Copy link
Copy Markdown
Member Author

Good point, thanks! I've updated the test to assert that the attached file still exists on disk after the callback runs.

@westonruter

Copy link
Copy Markdown
Member

Hummm. I'm not sure about this change. Even though return values from action callbacks aren't used, it doesn't seem wrong for a callback to return a value. I'm certain there are examples in core of functions being used in action callbacks which also return values. I'm going to have Claude follow up with an analysis. My sense is that this is making the function less useful as it can't be used as easily for unit testing since you can't assert the return value anymore.

@westonruter

Copy link
Copy Markdown
Member

🤖 Comment by Claude Opus 5.5 · medium effort

It is questionable whether this change should be made. Although do_action() discards the return values of its callbacks, returning a value is not wrong. It allows callers, and especially unit tests, to determine whether the callback actually did anything.

Where the rule comes from

The return.void error ("Action callback returns bool but should not return anything.") is not a WordPress coding standard. It comes from HookCallbackRule::validateActionReturnType() in szepeviktor/phpstan-wordpress, which reports any action callback whose return type is not void or never. It was introduced in szepeviktor/phpstan-wordpress#107, where "The callback for add_action must return void" is listed as one of the checks, with no rationale given or discussed. No upstream issue discussing it could be found either.

Core does not follow this convention

Matching core's add_action( 'hook', 'callback' ) calls against each callback's @return tag turns up 28 action callbacks documented to return a value. Among them:

Callback Returns Hooked to Return value used in tests
_wp_cron() int|false wp_loaded, shutdown —
wp_set_up_cross_origin_isolation() bool load-post.php, load-upload.php, … yes, three test files
wp_start_template_enhancement_output_buffer() bool wp_before_include_template yes, tests/template.php
wp_save_post_revision() int|WP_Error|null post_updated yes, tests/post/revisions.php
wp_new_comment_notify_postauthor(), wp_new_comment_notify_moderator() bool comment_post, wp_set_comment_status yes, tests/comment.php
redirect_canonical() string|null template_redirect —
wp_print_styles(), wp_print_head_scripts() string[] wp_head —
wp_initialize_site(), wp_uninitialize_site() true|WP_Error the hooks of the same name —

Others include _wp_admin_bar_init(), generic_ping(), newblog_notify_siteadmin(), and the wpmu_*_notification() functions.

Two of these are especially relevant:

  • wp_set_up_cross_origin_isolation() (since 7.1.0) is from the same client-side media feature as gutenberg_delete_heic_companion_file(), and its tests rely on its bool return value.
  • _wp_cron() had a return value added deliberately in 5.1.0, documented as "Return value added to indicate success or failure," while being an action callback.

Effect on the tests

To satisfy the rule, this PR weakens the tests. Previously, they asserted the function's result directly (assertTrue() when a companion file is deleted and assertFalse() when there is nothing to delete). Now they can only check whether files exist afterward. In the no-op case, that can no longer distinguish an early bail-out from a run that reached wp_delete_file_from_directory() and failed.

Suggested alternative: ignore this specific error

The extension does not provide a parameter to turn off this check. Removing HookCallbackRule entirely is undesirable, because it also verifies that $accepted_args matches the callback's parameter count (arguments.count) and that filter callbacks return a value (return.missing). Ignoring the return.void identifier alone would be too broad, since PHPStan itself uses it for genuine errors like "Function foo() with return type void returns int but should not return anything." in FunctionReturnTypeCheck.

Instead, the error can be ignored by message and identifier together in tools/phpstan/base.neon:

parameters:
	ignoreErrors:
		# Return values from action callbacks are discarded by do_action(), but returning one is
		# harmless and lets callers and tests know whether the callback did anything. Core has many
		# such callbacks, like _wp_cron() and wp_set_up_cross_origin_isolation().
		-
			message: '#^Action callback returns .+ but should not return anything\.$#'
			identifier: return.void

Along with this, the existing return.void entry for lib/media/load.php would be removed from tools/phpstan/baseline.neon (it is the only one), since otherwise reportUnmatchedIgnoredErrors: true would report it as unmatched. gutenberg_delete_heic_companion_file() would keep its bool return type, and its tests would remain unchanged.

Follow-up

A follow-up upstream in szepeviktor/phpstan-wordpress should add a parameter for HookCallbackRule to configure this behavior, for example to allow action callbacks to return values. That would replace the message-based ignore with a proper configuration option, and benefit other projects (including core) that use the extension.

@westonruter westonruter left a comment

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.

See comments.

@noruzzamans
noruzzamans force-pushed the fix/phpstan-media-delete-attachment-void branch from 644f348 to b825099 Compare November 3, 2026 09:01
@noruzzamans noruzzamans changed the title Media: Avoid returning a value from delete_attachment action callback PHPStan: Ignore return.void errors for action callbacks Nov 3, 2026
@noruzzamans

Copy link
Copy Markdown
Member Author

Rebased onto latest trunk and updated to the suggested approach:

  1. Reverted lib/media/load.php and phpunit/media/gutenberg-delete-heic-companion-file-test.php so gutenberg_delete_heic_companion_file() retains its bool return type and tests continue to assert the return value directly.
  2. Added the ignoreErrors entry for return.void on action callbacks in tools/phpstan/base.neon.
  3. Removed the corresponding entry from tools/phpstan/baseline.neon.

@manzoorwanijk
manzoorwanijk removed their request for review November 3, 2026 09:02
@westonruter
westonruter merged commit 2b5a07f into WordPress:trunk Nov 4, 2026
82 checks passed
@github-actions github-actions Bot added this to the Gutenberg 24.5 milestone Nov 4, 2026
Vedant-Gandhi pushed a commit to Vedant-Gandhi/gutenberg that referenced this pull request Nov 30, 2026
…ion (WordPress#86108)

Co-authored-by: noruzzamans <noruzzaman@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: huzaifaalmesbah <huzaifaalmesbah@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

No Core Sync Required Indicates that any changes do not need to be synced to WordPress Core [Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants