private function getNewOrder(array $uses, Tokens $tokens) { $uses = array_reverse($uses); $indexes = array(); $originalIndexes = array(); foreach ($uses as $index) { $endIndex = $tokens->getNextTokenOfKind($index, array(';')); $startIndex = $tokens->getTokenNotOfKindSibling($index + 1, 1, array(array(T_WHITESPACE))); $namespace = ''; $index = $startIndex; while ($index <= $endIndex) { $token = $tokens[$index]; /** @var Token $token */ if ($token->equals(',') || $index === $endIndex) { $indexes[$startIndex] = array($namespace, $startIndex, $index - 1); $originalIndexes[] = $startIndex; $namespace = ''; $nextPartIndex = $tokens->getTokenNotOfKindSibling($index, 1, array(array(','), array(T_WHITESPACE))); $startIndex = $nextPartIndex; $index = $nextPartIndex; continue; } $namespace .= $token->getContent(); ++$index; } } uasort($indexes, 'self::sortingCallBack'); $i = -1; $usesOrder = array(); // Loop trough the index but use original index order foreach ($indexes as $v) { $usesOrder[$originalIndexes[++$i]] = $v; } return $usesOrder; }
/** * {@inheritdoc} */ public function process(Tokens $tokens) { foreach ($tokens->findGivenKind(T_ARRAY) as $index => $token) { $nextIndex = $tokens->getTokenNotOfKindSibling($index, 1, array(array(T_WHITESPACE), array(T_COMMENT), array(T_DOC_COMMENT))); $nextToken = $tokens[$nextIndex]; if (!$nextToken->equals('(')) { $token->override(array(CT_ARRAY_TYPEHINT, $token->getContent(), $token->getLine())); } } }
/** * {@inheritdoc} */ public function process(Tokens $tokens) { foreach ($tokens->findGivenKind(T_CLASS) as $index => $token) { $prevIndex = $tokens->getTokenNotOfKindSibling($index, -1, array(array(T_WHITESPACE), array(T_COMMENT), array(T_DOC_COMMENT))); $prevToken = $tokens[$prevIndex]; if ($prevToken->isGivenKind(T_DOUBLE_COLON)) { $token->override(array(CT_CLASS_CONSTANT, $token->getContent(), $token->getLine())); } } }
private function fixArray(Tokens $tokens, $index) { if (!$tokens->isArrayMultiLine($index)) { return; } $startIndex = $index; if ($tokens[$startIndex]->isGivenKind(T_ARRAY)) { $startIndex = $tokens->getNextTokenOfKind($startIndex, array('(')); $endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startIndex); } else { $endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_SQUARE_BRACE, $startIndex); } $beforeEndIndex = $tokens->getTokenNotOfKindSibling($endIndex, -1, array(array(T_WHITESPACE), array(T_COMMENT), array(T_DOC_COMMENT))); $beforeEndToken = $tokens[$beforeEndIndex]; // if there is some item between braces then add `,` after it if ($startIndex !== $beforeEndIndex && !$beforeEndToken->equalsAny(array(',', array(T_END_HEREDOC)))) { $tokens->insertAt($beforeEndIndex + 1, new Token(',')); } }
private function fixArray(Tokens $tokens, $index) { if ($tokens->isArrayMultiLine($index)) { return; } $startIndex = $index; if ($tokens[$startIndex]->isGivenKind(T_ARRAY)) { $startIndex = $tokens->getNextTokenOfKind($startIndex, array('(')); $endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startIndex); } else { $endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_SQUARE_BRACE, $startIndex); } $beforeEndIndex = $tokens->getTokenNotOfKindSibling($endIndex, -1, array(array(T_WHITESPACE), array(T_COMMENT), array(T_DOC_COMMENT))); $beforeEndToken = $tokens[$beforeEndIndex]; if ($beforeEndToken->equals(',')) { $tokens->removeTrailingWhitespace($beforeEndIndex); $beforeEndToken->clear(); } }
private function getNewOrder(array $uses, Tokens $tokens) { $uses = array_reverse($uses); $indexes = array(); $originalIndexes = array(); foreach ($uses as $index) { $startIndex = $tokens->getTokenNotOfKindSibling($index + 1, 1, array(array(T_WHITESPACE))); $endIndex = $tokens->getNextTokenOfKind($startIndex, array(';', array(T_CLOSE_TAG))); $previous = $tokens->getPrevMeaningfulToken($endIndex); $group = $tokens[$previous]->equals('}'); if ($tokens[$startIndex]->isGivenKind(array(T_CONST))) { $type = self::IMPORT_TYPE_CONST; } elseif ($tokens[$startIndex]->isGivenKind(array(T_FUNCTION))) { $type = self::IMPORT_TYPE_FUNCTION; } else { $type = self::IMPORT_TYPE_CLASS; } $namespace = ''; $index = $startIndex; while ($index <= $endIndex) { $token = $tokens[$index]; if ($index === $endIndex || !$group && $token->equals(',')) { $indexes[$startIndex] = array('namespace' => $namespace, 'startIndex' => $startIndex, 'endIndex' => $index - 1, 'importType' => $type); $originalIndexes[] = $startIndex; if ($index === $endIndex) { break; } $namespace = ''; $nextPartIndex = $tokens->getTokenNotOfKindSibling($index, 1, array(array(','), array(T_WHITESPACE))); $startIndex = $nextPartIndex; $index = $nextPartIndex; continue; } $namespace .= $token->getContent(); ++$index; } } uasort($indexes, 'self::sortingCallBack'); $index = -1; $usesOrder = array(); // Loop trough the index but use original index order foreach ($indexes as $v) { $usesOrder[$originalIndexes[++$index]] = $v; } return $usesOrder; }