ControlStructures
Utility functions for use when examining control structures.
Table of Contents
| hasBody() | Check whether a control structure has a body. | bool |
|---|---|---|
| isElseIf() | Check whether an IF or ELSE token is part of an `else if`. | bool |
| getDeclareScopeOpenClose() | Get the scope opener and closer for a `declare` statement. | array|bool |
Methods
hasBody()
Check whether a control structure has a body.
public
static hasBody(
$phpcsFile :
File
, $stackPtr :
int
[, $allowEmpty :
bool
= 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
Return values
bool —True when the control structure has a body, or when $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(
$phpcsFile :
File
, $stackPtr :
int
)
: bool
Parameters
- $phpcsFile : File
The file being scanned.
- $stackPtr : int
The position of the token we are checking.
Tags
Return values
boolgetDeclareScopeOpenClose()
Get the scope opener and closer for a `declare` statement.
public
static getDeclareScopeOpenClose(
$phpcsFile :
File
, $stackPtr :
int
)
: array|bool
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
Return values
array|bool —Array with two keys opener, closer or false if
not a declare token or if the opener/closer
could not be determined.