/** * {@inheritdoc} */ public function process(Tokens $tokens) { foreach ($tokens->findGivenKind(T_DOLLAR_OPEN_CURLY_BRACES) as $index => $token) { $nextIndex = $tokens->getNextTokenOfKind($index, array('}')); $tokens[$nextIndex]->override(array(CT_DOLLAR_CLOSE_CURLY_BRACES, '}', $tokens[$nextIndex]->getLine())); } }
/** * Replace all `else if` (T_ELSE T_IF) with `elseif` (T_ELSEIF). * * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { foreach ($tokens as $index => $token) { if (!$token->isGivenKind(T_ELSE)) { continue; } $nextIndex = $tokens->getNextNonWhitespace($index); $nextToken = $tokens[$nextIndex]; // if next meaning token is not T_IF - continue searching, this is not the case for fixing if (!$nextToken->isGivenKind(T_IF)) { continue; } // now we have T_ELSE following by T_IF so we could fix this // 1. clear whitespaces between T_ELSE and T_IF $tokens[$index + 1]->clear(); // 2. change token from T_ELSE into T_ELSEIF $tokens->overrideAt($index, array(T_ELSEIF, 'elseif')); // 3. clear succeeding T_IF $nextToken->clear(); } # handle `T_ELSE T_WHITESPACE T_IF` treated as single `T_ELSEIF` by HHVM # see https://github.com/facebook/hhvm/issues/4796 if (defined('HHVM_VERSION')) { foreach ($tokens->findGivenKind(T_ELSEIF) as $token) { $token->setContent('elseif'); } } }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { /** @var $token \Symfony\CS\Tokenizer\Token */ foreach ($tokens->findGivenKind(T_STRING) as $index => $token) { // skip expressions without parameters list $nextToken = $tokens[$tokens->getNextMeaningfulToken($index)]; if (!$nextToken->equals('(')) { continue; } // skip expressions which are not function reference $prevTokenIndex = $tokens->getPrevMeaningfulToken($index); $prevToken = $tokens[$prevTokenIndex]; if ($prevToken->isGivenKind(array(T_DOUBLE_COLON, T_NEW, T_OBJECT_OPERATOR, T_FUNCTION))) { continue; } // handle function reference with namespaces if ($prevToken->isGivenKind(array(T_NS_SEPARATOR))) { $twicePrevTokenIndex = $tokens->getPrevMeaningfulToken($prevTokenIndex); $twicePrevToken = $tokens[$twicePrevTokenIndex]; if ($twicePrevToken->isGivenKind(array(T_DOUBLE_COLON, T_NEW, T_OBJECT_OPERATOR, T_FUNCTION, T_STRING, CT_NAMESPACE_OPERATOR))) { continue; } } // check mapping hit $tokenContent = strtolower($token->getContent()); if (!isset(self::$aliases[$tokenContent])) { continue; } $token->setContent(self::$aliases[$tokenContent]); } }
private function fixUseStatements(Tokens $tokens) { $classIndexes = array_keys($tokens->findGivenKind(T_CLASS)); if ($tokens->findSequence([[T_STRING, 'OptionsResolverInterface']], array_pop($classIndexes))) { return $this->addUseStatement($tokens, ['Symfony', 'Component', 'OptionsResolver', 'OptionsResolver']); } $this->renameUseStatements($tokens, ['Symfony', 'Component', 'OptionsResolver', 'OptionsResolverInterface'], 'OptionsResolver'); }
/** * {@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())); } } }
/** * {@inheritdoc} */ public function process(Tokens $tokens) { foreach ($tokens->findGivenKind(T_CLASS) as $index => $token) { $prevIndex = $tokens->getPrevMeaningfulToken($index); $prevToken = $tokens[$prevIndex]; if ($prevToken->isGivenKind(T_DOUBLE_COLON)) { $token->override(array(CT_CLASS_CONSTANT, $token->getContent(), $token->getLine())); } } }
public function process(Tokens $tokens) { foreach ($tokens->findGivenKind(T_ARRAY) as $index => $token) { $nextIndex = $tokens->getNextMeaningfulToken($index); $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_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_OBJECT_OPERATOR) as $index => $token) { if (!$tokens[$index + 1]->equals('{')) { continue; } $openIndex = $index + 1; $closeIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $openIndex); $tokens[$openIndex]->override(array(CT_DYNAMIC_PROP_BRACE_OPEN, '{', $tokens[$openIndex]->getLine())); $tokens[$closeIndex]->override(array(CT_DYNAMIC_PROP_BRACE_CLOSE, '}', $tokens[$closeIndex]->getLine())); } }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $token) { $doc = new DocBlock($token->getContent()); $annotations = $doc->getAnnotationsOfType(static::$search); if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $annotation->getTag()->setName(static::$replace); } $token->setContent($doc->getContent()); } }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $token) { $doc = new DocBlock($token->getContent()); $annotations = $doc->getAnnotationsOfType(static::$search); if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $line = $doc->getLine($annotation->getStart()); $line->setContent(str_replace(static::$input, static::$output, $line->getContent())); } $token->setContent($doc->getContent()); } }
public function process(Tokens $tokens) { foreach ($tokens->findGivenKind(T_CURLY_OPEN) as $index => $token) { $level = 1; $nestIndex = $index; while (0 < $level) { ++$nestIndex; if ('{' === $tokens[$nestIndex]->getContent()) { ++$level; continue; } if ('}' === $tokens[$nestIndex]->getContent()) { --$level; } } $tokens[$nestIndex]->override(array(CT_CURLY_CLOSE, '}', $tokens[$nestIndex]->getLine())); } }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { if (!$tokens->isMonolithicPhp()) { return; } $closeTags = $tokens->findGivenKind(T_CLOSE_TAG); if (empty($closeTags)) { return; } list($index, $token) = each($closeTags); $tokens->removeLeadingWhitespace($index); $token->clear(); $prevIndex = $tokens->getPrevNonWhitespace($index); $prevToken = $tokens[$prevIndex]; if (!$prevToken->equalsAny(array(';', '}'))) { $tokens->insertAt($prevIndex + 1, new Token(';')); } }
private function findIncludies(Tokens $tokens) { static $includyTokenKinds = array(T_REQUIRE, T_REQUIRE_ONCE, T_INCLUDE, T_INCLUDE_ONCE); $includies = array(); foreach ($tokens->findGivenKind($includyTokenKinds) as $includyTokens) { foreach ($includyTokens as $index => $token) { $includy = array('begin' => $index, 'braces' => null, 'end' => $tokens->getNextTokenOfKind($index, array(';'))); $nextTokenIndex = $tokens->getNextMeaningfulToken($index); $nextToken = $tokens[$nextTokenIndex]; if ($nextToken->equals('(')) { $braceCloseIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $nextTokenIndex); if ($tokens[$tokens->getNextMeaningfulToken($braceCloseIndex)]->equals(';')) { $includy['braces'] = array('open' => $nextTokenIndex, 'close' => $braceCloseIndex); } } $includies[] = $includy; } } return $includies; }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { $foundNamespace = $tokens->findGivenKind(T_NAMESPACE); if (empty($foundNamespace)) { return; } $tokensAnalyzer = new TokensAnalyzer($tokens); $firstNamespaceIdx = key($foundNamespace); $usesIdxs = $tokensAnalyzer->getImportUseIndexes(); foreach ($usesIdxs as $idx) { if ($idx < $firstNamespaceIdx) { continue; } $nextTokenIdx = $tokens->getNextNonWhitespace($idx); $nextToken = $tokens[$nextTokenIdx]; if ($nextToken->isGivenKind(T_NS_SEPARATOR)) { $nextToken->clear(); } } }
private function findIncludies(Tokens $tokens) { static $includyTokenKinds = array(T_REQUIRE, T_REQUIRE_ONCE, T_INCLUDE, T_INCLUDE_ONCE); $includies = array(); foreach ($tokens->findGivenKind($includyTokenKinds) as $includyTokens) { foreach ($includyTokens as $index => $token) { $includy = array('begin' => $index, 'braces' => null, 'end' => $tokens->getNextTokenOfKind($index, array(';'))); // Don't remove when the statement is wrapped. include is also legal as function parameter // but requires being wrapped then if (!$tokens[$tokens->getPrevNonWhitespace($index)]->equals('(')) { $nextTokenIndex = $tokens->getNextNonWhitespace($index); $nextToken = $tokens[$nextTokenIndex]; if ($nextToken->equals('(')) { $includy['braces'] = array('open' => $nextTokenIndex, 'close' => $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $nextTokenIndex)); } } $includies[] = $includy; } } return $includies; }
private function findIncludies(Tokens $tokens) { static $includyTokenKinds = array(T_REQUIRE, T_REQUIRE_ONCE, T_INCLUDE, T_INCLUDE_ONCE); $includies = array(); foreach ($tokens->findGivenKind($includyTokenKinds) as $includyTokens) { foreach ($includyTokens as $index => $token) { $includy = array('begin' => $index, 'braces' => null, 'end' => $tokens->getNextTokenOfKind($index, array(';', array(T_CLOSE_TAG)))); $nextTokenIndex = $tokens->getNextMeaningfulToken($index); $nextToken = $tokens[$nextTokenIndex]; if ($nextToken->equals('(')) { // Don't remove braces when the statement is wrapped. // Include is also legal as function parameter or condition statement but requires being wrapped then. $braceCloseIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $nextTokenIndex); if ($tokens[$tokens->getNextMeaningfulToken($braceCloseIndex)]->equalsAny(array(';', array(T_CLOSE_TAG)))) { $includy['braces'] = array('open' => $nextTokenIndex, 'close' => $braceCloseIndex); } } $includies[] = $includy; } } return $includies; }
private function fixParent(Tokens $tokens, $classStart, $classEnd) { foreach ($tokens->findGivenKind(T_EXTENDS) as $index => $token) { $parentIndex = $tokens->getNextMeaningfulToken($index); $parentClass = $tokens[$parentIndex]->getContent(); $parentSeq = $tokens->findSequence(array(array(T_STRING), array(T_DOUBLE_COLON), array(T_STRING, $parentClass), '('), $classStart, $classEnd, array(2 => false)); if (null !== $parentSeq) { $parentSeq = array_keys($parentSeq); if ($tokens[$parentSeq[0]]->equalsAny(array(array(T_STRING, 'parent'), array(T_STRING, $parentClass)), false)) { $tokens[$parentSeq[0]]->setContent('parent'); $tokens[$parentSeq[2]]->setContent('__construct'); } } $parentSeq = $tokens->findSequence(array(array(T_VARIABLE, '$this'), array(T_OBJECT_OPERATOR), array(T_STRING, $parentClass), '('), $classStart, $classEnd, array(2 => false)); if (null !== $parentSeq) { $parentSeq = array_keys($parentSeq); $tokens[$parentSeq[0]] = new Token(array(T_STRING, 'parent', $tokens[$parentSeq[0]]->getLine())); $tokens[$parentSeq[1]] = new Token(array(T_DOUBLE_COLON, '::', $tokens[$parentSeq[1]]->getLine())); $tokens[$parentSeq[2]]->setContent('__construct'); } } }
/** * Fix calls to the parent constructor within a class. * * @param Tokens $tokens the Tokens instance * @param int $classStart the class start index * @param int $classEnd the class end index */ private function fixParent(Tokens $tokens, $classStart, $classEnd) { // check calls to the parent constructor foreach ($tokens->findGivenKind(T_EXTENDS) as $index => $token) { $parentIndex = $tokens->getNextMeaningfulToken($index); $parentClass = $tokens[$parentIndex]->getContent(); // using parent::ParentClassName() or ParentClassName::ParentClassName() $parentSeq = $tokens->findSequence(array(array(T_STRING), array(T_DOUBLE_COLON), array(T_STRING, $parentClass), '('), $classStart, $classEnd, array(2 => false)); if (null !== $parentSeq) { // we only need indexes $parentSeq = array_keys($parentSeq); // match either of the possibilities if ($tokens[$parentSeq[0]]->equalsAny(array(array(T_STRING, 'parent'), array(T_STRING, $parentClass)), false)) { // replace with parent::__construct $tokens[$parentSeq[0]]->setContent('parent'); $tokens[$parentSeq[2]]->setContent('__construct'); } } // using $this->ParentClassName() $parentSeq = $tokens->findSequence(array(array(T_VARIABLE, '$this'), array(T_OBJECT_OPERATOR), array(T_STRING, $parentClass), '('), $classStart, $classEnd, array(2 => false)); if (null !== $parentSeq) { // we only need indexes $parentSeq = array_keys($parentSeq); // replace call with parent::__construct() $tokens[$parentSeq[0]] = new Token(array(T_STRING, 'parent', $tokens[$parentSeq[0]]->getLine())); $tokens[$parentSeq[1]] = new Token(array(T_DOUBLE_COLON, '::', $tokens[$parentSeq[1]]->getLine())); $tokens[$parentSeq[2]]->setContent('__construct'); } } }
/** * Returns an array with `implements` data. * * Returns array: * * int 'breakAt' index of the Token of type T_IMPLEMENTS for the definition, or 0 * * int 'numberOfInterfaces' * * bool 'multiLine' * * @param Tokens $tokens * @param int $start * @param int $classyOpen * * @return array */ private function getMultiLineInfo(Tokens $tokens, $start, $classyOpen) { $implementsInfo = array('breakAt' => 0, 'numberOfInterfaces' => 0, 'multiLine' => false); $breakAtToken = $tokens->findGivenKind($tokens[$start]->isGivenKind(T_INTERFACE) ? T_EXTENDS : T_IMPLEMENTS, $start, $classyOpen); if (count($breakAtToken) < 1) { return $implementsInfo; } $implementsInfo['breakAt'] = key($breakAtToken); $classyOpen = $tokens->getPrevNonWhitespace($classyOpen); for ($j = $implementsInfo['breakAt'] + 1; $j < $classyOpen; ++$j) { if ($tokens[$j]->isGivenKind(T_STRING)) { ++$implementsInfo['numberOfInterfaces']; continue; } if (!$implementsInfo['multiLine'] && ($tokens[$j]->isWhitespace() || $tokens[$j]->isComment()) && false !== strpos($tokens[$j]->getContent(), "\n")) { $implementsInfo['multiLine'] = true; } } return $implementsInfo; }
/** * @param Tokens $tokens */ private function replaceClassKeywordsSection(Tokens $tokens, $startIndex, $endIndex) { $CTClassTokens = $tokens->findGivenKind(CT_CLASS_CONSTANT, $startIndex, $endIndex); if (!empty($CTClassTokens)) { $this->replaceClassKeyword($tokens, current(array_keys($CTClassTokens))); $this->replaceClassKeywordsSection($tokens, $startIndex, $endIndex); } }