PhpCsFixer\Tokenizer\Tokens::getPrevTokenOfKind PHP Method

getPrevTokenOfKind() public method

This method is shorthand for getTokenOfKindSibling method.
public getPrevTokenOfKind ( integer $index, array $tokens = [], boolean $caseSensitive = true ) : integer | null
$index integer token index
$tokens array possible tokens
$caseSensitive boolean perform a case sensitive comparison
return integer | null
    public function getPrevTokenOfKind($index, array $tokens = array(), $caseSensitive = true)
    {
        return $this->getTokenOfKindSibling($index, -1, $tokens, $caseSensitive);
    }

Usage Example

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     $classes = array_keys($tokens->findGivenKind(T_CLASS));
     $numClasses = count($classes);
     for ($i = 0; $i < $numClasses; ++$i) {
         $index = $classes[$i];
         // is it an an anonymous class definition?
         if ($tokensAnalyzer->isAnonymousClass($index)) {
             continue;
         }
         // is it inside a namespace?
         $nspIndex = $tokens->getPrevTokenOfKind($index, array(array(T_NAMESPACE, 'namespace')));
         if (null !== $nspIndex) {
             $nspIndex = $tokens->getNextMeaningfulToken($nspIndex);
             // make sure it's not the global namespace, as PHP4 constructors are allowed in there
             if (!$tokens[$nspIndex]->equals('{')) {
                 // unless it's the global namespace, the index currently points to the name
                 $nspIndex = $tokens->getNextTokenOfKind($nspIndex, array(';', '{'));
                 if ($tokens[$nspIndex]->equals(';')) {
                     // the class is inside a (non-block) namespace, no PHP4-code should be in there
                     break;
                 }
                 // the index points to the { of a block-namespace
                 $nspEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $nspIndex);
                 if ($index < $nspEnd) {
                     // the class is inside a block namespace, skip other classes that might be in it
                     for ($j = $i + 1; $j < $numClasses; ++$j) {
                         if ($classes[$j] < $nspEnd) {
                             ++$i;
                         }
                     }
                     // and continue checking the classes that might follow
                     continue;
                 }
             }
         }
         $classNameIndex = $tokens->getNextMeaningfulToken($index);
         $className = $tokens[$classNameIndex]->getContent();
         $classStart = $tokens->getNextTokenOfKind($classNameIndex, array('{'));
         $classEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $classStart);
         $this->fixConstructor($tokens, $className, $classStart, $classEnd);
         $this->fixParent($tokens, $classStart, $classEnd);
     }
 }
All Usage Examples Of PhpCsFixer\Tokenizer\Tokens::getPrevTokenOfKind