Documentation

Arrays

Utility functions for use when examining arrays.

Table of Contents

$doubleArrowTargets The tokens to target to find the double arrow in an array item. array
isShortArray() Determine whether a `T_OPEN/CLOSE_SHORT_ARRAY` token is a short array() construct and not a short list. bool
getOpenClose() Find the array opener & closer based on a T_ARRAY or T_OPEN_SHORT_ARRAY token. array|bool
getDoubleArrowPtr() Get the stack pointer position of the double arrow within an array item. int|bool

Properties

$doubleArrowTargets

The tokens to target to find the double arrow in an array item.

private static array $doubleArrowTargets = [\T_DOUBLE_ARROW => \T_DOUBLE_ARROW, \T_ARRAY => \T_ARRAY, \T_OPEN_SHORT_ARRAY => \T_OPEN_SHORT_ARRAY, \T_STRING => \T_STRING]
Note: this array does not contain the `T_FN` token as it may or may not exist. If it exists, it will be added in the `getDoubleArrowPtr()` function.

Methods

isShortArray()

Determine whether a `T_OPEN/CLOSE_SHORT_ARRAY` token is a short array() construct and not a short list.

public static isShortArray( $phpcsFile : File , $stackPtr : int ) : bool

This method also accepts T_OPEN/CLOSE_SQUARE_BRACKET tokens to allow it to be PHPCS cross-version compatible as the short array tokenizing has been plagued by a number of bugs over time.

Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the short array bracket token.

Tags
since
Return values
bool

True if the token passed is the open/close bracket of a short array. False if the token is a short list bracket, a plain square bracket or not one of the accepted tokens.

getOpenClose()

Find the array opener & closer based on a T_ARRAY or T_OPEN_SHORT_ARRAY token.

public static getOpenClose( $phpcsFile : File , $stackPtr : int [, $isShortArray : bool|null = null ] ) : array|bool

This method also accepts T_OPEN_SQUARE_BRACKET tokens to allow it to be PHPCS cross-version compatible as the short array tokenizing has been plagued by a number of bugs over time, which affects the short array determination.

Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the T_ARRAY or T_OPEN_SHORT_ARRAY token in the stack.

$isShortArray : bool|null = null

Short-circuit the short array check for T_OPEN_SHORT_ARRAY tokens if it isn't necessary. Efficiency tweak for when this has already been established, i.e. when encountering a nested array while walking the tokens in an array. Use with care.

Tags
since
Return values
array|bool

Array with two keys opener, closer or false if not a (short) array token or if the opener/closer could not be determined.

getDoubleArrowPtr()

Get the stack pointer position of the double arrow within an array item.

public static getDoubleArrowPtr( $phpcsFile : File , $start : int , $end : int ) : int|bool

Expects to be passed the array item start and end tokens as retrieved via \PHPCSUtils\Utils\PassedParameters::getParameters().

Parameters
$phpcsFile : File

The file being examined.

$start : int

Stack pointer to the start of the array item.

$end : int

Stack pointer to the end of the array item (inclusive).

Tags
since
since

Now allows for arrow functions in arrays.

throws

If the start or end positions are invalid.

Return values
int|bool

Stack pointer to the double arrow if this array item has a key or false otherwise.

Search results