Skip to content

Commit

Permalink
Merge pull request #142 from PHPCSStandards/feature/operators-isrefer…
Browse files Browse the repository at this point in the history
…ence-bugfix-closure-return-by-ref

Operators::isReference(): bug fix - closure return by reference not recognized
  • Loading branch information
jrfnl committed May 31, 2020
2 parents c4a63d9 + 66388b8 commit 66a48e9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions PHPCSUtils/Utils/Operators.php
Expand Up @@ -57,6 +57,8 @@ class Operators
* Determine if the passed token is a reference operator.
*
* Main differences with the PHPCS version:
* - Bug fixed: the `&` for a closure declared to return by reference was not recognized as
* a reference. {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/2977 Open PR upstream}
* - Defensive coding against incorrect calls to this method.
* - Improved handling of select tokenizer errors involving short lists/short arrays.
*
Expand All @@ -83,6 +85,7 @@ public static function isReference(File $phpcsFile, $stackPtr)
$tokenBefore = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);

if ($tokens[$tokenBefore]['code'] === \T_FUNCTION
|| $tokens[$tokenBefore]['code'] === T_CLOSURE
|| FunctionDeclarations::isArrowFunction($phpcsFile, $tokenBefore) === true
) {
// Function returns a reference.
Expand Down
3 changes: 3 additions & 0 deletions Tests/Utils/Operators/IsReferenceDiffTest.inc
Expand Up @@ -11,3 +11,6 @@ if ($foo) {}
/* testTokenizerIssue1284PHPCSlt280C */
if ($foo) {}
[&$a, $b];

/* testClosureReturnByReference */
$closure = function &($param) use ($value) {};
4 changes: 4 additions & 0 deletions Tests/Utils/Operators/IsReferenceDiffTest.php
Expand Up @@ -85,6 +85,10 @@ public function dataIsReference()
'/* testTokenizerIssue1284PHPCSlt280C */',
true,
],
'closure-return-by-reference' => [
'/* testClosureReturnByReference */',
true,
],
];
}
}

0 comments on commit 66a48e9

Please sign in to comment.