Namespaces
Utility functions for use when examining T_NAMESPACE tokens and to determine the namespace of arbitrary tokens.
Table of Contents
| getType() | Determine what a T_NAMESPACE token is used for. | string |
|---|---|---|
| isDeclaration() | Determine whether a T_NAMESPACE token is the keyword for a namespace declaration. | bool |
| isOperator() | Determine whether a T_NAMESPACE token is used as an operator. | bool |
| getDeclaredName() | Get the complete namespace name as declared. | string|bool |
| findNamespacePtr() | Determine the namespace an arbitrary token lives in. | int|bool |
| determineNamespace() | Determine the namespace name an arbitrary token lives in. | string |
Methods
getType()
Determine what a T_NAMESPACE token is used for.
public
static getType(
$phpcsFile :
File
, $stackPtr :
int
)
: string
Parameters
- $phpcsFile : File
The file being scanned.
- $stackPtr : int
The position of the T_NAMESPACE token.
Tags
Return values
string —Either 'declaration', 'operator'. An empty string will be returned if it couldn't be reliably determined what the T_NAMESPACE token is used for, which will normally mean the code contains a parse/fatal error.
isDeclaration()
Determine whether a T_NAMESPACE token is the keyword for a namespace declaration.
public
static isDeclaration(
$phpcsFile :
File
, $stackPtr :
int
)
: bool
Parameters
- $phpcsFile : File
The file being scanned.
- $stackPtr : int
The position of a T_NAMESPACE token.
Tags
Return values
bool —True if the token passed is the keyword for a namespace declaration. False if not.
isOperator()
Determine whether a T_NAMESPACE token is used as an operator.
public
static isOperator(
$phpcsFile :
File
, $stackPtr :
int
)
: bool
Parameters
- $phpcsFile : File
The file being scanned.
- $stackPtr : int
The position of a T_NAMESPACE token.
Tags
Return values
bool —True if the token passed is used as an operator. False if not.
getDeclaredName()
Get the complete namespace name as declared.
public
static getDeclaredName(
$phpcsFile :
File
, $stackPtr :
int
[, $clean :
bool
= true ]
)
: string|bool
For hierarchical namespaces, the name will be composed of several tokens, i.e. MyProject\Sub\Level which will be returned together as one string.
Parameters
- $phpcsFile : File
The file being scanned.
- $stackPtr : int
The position of a T_NAMESPACE token.
- $clean : bool = true
Optional. Whether to get the name stripped of potentially interlaced whitespace and/or comments. Defaults to true.
Tags
Return values
string|bool —The namespace name, or false if the specified position is not a T_NAMESPACE token, the token points to a namespace operator or when parse errors are encountered/during live coding. Note: The name can be an empty string for a valid global namespace declaration.
findNamespacePtr()
Determine the namespace an arbitrary token lives in.
public
static findNamespacePtr(
$phpcsFile :
File
, $stackPtr :
int
)
: int|bool
Note: when a namespace declaration token or a token which is part of the namespace name is passed to this method, the result will be false as technically, they are not within a namespace.
Note: this method has no opinion on whether the token passed is actually subject to namespacing.
Parameters
- $phpcsFile : File
The file being scanned.
- $stackPtr : int
The token for which to determine the namespace.
Tags
Return values
int|bool —Token pointer to the applicable namespace keyword or false if it couldn't be determined or no namespace applies.
determineNamespace()
Determine the namespace name an arbitrary token lives in.
public
static determineNamespace(
$phpcsFile :
File
, $stackPtr :
int
)
: string
Note: this method has no opinion on whether the token passed is actually subject to namespacing.
Parameters
- $phpcsFile : File
The file being scanned.
- $stackPtr : int
The token for which to determine the namespace.
Tags
Return values
string —Namespace name or empty string if it couldn't be determined or no namespace applies.