function File::getDeclarationName
Returns the declaration name for classes, interfaces, traits, enums, and functions.
Parameters
int $stackPtr The position of the declaration token which: declared the class, interface, trait, or function.
Return value
string|null The name of the class, interface, trait, or function; or NULL if the function or class is anonymous.
Throws
\PHP_CodeSniffer\Exceptions\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.
File
-
vendor/
squizlabs/ php_codesniffer/ src/ Files/ File.php, line 1282
Class
Namespace
PHP_CodeSniffer\FilesCode
public function getDeclarationName($stackPtr) {
$tokenCode = $this->tokens[$stackPtr]['code'];
if ($tokenCode === T_ANON_CLASS || $tokenCode === T_CLOSURE) {
return null;
}
if ($tokenCode !== T_FUNCTION && $tokenCode !== T_CLASS && $tokenCode !== T_INTERFACE && $tokenCode !== T_TRAIT && $tokenCode !== T_ENUM) {
throw new RuntimeException('Token type "' . $this->tokens[$stackPtr]['type'] . '" is not T_FUNCTION, T_CLASS, T_INTERFACE, T_TRAIT or T_ENUM');
}
if ($tokenCode === T_FUNCTION && strtolower($this->tokens[$stackPtr]['content']) !== 'function') {
// This is a function declared without the "function" keyword.
// So this token is the function name.
return $this->tokens[$stackPtr]['content'];
}
$content = null;
for ($i = $stackPtr; $i < $this->numTokens; $i++) {
if ($this->tokens[$i]['code'] === T_STRING) {
$content = $this->tokens[$i]['content'];
break;
}
}
return $content;
}