Skip to content

Commit

Permalink
BCTokens: add magicConstants() method
Browse files Browse the repository at this point in the history
Upstream PR 3013 introduces a new `$magicConstants` tokens array to PHPCS, same as already existed in PHPCSUtils as `Collections::$magicConstants`.

This PR backfills the PHPCS native array in the `BCTokens` class.

Includes unit test.
  • Loading branch information
jrfnl committed Jul 20, 2020
1 parent 58a4cdc commit 9727583
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
27 changes: 27 additions & 0 deletions PHPCSUtils/BackCompat/BCTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace PHPCSUtils\BackCompat;

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

/**
* Token arrays related utility methods.
Expand Down Expand Up @@ -395,4 +396,30 @@ public static function ooScopeTokens()

return self::$ooScopeTokens;
}

/**
* Tokens representing PHP magic constants.
*
* Retrieve the PHP magic constants tokens array in a cross-version compatible manner.
*
* Changelog for the PHPCS native array:
* - Introduced in PHPCS 3.5.6.
*
* @see \PHP_CodeSniffer\Util\Tokens::$magicConstants Original array.
* @see \PHPCSUtils\Tokens\Collections::$magicConstants Same array, pre-dating the PHPCS change.
*
* @link https://www.php.net/language.constants.predefined PHP Manual on magic constants
*
* @since 1.0.0-alpha4
*
* @return array <int|string> => <int|string>
*/
public static function magicConstants()
{
if (isset(Tokens::$magicConstants)) {
return Tokens::$magicConstants;
}

return Collections::$magicConstants;
}
}
48 changes: 48 additions & 0 deletions Tests/BackCompat/BCTokens/MagicConstantsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\BackCompat\BCTokens;

use PHPCSUtils\BackCompat\BCTokens;
use PHPUnit\Framework\TestCase;

/**
* Test class.
*
* @covers \PHPCSUtils\BackCompat\BCTokens::magicConstants
*
* @group tokens
*
* @since 1.0.0
*/
class MagicConstantsTest extends TestCase
{

/**
* Test the method.
*
* @return void
*/
public function testMagicConstants()
{
$expected = [
\T_CLASS_C => \T_CLASS_C,
\T_DIR => \T_DIR,
\T_FILE => \T_FILE,
\T_FUNC_C => \T_FUNC_C,
\T_LINE => \T_LINE,
\T_METHOD_C => \T_METHOD_C,
\T_NS_C => \T_NS_C,
\T_TRAIT_C => \T_TRAIT_C,
];

$this->assertSame($expected, BCTokens::magicConstants());
}
}

0 comments on commit 9727583

Please sign in to comment.