Skip to content

Commit caffb06

Browse files
jrfnlgsherwood
authored andcommitted
File::isReference(): bug fix - arrow function params passed by reference
The `&` for parameters passed by reference in an arrow function declaration were incorrectly not recognized as references. This is the root cause of issue 3049, which affected the `PSR12.Operators.OperatorSpacing` and the `Squiz.WhiteSpace.OperatorSpacing` sniffs (and possibly/probably more sniffs). Includes unit test. Fixes 3049
1 parent 9d036f8 commit caffb06

File tree

5 files changed

+21
-0
lines changed

5 files changed

+21
-0
lines changed

src/Files/File.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,7 @@ public function isReference($stackPtr)
19811981
$owner = $this->tokens[$this->tokens[$lastBracket]['parenthesis_owner']];
19821982
if ($owner['code'] === T_FUNCTION
19831983
|| $owner['code'] === T_CLOSURE
1984+
|| $owner['code'] === T_FN
19841985
) {
19851986
$params = $this->getMethodParameters($this->tokens[$lastBracket]['parenthesis_owner']);
19861987
foreach ($params as $param) {

src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@ function name($a = -1) {}
5656

5757
$a =& $ref;
5858
$a = [ 'a' => &$something ];
59+
60+
$fn = fn(array &$one) => 1;
61+
$fn = fn(array & $one) => 1;

src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@ function name($a = -1) {}
5656

5757
$a =& $ref;
5858
$a = [ 'a' => &$something ];
59+
60+
$fn = fn(array &$one) => 1;
61+
$fn = fn(array & $one) => 1;

tests/Core/File/IsReferenceTest.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,11 @@ $closure = function() use (&$var){};
140140
/* testArrowFunctionReturnByReference */
141141
fn&($x) => $x;
142142

143+
/* testArrowFunctionPassByReferenceA */
144+
$fn = fn(array &$one) => 1;
145+
146+
/* testArrowFunctionPassByReferenceB */
147+
$fn = fn($param, &...$moreParams) => 1;
148+
143149
/* testClosureReturnByReference */
144150
$closure = function &($param) use ($value) {};

tests/Core/File/IsReferenceTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,14 @@ public function dataIsReference()
228228
'/* testArrowFunctionReturnByReference */',
229229
true,
230230
],
231+
[
232+
'/* testArrowFunctionPassByReferenceA */',
233+
true,
234+
],
235+
[
236+
'/* testArrowFunctionPassByReferenceB */',
237+
true,
238+
],
231239
[
232240
'/* testClosureReturnByReference */',
233241
true,

0 commit comments

Comments
 (0)