PHPCSUtils 1.0.11

Namespaces
in package

FinalYes

Utility functions for use when examining T_NAMESPACE tokens and to determine the namespace of arbitrary tokens.

Tags
link

PHP Manual on namespaces.

since
1.0.0

Table of Contents

Methods

determineNamespace()  : string
Determine the namespace name an arbitrary token lives in.
findNamespacePtr()  : int|false
Find the stack pointer to the namespace declaration applicable for an arbitrary token.
getDeclaredName()  : string|false
Get the complete namespace name as declared.
getType()  : string
Determine what a T_NAMESPACE token is used for.
isDeclaration()  : bool
Determine whether a T_NAMESPACE token is the keyword for a namespace declaration.
isOperator()  : bool
Determine whether a T_NAMESPACE token is used as an operator.

Methods

determineNamespace()

Determine the namespace name an arbitrary token lives in.

public static determineNamespace(File $phpcsFile, int $stackPtr) : 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
since
1.0.0
Return values
string

Namespace name; or an empty string if the namespace couldn't be determined or when no namespace applies.

findNamespacePtr()

Find the stack pointer to the namespace declaration applicable for an arbitrary token.

public static findNamespacePtr(File $phpcsFile, int $stackPtr) : int|false

Take note:

  1. 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, these tokens are not within a namespace.
  2. 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
since
1.0.0
Return values
int|false

Token pointer to the namespace keyword for the applicable namespace declaration; or FALSE if it couldn't be determined or if no namespace applies.

getDeclaredName()

Get the complete namespace name as declared.

public static getDeclaredName(File $phpcsFile, int $stackPtr[, bool $clean = true ]) : string|false

For hierarchical namespaces, the namespace 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
since
1.0.0
Return values
string|false

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.

getType()

Determine what a T_NAMESPACE token is used for.

public static getType(File $phpcsFile, int $stackPtr) : string
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the T_NAMESPACE token.

Tags
since
1.0.0
throws
RuntimeException

If the specified position is not a T_NAMESPACE token.

Return values
string

Either 'declaration', 'operator' or an empty string. An empty string will be returned if it couldn't be reliably determined what the T_NAMESPACE token is used for, which, in most cases, will 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(File $phpcsFile, int $stackPtr) : bool
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of a T_NAMESPACE token.

Tags
since
1.0.0
throws
RuntimeException

If the specified position is not a T_NAMESPACE token.

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(File $phpcsFile, int $stackPtr) : bool
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of a T_NAMESPACE token.

Tags
link

PHP Manual about the use of the namespace keyword as an operator.

since
1.0.0
throws
RuntimeException

If the specified position is not a T_NAMESPACE token.

Return values
bool

TRUE if the namespace token passed is used as an operator. FALSE if not.


        
On this page

Search results