Пример #1
0
 /**
  * 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 as $index => $token) {
         // if token is not T_ELSE - continue searching, this is not right token to fix
         if (!$token->isGivenKind(T_ELSE)) {
             continue;
         }
         $nextIndex = null;
         $nextToken = $tokens->getNextNonWhitespace($index, array(), $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->content = 'elseif';
         $token->id = T_ELSEIF;
         // 3. clear succeeding T_IF
         $nextToken->clear();
     }
     return $tokens->generateCode();
 }
Пример #2
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     $uses = array_reverse($tokens->getNamespaceUseIndexes());
     foreach ($uses as $index) {
         $endIndex = null;
         $tokens->getNextTokenOfKind($index, array(';'), $endIndex);
         $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();
         $tokens->insertAt($index, $declarationTokens);
     }
     return $tokens->generateCode();
 }
Пример #3
0
 /**
  * {@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->id) {
             continue;
         }
         $nextIndex = null;
         $nextToken = $tokens->getNextTokenOfKind($index, array(';', ',', '(', ')', '[', ']'), $nextIndex);
         // no correct end of code - break
         if (null === $nextToken) {
             break;
         }
         // entrance into array index syntax - need to look for exit
         if (!$nextToken->isArray() && '[' === $nextToken->content) {
             $braceLevel = 1;
             while (0 < $braceLevel) {
                 $nextToken = $tokens->getNextTokenOfKind($nextIndex, array('[', ']'), $nextIndex);
                 $braceLevel += '[' === $nextToken->content ? 1 : -1;
             }
             $nextToken = $tokens[++$nextIndex];
         }
         // new statement with () - nothing to do
         if (!$nextToken->isArray() && '(' === $nextToken->content) {
             continue;
         }
         $meaningBeforeNextIndex = null;
         $tokens->getPrevNonWhitespace($nextIndex, array(), $meaningBeforeNextIndex);
         $tokens->insertAt($meaningBeforeNextIndex + 1, array(new Token('('), new Token(')')));
     }
     return $tokens->generateCode();
 }
Пример #4
0
 /**
  * {@inheritdoc}
  */
 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->getPrevNonWhitespace($index);
         if (!$prevNonWhitespaceToken->equalsAny(array(';', '}'))) {
             continue;
         }
         $prevToken = $tokens[$index - 1];
         if ($prevToken->isWhitespace()) {
             $parts = explode("\n", $prevToken->content);
             $countParts = count($parts);
             if (1 === $countParts) {
                 $prevToken->content = rtrim($prevToken->content, " \t") . "\n\n";
             } elseif (count($parts) <= 2) {
                 $prevToken->content = "\n" . $prevToken->content;
             }
         } else {
             $tokens->insertAt($index, new Token(array(T_WHITESPACE, "\n\n")));
             ++$index;
             ++$limit;
         }
     }
     return $tokens->generateCode();
 }
Пример #5
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     $includies = $this->findIncludies($tokens);
     $this->clearIncludies($tokens, $includies);
     return $tokens->generateCode();
 }
Пример #6
0
 /**
  * {@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->content);
         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->content = $content;
     }
     return $tokens->generateCode();
 }
Пример #7
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     $namespacesImports = $tokens->getNamespaceUseIndexes(true);
     $usesOrder = array();
     if (!count($namespacesImports)) {
         return $content;
     }
     foreach ($namespacesImports as $uses) {
         $uses = array_reverse($uses);
         $usesOrder = array_replace($usesOrder, $this->getNewOrder($uses, $tokens));
     }
     // First clean the old content
     // This must be done first as the indexes can be scattered
     foreach ($usesOrder as $use) {
         for ($i = $use[1]; $i <= $use[2]; ++$i) {
             $tokens[$i]->clear();
         }
     }
     $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[0] . ';');
         $declarationTokens[0]->clear();
         // <?php
         $declarationTokens[1]->clear();
         // use
         $declarationTokens[2]->clear();
         // space
         $declarationTokens[count($declarationTokens) - 1]->clear();
         // ;
         $tokens->insertAt($index, $declarationTokens);
     }
     return $tokens->generateCode();
 }
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     foreach ($tokens as $token) {
         if ($token->isKeyword()) {
             $token->content = strtolower($token->content);
         }
     }
     return $tokens->generateCode();
 }
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     foreach ($tokens as $index => $token) {
         if ($token->isGivenKind(T_IS_NOT_EQUAL)) {
             $tokens[$index]->content = '!=';
         }
     }
     return $tokens->generateCode();
 }
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     for ($index = $tokens->count() - 1; $index >= 0; --$index) {
         if ($tokens->isArray($index)) {
             $this->fixArray($tokens, $index);
         }
     }
     return $tokens->generateCode();
 }
Пример #11
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     foreach ($tokens as $index => $token) {
         if (!$token->isWhitespace()) {
             continue;
         }
         $tokens[$index]->content = preg_replace('/(?:(?<! ) {1,3})?\\t/', '    ', $token->content);
     }
     return $tokens->generateCode();
 }
Пример #12
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     foreach ($tokens as $index => $token) {
         if (!$token->isGivenKind(T_DOC_COMMENT)) {
             continue;
         }
         $tokens[$index]->content = $this->fixDocBlock($token->content);
     }
     return $tokens->generateCode();
 }
Пример #13
0
 public function testReadFromCacheAfterClearing()
 {
     $code = '<?php echo 1;';
     $tokens = Tokens::fromCode($code);
     $countBefore = $tokens->count();
     for ($i = 0; $i < $countBefore; ++$i) {
         $tokens[$i]->clear();
     }
     $tokens = Tokens::fromCode($code);
     $this->assertSame($countBefore, $tokens->count());
 }
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     $whitespaces = array('whitespaces' => " \t");
     foreach ($tokens as $index => $token) {
         if (!$token->isArray() && '.' === $token->content) {
             $tokens->removeLeadingWhitespace($index, $whitespaces);
             $tokens->removeTrailingWhitespace($index, $whitespaces);
         }
     }
     return $tokens->generateCode();
 }
Пример #15
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     $this->fixCommentBeforeBrace($tokens);
     $this->fixMissingControlBraces($tokens);
     $this->fixIndents($tokens);
     $this->fixControlContinuationBraces($tokens);
     $this->fixSpaceAroundToken($tokens);
     $this->fixDoWhile($tokens);
     $this->fixLambdas($tokens);
     return $tokens->generateCode();
 }
Пример #16
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     for ($index = 0, $c = $tokens->count(); $index < $c; ++$index) {
         $token = $tokens[$index];
         if ($token->isGivenKind(T_ARRAY) && '(' === $tokens->getNextNonWhitespace($index)->content) {
             $this->fixArray($tokens, $index);
             continue;
         }
     }
     return $tokens->generateCode();
 }
Пример #17
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     $namespace = $this->detectNamespace($tokens);
     $useDeclarationsIndexes = $tokens->getNamespaceUseIndexes();
     $useDeclarations = $this->getNamespaceUseDeclarations($tokens, $useDeclarationsIndexes);
     $contentWithoutUseDeclarations = $this->generateCodeWithoutUses($tokens, $useDeclarations);
     $useUsages = $this->detectUseUsages($contentWithoutUseDeclarations, $useDeclarations);
     $this->removeUnusedUseDeclarations($tokens, $useDeclarations, $useUsages);
     $this->removeUsesInSameNamespace($tokens, $useDeclarations, $namespace);
     return $tokens->generateCode();
 }
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     foreach ($tokens as $index => $token) {
         if ($token->isNativeConstant()) {
             if ($tokens->getPrevNonWhitespace($index, array('whitespaces' => " \t\n"))->isArray() || $tokens->getNextNonWhitespace($index, array('whitespaces' => " \t\n"))->isArray()) {
                 continue;
             }
             $token->content = strtolower($token->content);
         }
     }
     return $tokens->generateCode();
 }
Пример #19
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     for ($index = $tokens->count() - 1; $index >= 0; --$index) {
         $token = $tokens[$index];
         if (!$token->isArray() && '.' === $token->content) {
             if (!$tokens[$index + 1]->isWhitespace()) {
                 $tokens->insertAt($index + 1, new Token(array(T_WHITESPACE, ' ')));
             }
             if (!$tokens[$index - 1]->isWhitespace()) {
                 $tokens->insertAt($index, new Token(array(T_WHITESPACE, ' ')));
             }
         }
     }
     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 = null;
         $tokens->getNextTokenOfKind($index, array('('), $startParenthesisIndex);
         $endParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startParenthesisIndex);
         $startBraceIndex = null;
         $startBraceToken = $tokens->getNextTokenOfKind($endParenthesisIndex, array(';', '{'), $startBraceIndex);
         if ($startBraceToken->equals('{')) {
             // fix single-line whitespace before {
             // eg: `function foo(){}` => `function foo() {}`
             // eg: `function foo()   {}` => `function foo() {}`
             if (!$tokens[$startBraceIndex - 1]->isWhitespace() || $tokens[$startBraceIndex - 1]->isWhitespace($this->singleLineWhitespaceOptions)) {
                 $tokens->ensureWhitespaceAtIndex($startBraceIndex - 1, 1, ' ');
             }
         }
         $afterParenthesisIndex = null;
         $afterParenthesisToken = $tokens->getNextNonWhitespace($endParenthesisIndex, array(), $afterParenthesisIndex);
         if ($afterParenthesisToken->isGivenKind(T_USE)) {
             $useStartParenthesisIndex = null;
             $tokens->getNextTokenOfKind($afterParenthesisIndex, array('('), $useStartParenthesisIndex);
             $useEndParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $useStartParenthesisIndex);
             // fix whitespace after T_USE
             $tokens->ensureWhitespaceAtIndex($afterParenthesisIndex + 1, 0, ' ');
             // remove single-line edge whitespaces inside use parentheses
             $this->fixParenthesisInnerEdge($tokens, $useStartParenthesisIndex, $useEndParenthesisIndex);
             // fix whitespace before T_USE
             $tokens->ensureWhitespaceAtIndex($afterParenthesisIndex - 1, 1, ' ');
         }
         // remove single-line edge whitespaces inside parameters list parentheses
         $this->fixParenthesisInnerEdge($tokens, $startParenthesisIndex, $endParenthesisIndex);
         // remove whitespace before (
         // eg: `function foo () {}` => `function foo() {}`
         if ($tokens[$startParenthesisIndex - 1]->isWhitespace()) {
             $tokens[$startParenthesisIndex - 1]->clear();
         }
         // fix whitespace after T_FUNCTION
         // eg: `function     foo() {}` => `function foo() {}`
         $tokens->ensureWhitespaceAtIndex($index + 1, 0, ' ');
     }
     return $tokens->generateCode();
 }
Пример #21
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     // replace all <? with <?php to replace all short open tags even without short_open_tag option enabled
     $newContent = preg_replace('/<\\?(\\s|$)/', '<?php$1', $content);
     /* the following code is magic to revert previous replacements which should NOT be replaced, for example incorrectly replacing
      * > echo '<? ';
      * with
      * > echo '<?php ';
      */
     $tokens = Tokens::fromCode($newContent);
     $tokensOldContent = '';
     $tokensOldContentLength = 0;
     foreach ($tokens as $token) {
         if ($token->isGivenKind(T_OPEN_TAG)) {
             $tokenContent = $token->content;
             if ('<?php' !== substr($content, $tokensOldContentLength, 5)) {
                 $tokenContent = '<? ';
             }
             $tokensOldContent .= $tokenContent;
             $tokensOldContentLength += strlen($tokenContent);
             continue;
         }
         if ($token->isGivenKind(array(T_COMMENT, T_DOC_COMMENT, T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE, T_STRING))) {
             $tokenContent = '';
             $tokenContentLength = 0;
             $parts = explode('<?php ', $token->content);
             $iLast = count($parts) - 1;
             foreach ($parts as $i => $part) {
                 $tokenContent .= $part;
                 $tokenContentLength += strlen($part);
                 if ($i !== $iLast) {
                     if ('<?php' === substr($content, $tokensOldContentLength + $tokenContentLength, 5)) {
                         $tokenContent .= '<?php ';
                         $tokenContentLength += 6;
                     } else {
                         $tokenContent .= '<? ';
                         $tokenContentLength += 3;
                     }
                 }
             }
             $token->content = $tokenContent;
         }
         $tokensOldContent .= $token->content;
         $tokensOldContentLength += strlen($token->content);
     }
     return $tokens->generateCode();
 }
Пример #22
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     static $insideCastSpaceReplaceMap = array(' ' => '', "\t" => '', "\n" => '');
     $tokens = Tokens::fromCode($content);
     foreach ($tokens as $index => $token) {
         if ($token->isCast()) {
             $token->content = strtr($token->content, $insideCastSpaceReplaceMap);
             // force single whitespace after cast token:
             if ($tokens[$index + 1]->isWhitespace(array('whitespaces' => " \t"))) {
                 // - if next token is whitespaces that contains only spaces and tabs - override next token with single space
                 $tokens[$index + 1]->content = ' ';
             } elseif (!$tokens[$index + 1]->isWhitespace()) {
                 // - if next token is not whitespaces that contains spaces, tabs and new lines - append single space to current token
                 $tokens->insertAt($index + 1, new Token(' '));
             }
         }
     }
     return $tokens->generateCode();
 }
Пример #23
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     $elements = $tokens->getClassyElements();
     foreach (array_reverse($elements, true) as $index => $element) {
         if ('method' === $element['type']) {
             $tokens->applyAttribs($index, $tokens->grabAttribsBeforeMethodToken($index));
             // force whitespace between function keyword and function name to be single space char
             $tokens[++$index]->content = ' ';
         } elseif ('property' === $element['type']) {
             $prevToken = $tokens->getPrevTokenOfKind($index, array(';', ','));
             $nextToken = $tokens->getNextTokenOfKind($index, array(';', ','));
             if ((!$prevToken || ',' !== $prevToken->content) && (!$nextToken || ',' !== $nextToken->content)) {
                 $tokens->applyAttribs($index, $tokens->grabAttribsBeforePropertyToken($index));
             }
         }
     }
     return $tokens->generateCode();
 }
Пример #24
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     // [Structure] there should not be space before or after T_OBJECT_OPERATOR
     $tokens = Tokens::fromCode($content);
     foreach ($tokens as $index => $token) {
         // skip if $token is not ->
         if (!$token->isGivenKind(T_OBJECT_OPERATOR)) {
             continue;
         }
         // clear whitespace before ->
         if ($tokens[$index - 1]->isWhitespace(array('whitespaces' => " \t")) && !$tokens[$index - 2]->isComment()) {
             $tokens[$index - 1]->clear();
         }
         // clear whitespace after ->
         if ($tokens[$index + 1]->isWhitespace(array('whitespaces' => " \t")) && !$tokens[$index + 2]->isComment()) {
             $tokens[$index + 1]->clear();
         }
     }
     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_NAMESPACE === $token->id) {
             $semicolonIndex = null;
             $semicolonToken = $tokens->getNextTokenOfKind($index, array(';', '{'), $semicolonIndex);
             if (!$semicolonToken || ';' !== $semicolonToken->content || !isset($tokens[$semicolonIndex + 1])) {
                 continue;
             }
             $nextToken = $tokens[$semicolonIndex + 1];
             if (!$nextToken->isWhitespace()) {
                 $tokens->insertAt($semicolonIndex + 1, new Token(array(T_WHITESPACE, "\n\n")));
             } else {
                 $nextToken->content = "\n\n" . ltrim($nextToken->content);
             }
         }
     }
     return $tokens->generateCode();
 }
Пример #26
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $ternaryLevel = 0;
     $tokens = Tokens::fromCode($content);
     foreach ($tokens as $index => $token) {
         if ($token->isArray()) {
             continue;
         }
         if ('?' === $token->content) {
             ++$ternaryLevel;
             $nextNonWhitespaceIndex = null;
             $nextNonWhitespaceToken = $tokens->getNextNonWhitespace($index, array(), $nextNonWhitespaceIndex);
             if (!$nextNonWhitespaceToken->isArray() && ':' === $nextNonWhitespaceToken->content) {
                 // for `$a ?: $b` remove spaces between `?` and `:`
                 if ($tokens[$index + 1]->isWhitespace()) {
                     $tokens[$index + 1]->clear();
                 }
             } else {
                 // for `$a ? $b : $c` ensure space after `?`
                 $this->ensureWhitespaceExistance($tokens, $index + 1, true);
             }
             // for `$a ? $b : $c` ensure space before `?`
             $this->ensureWhitespaceExistance($tokens, $index - 1, false);
             continue;
         }
         if ($ternaryLevel && ':' === $token->content) {
             // for `$a ? $b : $c` ensure space after `:`
             $this->ensureWhitespaceExistance($tokens, $index + 1, true);
             $prevNonWhitespaceToken = $tokens->getPrevNonWhitespace($index);
             if ($prevNonWhitespaceToken->isArray() || '?' !== $prevNonWhitespaceToken->content) {
                 // for `$a ? $b : $c` ensure space before `:`
                 $this->ensureWhitespaceExistance($tokens, $index - 1, false);
             }
             --$ternaryLevel;
         }
     }
     return $tokens->generateCode();
 }
Пример #27
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, $content)
 {
     $tokens = Tokens::fromCode($content);
     $this->fixComparisons($tokens);
     return $tokens->generateCode();
 }