PhpCsFixer\Tokenizer\TokensAnalyzer::getMethodAttributes PHP Method

getMethodAttributes() public method

The array has the following items: 'visibility' int|null T_PRIVATE, T_PROTECTED or T_PUBLIC 'static' bool 'abstract' bool 'final' bool
public getMethodAttributes ( integer $index ) : array
$index integer Token index of the method (T_FUNCTION)
return array
    public function getMethodAttributes($index)
    {
        $tokens = $this->tokens;
        $token = $tokens[$index];
        if (!$token->isGivenKind(T_FUNCTION)) {
            throw new \LogicException(sprintf('No T_FUNCTION at given index %d, got %s.', $index, $token->getName()));
        }
        $attributes = array('visibility' => null, 'static' => false, 'abstract' => false, 'final' => false);
        for ($i = $index; $i >= 0; --$i) {
            $tokenIndex = $tokens->getPrevMeaningfulToken($i);
            if (null === $tokenIndex) {
                break;
            }
            $i = $tokenIndex;
            $token = $tokens[$tokenIndex];
            if ($token->isGivenKind(array(T_STATIC))) {
                $attributes['static'] = true;
                continue;
            }
            if ($token->isGivenKind(array(T_FINAL))) {
                $attributes['final'] = true;
                continue;
            }
            if ($token->isGivenKind(array(T_ABSTRACT))) {
                $attributes['abstract'] = true;
                continue;
            }
            // visibility
            if ($token->isGivenKind(array(T_PRIVATE))) {
                $attributes['visibility'] = T_PRIVATE;
                continue;
            }
            if ($token->isGivenKind(array(T_PROTECTED))) {
                $attributes['visibility'] = T_PROTECTED;
                continue;
            }
            if ($token->isGivenKind(array(T_PUBLIC))) {
                $attributes['visibility'] = T_PUBLIC;
                continue;
            }
            // found a meaningful token that is not part of
            // the function signature; stop looking
            break;
        }
        return $attributes;
    }

Usage Example

 /**
  * @param Tokens         $tokens
  * @param TokensAnalyzer $tokensAnalyzer
  * @param int            $classStart
  * @param int            $classEnd
  */
 private function fixClass(Tokens $tokens, TokensAnalyzer $tokensAnalyzer, $classStart, $classEnd)
 {
     for ($index = $classEnd; $index > $classStart; --$index) {
         if (!$tokens[$index]->isGivenKind(T_FUNCTION) || $tokensAnalyzer->isLambda($index)) {
             continue;
         }
         $attributes = $tokensAnalyzer->getMethodAttributes($index);
         if (true === $attributes['abstract']) {
             $methodEnd = $tokens->getNextTokenOfKind($index, array(';'));
         } else {
             $methodStart = $tokens->getNextTokenOfKind($index, array('{'));
             $methodEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $methodStart, true);
         }
         $this->fixSpaceBelowMethod($tokens, $classEnd, $methodEnd);
         $this->fixSpaceAboveMethod($tokens, $classStart, $index);
     }
 }
All Usage Examples Of PhpCsFixer\Tokenizer\TokensAnalyzer::getMethodAttributes