From 58fe69e2022737b063fe8461fd34836c12e34fc8 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 1 Aug 2020 00:17:26 +0200 Subject: [PATCH 1/2] Collections::objectOperators(): account for the backfill in PHPCS PHPCS has backfilled the PHP 8.0 `T_NULLSAFE_OBJECT_OPERATOR` token. This backfill will be included in PHPCS 3.5.7. --- PHPCSUtils/Tokens/Collections.php | 22 +++++++++---------- .../NullsafeObjectOperatorTokensBCTest.php | 3 +++ .../Collections/ObjectOperatorsBCTest.php | 3 +++ .../Collections/ObjectOperatorsTest.php | 3 +++ 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/PHPCSUtils/Tokens/Collections.php b/PHPCSUtils/Tokens/Collections.php index 38c4e694..f5380726 100644 --- a/PHPCSUtils/Tokens/Collections.php +++ b/PHPCSUtils/Tokens/Collections.php @@ -585,14 +585,14 @@ public static function functionDeclarationTokensBC() * Note: this is a method, not a property as the `T_NULLSAFE_OBJECT_OPERATOR` token may not exist. * * Sister-method to the {@see Collections::objectOperatorsBC()} method. - * This method supports PHP 8.0 and up. - * The {@see Collections::objectOperatorsBC()} method supports PHP < 8.0. + * This method supports PHPCS 3.5.7 and up. + * The {@see Collections::objectOperatorsBC()} method supports PHPCS 2.6.0 and up. * * This method can also safely be used if the token collection is only used when looking back * via `$phpcsFile->findPrevious()` as in that case, a non-backfilled nullsafe object operator * will still match the "normal" object operator. * - * @see \PHPCSUtils\Tokens\Collections::objectOperatorsBC() Related method (PHP < 8.0). + * @see \PHPCSUtils\Tokens\Collections::objectOperatorsBC() Related method (PHPCS 2.6.0+). * * @since 1.0.0-alpha4 * @@ -606,7 +606,7 @@ public static function objectOperators() ]; if (\defined('T_NULLSAFE_OBJECT_OPERATOR') === true) { - // PHP 8.0. + // PHP >= 8.0 or PHPCS >= 3.5.7. $tokens[\T_NULLSAFE_OBJECT_OPERATOR] = \T_NULLSAFE_OBJECT_OPERATOR; } @@ -619,15 +619,15 @@ public static function objectOperators() * Note: this is a method, not a property as the `T_NULLSAFE_OBJECT_OPERATOR` token may not exist. * * Sister-method to the {@see Collections::objectOperators()} method. - * The {@see Collections::objectOperators()} method supports PHP 8.0 and up. - * This method supports PHP < 8.0. + * The {@see Collections::objectOperators()} method supports PHPCS 3.5.7 and up. + * This method supports PHPCS 2.6.0 and up. * * Notable difference: * - This method accounts for tokens which may be encountered when the `T_NULLSAFE_OBJECT_OPERATOR` token * doesn't exist. * * It is recommended to use the {@see Collections::objectOperators()} method instead of - * this method if a standard does not need to support PHP < 8.0. + * this method if a standard does not need to support PHPCS < 3.5.7. * * The {@see Collections::objectOperators()} method can also safely be used if the token collection * is only used when looking back via `$phpcsFile->findPrevious()` as in that case, a non-backfilled @@ -637,11 +637,11 @@ public static function objectOperators() * method needs to be used on potential nullsafe object operator tokens to verify whether it really * is a nullsafe object operator or not. * - * @see \PHPCSUtils\Tokens\Collections::objectOperators() Related method (PHP 8.0+). + * @see \PHPCSUtils\Tokens\Collections::objectOperators() Related method (PHPCS 3.5.7+). * @see \PHPCSUtils\Tokens\Collections::nullsafeObjectOperatorBC() Tokens which can represent a * nullsafe object operator. * @see \PHPCSUtils\Utils\Operators::isNullsafeObjectOperator() Nullsafe object operator detection for - * PHP < 8.0. + * PHPCS < 3.5.7. * * @since 1.0.0-alpha4 * @@ -671,7 +671,7 @@ public static function objectOperatorsBC() * is a nullsafe object operator or not. * * @see \PHPCSUtils\Utils\Operators::isNullsafeObjectOperator() Nullsafe object operator detection for - * PHP < 8.0. + * PHPCS < 3.5.7. * * @since 1.0.0-alpha4 * @@ -680,7 +680,7 @@ public static function objectOperatorsBC() public static function nullsafeObjectOperatorBC() { if (\defined('T_NULLSAFE_OBJECT_OPERATOR') === true) { - // PHP 8.0. + // PHP >= 8.0 / PHPCS >= 3.5.7. return [ \T_NULLSAFE_OBJECT_OPERATOR => \T_NULLSAFE_OBJECT_OPERATOR, ]; diff --git a/Tests/Tokens/Collections/NullsafeObjectOperatorTokensBCTest.php b/Tests/Tokens/Collections/NullsafeObjectOperatorTokensBCTest.php index 80bfc755..057c7620 100644 --- a/Tests/Tokens/Collections/NullsafeObjectOperatorTokensBCTest.php +++ b/Tests/Tokens/Collections/NullsafeObjectOperatorTokensBCTest.php @@ -10,6 +10,7 @@ namespace PHPCSUtils\Tests\Tokens\Collections; +use PHPCSUtils\BackCompat\Helper; use PHPCSUtils\Tokens\Collections; use PHPUnit\Framework\TestCase; @@ -32,8 +33,10 @@ class NullsafeObjectOperatorBCTest extends TestCase */ public function testNullsafeObjectOperatorBC() { + $version = Helper::getVersion(); $expected = []; if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true + || \version_compare($version, '3.5.7', '>=') === true ) { $expected = [ \T_NULLSAFE_OBJECT_OPERATOR => \T_NULLSAFE_OBJECT_OPERATOR, diff --git a/Tests/Tokens/Collections/ObjectOperatorsBCTest.php b/Tests/Tokens/Collections/ObjectOperatorsBCTest.php index f8204099..a9ec6c96 100644 --- a/Tests/Tokens/Collections/ObjectOperatorsBCTest.php +++ b/Tests/Tokens/Collections/ObjectOperatorsBCTest.php @@ -10,6 +10,7 @@ namespace PHPCSUtils\Tests\Tokens\Collections; +use PHPCSUtils\BackCompat\Helper; use PHPCSUtils\Tokens\Collections; use PHPUnit\Framework\TestCase; @@ -32,12 +33,14 @@ class ObjectOperatorsBCTest extends TestCase */ public function testObjectOperatorsBC() { + $version = Helper::getVersion(); $expected = [ \T_OBJECT_OPERATOR => \T_OBJECT_OPERATOR, \T_DOUBLE_COLON => \T_DOUBLE_COLON, ]; if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true + || \version_compare($version, '3.5.7', '>=') === true ) { $expected[\T_NULLSAFE_OBJECT_OPERATOR] = \T_NULLSAFE_OBJECT_OPERATOR; } else { diff --git a/Tests/Tokens/Collections/ObjectOperatorsTest.php b/Tests/Tokens/Collections/ObjectOperatorsTest.php index a6f8c591..da2dfb33 100644 --- a/Tests/Tokens/Collections/ObjectOperatorsTest.php +++ b/Tests/Tokens/Collections/ObjectOperatorsTest.php @@ -10,6 +10,7 @@ namespace PHPCSUtils\Tests\Tokens\Collections; +use PHPCSUtils\BackCompat\Helper; use PHPCSUtils\Tokens\Collections; use PHPUnit\Framework\TestCase; @@ -32,12 +33,14 @@ class ObjectOperatorsTest extends TestCase */ public function testObjectOperators() { + $version = Helper::getVersion(); $expected = [ \T_OBJECT_OPERATOR => \T_OBJECT_OPERATOR, \T_DOUBLE_COLON => \T_DOUBLE_COLON, ]; if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true + || \version_compare($version, '3.5.7', '>=') === true ) { $expected[\T_NULLSAFE_OBJECT_OPERATOR] = \T_NULLSAFE_OBJECT_OPERATOR; } From 32ab208a4647f179df24fd3455bbcf3455cd7896 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 1 Aug 2020 00:22:37 +0200 Subject: [PATCH 2/2] Operators::isNullsafeObjectOperator(): document the backfill in PHPCS PHPCS has backfilled the PHP 8.0 `T_NULLSAFE_OBJECT_OPERATOR` token. This backfill will be included in PHPCS 3.5.7. --- PHPCSUtils/Utils/Operators.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/PHPCSUtils/Utils/Operators.php b/PHPCSUtils/Utils/Operators.php index 0672737e..42e851d5 100644 --- a/PHPCSUtils/Utils/Operators.php +++ b/PHPCSUtils/Utils/Operators.php @@ -316,6 +316,7 @@ public static function isTypeUnion(File $phpcsFile, $stackPtr) * * Helper method for PHP < 8.0 in combination with PHPCS versions in which the * `T_NULLSAFE_OBJECT_OPERATOR` token is not yet backfilled. + * PHPCS backfills the token as of PHPCS 3.5.7. * * @since 1.0.0-alpha4 * @@ -332,7 +333,10 @@ public static function isNullsafeObjectOperator(File $phpcsFile, $stackPtr) return false; } - // Safeguard in case this method is used on PHP 8 and the nullsafe object operator would be passed. + /* + * Safeguard in case this method is used on PHP >= 8.0 or PHPCS >= 3.5.7 + * and the nullsafe object operator would be passed. + */ if ($tokens[$stackPtr]['type'] === 'T_NULLSAFE_OBJECT_OPERATOR') { return true; }