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: add magicConstants() method #172

Merged
merged 1 commit into from
Jul 20, 2020
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: 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());
}
}