PHPCSUtils

ControlStructures
in package

Utility functions for use when examining control structures.

Tags
since
1.0.0
since
1.0.0-alpha4

Dropped support for PHPCS < 3.7.1.

Table of Contents

getCaughtExceptions()  : array<string|int, mixed>
Retrieve the exception(s) being caught in a CATCH condition.
getDeclareScopeOpenClose()  : array<string|int, mixed>|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<string|int, mixed>
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the token we are checking.

Tags
since
1.0.0-alpha3
since
1.0.0-alpha4

Added support for PHP 8.0 identifier name tokenization.

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<string|int, mixed>

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<string|int, mixed>|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 was fixed in PHPCS 3.5.4.

Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the token we are checking.

Tags
link

PHPCS PR #2843

since
1.0.0
deprecated
1.0.0-alpha4

Check the scope_opener/scope_closer instead.

Return values
array<string|int, mixed>|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
since
1.0.0-alpha4

Added support for PHP 8.0 match control structures.

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