Skip to content

Fix bugs and adopt PHPCSUtils in AlwaysReturnInFilterSniff#881

Merged
GaryJones merged 1 commit intodevelopfrom
GaryJones/fix-always-return
Mar 22, 2026
Merged

Fix bugs and adopt PHPCSUtils in AlwaysReturnInFilterSniff#881
GaryJones merged 1 commit intodevelopfrom
GaryJones/fix-always-return

Conversation

@GaryJones
Copy link
Copy Markdown
Contributor

Summary

The AlwaysReturnInFilterSniff had a subtle bug in isReturningVoid() where Tokens::$emptyTokens was wrapped in an extra array ([ Tokens::$emptyTokens ]). Since PHPCS's findNext compares each value in the exclusion array against token codes using ===, the nested array never matched anything, meaning no tokens were skipped. This caused return ; (with whitespace before the semicolon) to go undetected as a void return, while return; worked by accident because the next token happened to be T_SEMICOLON.

This PR fixes that bug and makes several related quality improvements:

The duplicate findNext call in processFunction() — where the while condition called it and discarded the result, then the next line called it identically — is collapsed into a single assignment-in-condition pattern, matching the style used elsewhere in the codebase.

Manual name resolution via findNext(Tokens::$emptyTokens, ...) followed by a string content check is replaced with FunctionDeclarations::getName() from PHPCSUtils, and substr($content, 1, -1) for quote stripping is replaced with TextStrings::stripQuotes().

Arrow function (T_FN) callbacks are now handled: since they always implicitly return a value, they are safe as filter callbacks and the sniff now returns early rather than falling through to flag them.

New test cases cover the void return with whitespace bug fix and both arrow function scenarios.

Test plan

  • composer test passes (specifically AlwaysReturnInFilterUnitTest)
  • return ; with whitespace is now correctly detected as a void return
  • Arrow function callbacks (fn($x) => ...) do not trigger false positives

@GaryJones GaryJones requested a review from a team as a code owner March 22, 2026 13:58
The `isReturningVoid()` method wrapped `Tokens::$emptyTokens` in an
extra array, preventing whitespace tokens from being skipped. This
meant `return ;` (with a space) was not detected as a void return.

Additional improvements:
- Eliminate duplicate `findNext()` call in `processFunction()`
- Use `FunctionDeclarations::getName()` for reliable name matching
- Use `TextStrings::stripQuotes()` instead of `substr()` for
  stripping quotes from callback function names
- Handle arrow function (`T_FN`) callbacks, which always return
  a value implicitly

Ref #520
@GaryJones GaryJones force-pushed the GaryJones/fix-always-return branch from f10c972 to 74ba5b6 Compare March 22, 2026 16:18
@GaryJones GaryJones merged commit 208c4c4 into develop Mar 22, 2026
26 checks passed
@GaryJones GaryJones deleted the GaryJones/fix-always-return branch March 22, 2026 16:45
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.

1 participant