Cover the reference operator in ImplicitCastSpacing - #82
Merged
Conversation
The sniff already owned this shape for `!`, `@` and unary minus, so the reference marker joins it rather than arriving as a separate sniff. That also lets psr2r-sniffer, which enables this rule already, drop its own UnaryOperatorSpacing: the reference marker was the only thing it still covered that nothing here did. Telling a reference from a bitwise and needs more than the preceding token. A type hint precedes the marker in `function f(array & $items)`, and a type hint reads exactly like the left operand of a bitwise and. So a reference is one that stands in front of a variable, or the ellipsis of a by-reference variadic, and either follows something that cannot end a value or sits in a parameter list. Requiring the variable on the right keeps a default such as `function f(int $x = self::A & self::B)` out of it. Covered by fixtures: plain `$a = & $list`, by-reference foreach in both forms, a typed parameter, a variadic, and the bitwise and boolean operators that must stay untouched.
There was a problem hiding this comment.
Pull request overview
Extends PhpCollective.WhiteSpace.ImplicitCastSpacing to also enforce no whitespace after the reference marker (&) when it is used as a by-reference operator, while leaving bitwise & spacing untouched.
Changes:
- Registers
T_BITWISE_ANDand adds logic to treat&as an implicit-cast-style operator only when it’s a reference marker. - Introduces
isReferenceOperator()to distinguish reference&from bitwise&(including parameter-list handling). - Expands the fixture and updates expected fixable error counts accordingly.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| PhpCollective/Sniffs/WhiteSpace/ImplicitCastSpacingSniff.php | Adds & handling with reference-vs-bitwise disambiguation. |
| tests/PhpCollective/Sniffs/WhiteSpace/ImplicitCastSpacingSniffTest.php | Updates expected error/fix counts to match new fixture cases. |
| tests/_data/ImplicitCastSpacing/before.php | Adds reference-operator spacing violations to be detected/fixed. |
| tests/_data/ImplicitCastSpacing/after.php | Adds the expected fixed output for the new reference-operator cases. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+26
to
+33
| $reference = & $items; | ||
| $validReference = &$items; | ||
| $validBitwiseAnd = $count & $mask; | ||
|
|
||
| foreach ($items as & $item) { | ||
| $item = (int)$item; | ||
| } | ||
| unset($item); |
dereuromark
added a commit
that referenced
this pull request
Aug 6, 2026
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.
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.
- $a,! $band@ f()are already handled byPhpCollective.WhiteSpace.ImplicitCastSpacing. The reference marker is the same shape, so it joins that sniff rather than arriving as a new one.This closes the last gap against
psr2r-sniffer'sUnaryOperatorSpacing, which can now be deleted there -&was the only construct it still covered that this standard did not:- $a! $b@ f()& $listReference or bitwise and
The preceding token is not enough on its own. In
function f(array & $items)the marker follows a type hint, and a type hint looks exactly like the left operand of a bitwise and.So a reference is a
&that stands in front of a variable - or the ellipsis of a by-reference variadic - and either follows something that cannot end a value, or sits inside a parameter list. Requiring the variable on the right is what keeps a default value out of it:Every one of those is in the fixture.