Lists
Utility functions to retrieve information when working with lists.
Table of Contents
| isShortList() | Determine whether a `T_OPEN/CLOSE_SHORT_ARRAY` token is a short list() construct. | bool |
|---|---|---|
| getOpenClose() | Find the list opener & closer based on a T_LIST or T_OPEN_SHORT_ARRAY token. | array|bool |
| getAssignments() | Retrieves information on the assignments made in the specified (long/short) list. | array |
Methods
isShortList()
Determine whether a `T_OPEN/CLOSE_SHORT_ARRAY` token is a short list() construct.
public
static isShortList(
$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, which affects the short list determination.
Parameters
- $phpcsFile : File
The file being scanned.
- $stackPtr : int
The position of the short array bracket token.
Tags
Return values
bool —True if the token passed is the open/close bracket of a short list. False if the token is a short array bracket or plain square bracket or not one of the accepted tokens.
getOpenClose()
Find the list opener & closer based on a T_LIST or T_OPEN_SHORT_ARRAY token.
public
static getOpenClose(
$phpcsFile :
File
, $stackPtr :
int
[, $isShortList :
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 list determination.
Parameters
- $phpcsFile : File
The file being scanned.
- $stackPtr : int
The position of the T_LIST or T_OPEN_SHORT_ARRAY token in the stack.
- $isShortList : bool|null = null
Short-circuit the short list 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 list while walking the tokens in a list. Use with care.
Tags
Return values
array|bool —Array with two keys opener, closer or false if
not a (short) list token or if the opener/closer
could not be determined.
getAssignments()
Retrieves information on the assignments made in the specified (long/short) list.
public
static getAssignments(
$phpcsFile :
File
, $stackPtr :
int
)
: array
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 list determination.
The returned array will contain the following basic information for each assignment:
0 => array(
'raw' => string, // The full content of the variable definition, including
// whitespace and comments.
// This may be an empty string when an item is being skipped.
'is_empty' => bool, // Whether this is an empty list item, i.e. the
// second item in list($a, , $b).
)
Non-empty list items will have the following additional array indexes set:
'assignment' => string, // The content of the assignment part, cleaned of comments.
// This could be a nested list.
'nested_list' => bool, // Whether this is a nested list.
'assign_by_reference' => bool, // Is the variable assigned by reference?
'reference_token' => int|false, // The stack pointer to the reference operator or
// FALSE when not a reference assignment.
'variable' => string|false, // The base variable being assigned to or
// FALSE in case of a nested list or variable variable.
// I.e. $a in list($a['key']).
'assignment_token' => int, // The start pointer for the assignment.
'assignment_end_token' => int, // The end pointer for the assignment.
Assignments with keys will have the following additional array indexes set:
'key' => string, // The content of the key, cleaned of comments.
'key_token' => int, // The stack pointer to the start of the key.
'key_end_token' => int, // The stack pointer to the end of the key.
'double_arrow_token' => int, // The stack pointer to the double arrow.
Parameters
- $phpcsFile : File
The file being scanned.
- $stackPtr : int
The position in the stack of the function token to acquire the parameters for.
Tags
Return values
array —An array with information on each assignment made, including skipped assignments (empty), or an empty array if no assignments are made at all (fatal error in PHP 7+).