ObjectDeclarations
Utility functions for use when examining object declaration statements.
Tags
Table of Contents
- findExtendedClassName() : string|false
- Retrieves the name of the class that the specified class extends.
- findExtendedInterfaceNames() : array|false
- Retrieves the names of the interfaces that the specified interface extends.
- findImplementedInterfaceNames() : array|false
- Retrieves the names of the interfaces that the specified class implements.
- getClassProperties() : array
- Retrieves the implementation properties of a class.
- getName() : string|null
- Retrieves the declaration name for classes, interfaces, traits, and functions.
Methods
findExtendedClassName()
Retrieves 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, 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
Return values
string|false —The extended class name or FALSE
on error or if there
is no extended class name.
findExtendedInterfaceNames()
Retrieves the names of the interfaces that the specified interface extends.
public
static findExtendedInterfaceNames(File $phpcsFile, int $stackPtr) : array|false
Parameters
- $phpcsFile : File
-
The file where this token was found.
- $stackPtr : int
-
The stack position of the interface keyword.
Tags
Return values
array|false —Array with names of the extended interfaces or FALSE
on
error or if there are no extended interface names.
findImplementedInterfaceNames()
Retrieves the names of the interfaces that the specified class implements.
public
static findImplementedInterfaceNames(File $phpcsFile, int $stackPtr) : array|false
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
Return values
array|false —Array with names of the implemented interfaces or FALSE
on
error or if there are no implemented interface names.
getClassProperties()
Retrieves the implementation properties of a class.
public
static getClassProperties(File $phpcsFile, int $stackPtr) : array
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
Return values
array —Array with implementation properties of a class. 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.
);
getName()
Retrieves the declaration name for classes, interfaces, traits, and functions.
public
static getName(File $phpcsFile, int $stackPtr) : string|null
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).
Note:
- For ES6 classes in combination with PHPCS 2.x, passing a
T_STRING
token to this method will be accepted for JS files. - Support for JS ES6 method syntax has not been back-filled for PHPCS < 3.0.0.
Parameters
- $phpcsFile : File
-
The file being scanned.
- $stackPtr : int
-
The position of the declaration token which declared the class, interface, trait, or function.
Tags
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.