PHPCSUtils 1.0.10

BCFile
in package

FinalYes

PHPCS native utility functions.

Backport of the latest versions of PHPCS native utility functions to make them available in older PHPCS versions without the bugs and other quirks that the older versions of the native functions had.

Additionally, this class works round the following tokenizer issues for any affected utility functions:

  • readonly classes.
  • Constructor property promotion with readonly without visibility.
  • OO methods called self, parent or static.

Most functions in this class will have a related twin-function in the relevant class in the PHPCSUtils\Utils namespace. These will be indicated with @see tags in the docblock of the function.

The PHPCSUtils native twin-functions will often have additional features and/or improved functionality, but will generally be fully compatible with the PHPCS native functions. The differences between the functions here and the twin functions are documented in the docblock of the respective twin-function.

Tags
see
File

Original source of these utility methods.

since
1.0.0

Table of Contents

Methods

findEndOfStatement()  : int
Returns the position of the last non-whitespace token in a statement.
findExtendedClassName()  : string|false
Returns the name of the class that the specified class extends.
findImplementedInterfaceNames()  : array<string|int, string>|false
Returns the names of the interfaces that the specified class or enum implements.
findStartOfStatement()  : int
Returns the position of the first non-whitespace token in a statement.
getClassProperties()  : array<string, bool>
Returns the implementation properties of a class.
getCondition()  : int|false
Return the position of the condition for the passed token.
getDeclarationName()  : string|null
Returns the declaration name for classes, interfaces, traits, enums, and functions.
getMemberProperties()  : array<string, mixed>
Returns the visibility and implementation properties of a class member var.
getMethodParameters()  : array<int, array<string, mixed>>
Returns the method parameters for the specified function token.
getMethodProperties()  : array<string, mixed>
Returns the visibility and implementation properties of a method.
getTokensAsString()  : string
Returns the content of the tokens from the specified start position in the token stack for the specified length.
hasCondition()  : bool
Determine if the passed token has a condition of one of the passed types.
isReference()  : bool
Determine if the passed token is a reference operator.

Methods

findEndOfStatement()

Returns the position of the last non-whitespace token in a statement.

public static findEndOfStatement(File $phpcsFile, int $start[, int|string|array<string|int, int|string> $ignore = null ]) : int

PHPCS cross-version compatible version of the `File::findEndOfStatement() method.

Changelog for the PHPCS native function:

  • Introduced in PHPCS 2.1.0.
  • The upstream method has received no significant updates since PHPCS 3.9.0.
Parameters
$phpcsFile : File

The file being scanned.

$start : int

The position to start searching from in the token stack.

$ignore : int|string|array<string|int, int|string> = null

Token types that should not be considered stop points.

Tags
see
File::findEndOfStatement()

Original source.

since
1.0.0
Return values
int

findExtendedClassName()

Returns the name of the class that the specified class extends.

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

(works for classes, anonymous classes and interfaces)

PHPCS cross-version compatible version of the File::findExtendedClassName() method.

Changelog for the PHPCS native function:

  • Introduced in PHPCS 1.2.0.
  • The upstream method has received no significant updates since PHPCS 3.9.0.
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The stack position of the class or interface.

Tags
see
File::findExtendedClassName()

Original source.

see
ObjectDeclarations::findExtendedClassName()

PHPCSUtils native improved version.

since
1.0.0
Return values
string|false

The extended class name or FALSE on error or if there is no extended class name.

findImplementedInterfaceNames()

Returns the names of the interfaces that the specified class or enum implements.

public static findImplementedInterfaceNames(File $phpcsFile, int $stackPtr) : array<string|int, string>|false

PHPCS cross-version compatible version of the File::findImplementedInterfaceNames() method.

Changelog for the PHPCS native function:

  • Introduced in PHPCS 2.7.0.
  • The upstream method has received no significant updates since PHPCS 3.9.0.
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The stack position of the class or enum token.

Tags
see
File::findImplementedInterfaceNames()

Original source.

see
ObjectDeclarations::findImplementedInterfaceNames()

PHPCSUtils native improved version.

since
1.0.0
Return values
array<string|int, string>|false

Array with names of the implemented interfaces or FALSE on error or if there are no implemented interface names.

findStartOfStatement()

Returns the position of the first non-whitespace token in a statement.

public static findStartOfStatement(File $phpcsFile, int $start[, int|string|array<string|int, int|string> $ignore = null ]) : int

PHPCS cross-version compatible version of the File::findStartOfStatement() method.

Changelog for the PHPCS native function:

  • Introduced in PHPCS 2.1.0.
  • The upstream method has received no significant updates since PHPCS 3.9.0.
Parameters
$phpcsFile : File

The file being scanned.

$start : int

The position to start searching from in the token stack.

$ignore : int|string|array<string|int, int|string> = null

Token types that should not be considered stop points.

Tags
see
File::findStartOfStatement()

Original source.

since
1.0.0
Return values
int

getClassProperties()

Returns the implementation properties of a class.

public static getClassProperties(File $phpcsFile, int $stackPtr) : array<string, bool>

The format of the return value is:

array(
  'is_abstract' => boolean, // TRUE if the abstract keyword was found.
  'is_final'    => boolean, // TRUE if the final keyword was found.
  'is_readonly' => boolean, // TRUE if the readonly keyword was found.
);

PHPCS cross-version compatible version of the File::getClassProperties() method.

Changelog for the PHPCS native function:

  • Introduced in PHPCS 1.3.0.
  • The upstream method has received no significant updates since PHPCS 3.9.0.
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position in the stack of the T_CLASS token to acquire the properties for.

Tags
see
File::getClassProperties()

Original source.

see
ObjectDeclarations::getClassProperties()

PHPCSUtils native improved version.

since
1.0.0
throws
RuntimeException

If the specified position is not a T_CLASS token.

Return values
array<string, bool>

getCondition()

Return the position of the condition for the passed token.

public static getCondition(File $phpcsFile, int $stackPtr, int|string $type[, bool $first = true ]) : int|false

PHPCS cross-version compatible version of the File::getCondition() method.

Changelog for the PHPCS native function:

  • Introduced in PHPCS 1.3.0.
  • The upstream method has received no significant updates since PHPCS 3.9.0.
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the token we are checking.

$type : int|string

The type of token to search for.

$first : bool = true

If true, will return the matched condition furthest away from the passed token. If false, will return the matched condition closest to the passed token.

Tags
see
File::getCondition()

Original source.

see
Conditions::getCondition()

More versatile alternative.

since
1.0.0
Return values
int|false

Integer stack pointer to the condition or FALSE if the token does not have the condition.

getDeclarationName()

Returns the declaration name for classes, interfaces, traits, enums, and functions.

public static getDeclarationName(File $phpcsFile, int $stackPtr) : string|null

PHPCS cross-version compatible version of the File::getDeclarationName() method.

Changelog for the PHPCS native function:

  • Introduced in PHPCS 0.0.5.
  • The upstream method has received no significant updates since PHPCS 3.9.0.
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the declaration token which declared the class, interface, trait, enum or function.

Tags
see
File::getDeclarationName()

Original source.

see
ObjectDeclarations::getName()

PHPCSUtils native improved version.

since
1.0.0
throws
RuntimeException

If the specified token is not of type T_FUNCTION, T_CLASS, T_ANON_CLASS, T_CLOSURE, T_TRAIT, T_ENUM or T_INTERFACE.

Return values
string|null

The name of the class, interface, trait, enum, or function; or NULL if the function or class is anonymous or in case of a parse error/live coding.

getMemberProperties()

Returns the visibility and implementation properties of a class member var.

public static getMemberProperties(File $phpcsFile, int $stackPtr) : array<string, mixed>

The format of the return value is:

array(
  'scope'           => string,        // Public, private, or protected.
  'scope_specified' => boolean,       // TRUE if the scope was explicitly specified.
  'is_static'       => boolean,       // TRUE if the static keyword was found.
  'is_readonly'     => boolean,       // TRUE if the readonly keyword was found.
  'type'            => string,        // The type of the var (empty if no type specified).
  'type_token'      => integer|false, // The stack pointer to the start of the type
                                      // or FALSE if there is no type.
  'type_end_token'  => integer|false, // The stack pointer to the end of the type
                                      // or FALSE if there is no type.
  'nullable_type'   => boolean,       // TRUE if the type is preceded by the
                                      // nullability operator.
);

PHPCS cross-version compatible version of the `File::getMemberProperties() method.

Changelog for the PHPCS native function:

  • Introduced in PHPCS 0.0.5.
  • The upstream method has received no significant updates since PHPCS 3.9.0.
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position in the stack of the T_VARIABLE token to acquire the properties for.

Tags
see
File::getMemberProperties()

Original source.

see
Variables::getMemberProperties()

PHPCSUtils native improved version.

since
1.0.0
throws
RuntimeException

If the specified position is not a T_VARIABLE token, or if the position is not a class member variable.

Return values
array<string, mixed>

getMethodParameters()

Returns the method parameters for the specified function token.

public static getMethodParameters(File $phpcsFile, int $stackPtr) : array<int, array<string, mixed>>

Also supports passing in a T_USE token for a closure use group.

Each parameter is in the following format:

0 => array(
  'name'                => string,        // The variable name.
  'token'               => integer,       // The stack pointer to the variable name.
  'content'             => string,        // The full content of the variable definition.
  'has_attributes'      => boolean,       // Does the parameter have one or more attributes attached ?
  'pass_by_reference'   => boolean,       // Is the variable passed by reference?
  'reference_token'     => integer|false, // The stack pointer to the reference operator
                                          // or FALSE if the param is not passed by reference.
  'variable_length'     => boolean,       // Is the param of variable length through use of `...` ?
  'variadic_token'      => integer|false, // The stack pointer to the ... operator
                                          // or FALSE if the param is not variable length.
  'type_hint'           => string,        // The type hint for the variable.
  'type_hint_token'     => integer|false, // The stack pointer to the start of the type hint
                                          // or FALSE if there is no type hint.
  'type_hint_end_token' => integer|false, // The stack pointer to the end of the type hint
                                          // or FALSE if there is no type hint.
  'nullable_type'       => boolean,       // TRUE if the param type is preceded by the nullability
                                          // operator.
  'comma_token'         => integer|false, // The stack pointer to the comma after the param
                                          // or FALSE if this is the last param.
)

Parameters with default values have the following additional array indexes:

  'default'             => string,  // The full content of the default value.
  'default_token'       => integer, // The stack pointer to the start of the default value.
  'default_equal_token' => integer, // The stack pointer to the equals sign.

Parameters declared using PHP 8 constructor property promotion, have these additional array indexes:

  'property_visibility' => string,        // The property visibility as declared.
  'visibility_token'    => integer|false, // The stack pointer to the visibility modifier token.
                                          // or FALSE if the visibility is not explicitly declared.
  'property_readonly'   => boolean,       // TRUE if the readonly keyword was found.
  'readonly_token'      => integer,       // The stack pointer to the readonly modifier token.
                                          // This index will only be set if the property is readonly.

PHPCS cross-version compatible version of the File::getMethodParameters() method.

Changelog for the PHPCS native function:

  • Introduced in PHPCS 0.0.5.
  • PHPCS 3.8.0: Added support for constructor property promotion with readonly without explicit visibility.
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position in the stack of the function token to acquire the parameters for.

Tags
see
File::getMethodParameters()

Original source.

see
FunctionDeclarations::getParameters()

PHPCSUtils native improved version.

since
1.0.0
since
1.0.6

Sync with PHPCS 3.8.0, support for readonly properties without explicit visibility. PHPCS#3801.

throws
RuntimeException

If the specified $stackPtr is not of type T_FUNCTION, T_CLOSURE, T_USE, or T_FN.

Return values
array<int, array<string, mixed>>

getMethodProperties()

Returns the visibility and implementation properties of a method.

public static getMethodProperties(File $phpcsFile, int $stackPtr) : array<string, mixed>

The format of the return value is:

array(
  'scope'                 => string,        // Public, private, or protected
  'scope_specified'       => boolean,       // TRUE if the scope keyword was found.
  'return_type'           => string,        // The return type of the method.
  'return_type_token'     => integer|false, // The stack pointer to the start of the return type
                                            // or FALSE if there is no return type.
  'return_type_end_token' => integer|false, // The stack pointer to the end of the return type
                                            // or FALSE if there is no return type.
  'nullable_return_type'  => boolean,       // TRUE if the return type is preceded by
                                            // the nullability operator.
  'is_abstract'           => boolean,       // TRUE if the abstract keyword was found.
  'is_final'              => boolean,       // TRUE if the final keyword was found.
  'is_static'             => boolean,       // TRUE if the static keyword was found.
  'has_body'              => boolean,       // TRUE if the method has a body
);

PHPCS cross-version compatible version of the File::getMethodProperties() method.

Changelog for the PHPCS native function:

  • Introduced in PHPCS 0.0.5.
  • The upstream method has received no significant updates since PHPCS 3.9.0.
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position in the stack of the function token to acquire the properties for.

Tags
see
File::getMethodProperties()

Original source.

see
FunctionDeclarations::getProperties()

PHPCSUtils native improved version.

since
1.0.0
throws
RuntimeException

If the specified position is not a T_FUNCTION, T_CLOSURE, or T_FN token.

Return values
array<string, mixed>

getTokensAsString()

Returns the content of the tokens from the specified start position in the token stack for the specified length.

public static getTokensAsString(File $phpcsFile, int $start, int $length[, bool $origContent = false ]) : string

PHPCS cross-version compatible version of the File::getTokensAsString() method.

Changelog for the PHPCS native function:

  • Introduced in PHPCS 0.0.5.
  • The upstream method has received no significant updates since PHPCS 3.9.0.
Parameters
$phpcsFile : File

The file being scanned.

$start : int

The position to start from in the token stack.

$length : int

The length of tokens to traverse from the start pos.

$origContent : bool = false

Whether the original content or the tab replaced content should be used.

Tags
see
File::getTokensAsString()

Original source.

see
GetTokensAsString

Related set of functions.

since
1.0.0
throws
RuntimeException

If the specified start position does not exist.

Return values
string

The token contents.

hasCondition()

Determine if the passed token has a condition of one of the passed types.

public static hasCondition(File $phpcsFile, int $stackPtr, int|string|array<string|int, int|string> $types) : bool

PHPCS cross-version compatible version of the File::hasCondition() method.

Changelog for the PHPCS native function:

  • Introduced in PHPCS 0.0.5.
  • The upstream method has received no significant updates since PHPCS 3.9.0.
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the token we are checking.

$types : int|string|array<string|int, int|string>

The type(s) of tokens to search for.

Tags
see
File::hasCondition()

Original source.

see
Conditions::hasCondition()

PHPCSUtils native alternative.

since
1.0.0
Return values
bool

isReference()

Determine if the passed token is a reference operator.

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

PHPCS cross-version compatible version of the File::isReference() method.

Changelog for the PHPCS native function:

  • Introduced in PHPCS 0.0.5.
  • The upstream method has received no significant updates since PHPCS 3.9.0.
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the T_BITWISE_AND token.

Tags
see
File::isReference()

Original source.

see
Operators::isReference()

PHPCSUtils native improved version.

since
1.0.0
Return values
bool

TRUE if the specified token position represents a reference. FALSE if the token represents a bitwise operator.


        
On this page

Search results