PHPCSUtils

Conditions

Utility functions for use when examining token conditions.

Tags
since
1.0.0

The Conditions::getCondition() and Conditions::hasCondition() methods are based on and inspired by the methods of the same name in the PHPCS native PHP_CodeSniffer\Files\File class. Also see {@see \PHPCSUtils\BackCompat\BCFile}.

Table of Contents

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

Methods

getCondition()

Retrieve the position of a condition for the passed token.

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

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
File::getCondition()

Original source.

see
BCFile::getCondition()

Cross-version compatible version of the original.

since
1.0.0
since
1.0.0-alpha2

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|false

Integer stack pointer to the condition; or FALSE if the token does not have the condition or has no conditions at all.

getFirstCondition()

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

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

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
1.0.0
Return values
int|false

Integer stack pointer to the condition; or FALSE if the token does not have the condition or has no conditions at all.

getLastCondition()

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

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

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
1.0.0
Return values
int|false

Integer stack pointer to the condition; or FALSE if the token does not have the condition or has no conditions at all.

hasCondition()

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

public static hasCondition(File $phpcsFile, int $stackPtr, int|string|array $types) : 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
File::hasCondition()

Original source.

see
BCFile::hasCondition()

Cross-version compatible version of the original.

since
1.0.0
Return values
bool

Search results