Skip to content

Commit

Permalink
Implement
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfnl committed Apr 11, 2022
1 parent ef0a74e commit 6b3e66d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 17 deletions.
13 changes: 7 additions & 6 deletions PHPCSUtils/BackCompat/BCTokens.php
Expand Up @@ -12,6 +12,7 @@

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Tokens\Collections;
use PHPCSUtils\Tokens\TokenHelper;

/**
* Token arrays related utility methods.
Expand Down Expand Up @@ -158,7 +159,7 @@ public static function assignmentTokens()
* The `T_COALESCE_EQUAL` token may be available pre-PHPCS 2.8.1 depending on
* the PHP version used to run PHPCS.
*/
if (\defined('T_COALESCE_EQUAL')) {
if (TokenHelper::tokenExists('T_COALESCE_EQUAL')) {
$tokens[\T_COALESCE_EQUAL] = \T_COALESCE_EQUAL;
}

Expand Down Expand Up @@ -250,7 +251,7 @@ public static function operators()
$tokens[\T_COALESCE] = \T_COALESCE;
}

if (\defined('T_COALESCE_EQUAL')) {
if (TokenHelper::tokenExists('T_COALESCE_EQUAL')) {
unset($tokens[\T_COALESCE_EQUAL]);
}

Expand Down Expand Up @@ -292,7 +293,7 @@ public static function parenthesisOpeners()
* The `T_MATCH` token may be available pre-PHPCS 3.6.0 depending on the PHP version
* used to run PHPCS.
*/
if (\defined('T_MATCH')) {
if (TokenHelper::tokenExists('T_MATCH')) {
$tokens[\T_MATCH] = \T_MATCH;
}

Expand Down Expand Up @@ -321,15 +322,15 @@ public static function scopeOpeners()
* The `T_MATCH` token may be available pre-PHPCS 3.6.0 depending on the PHP version
* used to run PHPCS.
*/
if (\defined('T_MATCH')) {
if (TokenHelper::tokenExists('T_MATCH')) {
$tokens[\T_MATCH] = \T_MATCH;
}

/*
* The `T_ENUM` token may be available pre-PHPCS 3.7.0 depending on the PHP version
* used to run PHPCS.
*/
if (\defined('T_ENUM')) {
if (TokenHelper::tokenExists('T_ENUM')) {
$tokens[\T_ENUM] = \T_ENUM;
}

Expand Down Expand Up @@ -451,7 +452,7 @@ public static function ooScopeTokens()
* The `T_ENUM` token may be available pre-PHPCS 3.7.0 depending on the PHP version
* used to run PHPCS.
*/
if (\defined('T_ENUM')) {
if (TokenHelper::tokenExists('T_ENUM')) {
$tokens[\T_ENUM] = \T_ENUM;
}

Expand Down
16 changes: 9 additions & 7 deletions PHPCSUtils/Tokens/Collections.php
Expand Up @@ -10,6 +10,8 @@

namespace PHPCSUtils\Tokens;

use PHPCSUtils\Tokens\TokenHelper;

/**
* Collections of related tokens as often used and needed for sniffs.
*
Expand Down Expand Up @@ -516,7 +518,7 @@ public static function arrowFunctionTokensBC()
\T_STRING => \T_STRING,
];

if (\defined('T_FN') === true) {
if (TokenHelper::tokenExists('T_FN') === true) {
// PHP 7.4 or PHPCS 3.5.3+.
$tokens[\T_FN] = \T_FN;
}
Expand Down Expand Up @@ -569,7 +571,7 @@ public static function functionDeclarationTokens()
\T_CLOSURE => \T_CLOSURE,
];

if (\defined('T_FN') === true) {
if (TokenHelper::tokenExists('T_FN') === true) {
// PHP 7.4 or PHPCS 3.5.3+.
$tokens[\T_FN] = \T_FN;
}
Expand Down Expand Up @@ -663,15 +665,15 @@ public static function nameTokens()
* 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) {
if (TokenHelper::tokenExists('T_NAME_QUALIFIED') === true) {
$tokens[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED;
}

if (\defined('T_NAME_FULLY_QUALIFIED') === true) {
if (TokenHelper::tokenExists('T_NAME_FULLY_QUALIFIED') === true) {
$tokens[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED;
}

if (\defined('T_NAME_RELATIVE') === true) {
if (TokenHelper::tokenExists('T_NAME_RELATIVE') === true) {
$tokens[\T_NAME_RELATIVE] = \T_NAME_RELATIVE;
}

Expand Down Expand Up @@ -704,7 +706,7 @@ public static function objectOperators()
\T_DOUBLE_COLON => \T_DOUBLE_COLON,
];

if (\defined('T_NULLSAFE_OBJECT_OPERATOR') === true) {
if (TokenHelper::tokenExists('T_NULLSAFE_OBJECT_OPERATOR') === true) {
// PHP >= 8.0 or PHPCS >= 3.5.7.
$tokens[\T_NULLSAFE_OBJECT_OPERATOR] = \T_NULLSAFE_OBJECT_OPERATOR;
}
Expand Down Expand Up @@ -778,7 +780,7 @@ public static function objectOperatorsBC()
*/
public static function nullsafeObjectOperatorBC()
{
if (\defined('T_NULLSAFE_OBJECT_OPERATOR') === true) {
if (TokenHelper::tokenExists('T_NULLSAFE_OBJECT_OPERATOR') === true) {
// PHP >= 8.0 / PHPCS >= 3.5.7.
return [
\T_NULLSAFE_OBJECT_OPERATOR => \T_NULLSAFE_OBJECT_OPERATOR,
Expand Down
3 changes: 2 additions & 1 deletion PHPCSUtils/Utils/Parentheses.php
Expand Up @@ -13,6 +13,7 @@
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Tokens\Collections;
use PHPCSUtils\Tokens\TokenHelper;
use PHPCSUtils\Utils\FunctionDeclarations;

/**
Expand Down Expand Up @@ -146,7 +147,7 @@ public static function isOwnerIn(File $phpcsFile, $stackPtr, $validOwners)
/*
* Allow for T_FN token being tokenized as T_STRING before PHPCS 3.5.3.
*/
if (\defined('T_FN') && \in_array(\T_FN, $validOwners, true)) {
if (TokenHelper::tokenExists('T_FN') && \in_array(\T_FN, $validOwners, true)) {
$validOwners += Collections::arrowFunctionTokensBC();
}

Expand Down
3 changes: 2 additions & 1 deletion Tests/Utils/Operators/IsNullsafeObjectOperatorTest.php
Expand Up @@ -12,6 +12,7 @@

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Tokens\TokenHelper;
use PHPCSUtils\Utils\Operators;

/**
Expand Down Expand Up @@ -147,7 +148,7 @@ private function getTargetTokensTypes()
\T_INLINE_THEN,
];

if (\defined('T_NULLSAFE_OBJECT_OPERATOR') === true) {
if (TokenHelper::tokenExists('T_NULLSAFE_OBJECT_OPERATOR') === true) {
$targets[] = \T_NULLSAFE_OBJECT_OPERATOR;
}

Expand Down
3 changes: 2 additions & 1 deletion Tests/Utils/Operators/IsShortTernaryTest.php
Expand Up @@ -11,6 +11,7 @@
namespace PHPCSUtils\Tests\Utils\Operators;

use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Tokens\TokenHelper;
use PHPCSUtils\Utils\Operators;

/**
Expand Down Expand Up @@ -131,7 +132,7 @@ public function dataIsShortTernaryTokenizerIssues()
}

$targetCoalesceAndEquals = $targetCoalesce;
if (\defined('T_COALESCE_EQUAL')) {
if (TokenHelper::tokenExists('T_COALESCE_EQUAL')) {
$targetCoalesceAndEquals[] = \T_COALESCE_EQUAL;
}

Expand Down
3 changes: 2 additions & 1 deletion Tests/Utils/Parentheses/ParenthesesTest.php
Expand Up @@ -13,6 +13,7 @@
use PHPCSUtils\BackCompat\BCTokens;
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Tokens\Collections;
use PHPCSUtils\Tokens\TokenHelper;
use PHPCSUtils\Utils\Parentheses;

/**
Expand Down Expand Up @@ -955,7 +956,7 @@ public function testHasOwner($testName, $expectedResults)
// Add expected results for all owner types not listed in the data provider.
$expectedResults += $this->ownerDefaults;

if (\defined('T_FN') === false) {
if (TokenHelper::tokenExists('T_FN') === false) {
$expectedResults['T_STRING'] = $expectedResults['T_FN'];
unset($expectedResults['T_FN']);
}
Expand Down

0 comments on commit 6b3e66d

Please sign in to comment.