Documentation

BCTokens

Token arrays related utility methods.

PHPCS provides a number of static token arrays in the \PHP_CodeSniffer\Util\Tokens class. Some of these token arrays will not be available in older PHPCS versions. Some will not contain the same set of tokens across PHPCS versions.

This class is a compatibility layer to allow for retrieving these token arrays with a consistent token content across PHPCS versions. The one caveat is that the token constants do need to be available.

Recommended usage: Only use the methods in this class when needed. I.e. when your sniff unit tests indicate a PHPCS cross-version compatibility issue related to inconsistent token arrays.

All PHPCS token arrays are supported, though only a limited number of them are different across PHPCS versions.

The names of the PHPCS native token arrays translate one-on-one to the methods in this class: PHP_CodeSniffer\Util\Tokens::$emptyTokens => PHPCSUtils\BackCompat\BCTokens::emptyTokens() PHP_CodeSniffer\Util\Tokens::$operators => PHPCSUtils\BackCompat\BCTokens::operators() ... etc

The order of the tokens in the arrays may differ between the PHPCS native token arrays and the token arrays returned by this class.

Table of Contents

$phpcsCommentTokensTypes Token types that are comments containing PHPCS instructions. string[]
$ooScopeTokens Tokens that open class and object scopes. array
$textStringTokens Tokens that represent text strings. array
__callStatic() Handle calls to (undeclared) methods for token arrays which haven't received any changes since PHPCS 2.6.0. array
assignmentTokens() Retrieve the PHPCS assignment tokens array in a cross-version compatible manner. array
comparisonTokens() Retrieve the PHPCS comparison tokens array in a cross-version compatible manner. array
arithmeticTokens() Retrieve the PHPCS arithmetic tokens array in a cross-version compatible manner. array
operators() Retrieve the PHPCS operator tokens array in a cross-version compatible manner. array
parenthesisOpeners() Retrieve the PHPCS parenthesis openers tokens array in a cross-version compatible manner. array
phpcsCommentTokens() Retrieve the PHPCS comment tokens array in a cross-version compatible manner. array
textStringTokens() Retrieve the PHPCS text string tokens array in a cross-version compatible manner. array
functionNameTokens() Retrieve the PHPCS function name tokens array in a cross-version compatible manner. array
ooScopeTokens() Retrieve the OO scope tokens array in a cross-version compatible manner. array

Properties

$phpcsCommentTokensTypes

Token types that are comments containing PHPCS instructions.

protected static string[] $phpcsCommentTokensTypes = ['T_PHPCS_ENABLE', 'T_PHPCS_DISABLE', 'T_PHPCS_SET', 'T_PHPCS_IGNORE', 'T_PHPCS_IGNORE_FILE']

$ooScopeTokens

Tokens that open class and object scopes.

protected static array $ooScopeTokens = [\T_CLASS => \T_CLASS, \T_ANON_CLASS => \T_ANON_CLASS, \T_INTERFACE => \T_INTERFACE, \T_TRAIT => \T_TRAIT]

$textStringTokens

Tokens that represent text strings.

protected static array $textStringTokens = [\T_CONSTANT_ENCAPSED_STRING => \T_CONSTANT_ENCAPSED_STRING, \T_DOUBLE_QUOTED_STRING => \T_DOUBLE_QUOTED_STRING, \T_INLINE_HTML => \T_INLINE_HTML, \T_HEREDOC => \T_HEREDOC, \T_NOWDOC => \T_NOWDOC]

Methods

__callStatic()

Handle calls to (undeclared) methods for token arrays which haven't received any changes since PHPCS 2.6.0.

public static __callStatic( $name : string , $args : array ) : array
Parameters
$name : string

The name of the method which has been called.

$args : array

Any arguments passed to the method. Unused as none of the methods take arguments.

Tags
since
Return values
array

<int|string> => <int|string> Token array

assignmentTokens()

Retrieve the PHPCS assignment tokens array in a cross-version compatible manner.

public static assignmentTokens( ) : array

Changelog for the PHPCS native array:

  • Introduced in PHPCS 0.0.5.
  • PHPCS 2.9.0: The PHP 7.4 T_COALESCE_EQUAL token was added to the array. The T_COALESCE_EQUAL token was introduced in PHPCS 2.8.1.
  • PHPCS 3.2.0: The JS T_ZSR_EQUAL token was added to the array. The T_ZSR_EQUAL token was introduced in PHPCS 2.8.0.
Tags
see

Original array.

since
Return values
array

<int|string> => <int|string> Token array.

comparisonTokens()

Retrieve the PHPCS comparison tokens array in a cross-version compatible manner.

public static comparisonTokens( ) : array

Changelog for the PHPCS native array:

  • Introduced in PHPCS 0.5.0.
  • PHPCS 2.9.0: The PHP 7.0 T_COALESCE token was added to the array. The T_COALESCE token was introduced in PHPCS 2.6.1.
  • PHPCS 2.9.0: The PHP 7.0 T_SPACESHIP token was added to the array. The T_SPACESHIP token was introduced in PHPCS 2.5.1.
Tags
see

Original array.

since
Return values
array

<int|string> => <int|string> Token array.

arithmeticTokens()

Retrieve the PHPCS arithmetic tokens array in a cross-version compatible manner.

public static arithmeticTokens( ) : array

Changelog for the PHPCS native array:

  • Introduced in PHPCS 0.5.0.
  • PHPCS 2.9.0: The PHP 5.6 T_POW token was added to the array. The T_POW token was introduced in PHPCS 2.4.0.
Tags
see

Original array.

since
Return values
array

<int|string> => <int|string> Token array or an empty array for PHPCS versions in which the PHPCS native comment tokens did not exist yet.

operators()

Retrieve the PHPCS operator tokens array in a cross-version compatible manner.

public static operators( ) : array

Changelog for the PHPCS native array:

  • Introduced in PHPCS 0.0.5.
  • PHPCS 2.6.1: The PHP 7.0 T_COALESCE token was backfilled and added to the array.
  • PHPCS 2.8.1: The PHP 7.4 T_COALESCE_EQUAL token was backfilled and (incorrectly) added to the array.
  • PHPCS 2.9.0: The T_COALESCE_EQUAL token was removed from the array.
Tags
see

Original array.

since
Return values
array

<int|string> => <int|string> Token array.

parenthesisOpeners()

Retrieve the PHPCS parenthesis openers tokens array in a cross-version compatible manner.

public static parenthesisOpeners( ) : array

Changelog for the PHPCS native array:

  • Introduced in PHPCS 0.0.5.
  • PHPCS 3.5.0: T_LIST and T_ANON_CLASS added to the array.

Note: While T_LIST and T_ANON_CLASS will be included in the return value for this method, the associated parentheses will not have the 'parenthesis_owner' index set until PHPCS 3.5.0.

Tags
see

Original array.

since
Return values
array

<int|string> => <int|string> Token array.

phpcsCommentTokens()

Retrieve the PHPCS comment tokens array in a cross-version compatible manner.

public static phpcsCommentTokens( ) : array

Changelog for the PHPCS native array:

  • Introduced in PHPCS 3.2.3.
Tags
see

Original array.

since
Return values
array => Token array or an empty array for PHPCS versions in which the PHPCS native annotation tokens did not exist yet.

textStringTokens()

Retrieve the PHPCS text string tokens array in a cross-version compatible manner.

public static textStringTokens( ) : array

Changelog for the PHPCS native array:

  • Introduced in PHPCS 2.9.0.
Tags
see

Original array.

since
Return values
array

<int|string> => <int|string> Token array.

functionNameTokens()

Retrieve the PHPCS function name tokens array in a cross-version compatible manner.

public static functionNameTokens( ) : array

Changelog for the PHPCS native array:

  • Introduced in PHPCS 2.3.3.
  • PHPCS 3.1.0: T_SELF and T_STATIC added to the array.
Tags
see

Original array.

since
Return values
array

<int|string> => <int|string> Token array.

ooScopeTokens()

Retrieve the OO scope tokens array in a cross-version compatible manner.

public static ooScopeTokens( ) : array

Changelog for the PHPCS native array:

  • Introduced in PHPCS 3.1.0.
Tags
see

Original array.

since
Return values
array

<int|string> => <int|string> Token array.

Search results