Helpers for working with tokens.
- tokenExists() : bool
- Check whether a PHP native token exists (for real).
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
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.
- $name : string
The token name.