Documentation

Conditions

Utility functions for use when examining token conditions.

Table of Contents

getCondition() Retrieve the position of a condition for the passed token. int|bool
hasCondition() Determine if the passed token has a condition of one of the passed types. bool
getFirstCondition() Return the position of the first (outermost) condition of a certain type for the passed token. int|bool
getLastCondition() Return the position of the last (innermost) condition of a certain type for the passed token. int|bool

Methods

getCondition()

Retrieve the position of a condition for the passed token.

public static getCondition( $phpcsFile : File , $stackPtr : int [, $types : int|string|array = [] ] [, $first : bool = true ] ) : int|bool

If no types are specified, the first condition for the token - or if $first=false, the last condition - will be returned.

Main differences with the PHPCS version:

  • The singular $type parameter has become the more flexible $types parameter allowing to search for several types of conditions in one go.
  • The $types parameter is now optional.
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the token we are checking.

$types : int|string|array = []

Optional. The type(s) of tokens to search for.

$first : bool = true

Optional. Whether to search for the first (outermost) (true) or the last (innermost) condition (false) of the specified type(s).

Tags
see

Original source.

see

Cross-version compatible version of the original.

since
since

The $reverse parameter has been renamed to $first and the meaning of the boolean reversed (true = first, false = last, was: true = last, false = first) to be in line with PHPCS itself which added the $first parameter in v 3.5.4 to allow for the same/similar functionality as $reverse already offered.

Return values
int|bool

Integer stack pointer to the condition or FALSE if the token does not have the condition.

hasCondition()

Determine if the passed token has a condition of one of the passed types.

public static hasCondition( $phpcsFile : File , $stackPtr : int , $types : int|string|array ) : bool

This method is not significantly different from the PHPCS native version.

Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the token we are checking.

$types : int|string|array

The type(s) of tokens to search for.

Tags
see

Original source.

see

Cross-version compatible version of the original.

since
Return values
bool

getFirstCondition()

Return the position of the first (outermost) condition of a certain type for the passed token.

public static getFirstCondition( $phpcsFile : File , $stackPtr : int [, $types : int|string|array = [] ] ) : int|bool

If no types are specified, the first condition for the token, independently of type, will be returned.

Parameters
$phpcsFile : File

The file where this token was found.

$stackPtr : int

The position of the token we are checking.

$types : int|string|array = []

Optional. The type(s) of tokens to search for.

Tags
since
Return values
int|bool

StackPtr to the condition or false if the token does not have the condition.

getLastCondition()

Return the position of the last (innermost) condition of a certain type for the passed token.

public static getLastCondition( $phpcsFile : File , $stackPtr : int [, $types : int|string|array = [] ] ) : int|bool

If no types are specified, the last condition for the token, independently of type, will be returned.

Parameters
$phpcsFile : File

The file where this token was found.

$stackPtr : int

The position of the token we are checking.

$types : int|string|array = []

Optional. The type(s) of tokens to search for.

Tags
since
Return values
int|bool

StackPtr to the condition or false if the token does not have the condition.

Search results