Skip to content

Commit

Permalink
Merge pull request #204 from PHPCSStandards/feature/collections-new-n…
Browse files Browse the repository at this point in the history
…ametokens-method

Collections: new nameTokens() method
  • Loading branch information
jrfnl committed Sep 13, 2020
2 parents e01c170 + 96bc79b commit f9c3588
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
34 changes: 34 additions & 0 deletions PHPCSUtils/Tokens/Collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,40 @@ public static function namespacedNameTokens()
return $tokens;
}

/**
* The tokens used for "names", be it namespace, OO, function or constant names.
*
* Includes the tokens introduced in PHP 8.0 for "Namespaced names as single token" when available.
*
* Note: this is a method, not a property as the PHP 8.0 identifier name tokens may not exist.
*
* @link https://wiki.php.net/rfc/namespaced_names_as_token
*
* @since 1.0.0-alpha4
*
* @return array <int|string> => <int|string>
*/
public static function nameTokens()
{
$tokens = [
\T_STRING => \T_STRING,
];

if (\defined('T_NAME_QUALIFIED') === true) {
$tokens[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED;
}

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

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

return $tokens;
}

/**
* Object operators.
*
Expand Down
47 changes: 47 additions & 0 deletions Tests/Tokens/Collections/NameTokensTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
*
* @package PHPCSUtils
* @copyright 2019-2020 PHPCSUtils Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSUtils
*/

namespace PHPCSUtils\Tests\Tokens\Collections;

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

/**
* Test class.
*
* @covers \PHPCSUtils\Tokens\Collections::nameTokens
*
* @group collections
*
* @since 1.0.0
*/
class NameTokensTest extends TestCase
{

/**
* Test the method.
*
* @return void
*/
public function testNameTokens()
{
$expected = [
\T_STRING => \T_STRING,
];

if (\version_compare(\PHP_VERSION_ID, '80000', '>=') === true) {
$expected[\T_NAME_QUALIFIED] = \T_NAME_QUALIFIED;
$expected[\T_NAME_FULLY_QUALIFIED] = \T_NAME_FULLY_QUALIFIED;
$expected[\T_NAME_RELATIVE] = \T_NAME_RELATIVE;
}

$this->assertSame($expected, Collections::nameTokens());
}
}

0 comments on commit f9c3588

Please sign in to comment.