Repository navigation
PHPStan: Ignore return.void errors for action callbacks - #86108
westonruter merged 1 commit into
Conversation
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. 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:
Updated as activity occurs, without notifying anyone named here. Add the |
|
Makes sense for an action callback. One small thing: the no-op test ( |
|
Good point, thanks! I've updated the test to assert that the attached file still exists on disk after the callback runs. |
|
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. |
|
🤖 Comment by Claude Opus 5.5 · medium effort It is questionable whether this change should be made. Although Where the rule comes fromThe Core does not follow this conventionMatching core's
Others include Two of these are especially relevant:
Effect on the testsTo satisfy the rule, this PR weakens the tests. Previously, they asserted the function's result directly ( Suggested alternative: ignore this specific errorThe extension does not provide a parameter to turn off this check. Removing Instead, the error can be ignored by message and identifier together in 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.voidAlong with this, the existing Follow-upA follow-up upstream in |
644f348 to
b825099
Compare
|
Rebased onto latest trunk and updated to the suggested approach:
|
…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>
What?
See #86088 and discussion in review.
Adds an
ignoreErrorsentry intools/phpstan/base.neonfor action callbackreturn.voiderrors and removes the baselined occurrence forlib/media/load.phpfromtools/phpstan/baseline.neon. Reverts the signature and test changes togutenberg_delete_heic_companion_file()so it retains itsboolreturn type.Why?
gutenberg_delete_heic_companion_file()returnsboolto signal to callers and unit tests whether a companion file was deleted. While action callbacks hooked viaadd_action()have their return values discarded bydo_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
voidwould 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 inbase.neonpreserves both static analysis cleanliness and test fidelity.How?
ignoreErrorsintools/phpstan/base.neonmatching#^Action callback returns .+ but should not return anything\.$#with identifierreturn.void.return.voidentry forlib/media/load.phpintools/phpstan/baseline.neon.lib/media/load.phpand its unit tests intact withboolreturn value assertions.Testing Instructions
composer phpstanand confirm it passes with no unmatched ignored errors.composer phpstan:baselinesand confirmtools/phpstan/baseline.neonhas no further changes.