PHP_CodeSniffer_File::getDeclarationName PHP Method

getDeclarationName() public method

Returns the declaration names for T_CLASS, T_INTERFACE and T_FUNCTION tokens.
public getDeclarationName ( integer $stackPtr ) : string | null
$stackPtr integer The position of the declaration token which declared the class, interface or function.
return string | null The name of the class, interface or function. or NULL if the function is a closure.
    public function getDeclarationName($stackPtr)
    {
        $tokenCode = $this->_tokens[$stackPtr]['code'];
        if ($tokenCode !== T_FUNCTION && $tokenCode !== T_CLASS && $tokenCode !== T_INTERFACE && $tokenCode !== T_TRAIT) {
            throw new PHP_CodeSniffer_Exception('Token type "' . $this->_tokens[$stackPtr]['type'] . '" is not T_FUNCTION, T_CLASS, T_INTERFACE or T_TRAIT');
        }
        if ($tokenCode === T_FUNCTION && $this->isAnonymousFunction($stackPtr) === true) {
            return null;
        }
        $content = null;
        for ($i = $stackPtr; $i < $this->numTokens; $i++) {
            if ($this->_tokens[$i]['code'] === T_STRING) {
                $content = $this->_tokens[$i]['content'];
                break;
            }
        }
        return $content;
    }

Usage Example

 public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $scope = $phpcsFile->getDeclarationName($stackPtr);
     $class = $phpcsFile->getDeclarationName($phpcsFile->findPrevious([T_CLASS, T_INTERFACE], $stackPtr));
     $tokens = $phpcsFile->getTokens();
     $function = $tokens[$stackPtr];
     if (!isset($function['scope_opener'])) {
         // No scope means it's an abstract/interface function declaration
         return;
     }
     $scopeTypes = [T_IF, T_ELSE, T_ELSEIF, T_FOR, T_FOREACH, T_SWITCH, T_WHILE];
     $functionStartPtr = $function['scope_opener'];
     $functionEndPtr = $function['scope_closer'];
     $ptr = $functionStartPtr;
     // var_dump($class, $scope);
     while ($ptr = $phpcsFile->findNext($scopeTypes, $ptr + 1, $functionEndPtr)) {
         $token = $tokens[$ptr];
         if ($token['code'] != T_IF && $token['code'] != T_ELSE && $token['code'] != T_ELSEIF) {
             $this->getBlankLineCountBefore($phpcsFile, $tokens, $functionStartPtr, $ptr - 1);
             $this->getBlankLineCountAfter($phpcsFile, $tokens, $token['scope_closer'] + 1, $functionEndPtr);
         } elseif ($token['code'] == T_IF) {
             $this->getBlankLineCountBefore($phpcsFile, $tokens, $functionStartPtr, $ptr);
             $this->getBlankLineCountAfter($phpcsFile, $tokens, $token['scope_closer'] + 1, $functionEndPtr, [T_ELSE, T_ELSEIF]);
         } elseif ($token['code'] == T_ELSEIF) {
             $this->getBlankLineCountBefore($phpcsFile, $tokens, $functionStartPtr, $ptr, [T_CLOSE_CURLY_BRACKET]);
             $this->getBlankLineCountAfter($phpcsFile, $tokens, $token['scope_closer'] + 1, $functionEndPtr, [T_ELSE, T_ELSEIF]);
         } elseif ($token['code'] == T_ELSE) {
             $this->getBlankLineCountBefore($phpcsFile, $tokens, $functionStartPtr, $ptr, [T_CLOSE_CURLY_BRACKET]);
             $this->getBlankLineCountAfter($phpcsFile, $tokens, $token['scope_closer'] + 1, $functionEndPtr);
         }
     }
 }
All Usage Examples Of PHP_CodeSniffer_File::getDeclarationName