/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); for ($index = 0, $count = $tokens->count(); $index < $count; ++$index) { // skip T_FOR parenthesis to ignore duplicated `;` like `for ($i = 1; ; ++$i) {...}` if ($tokens[$index]->isGivenKind(T_FOR)) { $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $tokens->getNextMeaningfulToken($index)) + 1; continue; } if (!$tokens[$index]->equals(';')) { continue; } $previousMeaningfulIndex = $tokens->getPrevMeaningfulToken($index); // A semicolon can always be removed if it follows a semicolon, '{' or opening tag. if ($tokens[$previousMeaningfulIndex]->equalsAny(array('{', ';', array(T_OPEN_TAG)))) { $tokens->clearTokenAndMergeSurroundingWhitespace($index); continue; } // A semicolon might be removed if it follows a '}' but only if the brace is part of certain structures. if ($tokens[$previousMeaningfulIndex]->equals('}')) { $this->fixSemicolonAfterCurlyBraceClose($tokens, $index, $previousMeaningfulIndex); } } return $tokens->generateCode(); }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); $namespacesImports = $tokens->getImportUseIndexes(true); if (0 === count($namespacesImports)) { return $content; } $usesOrder = array(); foreach ($namespacesImports as $uses) { $usesOrder = array_replace($usesOrder, $this->getNewOrder(array_reverse($uses), $tokens)); } // First clean the old content // This must be done first as the indexes can be scattered foreach ($usesOrder as $use) { $tokens->clearRange($use['startIndex'], $use['endIndex']); } $usesOrder = array_reverse($usesOrder, true); // Now insert the new tokens, starting from the end foreach ($usesOrder as $index => $use) { $declarationTokens = Tokens::fromCode('<?php use ' . $use['namespace'] . ';'); $declarationTokens->clearRange(0, 2); // clear `<?php use ` $declarationTokens[count($declarationTokens) - 1]->clear(); // clear `;` $declarationTokens->clearEmptyTokens(); $tokens->insertAt($index, $declarationTokens); } return $tokens->generateCode(); }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); // ignore non-monolithic files if (!$tokens->isMonolithicPhp()) { return $content; } // ignore files with short open tag if (!$tokens[0]->isGivenKind(T_OPEN_TAG)) { return $content; } $newlineFound = false; /** @var Token $token */ foreach ($tokens as $token) { if ($token->isWhitespace(array('whitespaces' => "\n"))) { $newlineFound = true; break; } } // ignore one-line files if (!$newlineFound) { return $content; } $token = $tokens[0]; if (false === strpos($token->getContent(), "\n")) { $token->setContent(rtrim($token->getContent()) . "\n"); } if (!$tokens[1]->isWhitespace() && false === strpos($tokens[1]->getContent(), "\n")) { $tokens->insertAt(1, new Token(array(T_WHITESPACE, "\n"))); } return $tokens->generateCode(); }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); // Checks if specific statements are set and uses them in this case. $loops = array_intersect_key(self::$loops, array_flip(self::$controlStatements)); foreach ($tokens as $index => $token) { if (!$token->equals('(')) { continue; } $blockStartIndex = $index; $index = $tokens->getPrevMeaningfulToken($index); $token = $tokens[$index]; foreach ($loops as $loop) { if (!$token->isGivenKind($loop['lookupTokens'])) { continue; } $blockEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $blockStartIndex); $blockEndNextIndex = $tokens->getNextMeaningfulToken($blockEndIndex); if (!$tokens[$blockEndNextIndex]->equalsAny($loop['neededSuccessors'])) { continue; } if ($tokens[$blockStartIndex - 1]->isWhitespace() || $tokens[$blockStartIndex - 1]->isComment()) { $this->clearParenthesis($tokens, $blockStartIndex); } else { // Adds a space to prevent broken code like `return2`. $tokens->overrideAt($blockStartIndex, array(T_WHITESPACE, ' ')); } $this->clearParenthesis($tokens, $blockEndIndex); } } return $tokens->generateCode(); }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { $tokensAnalyzer = new TokensAnalyzer($tokens); $namespacesImports = $tokensAnalyzer->getImportUseIndexes(true); $usesOrder = array(); if (!count($namespacesImports)) { return; } foreach ($namespacesImports as $uses) { $uses = array_reverse($uses); $usesOrder = array_replace($usesOrder, $this->getNewOrder($uses, $tokens)); } $usesOrder = array_reverse($usesOrder, true); $mapStartToEnd = array(); foreach ($usesOrder as $use) { $mapStartToEnd[$use[1]] = $use[2]; } // Now insert the new tokens, starting from the end foreach ($usesOrder as $index => $use) { $declarationTokens = Tokens::fromCode('<?php use ' . $use[0] . ';'); $declarationTokens->clearRange(0, 2); // clear `<?php use ` $declarationTokens[count($declarationTokens) - 1]->clear(); // clear `;` $declarationTokens->clearEmptyTokens(); $tokens->overrideRange($index, $mapStartToEnd[$index], $declarationTokens); } }
/** * Inject into the text placeholders of candidates of vertical alignment. * * Output structure: * * Tokens $tokens * * int $deepestLevel * * @param string $content * * @return array */ private function injectAlignmentPlaceholders($content) { $deepestLevel = 0; $parenCount = 0; $bracketCount = 0; $tokens = Tokens::fromCode($content); foreach ($tokens as $token) { $tokenContent = $token->getContent(); if (0 === $parenCount && 0 === $bracketCount && $token->equals('=')) { $token->setContent(sprintf(self::ALIGNABLE_PLACEHOLDER, $deepestLevel) . $tokenContent); continue; } if ($token->isGivenKind(T_FUNCTION)) { ++$deepestLevel; } elseif ($token->equals('(')) { ++$parenCount; } elseif ($token->equals(')')) { --$parenCount; } elseif ($token->equals('[')) { ++$bracketCount; } elseif ($token->equals(']')) { --$bracketCount; } } return array('tokens' => $tokens, 'deepestLevel' => $deepestLevel); }
/** * Replace all `else if` (T_ELSE T_IF) with `elseif` (T_ELSEIF). * * {@inheritdoc} */ public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); foreach ($tokens->findGivenKind(T_ELSE) as $index => $token) { $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 $token->override(array(T_ELSEIF, 'elseif', $token->getLine())); // 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'); } } return $tokens->generateCode(); }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); if (!$tokens->isMonolithicPhp()) { return $content; } $index = 1; $hasNs = false; for ($i = 1; $i < $tokens->count(); $i++) { if ($tokens[$i]->isGivenKind(T_NAMESPACE)) { $hasNs = true; while ($tokens[$i]->getContent() !== ';') { $i++; } $i++; $index = $i + 1; } elseif (!$tokens[$i]->isWhitespace() && !$tokens[$i]->isGivenKind(T_COMMENT)) { break; } $tokens[$i]->clear(); } if ('' !== self::$headerComment) { $tokens->insertAt($index, [new Token([T_WHITESPACE, "\n" . ($hasNs ? "\n" : '')]), new Token([T_COMMENT, self::$headerComment]), new Token([T_WHITESPACE, "\n\n"])]); } $tokens->clearEmptyTokens(); return $tokens->generateCode(); }
public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); $functionyTokens = $this->getFunctionyTokenKinds(); $languageConstructionTokens = $this->getLanguageConstructionTokenKinds(); foreach ($tokens as $index => $token) { if (!$token->equals('(')) { continue; } $lastTokenIndex = $tokens->getPrevNonWhitespace($index); if (null === $lastTokenIndex) { continue; } $endParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index); $nextNonWhiteSpace = $tokens->getNextMeaningfulToken($endParenthesisIndex); if (null !== $nextNonWhiteSpace && $tokens[$nextNonWhiteSpace]->equals('?') && $tokens[$lastTokenIndex]->isGivenKind($languageConstructionTokens)) { continue; } if ($tokens[$lastTokenIndex]->isGivenKind($functionyTokens)) { $this->fixFunctionCall($tokens, $index); } elseif ($tokens[$lastTokenIndex]->isGivenKind(T_STRING)) { $possibleDefinitionIndex = $tokens->getPrevMeaningfulToken($lastTokenIndex); if (!$tokens[$possibleDefinitionIndex]->isGivenKind(T_FUNCTION)) { $this->fixFunctionCall($tokens, $index); } } } return $tokens->generateCode(); }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); for ($index = count($tokens) - 1; 0 <= $index; --$index) { $token = $tokens[$index]; if (!$token->isGivenKind(T_NAMESPACE)) { continue; } $beforeNamespace = $tokens[$index - 1]; if (!$beforeNamespace->isWhitespace()) { if (!self::endsWithWhitespace($beforeNamespace->getContent())) { $tokens->insertAt($index, new Token(array(T_WHITESPACE, "\n"))); } continue; } $lastNewline = strrpos($beforeNamespace->getContent(), "\n"); if (false === $lastNewline) { $beforeBeforeNamespace = $tokens[$index - 2]; if (self::endsWithWhitespace($beforeBeforeNamespace->getContent())) { $beforeNamespace->clear(); } else { $beforeNamespace->setContent(' '); } } else { $beforeNamespace->setContent(substr($beforeNamespace->getContent(), 0, $lastNewline + 1)); } } return $tokens->generateCode(); }
public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); if (!$tokens->isMonolithicPhp()) { return $content; } $closeTags = $tokens->findGivenKind(T_CLOSE_TAG); if (empty($closeTags)) { return $content; } 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(';')); } return $tokens->generateCode(); }
public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); $count = $tokens->count(); if (0 === $count) { return ''; } $token = $tokens[$count - 1]; if ($token->isGivenKind(array(T_INLINE_HTML, T_CLOSE_TAG, T_OPEN_TAG))) { return $content; } $isSingleLineComment = function (Token $token) { return $token->isComment() && '/*' !== substr($token->getContent(), 0, 2); }; $clearSingleLineComment = function (Token $token) { $content = $token->getContent(); $content = rtrim($content, "\n") . "\n"; $token->setContent($content); }; if ($token->isWhitespace()) { if ($count > 1 && $isSingleLineComment($tokens[$count - 2])) { $clearSingleLineComment($tokens[$count - 2]); $token->clear(); } else { $lineBreak = false === strrpos($token->getContent(), "\r") ? "\n" : "\r\n"; $token->setContent($lineBreak); } } elseif ($isSingleLineComment($token)) { $clearSingleLineComment($token); } else { $tokens->insertAt($count, new Token(array(T_WHITESPACE, "\n"))); } return $tokens->generateCode(); }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); foreach ($tokens as $index => $token) { if (!$token->isGivenKind(T_START_HEREDOC) || false !== strpos($token->getContent(), "'")) { continue; } if ($tokens[$index + 1]->isGivenKind(T_END_HEREDOC)) { $this->convertToNowdoc($token); continue; } if (!$tokens[$index + 1]->isGivenKind(T_ENCAPSED_AND_WHITESPACE) || !$tokens[$index + 2]->isGivenKind(T_END_HEREDOC)) { continue; } $content = $tokens[$index + 1]->getContent(); // regex: odd number of backslashes, not followed by dollar if (preg_match('/(?<!\\\\)(?:\\\\{2})*\\\\(?![$\\\\])/', $content)) { continue; } $this->convertToNowdoc($token); $content = str_replace(array('\\\\', '\\$'), array('\\', '$'), $content); $tokens[$index + 1]->setContent($content); } return $tokens->generateCode(); }
public function fix(\SplFileInfo $file, $content) { static $map = null; if (null === $map) { $trueToken = new Token(array(T_STRING, 'true')); $map = array( 'in_array' => array(null, null, $trueToken), 'base64_decode' => array(null, $trueToken), 'array_search' => array(null, null, $trueToken), 'array_keys' => array(null, null, $trueToken), 'mb_detect_encoding' => array(null, array(new Token(array(T_STRING, 'mb_detect_order')), new Token('('), new Token(')')), $trueToken), ); } $tokens = Tokens::fromCode($content); for ($index = $tokens->count() - 1; 0 <= $index; --$index) { $token = $tokens[$index]; if ($token->isGivenKind(T_STRING) && isset($map[$token->getContent()])) { $this->fixFunction($tokens, $index, $map[$token->getContent()]); } } return $tokens->generateCode(); }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); for ($index = $tokens->count() - 1; $index >= 0; --$index) { $token = $tokens[$index]; if (!$token->isGivenKind(T_FUNCTION)) { continue; } $startParenthesisIndex = $tokens->getNextTokenOfKind($index, array('(')); $endParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startParenthesisIndex); for ($iter = $endParenthesisIndex - 1; $iter > $startParenthesisIndex; --$iter) { if (!$tokens[$iter]->isGivenKind(T_VARIABLE)) { continue; } // skip ... before $variable for variadic parameter if (defined('T_ELLIPSIS')) { $prevNonWhitespaceIndex = $tokens->getPrevNonWhitespace($iter); if ($tokens[$prevNonWhitespaceIndex]->isGivenKind(T_ELLIPSIS)) { $iter = $prevNonWhitespaceIndex; } } // skip & before $variable for parameter passed by reference $prevNonWhitespaceIndex = $tokens->getPrevNonWhitespace($iter); if ($tokens[$prevNonWhitespaceIndex]->equals('&')) { $iter = $prevNonWhitespaceIndex; } if (!$tokens[$iter - 1]->equalsAny(array(array(T_WHITESPACE), array(T_COMMENT), array(T_DOC_COMMENT), '(', ','))) { $tokens->insertAt($iter, new Token(array(T_WHITESPACE, ' ', $tokens[$iter]->getLine()))); } } } return $tokens->generateCode(); }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); for ($index = $tokens->count() - 1; $index >= 0; --$index) { $token = $tokens[$index]; if (T_NEW !== $token->getId()) { continue; } $nextIndex = $tokens->getNextTokenOfKind($index, array(';', ',', '(', ')', '[', ']')); $nextToken = $tokens[$nextIndex]; // entrance into array index syntax - need to look for exit if ($nextToken->equals('[')) { $nextIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_SQUARE_BRACE, $nextIndex) + 1; $nextToken = $tokens[$nextIndex]; } // new statement has a gap in it - advance to the next token if ($nextToken->isGivenKind(T_WHITESPACE)) { $nextIndex = $tokens->getNextNonWhitespace($nextIndex); $nextToken = $tokens[$nextIndex]; } // new statement with () - nothing to do if ($nextToken->equals('(')) { continue; } $meaningBeforeNextIndex = $tokens->getPrevNonWhitespace($nextIndex); $tokens->insertAt($meaningBeforeNextIndex + 1, array(new Token('('), new Token(')'))); } return $tokens->generateCode(); }
public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); foreach ($tokens as $index => $token) { if (!$token->equals('(')) { continue; } $prevIndex = $tokens->getPrevMeaningfulToken($index); if (null !== $prevIndex && $tokens[$prevIndex]->isGivenKind(T_ARRAY)) { continue; } $endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index); $this->removeSpaceAroundToken($tokens, $index, 1); if (!$tokens[$tokens->getPrevMeaningfulToken($endIndex)]->equals(',')) { $this->removeSpaceAroundToken($tokens, $endIndex, -1); } } return $tokens->generateCode(); }
public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); $startAt = -1; foreach ($tokens as $i => $token) { if (trim($token->getContent()) == '<?php') { $token->setContent("<?php\n"); $startAt = $i; } if (trim($token->getContent()) == 'declare' && $token->isGivenKind(336)) { do { $content = $tokens[$i]->getContent(); $tokens[$i]->clear(); $i++; } while ($content != ';'); if ($tokens[$i]->isGivenKind(T_WHITESPACE)) { $content = $tokens[$i]->getContent(); if (strlen($content) < 2) { $tokens[$i]->clear(); } else { $tokens[$i]->setContent(substr($content, 1)); } } break; } } $tokens->clearEmptyTokens(); $tokens->insertAt($startAt + 1, [new Token([336, 'declare']), new Token('('), new Token([319, 'strict_types']), new Token('='), new Token([317, '1']), new Token(')'), new Token(';'), new Token([382, "\n"])]); return $tokens->generateCode(); }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); foreach ($tokens as $token) { if (!$token->isGivenKind(T_DOC_COMMENT)) { continue; } $content = $token->getContent(); // Move `@` inside tag, for example @{tag} -> {@tag}, replace multiple curly brackets, // remove spaces between '{' and '@', remove 's' at the end of tag. // Make sure the tags are written in lower case, remove white space between end // of text and closing bracket and between the tag and inline comment. $content = preg_replace_callback('#(?:@{+|{+[ \\t]*@)[ \\t]*(example|id|internal|inheritdoc|link|source|toc|tutorial)s?([^}]*)(?:}*)#i', function (array $matches) { $doc = trim($matches[2]); if ('' === $doc) { return '{@' . strtolower($matches[1]) . '}'; } return '{@' . strtolower($matches[1]) . ' ' . $doc . '}'; }, $content); // Always make inheritdoc inline using with '{' '}' when needed, remove trailing 's', // make sure lowercase. $content = preg_replace('#(?<!{)@inheritdocs?(?!})#i', '{@inheritdoc}', $content); $token->setContent($content); } return $tokens->generateCode(); }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, $content) { static $controlStructures = array(T_FOREACH, T_IF, T_SWITCH, T_WHILE, T_FOR); $tokens = Tokens::fromCode($content); foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $index => $token) { $nextIndex = $tokens->getNextMeaningfulToken($index); $nextToken = null !== $nextIndex ? $tokens[$nextIndex] : null; if (null === $nextToken || $nextToken->equals('}')) { $tokens->overrideAt($index, array(T_COMMENT, '/*' . ltrim($token->getContent(), '/*'), $token->getLine())); continue; } if ($this->isStructuralElement($nextToken)) { continue; } if ($nextToken->isGivenkind($controlStructures) && $this->isValidControl($tokens, $token, $nextIndex)) { continue; } if ($nextToken->isGivenkind(T_VARIABLE) && $this->isValidVariable($tokens, $token, $nextIndex)) { continue; } if ($nextToken->isGivenkind(T_LIST) && $this->isValidList($tokens, $token, $nextIndex)) { continue; } // First docblock after open tag can be file-level docblock, so its left as is. $prevIndex = $tokens->getPrevMeaningfulToken($index); if ($tokens[$prevIndex]->isGivenKind(T_OPEN_TAG)) { continue; } $tokens->overrideAt($index, array(T_COMMENT, '/*' . ltrim($token->getContent(), '/*'), $token->getLine())); } return $tokens->generateCode(); }
public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); foreach ($tokens as $token) { if (!$token->isGivenKind(T_DOC_COMMENT)) { continue; } $doc = new DocBlock($token->getContent()); $annotations = $doc->getAnnotationsOfType(static::$tags); if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $this->fixTypes($annotation); } $token->setContent($doc->getContent()); } return $tokens->generateCode(); }
protected function makeTest($expected, $input = null, \SplFileInfo $file = null, FixerInterface $fixer = null) { if ($expected === $input) { throw new \InvalidArgumentException('Input parameter must not be equal to expected parameter.'); } $fixer = $fixer ?: $this->getFixer(); $file = $file ?: $this->getTestFile(); $fileIsSupported = $fixer->supports($file); if (null !== $input) { Tokens::clearCache(); $tokens = Tokens::fromCode($input); if ($fileIsSupported) { $this->assertTrue($fixer->isCandidate($tokens), 'Fixer must be a candidate for input code.'); $fixResult = $fixer->fix($file, $tokens); $this->assertNull($fixResult, '->fix method must return null.'); } $this->assertTrue($tokens->isChanged(), 'Tokens collection built on input code must be marked as changed after fixing.'); $this->assertSame($expected, $tokens->generateCode(), 'Code build on input code must match expected code.'); Tokens::clearCache(); $expectedTokens = Tokens::fromCode($expected); $tokens->clearEmptyTokens(); $this->assertTokens($expectedTokens, $tokens); } Tokens::clearCache(); $tokens = Tokens::fromCode($expected); if ($fileIsSupported) { $fixResult = $fixer->fix($file, $tokens); $this->assertNull($fixResult, '->fix method must return null.'); } $this->assertFalse($tokens->isChanged(), 'Tokens collection built on expected code must not be marked as changed after fixing.'); $this->assertSame($expected, $tokens->generateCode(), 'Code build on expected code must not change.'); }
public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); for ($index = $tokens->count() - 1; 0 <= $index; --$index) { $token = $tokens[$index]; if (!$token->isGivenKind(array(T_INC, T_DEC)) || !$tokens->isUnarySuccessorOperator($index)) { continue; } $nextToken = $tokens[$tokens->getNextMeaningfulToken($index)]; if (!$nextToken->equalsAny(array(';', ')'))) { continue; } $startIndex = $this->findStart($tokens, $index); $prevToken = $tokens[$tokens->getPrevMeaningfulToken($startIndex)]; if ($prevToken->equalsAny(array(';', '{', '}', array(T_OPEN_TAG)))) { $tokens->insertAt($startIndex, clone $token); $token->clear(); } } return $tokens->generateCode(); }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); foreach ($tokens as $token) { if (!$token->isGivenKind(T_WHITESPACE)) { continue; } $content = ''; $count = 0; $parts = explode("\n", $token->getContent()); for ($i = 0, $last = count($parts) - 1; $i <= $last; ++$i) { if ('' === $parts[$i]) { // if part is empty then we between two \n ++$count; } else { $count = 0; $content .= $parts[$i]; } if ($i !== $last && $count < 3) { $content .= "\n"; } } $token->setContent($content); } return $tokens->generateCode(); }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { $tokensAnalyzer = new TokensAnalyzer($tokens); $uses = array_reverse($tokensAnalyzer->getImportUseIndexes()); foreach ($uses as $index) { $endIndex = $tokens->getNextTokenOfKind($index, array(';')); $declarationContent = $tokens->generatePartialCode($index + 1, $endIndex - 1); $declarationParts = explode(',', $declarationContent); if (1 === count($declarationParts)) { continue; } $declarationContent = array(); foreach ($declarationParts as $declarationPart) { $declarationContent[] = 'use ' . trim($declarationPart) . ';'; } $declarationContent = implode("\n" . $this->detectIndent($tokens, $index), $declarationContent); for ($i = $index; $i <= $endIndex; ++$i) { $tokens[$i]->clear(); } $declarationTokens = Tokens::fromCode('<?php ' . $declarationContent); $declarationTokens[0]->clear(); $declarationTokens->clearEmptyTokens(); $tokens->insertAt($index, $declarationTokens); } }
public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); foreach ($tokens as $index => $token) { if (!$token->isWhitespace()) { continue; } $content = $token->getContent(); $lines = preg_split("/([\r\n])/", $content); if (count($lines) > 2 || count($lines) > 1 && !isset($tokens[$index + 1])) { $lMax = count($lines) - 1; if (!isset($tokens[$index + 1])) { ++$lMax; } $lStart = 1; if (isset($tokens[$index - 1]) && $tokens[$index - 1]->isGivenKind(T_OPEN_TAG) && "\n" === substr($tokens[$index - 1]->getContent(), -1)) { $lStart = 0; } for ($l = $lStart; $l < $lMax; ++$l) { $lines[$l] = preg_replace('/^\\h+$/', '', $lines[$l]); } $token->setContent(implode("\n", $lines)); } } return $tokens->generateCode(); }
public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); for ($index = 0, $limit = $tokens->count(); $index < $limit; ++$index) { $token = $tokens[$index]; if (!$token->isGivenKind(T_RETURN)) { continue; } $prevNonWhitespaceToken = $tokens[$tokens->getPrevNonWhitespace($index)]; if (!$prevNonWhitespaceToken->equalsAny(array(';', '}'))) { continue; } $prevToken = $tokens[$index - 1]; if ($prevToken->isWhitespace()) { $parts = explode("\n", $prevToken->getContent()); $countParts = count($parts); if (1 === $countParts) { $prevToken->setContent(rtrim($prevToken->getContent(), " \t") . "\n\n"); } elseif (count($parts) <= 2) { $prevToken->setContent("\n" . $prevToken->getContent()); } } else { $tokens->insertAt($index, new Token(array(T_WHITESPACE, "\n\n"))); ++$index; ++$limit; } } return $tokens->generateCode(); }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); $echoTokens = $tokens->findGivenKind(T_ECHO); foreach ($echoTokens as $echoIndex => $echoToken) { $nextTokenIndex = $tokens->getNextMeaningfulToken($echoIndex); $endTokenIndex = $tokens->getNextTokenOfKind($echoIndex, array(';', array(T_CLOSE_TAG))); $canBeConverted = true; for ($i = $nextTokenIndex; $i < $endTokenIndex; ++$i) { if ($tokens[$i]->equalsAny(array('(', '['))) { $blockType = $tokens->detectBlockType($tokens[$i]); $i = $tokens->findBlockEnd($blockType['type'], $i); } if ($tokens[$i]->equals(',')) { $canBeConverted = false; break; } } if (false === $canBeConverted) { continue; } $tokens->overrideAt($echoIndex, array(T_PRINT, 'print')); } return $tokens->generateCode(); }
/** * Inject into the text placeholders of candidates of vertical alignment. * * @param string $content * * @return array($code, $context_counter) */ private function injectAlignmentPlaceholders($content) { $contextCounter = 0; $parenCount = 0; $bracketCount = 0; $code = ''; $tokens = Tokens::fromCode($content); foreach ($tokens as $token) { $tokenContent = $token->getContent(); if ($token->equals('=') && 0 === $parenCount && 0 === $bracketCount) { $code .= sprintf(self::ALIGNABLE_EQUAL, $contextCounter) . $tokenContent; continue; } if ($token->isGivenKind(T_FUNCTION)) { ++$contextCounter; } elseif ($token->equals('(')) { ++$parenCount; } elseif ($token->equals(')')) { --$parenCount; } elseif ($token->equals('[')) { ++$bracketCount; } elseif ($token->equals(']')) { --$bracketCount; } $code .= $tokenContent; } return array($code, $contextCounter); }
public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); for ($index = $tokens->count() - 3; $index > 0; --$index) { $token = $tokens[$index]; if (!$token->isGivenKind(T_NEW)) { continue; } $nextIndex = $tokens->getNextTokenOfKind($index, array(';', ',', '(', ')', '[', ']', ':', array(T_CLOSE_TAG))); $nextToken = $tokens[$nextIndex]; while ($nextToken->equals('[')) { $nextIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_SQUARE_BRACE, $nextIndex) + 1; $nextToken = $tokens[$nextIndex]; } if ($nextToken->isGivenKind(T_WHITESPACE)) { $nextIndex = $tokens->getNextNonWhitespace($nextIndex); $nextToken = $tokens[$nextIndex]; } if ($nextToken->equals('(')) { continue; } $meaningBeforeNextIndex = $tokens->getPrevMeaningfulToken($nextIndex); $tokens->insertAt($meaningBeforeNextIndex + 1, array(new Token('('), new Token(')'))); } return $tokens->generateCode(); }