Skip to content

Commit

Permalink
File::isReference(): bug fix - arrow function params passed by reference
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jrfnl authored and gsherwood committed Sep 29, 2020
1 parent 9d036f8 commit caffb06
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -1981,6 +1981,7 @@ public function isReference($stackPtr)
$owner = $this->tokens[$this->tokens[$lastBracket]['parenthesis_owner']];
if ($owner['code'] === T_FUNCTION
|| $owner['code'] === T_CLOSURE
|| $owner['code'] === T_FN
) {
$params = $this->getMethodParameters($this->tokens[$lastBracket]['parenthesis_owner']);
foreach ($params as $param) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ function name($a = -1) {}

$a =& $ref;
$a = [ 'a' => &$something ];

$fn = fn(array &$one) => 1;
$fn = fn(array & $one) => 1;
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ function name($a = -1) {}

$a =& $ref;
$a = [ 'a' => &$something ];

$fn = fn(array &$one) => 1;
$fn = fn(array & $one) => 1;
6 changes: 6 additions & 0 deletions tests/Core/File/IsReferenceTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,11 @@ $closure = function() use (&$var){};
/* testArrowFunctionReturnByReference */
fn&($x) => $x;

/* testArrowFunctionPassByReferenceA */
$fn = fn(array &$one) => 1;

/* testArrowFunctionPassByReferenceB */
$fn = fn($param, &...$moreParams) => 1;

/* testClosureReturnByReference */
$closure = function &($param) use ($value) {};
8 changes: 8 additions & 0 deletions tests/Core/File/IsReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ public function dataIsReference()
'/* testArrowFunctionReturnByReference */',
true,
],
[
'/* testArrowFunctionPassByReferenceA */',
true,
],
[
'/* testArrowFunctionPassByReferenceB */',
true,
],
[
'/* testClosureReturnByReference */',
true,
Expand Down

0 comments on commit caffb06

Please sign in to comment.