Documentation

UseStatements

Utility functions for examining use statements.

Table of Contents

getType() Determine what a T_USE token is used for. string
isClosureUse() Determine whether a T_USE token represents a closure use statement. bool
isImportUse() Determine whether a T_USE token represents a class/function/constant import use statement. bool
isTraitUse() Determine whether a T_USE token represents a trait use statement. bool
splitImportUseStatement() Split an import use statement into individual imports. array

Methods

getType()

Determine what a T_USE 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_USE token.

Tags
since
throws

If the specified position is not a T_USE token.

Return values
string

Either 'closure', 'import' or 'trait'. An empty string will be returned if the token is used in an invalid context or if it couldn't be reliably determined what the T_USE token is used for.

isClosureUse()

Determine whether a T_USE token represents a closure use statement.

public static isClosureUse( $phpcsFile : File , $stackPtr : int ) : bool
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the T_USE token.

Tags
since
throws

If the specified position is not a T_USE token.

Return values
bool

True if the token passed is a closure use statement. False if it's not.

isImportUse()

Determine whether a T_USE token represents a class/function/constant import use statement.

public static isImportUse( $phpcsFile : File , $stackPtr : int ) : bool
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the T_USE token.

Tags
since
throws

If the specified position is not a T_USE token.

Return values
bool

True if the token passed is an import use statement. False if it's not.

isTraitUse()

Determine whether a T_USE token represents a trait use statement.

public static isTraitUse( $phpcsFile : File , $stackPtr : int ) : bool
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the T_USE token.

Tags
since
throws

If the specified position is not a T_USE token.

Return values
bool

True if the token passed is a trait use statement. False if it's not.

splitImportUseStatement()

Split an import use statement into individual imports.

public static splitImportUseStatement( $phpcsFile : File , $stackPtr : int ) : array

Handles single import, multi-import and group-import statements.

Parameters
$phpcsFile : File

The file where this token was found.

$stackPtr : int

The position in the stack of the T_USE token.

Tags
since
throws

If the specified position is not a T_USE token or not an import use statement.

Return values
array

A multi-level array containing information about the use statement. The first level is 'name', 'function' and 'const'. These keys will always exist. If any statements are found for any of these categories, the second level will contain the alias/name as the key and the full original use name as the value for each of the found imports or an empty array if no imports were found in this use statement for this category.

For example, for this function group use statement: use function Vendor\Package\{LevelA\Name as Alias, LevelB\Another_Name} the return value would look like this: [ 'name' => [], 'function' => [ 'Alias' => 'Vendor\Package\LevelA\Name', 'Another_Name' => 'Vendor\Package\LevelB\Another_Name', ], 'const' => [], ]

Search results