PHPCSUtils

ControlStructures

Utility functions for use when examining control structures.

Tags
since
1.0.0

Table of Contents

getCaughtExceptions()  : array
Retrieve the exception(s) being caught in a CATCH condition.
getDeclareScopeOpenClose()  : array|false
Get the scope opener and closer for a DECLARE statement.
hasBody()  : bool
Check whether a control structure has a body.
isElseIf()  : bool
Check whether an IF or ELSE token is part of an "else if".

Methods

getCaughtExceptions()

Retrieve the exception(s) being caught in a CATCH condition.

public static getCaughtExceptions(File $phpcsFile, int $stackPtr) : array
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the token we are checking.

Tags
since
1.0.0-alpha3
throws
RuntimeException

If the specified $stackPtr is not of type T_CATCH or doesn't exist.

throws
RuntimeException

If no parenthesis opener or closer can be determined (parse error).

Return values
array

Array with information about the caught Exception(s). The returned array will contain the following information for each caught exception:

0 => array(
  'type'           => string,  // The type declaration for the exception being caught.
  'type_token'     => integer, // The stack pointer to the start of the type declaration.
  'type_end_token' => integer, // The stack pointer to the end of the type declaration.
)

getDeclareScopeOpenClose()

Get the scope opener and closer for a DECLARE statement.

public static getDeclareScopeOpenClose(File $phpcsFile, int $stackPtr) : array|false

A declare statement can be:

  • applied to the rest of the file, like declare(ticks=1);
  • applied to a limited scope using curly braces;
  • applied to a limited scope using the alternative control structure syntax.

In the first case, the statement - correctly - won't have a scope opener/closer. In the second case, the statement will have the scope opener/closer indexes. In the last case, due to a bug in the PHPCS Tokenizer, it won't have the scope opener/closer indexes, while it really should. This bug is fixed in PHPCS 3.5.4.

In other words, if a sniff needs to support PHPCS < 3.5.4 and needs to take the alternative control structure syntax into account, this method can be used to retrieve the scope opener/closer for the declare statement.

Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the token we are checking.

Tags
link
https://github.com/squizlabs/PHP_CodeSniffer/pull/2843

PHPCS PR #2843

since
1.0.0
Return values
array|false

An array with the token pointers; or FALSE if not a DECLARE 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 scope opener.
  'closer' => integer, // Stack pointer to the scope closer.
)

hasBody()

Check whether a control structure has a body.

public static hasBody(File $phpcsFile, int $stackPtr[, bool $allowEmpty = true ]) : bool

Some control structures - while, for and declare - can be declared without a body, like:

while (++$i < 10);

All other control structures will always have a body, though the body may be empty, where "empty" means: no code is found in the body. If a control structure body only contains a comment, it will be regarded as empty.

Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the token we are checking.

$allowEmpty : bool = true

Whether a control structure with an empty body should still be considered as having a body. Defaults to true.

Tags
since
1.0.0
Return values
bool

TRUE when the control structure has a body, or in case $allowEmpty is set to FALSE: when it has a non-empty body. FALSE in all other cases, including when a non-control structure token has been passed.

isElseIf()

Check whether an IF or ELSE token is part of an "else if".

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

The file being scanned.

$stackPtr : int

The position of the token we are checking.

Tags
since
1.0.0
Return values
bool

Search results