Context
in package
Utility functions for examining in which context a certain token is used.
Example use-cases:
- A sniff looking for the use of certain variables may want to disregard the "use"
of these variables within a call to
isset()
orempty()
. - A sniff looking for incrementor/decrementors may want to disregard these when used
as the third expression in a
for()
condition.
Tags
Table of Contents
Methods
- inAttribute() : bool
- Check whether an arbitrary token is within an attribute.
- inEmpty() : bool
- Check whether an arbitrary token is within a call to empty().
- inForCondition() : string|false
- Check whether an arbitrary token is in a for condition and if so, in which part: the first, second or third expression.
- inForeachCondition() : string|false
- Check whether an arbitrary token is in a foreach condition and if so, in which part: before or after the "as".
- inIsset() : bool
- Check whether an arbitrary token is within a call to isset().
- inUnset() : bool
- Check whether an arbitrary token is within a call to unset().
Methods
inAttribute()
Check whether an arbitrary token is within an attribute.
public
static inAttribute(File $phpcsFile, int $stackPtr) : bool
Parameters
- $phpcsFile : File
-
The file being scanned.
- $stackPtr : int
-
The position of the token we are checking.
Tags
Return values
boolinEmpty()
Check whether an arbitrary token is within a call to empty().
public
static inEmpty(File $phpcsFile, int $stackPtr) : bool
This method is a thin, descriptive wrapper around the Parentheses::getLastOwner() method. For more complex/combined queries, it is recommended to call the Parentheses::getLastOwner() method directly.
Parameters
- $phpcsFile : File
-
The file being scanned.
- $stackPtr : int
-
The position of the token we are checking.
Tags
Return values
boolinForCondition()
Check whether an arbitrary token is in a for condition and if so, in which part: the first, second or third expression.
public
static inForCondition(File $phpcsFile, int $stackPtr) : string|false
Note: the semicolons separating the conditions are regarded as belonging with the expression before it.
Parameters
- $phpcsFile : File
-
The file being scanned.
- $stackPtr : int
-
The position of the token we are checking.
Tags
Return values
string|false —String 'expr1'
, 'expr2'
or 'expr3'
when the token is within
a for
condition.
FALSE
in all other cases, including for parse errors.
inForeachCondition()
Check whether an arbitrary token is in a foreach condition and if so, in which part: before or after the "as".
public
static inForeachCondition(File $phpcsFile, int $stackPtr) : string|false
Parameters
- $phpcsFile : File
-
The file being scanned.
- $stackPtr : int
-
The position of the token we are checking.
Tags
Return values
string|false —String 'beforeAs'
, 'as'
or 'afterAs'
when the token is within
a foreach
condition.
FALSE
in all other cases, including for parse errors.
inIsset()
Check whether an arbitrary token is within a call to isset().
public
static inIsset(File $phpcsFile, int $stackPtr) : bool
This method is a thin, descriptive wrapper around the Parentheses::getLastOwner() method. For more complex/combined queries, it is recommended to call the Parentheses::getLastOwner() method directly.
Parameters
- $phpcsFile : File
-
The file being scanned.
- $stackPtr : int
-
The position of the token we are checking.
Tags
Return values
boolinUnset()
Check whether an arbitrary token is within a call to unset().
public
static inUnset(File $phpcsFile, int $stackPtr) : bool
This method is a thin, descriptive wrapper around the Parentheses::getLastOwner() method. For more complex/combined queries, it is recommended to call the Parentheses::getLastOwner() method directly.
Parameters
- $phpcsFile : File
-
The file being scanned.
- $stackPtr : int
-
The position of the token we are checking.