PHPCSUtils 1.0.10

UseStatements
in package

FinalYes

Utility functions for examining use statements.

Tags
since
1.0.0

Table of Contents

Methods

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.
mergeImportUseStatements()  : array<string, array<string, string>>
Merge two import use statement arrays.
splitAndMergeImportUseStatement()  : array<string, array<string, string>>
Split an import use statement into individual imports and merge it with an array of previously seen import use statements.
splitImportUseStatement()  : array<string, array<string, string>>
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.

mergeImportUseStatements()

Merge two import use statement arrays.

public static mergeImportUseStatements(array<string, array<string, string>> $previousUseStatements, array<string, array<string, string>> $currentUseStatement) : array<string, array<string, string>>

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.

Parameters
$previousUseStatements : array<string, array<string, string>>

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 UseStatements::splitImportUseStatement() method.

$currentUseStatement : array<string, array<string, string>>

The parsed import use statements to merge with the previously collected use statements. This should be the output of a call to the UseStatements::splitImportUseStatement() method.

Tags
see
UseStatements::splitImportUseStatement()
since
1.0.0
Return values
array<string, array<string, string>>

A multi-level array containing information about the current use statement combined with the previously collected use statement information. See UseStatements::splitImportUseStatement() for more details about the array format.

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<string, array<string, string>> $previousUseStatements) : array<string, array<string, string>>

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 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<string, array<string, string>>

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 UseStatements::splitImportUseStatement() method.

Tags
see
AbstractFileContextSniff
see
UseStatements::splitImportUseStatement()
see
UseStatements::mergeImportUseStatements()
since
1.0.0
Return values
array<string, array<string, string>>

A multi-level array containing information about the current use statement combined with the previously collected use statement information. See 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<string, array<string, string>>

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<string, array<string, string>>

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(),
)

        
On this page

Search results