Skip to content

Commit

Permalink
Merge 0587e5a into 1065c1b
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfnl committed Sep 5, 2020
2 parents 1065c1b + 0587e5a commit 331f8e1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
6 changes: 4 additions & 2 deletions PHPCSUtils/Utils/Operators.php
Expand Up @@ -66,6 +66,7 @@ class Operators
* Main differences with the PHPCS version:
* - Defensive coding against incorrect calls to this method.
* - Improved handling of select tokenizer errors involving short lists/short arrays.
* - Parameters passed by reference in arrow functions are recognized correctly.
*
* @see \PHP_CodeSniffer\Files\File::isReference() Original source.
* @see \PHPCSUtils\BackCompat\BCFile::isReference() Cross-version compatible version of the original.
Expand Down Expand Up @@ -121,8 +122,9 @@ public static function isReference(File $phpcsFile, $stackPtr)

$lastOpener = Parentheses::getLastOpener($phpcsFile, $stackPtr);
if ($lastOpener !== false) {
$lastOwner = Parentheses::lastOwnerIn($phpcsFile, $stackPtr, [\T_FUNCTION, \T_CLOSURE]);
if ($lastOwner !== false) {
$lastOwner = Parentheses::getOwner($phpcsFile, $lastOpener);

if (isset(Collections::functionDeclarationTokensBC()[$tokens[$lastOwner]['code']]) === true) {
$params = FunctionDeclarations::getParameters($phpcsFile, $lastOwner);
foreach ($params as $param) {
if ($param['reference_token'] === $stackPtr) {
Expand Down
9 changes: 9 additions & 0 deletions Tests/Utils/Operators/IsReferenceDiffTest.inc
Expand Up @@ -11,3 +11,12 @@ if ($foo) {}
/* testTokenizerIssue1284PHPCSlt280C */
if ($foo) {}
[&$a, $b];

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

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

/* testArrowFunctionNonReferenceInDefault */
$fn = fn( $one = E_NOTICE & E_STRICT) => 1;
12 changes: 12 additions & 0 deletions Tests/Utils/Operators/IsReferenceDiffTest.php
Expand Up @@ -85,6 +85,18 @@ public function dataIsReference()
'/* testTokenizerIssue1284PHPCSlt280C */',
true,
],
'issue-3049-arrow-function-pass-by-reference-A' => [
'/* testArrowFunctionPassByReferenceA */',
true,
],
'issue-3049-arrow-function-pass-by-reference-B' => [
'/* testArrowFunctionPassByReferenceB */',
true,
],
'issue-3049-arrow-function-parameter-default' => [
'/* testArrowFunctionNonReferenceInDefault */',
false,
],
];
}
}

0 comments on commit 331f8e1

Please sign in to comment.