From 4dbdd3a5032b0be19ddfd242cca8ddba1fde3637 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 21 Sep 2020 02:33:39 +0200 Subject: [PATCH 1/4] PHPCS 4.x | Collections/various methods: sync with PHPCS Upstream PR 3155 for PHPCS 4.x backfills the PHP 8.0 tokenization for identifier names for older PHP versions, including backfilling the PHP 8.0 token constants. This commit updates the unit tests for various token collections to handle this correctly. Refs: * squizlabs/PHP_CodeSniffer 3041 * squizlabs/PHP_CodeSniffer 3155 --- Tests/Tokens/Collections/FunctionCallTokensTest.php | 3 +-- Tests/Tokens/Collections/NameTokensTest.php | 3 +-- Tests/Tokens/Collections/NamespacedNameTokensTest.php | 3 +-- Tests/Tokens/Collections/ParameterPassingTokensTest.php | 3 +-- Tests/Tokens/Collections/ParameterTypeTokensTest.php | 3 +-- Tests/Tokens/Collections/PropertyTypeTokensTest.php | 3 +-- Tests/Tokens/Collections/ReturnTypeTokensTest.php | 3 +-- 7 files changed, 7 insertions(+), 14 deletions(-) diff --git a/Tests/Tokens/Collections/FunctionCallTokensTest.php b/Tests/Tokens/Collections/FunctionCallTokensTest.php index 82c26660..74fac21f 100644 --- a/Tests/Tokens/Collections/FunctionCallTokensTest.php +++ b/Tests/Tokens/Collections/FunctionCallTokensTest.php @@ -39,8 +39,7 @@ public function testFunctionCallTokens() ]; if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true - || (\version_compare($version, '3.5.7', '>=') === true - && \version_compare($version, '4.0.0', '<') === true) + || \version_compare($version, '3.5.7', '>=') === true ) { $expected[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED; $expected[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED; diff --git a/Tests/Tokens/Collections/NameTokensTest.php b/Tests/Tokens/Collections/NameTokensTest.php index 8beecb07..9bb2ad3e 100644 --- a/Tests/Tokens/Collections/NameTokensTest.php +++ b/Tests/Tokens/Collections/NameTokensTest.php @@ -39,8 +39,7 @@ public function testNameTokens() ]; if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true - || (\version_compare($version, '3.5.7', '>=') === true - && \version_compare($version, '4.0.0', '<') === true) + || \version_compare($version, '3.5.7', '>=') === true ) { $expected[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED; $expected[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED; diff --git a/Tests/Tokens/Collections/NamespacedNameTokensTest.php b/Tests/Tokens/Collections/NamespacedNameTokensTest.php index 185744fa..d99be896 100644 --- a/Tests/Tokens/Collections/NamespacedNameTokensTest.php +++ b/Tests/Tokens/Collections/NamespacedNameTokensTest.php @@ -41,8 +41,7 @@ public function testNamespacedNameTokens() ]; if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true - || (\version_compare($version, '3.5.7', '>=') === true - && \version_compare($version, '4.0.0', '<') === true) + || \version_compare($version, '3.5.7', '>=') === true ) { $expected[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED; $expected[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED; diff --git a/Tests/Tokens/Collections/ParameterPassingTokensTest.php b/Tests/Tokens/Collections/ParameterPassingTokensTest.php index e2b0d88c..64230b6a 100644 --- a/Tests/Tokens/Collections/ParameterPassingTokensTest.php +++ b/Tests/Tokens/Collections/ParameterPassingTokensTest.php @@ -39,8 +39,7 @@ public function testParameterPassingTokens() ]; if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true - || (\version_compare($version, '3.5.7', '>=') === true - && \version_compare($version, '4.0.0', '<') === true) + || \version_compare($version, '3.5.7', '>=') === true ) { $expected[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED; $expected[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED; diff --git a/Tests/Tokens/Collections/ParameterTypeTokensTest.php b/Tests/Tokens/Collections/ParameterTypeTokensTest.php index 20acb6c3..70344939 100644 --- a/Tests/Tokens/Collections/ParameterTypeTokensTest.php +++ b/Tests/Tokens/Collections/ParameterTypeTokensTest.php @@ -47,8 +47,7 @@ public function testParameterTypeTokens() ]; if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true - || (\version_compare($version, '3.5.7', '>=') === true - && \version_compare($version, '4.0.0', '<') === true) + || \version_compare($version, '3.5.7', '>=') === true ) { $expected[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED; $expected[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED; diff --git a/Tests/Tokens/Collections/PropertyTypeTokensTest.php b/Tests/Tokens/Collections/PropertyTypeTokensTest.php index 988dec9a..39833723 100644 --- a/Tests/Tokens/Collections/PropertyTypeTokensTest.php +++ b/Tests/Tokens/Collections/PropertyTypeTokensTest.php @@ -47,8 +47,7 @@ public function testPropertyTypeTokens() ]; if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true - || (\version_compare($version, '3.5.7', '>=') === true - && \version_compare($version, '4.0.0', '<') === true) + || \version_compare($version, '3.5.7', '>=') === true ) { $expected[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED; $expected[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED; diff --git a/Tests/Tokens/Collections/ReturnTypeTokensTest.php b/Tests/Tokens/Collections/ReturnTypeTokensTest.php index f70168f0..73095983 100644 --- a/Tests/Tokens/Collections/ReturnTypeTokensTest.php +++ b/Tests/Tokens/Collections/ReturnTypeTokensTest.php @@ -49,8 +49,7 @@ public function testReturnTypeTokens() ]; if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true - || (\version_compare($version, '3.5.7', '>=') === true - && \version_compare($version, '4.0.0', '<') === true) + || \version_compare($version, '3.5.7', '>=') === true ) { $expected[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED; $expected[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED; From e233c15c89f88878bdeb44c7ae47336678f2f7ff Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 10 Sep 2020 00:13:19 +0200 Subject: [PATCH 2/4] UtilityMethodTestCase::usesPhp8NameTokens(): account for PHPCS 4.x backfilling the PHP 8.0 identifier name tokenization Upstream PR 3155 for PHPCS 4.x backfills the PHP 8.0 tokenization for identifier names for older PHP versions. This updates the `UtilityMethodTestCase::usesPhp8NameTokens()` method to take this upstream change into account. --- PHPCSUtils/TestUtils/UtilityMethodTestCase.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/PHPCSUtils/TestUtils/UtilityMethodTestCase.php b/PHPCSUtils/TestUtils/UtilityMethodTestCase.php index 4259544a..21b0e195 100644 --- a/PHPCSUtils/TestUtils/UtilityMethodTestCase.php +++ b/PHPCSUtils/TestUtils/UtilityMethodTestCase.php @@ -330,9 +330,9 @@ public static function resetTestFile() public static function usesPhp8NameTokens() { $version = Helper::getVersion(); - if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true - && (\version_compare($version, '3.5.7', '<') === true - || \version_compare($version, '4.0.0', '>=') === true) + if ((\version_compare(\PHP_VERSION_ID, '80000', '>=') === true + && \version_compare($version, '3.5.7', '<') === true) + || \version_compare($version, '4.0.0', '>=') === true ) { return true; } From 4837a10172d87355724876fd841b3a3a32a84c98 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 11 Sep 2020 21:20:49 +0200 Subject: [PATCH 3/4] BCTokens::functionNameTokens(): account for identifier name tokens as added in PHPCS 4.x Upstream PR 3155 for PHPCS 4.x adds the PHP 8.0 tokens for identifier names to the `Tokens::$functionNameTokens` property. This updates the `BCTokens::functionNameTokens()` method to take this upstream change into account. Includes unit tests. --- PHPCSUtils/BackCompat/BCTokens.php | 2 + .../BCTokens/FunctionNameTokensTest.php | 43 ++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/PHPCSUtils/BackCompat/BCTokens.php b/PHPCSUtils/BackCompat/BCTokens.php index df41e850..0de817d8 100644 --- a/PHPCSUtils/BackCompat/BCTokens.php +++ b/PHPCSUtils/BackCompat/BCTokens.php @@ -358,6 +358,7 @@ public static function textStringTokens() * Changelog for the PHPCS native array: * - Introduced in PHPCS 2.3.3. * - PHPCS 3.1.0: `T_SELF` and `T_STATIC` added to the array. + * - PHPCS 4.0.0: `T_NAME_QUALIFIED`, `T_NAME_FULLY_QUALIFIED` and `T_NAME_RELATIVE` added to the array. * * @see \PHP_CodeSniffer\Util\Tokens::$functionNameTokens Original array. * @@ -370,6 +371,7 @@ public static function functionNameTokens() $tokens = Tokens::$functionNameTokens; $tokens[\T_SELF] = \T_SELF; $tokens[\T_STATIC] = \T_STATIC; + $tokens += Collections::nameTokens(); return $tokens; } diff --git a/Tests/BackCompat/BCTokens/FunctionNameTokensTest.php b/Tests/BackCompat/BCTokens/FunctionNameTokensTest.php index b8fcd426..f302a698 100644 --- a/Tests/BackCompat/BCTokens/FunctionNameTokensTest.php +++ b/Tests/BackCompat/BCTokens/FunctionNameTokensTest.php @@ -12,6 +12,7 @@ use PHP_CodeSniffer\Util\Tokens; use PHPCSUtils\BackCompat\BCTokens; +use PHPCSUtils\BackCompat\Helper; use PHPUnit\Framework\TestCase; /** @@ -33,6 +34,7 @@ class FunctionNameTokensTest extends TestCase */ public function testFunctionNameTokens() { + $version = Helper::getVersion(); $expected = [ \T_STRING => \T_STRING, \T_EVAL => \T_EVAL, @@ -48,7 +50,20 @@ public function testFunctionNameTokens() \T_STATIC => \T_STATIC, ]; - $this->assertSame($expected, BCTokens::functionNameTokens()); + if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true + || \version_compare($version, '3.5.7', '>=') === true + ) { + $expected[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED; + $expected[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED; + $expected[\T_NAME_RELATIVE] = \T_NAME_RELATIVE; + } + + \asort($expected); + + $result = BCTokens::functionNameTokens(); + \asort($result); + + $this->assertSame($expected, $result); } /** @@ -62,6 +77,30 @@ public function testFunctionNameTokens() */ public function testPHPCSFunctionNameTokens() { - $this->assertSame(Tokens::$functionNameTokens, BCTokens::functionNameTokens()); + $version = Helper::getVersion(); + + if (\version_compare($version, '3.99.99', '>') === true) { + $this->assertSame(Tokens::$functionNameTokens, BCTokens::functionNameTokens()); + } else { + /* + * Don't fail this test on the difference between PHPCS 4.x and 3.x. + * This test is only run against `dev-master` and `dev-master` is still PHPCS 3.x. + */ + $expected = Tokens::$functionNameTokens; + if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true + || \version_compare($version, '3.5.7', '>=') === true + ) { + $expected[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED; + $expected[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED; + $expected[\T_NAME_RELATIVE] = \T_NAME_RELATIVE; + } + + \asort($expected); + + $result = BCTokens::functionNameTokens(); + \asort($result); + + $this->assertSame($expected, $result); + } } } From 5c69cbba81c1899c936389e1287b3f7ff124f76e Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 22 Dec 2020 20:41:09 +0100 Subject: [PATCH 4/4] Tests: fix two tests for PHP 8 identifier names * One test didn't yet take PHP 8 identifier names into account. * The other did, but contained a typo/count error. --- Tests/BackCompat/BCFile/GetMethodPropertiesTest.php | 4 +++- Tests/Utils/PassedParameters/GetParametersNamedTest.php | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Tests/BackCompat/BCFile/GetMethodPropertiesTest.php b/Tests/BackCompat/BCFile/GetMethodPropertiesTest.php index 1dd8d3dd..ec24a9dd 100644 --- a/Tests/BackCompat/BCFile/GetMethodPropertiesTest.php +++ b/Tests/BackCompat/BCFile/GetMethodPropertiesTest.php @@ -341,12 +341,14 @@ public function testReturnNamespace() */ public function testReturnMultilineNamespace() { + $php8Names = parent::usesPhp8NameTokens(); + $expected = [ 'scope' => 'public', 'scope_specified' => false, 'return_type' => '\MyNamespace\MyClass\Foo', 'return_type_token' => 7, // Offset from the T_FUNCTION token. - 'return_type_end_token' => 23, // Offset from the T_FUNCTION token. + 'return_type_end_token' => ($php8Names === true) ? 20 : 23, // Offset from the T_FUNCTION token. 'nullable_return_type' => false, 'is_abstract' => false, 'is_final' => false, diff --git a/Tests/Utils/PassedParameters/GetParametersNamedTest.php b/Tests/Utils/PassedParameters/GetParametersNamedTest.php index 3b28667b..ecbe66ce 100644 --- a/Tests/Utils/PassedParameters/GetParametersNamedTest.php +++ b/Tests/Utils/PassedParameters/GetParametersNamedTest.php @@ -109,7 +109,7 @@ public function dataGetParameters() ], 3 => [ 'start' => ($php8Names === true) ? 7 : 8, - 'end' => ($php8Names === true) ? 7 : 11, + 'end' => ($php8Names === true) ? 8 : 11, 'raw' => 'MyNS\VALUE', ], ],