PHPCSUtils 1.0.10

ControlStructures
in package

FinalYes

Utility functions for use when examining control structures.

Tags
since
1.0.0

Table of Contents

Methods

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

The file being scanned.

$stackPtr : int

The position of the token we are checking.

Tags
since
1.0.0
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<int, array<string, string|int>>

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.
)

In case of an invalid catch structure, the array may be empty.

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

        
On this page

Search results