Documentation

ObjectDeclarations

Utility functions for use when examining object declaration statements.

Table of Contents

getName() Retrieves the declaration name for classes, interfaces, traits, and functions. string|null
getClassProperties() Retrieves the implementation properties of a class. array
findExtendedClassName() Retrieves the name of the class that the specified class extends. string|bool
findImplementedInterfaceNames() Retrieves the names of the interfaces that the specified class implements. array|bool
findExtendedInterfaceNames() Retrieves the names of the interfaces that the specified interface extends. array|bool
findNames() Retrieves the names of the extended classes or interfaces or the implemented interfaces that the specific class/interface declaration extends/implements. array|bool

Methods

getName()

Retrieves the declaration name for classes, interfaces, traits, and functions.

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

Note: For ES6 classes in combination with PHPCS 2.x, passing a T_STRING token to this method will be accepted for JS files. Note: support for JS ES6 method syntax has not (yet) been back-filled for PHPCS < 3.0.0.

Main differences with the PHPCS version:

  • Defensive coding against incorrect calls to this method.
  • Improved handling of invalid names, like names starting with a number. This allows sniffs to report on invalid names instead of ignoring them.
  • Bug fix: improved handling of parse errors. Using the original method, a parse error due to an invalid name could cause the method to return the name of the next construct, a partial name and/or the name of a class being extended/interface being implemented. Using this version of the utility method, either the complete name (invalid or not) will be returned or null in case of no name (parse error).
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

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

Tags
see

Original source.

see

Cross-version compatible version of the original.

since
throws

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

Return values
string|null

The name of the class, interface, trait, or function; or NULL if the passed token doesn't exist, the function or class is anonymous or in case of a parse error/live coding.

getClassProperties()

Retrieves the implementation properties of a class.

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

The format of the return value is: array( 'is_abstract' => false, // true if the abstract keyword was found. 'is_final' => false, // true if the final keyword was found. );

Main differences with the PHPCS version:

  • Bugs fixed:
    • Handling of PHPCS annotations.
    • Handling of unorthodox docblock placement.
    • A class cannot both be abstract as well as final, so this utility should not allow for that.
  • Defensive coding against incorrect calls to this method.
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

Original source.

see

Cross-version compatible version of the original.

since
throws

If the specified position is not a T_CLASS token.

Return values
array

findExtendedClassName()

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

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

Works for classes, anonymous classes and interfaces, though it is strongly recommended to use the \PHPCSUtils\Utils\ObjectDeclarations::findExtendedInterfaceNames() method to examine interfaces instead. Interfaces can extend multiple parent interfaces, and that use case is not handled by this method.

Main differences with the PHPCS version:

  • Bugs fixed:
    • Handling of PHPCS annotations.
    • Handling of comments.
  • Improved handling of parse errors.
  • The returned name will be clean of superfluous whitespace and/or comments.
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The stack position of the class or interface.

Tags
see

Original source.

see

Cross-version compatible version of the original.

see

Similar method for extended interfaces.

since
Return values
string|bool

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

findImplementedInterfaceNames()

Retrieves the names of the interfaces that the specified class implements.

public static findImplementedInterfaceNames( $phpcsFile : File , $stackPtr : int ) : array|bool

Main differences with the PHPCS version:

  • Bugs fixed:
    • Handling of PHPCS annotations.
    • Handling of comments.
  • Improved handling of parse errors.
  • The returned name(s) will be clean of superfluous whitespace and/or comments.
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The stack position of the class.

Tags
see

Original source.

see

Cross-version compatible version of the original.

since
Return values
array|bool

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

findExtendedInterfaceNames()

Retrieves the names of the interfaces that the specified interface extends.

public static findExtendedInterfaceNames( $phpcsFile : File , $stackPtr : int ) : array|bool
Parameters
$phpcsFile : File

The file where this token was found.

$stackPtr : int

The stack position of the interface keyword.

Tags
see

Similar method for extended classes.

since
Return values
array|bool

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

findNames()

Retrieves the names of the extended classes or interfaces or the implemented interfaces that the specific class/interface declaration extends/implements.

private static findNames( $phpcsFile : File , $stackPtr : int , $keyword : int , $allowedFor : array ) : array|bool
Parameters
$phpcsFile : File

The file where this token was found.

$stackPtr : int

The stack position of the class/interface declaration keyword.

$keyword : int

The token constant for the keyword to examine. Either T_EXTENDS or T_IMPLEMENTS.

$allowedFor : array

Array of OO types for which use of the keyword is allowed.

Tags
since
Return values
array|bool

Returns an array of names or false on error or when the object being declared does not extend/implement another object.

Search results