PHPCSUtils

Arrays

Utility functions for use when examining arrays.

Tags
since
1.0.0

Table of Contents

getDoubleArrowPtr()  : int|false
Get the stack pointer position of the double arrow within an array item.
getOpenClose()  : array|false
Find the array opener and closer based on a T_ARRAY or T_OPEN_SHORT_ARRAY token.
isShortArray()  : bool
Determine whether a T_OPEN/CLOSE_SHORT_ARRAY token is a short array construct and not a short list.

Methods

getDoubleArrowPtr()

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

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

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 last token in the array item.

Tags
since
1.0.0
since
1.0.0-alpha2

Now allows for arrow functions in arrays.

throws
RuntimeException

If the start or end positions are invalid.

Return values
int|false

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

getOpenClose()

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

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

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 : true|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
1.0.0
Return values
array|false

An array with the token pointers; or FALSE if this is not a (short) array token or if the opener/closer could not be determined. The format of the array return value is:

array(
  'opener' => integer, // Stack pointer to the array open bracket.
  'closer' => integer, // Stack pointer to the array close bracket.
)

isShortArray()

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

public static isShortArray(File $phpcsFile, int $stackPtr) : 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
1.0.0
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.

Search results