Skip to content

Commit 880eb36

Browse files
committed
Collections/various methods: sync with PHPCS
Upstream PR 3063 which was merged in PHPCS 3.5.7 effectively "undoes" the PHP 8.0 tokenization for identifier names for PHPCS 3.x, while it also includes backfilling the PHP 8.0 token constants. In PHPCS 4.x the PHP 8.0 tokenization will be backfilled instead. This commit annotates this change in the `Collections::nameTokens()` method and updates the unit tests for various token collections to handle this correctly as well. Refs: * squizlabs/PHP_CodeSniffer#3041 * squizlabs/PHP_CodeSniffer#3063
1 parent 242eac3 commit 880eb36

File tree

6 files changed

+30
-5
lines changed

6 files changed

+30
-5
lines changed

PHPCSUtils/Tokens/Collections.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,11 @@ public static function nameTokens()
620620
\T_STRING => \T_STRING,
621621
];
622622

623+
/*
624+
* PHP >= 8.0 in combination with PHPCS < 3.5.7 and all PHP versions in combination
625+
* with PHPCS >= 3.5.7, though when using PHPCS 3.5.7 < 4.0.0, these tokens are
626+
* not yet in use, i.e. the PHP 8.0 change is "undone" for PHPCS 3.x.
627+
*/
623628
if (\defined('T_NAME_QUALIFIED') === true) {
624629
$tokens[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED;
625630
}

Tests/Tokens/Collections/NameTokensTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace PHPCSUtils\Tests\Tokens\Collections;
1212

13+
use PHPCSUtils\BackCompat\Helper;
1314
use PHPCSUtils\Tokens\Collections;
1415
use PHPUnit\Framework\TestCase;
1516

@@ -32,11 +33,14 @@ class NameTokensTest extends TestCase
3233
*/
3334
public function testNameTokens()
3435
{
36+
$version = Helper::getVersion();
3537
$expected = [
3638
\T_STRING => \T_STRING,
3739
];
3840

39-
if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true) {
41+
if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true
42+
|| \version_compare($version, '3.5.7', '>=') === true
43+
) {
4044
$expected[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED;
4145
$expected[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED;
4246
$expected[\T_NAME_RELATIVE] = \T_NAME_RELATIVE;

Tests/Tokens/Collections/NamespacedNameTokensTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace PHPCSUtils\Tests\Tokens\Collections;
1212

13+
use PHPCSUtils\BackCompat\Helper;
1314
use PHPCSUtils\Tokens\Collections;
1415
use PHPUnit\Framework\TestCase;
1516

@@ -32,13 +33,16 @@ class NamespacedNameTokensTest extends TestCase
3233
*/
3334
public function testNamespacedNameTokens()
3435
{
36+
$version = Helper::getVersion();
3537
$expected = [
3638
\T_NS_SEPARATOR => \T_NS_SEPARATOR,
3739
\T_NAMESPACE => \T_NAMESPACE,
3840
\T_STRING => \T_STRING,
3941
];
4042

41-
if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true) {
43+
if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true
44+
|| \version_compare($version, '3.5.7', '>=') === true
45+
) {
4246
$expected[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED;
4347
$expected[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED;
4448
$expected[\T_NAME_RELATIVE] = \T_NAME_RELATIVE;

Tests/Tokens/Collections/ParameterTypeTokensTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace PHPCSUtils\Tests\Tokens\Collections;
1212

13+
use PHPCSUtils\BackCompat\Helper;
1314
use PHPCSUtils\Tokens\Collections;
1415
use PHPUnit\Framework\TestCase;
1516

@@ -32,6 +33,7 @@ class ParameterTypeTokensTest extends TestCase
3233
*/
3334
public function testParameterTypeTokens()
3435
{
36+
$version = Helper::getVersion();
3537
$expected = [
3638
\T_CALLABLE => \T_CALLABLE,
3739
\T_SELF => \T_SELF,
@@ -44,7 +46,9 @@ public function testParameterTypeTokens()
4446
\T_STRING => \T_STRING,
4547
];
4648

47-
if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true) {
49+
if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true
50+
|| \version_compare($version, '3.5.7', '>=') === true
51+
) {
4852
$expected[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED;
4953
$expected[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED;
5054
$expected[\T_NAME_RELATIVE] = \T_NAME_RELATIVE;

Tests/Tokens/Collections/PropertyTypeTokensTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace PHPCSUtils\Tests\Tokens\Collections;
1212

13+
use PHPCSUtils\BackCompat\Helper;
1314
use PHPCSUtils\Tokens\Collections;
1415
use PHPUnit\Framework\TestCase;
1516

@@ -32,6 +33,7 @@ class PropertyTypeTokensTest extends TestCase
3233
*/
3334
public function testPropertyTypeTokens()
3435
{
36+
$version = Helper::getVersion();
3537
$expected = [
3638
\T_CALLABLE => \T_CALLABLE,
3739
\T_SELF => \T_SELF,
@@ -44,7 +46,9 @@ public function testPropertyTypeTokens()
4446
\T_STRING => \T_STRING,
4547
];
4648

47-
if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true) {
49+
if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true
50+
|| \version_compare($version, '3.5.7', '>=') === true
51+
) {
4852
$expected[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED;
4953
$expected[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED;
5054
$expected[\T_NAME_RELATIVE] = \T_NAME_RELATIVE;

Tests/Tokens/Collections/ReturnTypeTokensTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace PHPCSUtils\Tests\Tokens\Collections;
1212

13+
use PHPCSUtils\BackCompat\Helper;
1314
use PHPCSUtils\Tokens\Collections;
1415
use PHPUnit\Framework\TestCase;
1516

@@ -32,6 +33,7 @@ class ReturnTypeTokensTest extends TestCase
3233
*/
3334
public function testReturnTypeTokens()
3435
{
36+
$version = Helper::getVersion();
3537
$expected = [
3638
\T_CALLABLE => \T_CALLABLE,
3739
\T_SELF => \T_SELF,
@@ -46,7 +48,9 @@ public function testReturnTypeTokens()
4648
\T_STRING => \T_STRING,
4749
];
4850

49-
if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true) {
51+
if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true
52+
|| \version_compare($version, '3.5.7', '>=') === true
53+
) {
5054
$expected[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED;
5155
$expected[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED;
5256
$expected[\T_NAME_RELATIVE] = \T_NAME_RELATIVE;

0 commit comments

Comments
 (0)