Warn instead of error for exit/die/throw in filter callbacks#883
Merged
Warn instead of error for exit/die/throw in filter callbacks#883
Conversation
When a filter callback uses exit, die, or throw instead of returning a value, the sniff previously flagged it as MissingReturnStatement. This is technically correct but misleading — the developer has intentionally chosen to terminate execution rather than accidentally forgetting a return. The sniff now distinguishes between these cases: callbacks containing terminating statements receive a TerminatingInsteadOfReturn warning with a more specific message, while genuinely missing returns remain errors. This gives users a distinct error code to suppress when the pattern is intentional, without silently ignoring problematic code. Refs #719.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a filter callback uses
exit,die, orthrowinstead of returning a value, theAlwaysReturnInFiltersniff previously flagged it with theMissingReturnStatementerror code. While technically correct — the function doesn't return a value to the filter chain — this is misleading in practice. Developers who useexitorthrowin a filter callback have made a deliberate choice to terminate execution; they haven't accidentally forgotten areturn.The canonical example is a redirect filter that either returns the URL or calls
wp_redirect()followed byexit. The current sniff treats this identically to a callback that simply forgot to return, which makes the error message unhelpful and forces developers to suppress a generic code that might mask genuine issues elsewhere.This PR introduces a
hasTerminatingStatement()check that scans the function body forT_EXIT(covering bothexitanddie) andT_THROWtokens. When the sniff finds no unconditional return but does find a terminating statement, it now emits aTerminatingInsteadOfReturnwarning with a specific message, rather than the genericMissingReturnStatementerror. Callbacks with genuinely missing returns (no terminating statements either) continue to receive the original error.This gives users a distinct, suppressible code (
WordPressVIPMinimum.Hooks.AlwaysReturnInFilter.TerminatingInsteadOfReturn) for cases where the pattern is intentional, without silently ignoring potentially problematic code.Refs #719.
Test plan
composer testpassesexit/die/throwreceive theTerminatingInsteadOfReturnwarning (not error)MissingReturnStatementerror