Skip to content

Cover return by reference in ImplicitCastSpacing - #83

Merged
dereuromark merged 1 commit into
masterfrom
return-by-reference
Aug 6, 2026
Merged

Cover return by reference in ImplicitCastSpacing#83
dereuromark merged 1 commit into
masterfrom
return-by-reference

Conversation

@dereuromark

Copy link
Copy Markdown
Contributor

The reference marker added in #82 required a variable or a variadic ellipsis on the right, so a return-by-reference declaration slipped through - the thing on the right is a function name:

public function & getItems(): array   // was not reported

That form is now checked first, keyed on the preceding function / function () use / fn keyword rather than on what follows.

Found while retiring psr2r-sniffer's UnaryOperatorSpacing in favour of this sniff. Comparing the two against the same inputs, return by reference was the one construct the PSR2R sniff still reported that this one did not - removing it there would have quietly dropped the check.

The reference marker added in #82 only matched in front of a variable or a
variadic ellipsis, so `function & name()` slipped through - the name is not a
variable. That form is checked first now, keyed on the preceding function,
closure or arrow-function keyword.

Found while retiring psr2r-sniffer's UnaryOperatorSpacing against this rule:
return by reference was the one construct it still reported that this sniff
did not.
Copilot AI lite review requested due to automatic review settings August 6, 2026 14:14
@dereuromark
dereuromark merged commit 709dc26 into master Aug 6, 2026
6 checks passed
@dereuromark
dereuromark deleted the return-by-reference branch August 6, 2026 14:16

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR closes a gap in PhpCollective.WhiteSpace.ImplicitCastSpacing by correctly detecting return-by-reference declarations (e.g., function & getItems()), which previously slipped through because the sniff’s reference detection relied on the token after & being a variable/ellipsis.

Changes:

  • Extend reference-operator detection to treat & immediately following function/closure/fn as a reference marker (return-by-reference) before applying the existing “variable/ellipsis on the right” heuristic.
  • Add a new fixture case covering protected function & byRefReturn(): array and its fixed form protected function &byRefReturn(): array.
  • Update the unit test expectations to account for the additional fixable error.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
PhpCollective/Sniffs/WhiteSpace/ImplicitCastSpacingSniff.php Adds an early return-by-reference detection path keyed off the preceding function token to correctly classify & in function & name() signatures.
tests/_data/ImplicitCastSpacing/before.php Adds a failing fixture for return-by-reference spacing (function & byRefReturn).
tests/_data/ImplicitCastSpacing/after.php Adds the corrected fixture output (function &byRefReturn).
tests/PhpCollective/Sniffs/WhiteSpace/ImplicitCastSpacingSniffTest.php Updates expected fixable error counts from 8 to 9 to include the new case.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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