Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. File.php

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

File

Namespace

PHP_CodeSniffer\Files

Code

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;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal