Skip to content

Commit

Permalink
Collections/various methods: sync with PHPCS
Browse files Browse the repository at this point in the history
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, but that PR has not yet been pulled.

This commit annotates the current 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
  • Loading branch information
jrfnl committed Sep 21, 2020
1 parent 242eac3 commit a5a6ba6
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 5 deletions.
5 changes: 5 additions & 0 deletions PHPCSUtils/Tokens/Collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,11 @@ public static function nameTokens()
\T_STRING => \T_STRING,
];

/*
* PHP >= 8.0 in combination with PHPCS < 3.5.7 and all PHP versions in combination
* with PHPCS >= 3.5.7, though when using PHPCS 3.5.7 < 4.0.0, these tokens are
* not yet in use, i.e. the PHP 8.0 change is "undone" for PHPCS 3.x.
*/
if (\defined('T_NAME_QUALIFIED') === true) {
$tokens[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED;
}
Expand Down
7 changes: 6 additions & 1 deletion Tests/Tokens/Collections/NameTokensTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace PHPCSUtils\Tests\Tokens\Collections;

use PHPCSUtils\BackCompat\Helper;
use PHPCSUtils\Tokens\Collections;
use PHPUnit\Framework\TestCase;

Expand All @@ -32,11 +33,15 @@ class NameTokensTest extends TestCase
*/
public function testNameTokens()
{
$version = Helper::getVersion();
$expected = [
\T_STRING => \T_STRING,
];

if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true) {
if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true
|| (\version_compare($version, '3.5.7', '>=') === true
&& \version_compare($version, '4.0.0', '<') === true)
) {
$expected[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED;
$expected[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED;
$expected[\T_NAME_RELATIVE] = \T_NAME_RELATIVE;
Expand Down
7 changes: 6 additions & 1 deletion Tests/Tokens/Collections/NamespacedNameTokensTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace PHPCSUtils\Tests\Tokens\Collections;

use PHPCSUtils\BackCompat\Helper;
use PHPCSUtils\Tokens\Collections;
use PHPUnit\Framework\TestCase;

Expand All @@ -32,13 +33,17 @@ class NamespacedNameTokensTest extends TestCase
*/
public function testNamespacedNameTokens()
{
$version = Helper::getVersion();
$expected = [
\T_NS_SEPARATOR => \T_NS_SEPARATOR,
\T_NAMESPACE => \T_NAMESPACE,
\T_STRING => \T_STRING,
];

if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true) {
if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true
|| (\version_compare($version, '3.5.7', '>=') === true
&& \version_compare($version, '4.0.0', '<') === true)
) {
$expected[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED;
$expected[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED;
$expected[\T_NAME_RELATIVE] = \T_NAME_RELATIVE;
Expand Down
7 changes: 6 additions & 1 deletion Tests/Tokens/Collections/ParameterTypeTokensTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace PHPCSUtils\Tests\Tokens\Collections;

use PHPCSUtils\BackCompat\Helper;
use PHPCSUtils\Tokens\Collections;
use PHPUnit\Framework\TestCase;

Expand All @@ -32,6 +33,7 @@ class ParameterTypeTokensTest extends TestCase
*/
public function testParameterTypeTokens()
{
$version = Helper::getVersion();
$expected = [
\T_CALLABLE => \T_CALLABLE,
\T_SELF => \T_SELF,
Expand All @@ -44,7 +46,10 @@ public function testParameterTypeTokens()
\T_STRING => \T_STRING,
];

if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true) {
if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true
|| (\version_compare($version, '3.5.7', '>=') === true
&& \version_compare($version, '4.0.0', '<') === true)
) {
$expected[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED;
$expected[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED;
$expected[\T_NAME_RELATIVE] = \T_NAME_RELATIVE;
Expand Down
7 changes: 6 additions & 1 deletion Tests/Tokens/Collections/PropertyTypeTokensTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace PHPCSUtils\Tests\Tokens\Collections;

use PHPCSUtils\BackCompat\Helper;
use PHPCSUtils\Tokens\Collections;
use PHPUnit\Framework\TestCase;

Expand All @@ -32,6 +33,7 @@ class PropertyTypeTokensTest extends TestCase
*/
public function testPropertyTypeTokens()
{
$version = Helper::getVersion();
$expected = [
\T_CALLABLE => \T_CALLABLE,
\T_SELF => \T_SELF,
Expand All @@ -44,7 +46,10 @@ public function testPropertyTypeTokens()
\T_STRING => \T_STRING,
];

if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true) {
if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true
|| (\version_compare($version, '3.5.7', '>=') === true
&& \version_compare($version, '4.0.0', '<') === true)
) {
$expected[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED;
$expected[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED;
$expected[\T_NAME_RELATIVE] = \T_NAME_RELATIVE;
Expand Down
7 changes: 6 additions & 1 deletion Tests/Tokens/Collections/ReturnTypeTokensTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace PHPCSUtils\Tests\Tokens\Collections;

use PHPCSUtils\BackCompat\Helper;
use PHPCSUtils\Tokens\Collections;
use PHPUnit\Framework\TestCase;

Expand All @@ -32,6 +33,7 @@ class ReturnTypeTokensTest extends TestCase
*/
public function testReturnTypeTokens()
{
$version = Helper::getVersion();
$expected = [
\T_CALLABLE => \T_CALLABLE,
\T_SELF => \T_SELF,
Expand All @@ -46,7 +48,10 @@ public function testReturnTypeTokens()
\T_STRING => \T_STRING,
];

if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true) {
if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true
|| (\version_compare($version, '3.5.7', '>=') === true
&& \version_compare($version, '4.0.0', '<') === true)
) {
$expected[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED;
$expected[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED;
$expected[\T_NAME_RELATIVE] = \T_NAME_RELATIVE;
Expand Down

0 comments on commit a5a6ba6

Please sign in to comment.