PhpCsFixer\Tokenizer\TokensAnalyzer::getClassyElements PHP Метод

getClassyElements() публичный Метод

Get indexes of methods and properties in classy code (classes, interfaces and traits).
public getClassyElements ( ) : array[]
Результат array[]
    public function getClassyElements()
    {
        $tokens = $this->tokens;
        $tokens->rewind();
        $elements = array();
        $inClass = false;
        $curlyBracesLevel = 0;
        $bracesLevel = 0;
        foreach ($tokens as $index => $token) {
            if ($token->isGivenKind(T_ENCAPSED_AND_WHITESPACE)) {
                continue;
            }
            if (!$inClass) {
                $inClass = $token->isClassy();
                continue;
            }
            if ($token->equals('(')) {
                ++$bracesLevel;
                continue;
            }
            if ($token->equals(')')) {
                --$bracesLevel;
                continue;
            }
            if ($token->equals('{')) {
                ++$curlyBracesLevel;
                continue;
            }
            if ($token->equals('}')) {
                --$curlyBracesLevel;
                if (0 === $curlyBracesLevel) {
                    $inClass = false;
                }
                continue;
            }
            if (1 !== $curlyBracesLevel || !$token->isArray()) {
                continue;
            }
            if (0 === $bracesLevel && $token->isGivenKind(T_VARIABLE)) {
                $elements[$index] = array('token' => $token, 'type' => 'property');
                continue;
            }
            if ($token->isGivenKind(T_FUNCTION)) {
                $elements[$index] = array('token' => $token, 'type' => 'method');
            } elseif ($token->isGivenKind(T_CONST)) {
                $elements[$index] = array('token' => $token, 'type' => 'const');
            }
        }
        return $elements;
    }

Usage Example

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $analyzer = new TokensAnalyzer($tokens);
     $elements = array_reverse($analyzer->getClassyElements(), true);
     foreach ($elements as $index => $element) {
         if (!in_array($element['type'], $this->configuration, true)) {
             continue;
             // not in configuration
         }
         $this->fixElement($tokens, $index);
     }
 }
All Usage Examples Of PhpCsFixer\Tokenizer\TokensAnalyzer::getClassyElements