PHPCSUtils 1.0.10

TokenHelper
in package

FinalYes

Helpers for working with tokens.

Tags
since
1.0.0

Table of Contents

Methods

tokenExists()  : bool
Check whether a PHP native token exists (for real).

Methods

tokenExists()

Check whether a PHP native token exists (for real).

public static tokenExists(string $name) : bool

Under normal circumstances, checking whether a token exists (either defined by PHP or by PHPCS) is as straight-forward as running defined('T_TOKEN_NAME').

Unfortunately, this doesn't work in all circumstances, most notably, if an external standard also uses PHP-Parser or when code coverage is run on a standard using PHPUnit >= 9.3 (which uses PHP-Parser), this logic breaks because PHP-Parser also polyfills tokens. This method takes potentially polyfilled tokens from PHP-Parser into account and will regard the token as undefined if it was declared by PHP-Parser.

Note: this method only needs to be used for PHP native tokens, not for PHPCS specific tokens. Also, realistically, it only needs to be used for tokens introduced in PHP in recent versions (PHP 7.4 and up). Having said that, the method will also work correctly when a name of a PHPCS native token is passed or of an older PHP native token.

Parameters
$name : string

The token name.

Tags
link

PHP-Code-Coverage#798

link

PHP-Parser Lexer code

since
1.0.0
Return values
bool

        
On this page

Search results