Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BCTokens: update scopeOpeners() and ooScopeTokens() #285

Merged
merged 1 commit into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions PHPCSUtils/BackCompat/BCTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ class BCTokens
* @since 1.0.0
* @since 1.0.0-alpha3 Visibility changed from `protected` to `private`.
*
* {@internal Note: T_ENUM is missing from this array and will be added dynamically
* in the ooScopeTokens() method when available.}
*
* @var array <int|string> => <int|string>
*/
private static $ooScopeTokens = [
Expand Down Expand Up @@ -304,6 +307,7 @@ public static function parenthesisOpeners()
* Changelog for the PHPCS native array:
* - Introduced in PHPCS 0.0.5.
* - PHPCS 3.6.0: `T_MATCH` added to the array.
* - PHPCS 3.7.0: `T_ENUM` added to the array.
*
* @since 1.0.0-alpha4
*
Expand All @@ -321,6 +325,14 @@ public static function scopeOpeners()
$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')) {
$tokens[\T_ENUM] = \T_ENUM;
}

return $tokens;
}

Expand Down Expand Up @@ -419,6 +431,7 @@ public static function functionNameTokens()
*
* Changelog for the PHPCS native array:
* - Introduced in PHPCS 3.1.0.
* - PHPCS 3.7.0: `T_ENUM` added to the array.
*
* @see \PHP_CodeSniffer\Util\Tokens::$ooScopeTokens Original array.
*
Expand All @@ -428,11 +441,21 @@ public static function functionNameTokens()
*/
public static function ooScopeTokens()
{
$tokens = self::$ooScopeTokens;

if (isset(Tokens::$ooScopeTokens)) {
return Tokens::$ooScopeTokens;
$tokens = Tokens::$ooScopeTokens;
}

return self::$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')) {
$tokens[\T_ENUM] = \T_ENUM;
}

return $tokens;
}

/**
Expand Down
15 changes: 14 additions & 1 deletion Tests/BackCompat/BCTokens/OoScopeTokensTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\BackCompat\BCTokens;
use PHPCSUtils\BackCompat\Helper;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -33,14 +34,26 @@ class OoScopeTokensTest extends TestCase
*/
public function testOoScopeTokens()
{
$version = Helper::getVersion();
$expected = [
\T_CLASS => \T_CLASS,
\T_ANON_CLASS => \T_ANON_CLASS,
\T_INTERFACE => \T_INTERFACE,
\T_TRAIT => \T_TRAIT,
];

$this->assertSame($expected, BCTokens::ooScopeTokens());
if (\version_compare($version, '3.7.0', '>=') === true
|| \version_compare(\PHP_VERSION_ID, '80099', '>=') === true
) {
$expected[\T_ENUM] = \T_ENUM;
}

\asort($expected);

$result = BCTokens::ooScopeTokens();
\asort($result);

$this->assertSame($expected, $result);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions Tests/BackCompat/BCTokens/ScopeOpenersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public function testScopeOpeners()
$expected[\T_MATCH] = \T_MATCH;
}

if (\version_compare($version, '3.7.0', '>=') === true
|| \version_compare(\PHP_VERSION_ID, '80099', '>=') === true
) {
$expected[\T_ENUM] = \T_ENUM;
}

\asort($expected);

$result = BCTokens::scopeOpeners();
Expand Down