/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { $tokensAnalyzer = new TokensAnalyzer($tokens); for ($index = $tokens->count() - 1; $index >= 0; --$index) { if ($tokensAnalyzer->isArray($index)) { $this->fixArray($tokens, $index); } } }
/** * @param Tokens $tokens * @param int $index */ private function fixElement(Tokens $tokens, $index) { $tokensAnalyzer = new TokensAnalyzer($tokens); $repeatIndex = $index; while (true) { $repeatIndex = $tokens->getNextMeaningfulToken($repeatIndex); $repeatToken = $tokens[$repeatIndex]; if ($tokensAnalyzer->isArray($repeatIndex)) { if ($repeatToken->isGivenKind(T_ARRAY)) { $repeatIndex = $tokens->getNextTokenOfKind($repeatIndex, array('(')); $repeatIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $repeatIndex); } else { $repeatIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $repeatIndex); } continue; } if ($repeatToken->equals(';')) { return; // no repeating found, no fixing needed } if ($repeatToken->equals(',')) { break; } } $start = $tokens->getPrevTokenOfKind($index, array(';', '{', '}')); $this->expandElement($tokens, $tokens->getNextMeaningfulToken($start), $tokens->getNextTokenOfKind($index, array(';'))); }
/** * @dataProvider provideArrayExceptions */ public function testIsNotArray($source, $tokenIndex) { $tokens = Tokens::fromCode($source); $tokensAnalyzer = new TokensAnalyzer($tokens); $this->assertFalse($tokensAnalyzer->isArray($tokenIndex)); }