Skip to content

Commit

Permalink
BCFile::isReference(): sync with upstream
Browse files Browse the repository at this point in the history
The bugfix for closures declared to return by reference as previously already fixed in the `Operators::isReference()` method, has now also been fixed upstream.

This synces the repo for the upstream fix:
* Apply the fix to the `BCFile::isReference()` method.
* Move the unit test from the test `Diff` file to the `BCFile::isReference()` test case file.

Includes adding a changelog entry to include this in the 1.0.0-alpha3 release.

Refs:
* 142
* squizlabs/php_codesniffer 2977
  • Loading branch information
jrfnl committed Jun 22, 2020
1 parent b97413b commit 0b8265e
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Notes:
* New [`PHPCSUtils\Utils\UseStatements::splitAndMergeImportUseStatement()`](https://phpcsutils.com/phpdoc/classes/PHPCSUtils-Utils-UseStatements.html#method_splitAndMergeImportUseStatement) method. [#117](https://github.com/PHPCSStandards/PHPCSUtils/pull/117)

#### PHPCS Backcompat
* `BCFile::getMethodProperties()`: support for "static" as a return type (PHP 8). [#134](https://github.com/PHPCSStandards/PHPCSUtils/pull/134)
* `BCFile::getMethodProperties()`: support for "static" as a return type (PHP 8). [#134](https://github.com/PHPCSStandards/PHPCSUtils/pull/134) [PHPCS#2952](https://github.com/squizlabs/PHP_CodeSniffer/pull/2952)

#### TestUtils
* [`UtilityMethodTestCase`]: new public `$phpcsVersion` property for use in tests. [#107](https://github.com/PHPCSStandards/PHPCSUtils/pull/107)
Expand Down Expand Up @@ -99,6 +99,7 @@ Notes:

#### PHPCS Backcompat
* `BCFile::findEndOfStatement()`: now supports arrow functions when used as a function argument, in line with the same change made in PHPCS 3.5.5. [#143](https://github.com/PHPCSStandards/PHPCSUtils/pull/143)
* `BcFile::isReference()`: bug fix, the reference operator was not recognized as such for closures declared to return by reference. [#160](https://github.com/PHPCSStandards/PHPCSUtils/pull/160) [PHPCS#2977](https://github.com/squizlabs/PHP_CodeSniffer/pull/2977)

#### Utils
* `FunctionDeclarations::getArrowFunctionOpenClose()`: now supports arrow functions when used as a function argument, in line with the same change made in PHPCS 3.5.5. [#143](https://github.com/PHPCSStandards/PHPCSUtils/pull/143)
Expand Down
3 changes: 3 additions & 0 deletions PHPCSUtils/BackCompat/BCFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,8 @@ public static function getClassProperties(File $phpcsFile, $stackPtr)
* - References to class properties with `self::`, `parent::`, `static::`,
* `namespace\ClassName::`, `classname::` were not recognized as references.
* - PHPCS 3.5.3: Added support for PHP 7.4 `T_FN` arrow functions returning by reference.
* - PHPCS 3.5.6: Bug fix: the reference operator for closures declared to return by reference was
* not recognized as a reference. PHPCS#2977.
*
* @see \PHP_CodeSniffer\Files\File::isReference() Original source.
* @see \PHPCSUtils\Utils\Operators::isReference() PHPCSUtils native improved version.
Expand All @@ -1000,6 +1002,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: 0 additions & 3 deletions PHPCSUtils/Utils/Operators.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ class Operators
* Determine if the passed token is a reference operator.
*
* Main differences with the PHPCS version:
* - Bug fixed: the reference operator for closures 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 Down
3 changes: 3 additions & 0 deletions Tests/BackCompat/BCFile/IsReferenceTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,6 @@ $closure = function() use (&$var){};

/* testArrowFunctionReturnByReference */
fn&($x) => $x;

/* testClosureReturnByReference */
$closure = function &($param) use ($value) {};
4 changes: 4 additions & 0 deletions Tests/BackCompat/BCFile/IsReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ public function dataIsReference()
'/* testArrowFunctionReturnByReference */',
true,
],
[
'/* testClosureReturnByReference */',
true,
],
];
}
}
3 changes: 0 additions & 3 deletions Tests/Utils/Operators/IsReferenceDiffTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,3 @@ if ($foo) {}
/* testTokenizerIssue1284PHPCSlt280C */
if ($foo) {}
[&$a, $b];

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

0 comments on commit 0b8265e

Please sign in to comment.