PHPCSUtils

UseStatements

Utility functions for examining use statements.

Tags
since
1.0.0

Table of Contents

getType()  : string
Determine what a T_USE token is used for.
isClosureUse()  : bool
Determine whether a T_USE token represents a closure use statement.
isImportUse()  : bool
Determine whether a T_USE token represents a class/function/constant import use statement.
isTraitUse()  : bool
Determine whether a T_USE token represents a trait use statement.
splitAndMergeImportUseStatement()  : array
Split an import use statement into individual imports and merge it with an array of previously seen import use statements.
splitImportUseStatement()  : array
Split an import use statement into individual imports.

Methods

getType()

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

Tags
since
1.0.0
throws
RuntimeException

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. An empty string being returned will normally mean the code being examined contains a parse error.

isClosureUse()

Determine whether a T_USE token represents a closure use statement.

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

The file being scanned.

$stackPtr : int

The position of the T_USE token.

Tags
since
1.0.0
throws
RuntimeException

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

The file being scanned.

$stackPtr : int

The position of the T_USE token.

Tags
since
1.0.0
throws
RuntimeException

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

The file being scanned.

$stackPtr : int

The position of the T_USE token.

Tags
since
1.0.0
throws
RuntimeException

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.

splitAndMergeImportUseStatement()

Split an import use statement into individual imports and merge it with an array of previously seen import use statements.

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

Beware: this method should only be used to combine the import use statements found in one file. Do NOT combine the statements of multiple files as the result will be inaccurate and unreliable.

In most cases when tempted to use this method, the \PHPCSUtils\AbstractSniffs\AbstractFileContextSniff (upcoming) should be used instead.

Parameters
$phpcsFile : File

The file where this token was found.

$stackPtr : int

The position in the stack of the T_USE token.

$previousUseStatements : array

The import use statements collected so far. This should be either the output of a previous call to this method or the output of an earlier call to the {@see \PHPCSUtils\Utils\UseStatements::splitImportUseStatement()} method.

Tags
see
AbstractFileContextSniff
see
UseStatements::splitImportUseStatement()
since
1.0.0-alpha3
Return values
array

A multi-level array containing information about the current use statement combined with the previously collected use statement information. See {@see \PHPCSUtils\Utils\UseStatements::splitImportUseStatement()} for more details about the array format.

splitImportUseStatement()

Split an import use statement into individual imports.

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

Handles single import, multi-import and group-import use 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
1.0.0
throws
RuntimeException

If the specified position is not a T_USE token.

throws
RuntimeException

If the T_USE token is not for 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 a particular 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:

array(
  'name'     => array(),
  'function' => array(
    'Alias'        => 'Vendor\Package\LevelA\Name',
    'Another_Name' => 'Vendor\Package\LevelB\Another_Name',
  ),
  'const'    => array(),
)

Search results