예제 #1
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     foreach ($tokens as $index => $token) {
         if (!$token->isGivenKind(T_ARRAY)) {
             continue;
         }
         $openIndex = $tokens->getNextTokenOfKind($index, array('('));
         $closeIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openIndex);
         $token->clear();
         $tokens->overrideAt($openIndex, array(CT_ARRAY_SQUARE_BRACE_OPEN, '['));
         $tokens->overrideAt($closeIndex, array(CT_ARRAY_SQUARE_BRACE_CLOSE, ']'));
     }
 }
예제 #2
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     for ($index = $tokens->count() - 1; 0 <= $index; --$index) {
         $token = $tokens[$index];
         if (!$token->isGivenKind(CT_ARRAY_SQUARE_BRACE_OPEN)) {
             continue;
         }
         $closeIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $index);
         $tokens->overrideAt($index, '(');
         $tokens->overrideAt($closeIndex, ')');
         $tokens->insertAt($index, new Token(array(T_ARRAY, 'array')));
     }
 }
예제 #3
0
 /**
  * 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');
         }
     }
 }
예제 #4
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     static $controlStructures = array(T_FOREACH, T_IF, T_SWITCH, T_WHILE, T_FOR);
     foreach ($tokens as $index => $token) {
         if (!$token->isGivenKind(T_DOC_COMMENT)) {
             continue;
         }
         $nextIndex = $tokens->getNextMeaningfulToken($index);
         $nextToken = null !== $nextIndex ? $tokens[$nextIndex] : null;
         if (null === $nextToken || $nextToken->equals('}')) {
             $tokens->overrideAt($index, array(T_COMMENT, '/*' . ltrim($token->getContent(), '/*')));
             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(), '/*')));
     }
 }
예제 #5
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     foreach ($tokens as $index => $token) {
         if (!$token->isGivenKind(T_ECHO)) {
             continue;
         }
         $nextTokenIndex = $tokens->getNextMeaningfulToken($index);
         $endTokenIndex = $tokens->getNextTokenOfKind($index, array(';', array(T_CLOSE_TAG)));
         $canBeConverted = true;
         for ($i = $nextTokenIndex; $i < $endTokenIndex; ++$i) {
             if ($tokens[$i]->equalsAny(array('(', array(CT_ARRAY_SQUARE_BRACE_OPEN)))) {
                 $blockType = $tokens->detectBlockType($tokens[$i]);
                 $i = $tokens->findBlockEnd($blockType['type'], $i);
             }
             if ($tokens[$i]->equals(',')) {
                 $canBeConverted = false;
                 break;
             }
         }
         if (false === $canBeConverted) {
             continue;
         }
         $tokens->overrideAt($index, array(T_PRINT, 'print'));
     }
 }
예제 #6
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     static $map = array(T_IS_EQUAL => array('id' => T_IS_IDENTICAL, 'content' => '==='), T_IS_NOT_EQUAL => array('id' => T_IS_NOT_IDENTICAL, 'content' => '!=='));
     foreach ($tokens as $index => $token) {
         $tokenId = $token->getId();
         if (isset($map[$tokenId])) {
             $tokens->overrideAt($index, array($map[$tokenId]['id'], $map[$tokenId]['content']));
         }
     }
 }
예제 #7
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     foreach ($tokens as $index => $token) {
         if (!$token->isGivenKind(T_PRINT)) {
             continue;
         }
         $prevToken = $tokens[$tokens->getPrevMeaningfulToken($index)];
         if (!$prevToken->equalsAny(array(';', '{', '}', array(T_OPEN_TAG)))) {
             continue;
         }
         $tokens->overrideAt($index, array(T_ECHO, 'echo'));
     }
 }
예제 #8
0
 /**
  * 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();
     }
 }
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     foreach ($tokens as $index => $token) {
         if (!$token->isGivenKind(array(T_CASE, T_DEFAULT))) {
             continue;
         }
         $ternariesCount = 0;
         for ($colonIndex = $index + 1;; ++$colonIndex) {
             // We have to skip ternary case for colons.
             if ($tokens[$colonIndex]->equals('?')) {
                 ++$ternariesCount;
             }
             if ($tokens[$colonIndex]->equalsAny(array(':', ';'))) {
                 if (0 === $ternariesCount) {
                     break;
                 }
                 --$ternariesCount;
             }
         }
         if ($tokens[$colonIndex]->equals(';')) {
             $tokens->overrideAt($colonIndex, ':');
         }
     }
 }
예제 #10
0
 /**
  * Clear comment token, but preserve trailing linebreak if there is any.
  *
  * @param Tokens $tokens
  * @param int    $index  T_COMMENT index
  *
  * @deprecated Will be removed in the 2.0
  */
 private function clearCommentToken(Tokens $tokens, $index)
 {
     if ("\n" !== substr($tokens[$index]->getContent(), -1, 1)) {
         $tokens->clearTokenAndMergeSurroundingWhitespace($index);
         return;
     }
     // if previous not-cleared token is whitespace;
     // append line break to content
     $previous = $tokens->getNonEmptySibling($index, -1);
     if ($tokens[$previous]->isWhitespace()) {
         $tokens[$previous]->setContent($tokens[$previous]->getContent() . "\n");
         $tokens->clearTokenAndMergeSurroundingWhitespace($index);
         return;
     }
     // elseif the next not-cleared token is whitespace;
     // prepend with line break
     $next = $tokens->getNonEmptySibling($index, 1);
     if (null !== $next && $tokens[$next]->isWhitespace()) {
         $tokens[$next]->setContent("\n" . $tokens[$next]->getContent());
         $tokens->clearTokenAndMergeSurroundingWhitespace($index);
         return;
     }
     // else
     // override with whitespace token linebreak
     $tokens->overrideAt($index, array(T_WHITESPACE, "\n", $tokens[$index]->getLine()));
 }
예제 #11
0
 private function removeUseDeclaration(Tokens $tokens, array $useDeclaration)
 {
     for ($index = $useDeclaration['start']; $index <= $useDeclaration['end']; ++$index) {
         $tokens[$index]->clear();
     }
     $prevToken = $tokens[$useDeclaration['start'] - 1];
     if ($prevToken->isWhitespace()) {
         $prevToken->setContent(rtrim($prevToken->getContent(), " \t"));
     }
     if (!isset($tokens[$useDeclaration['end'] + 1])) {
         return;
     }
     $nextIndex = $useDeclaration['end'] + 1;
     $nextToken = $tokens[$nextIndex];
     if ($nextToken->isWhitespace()) {
         $content = ltrim($nextToken->getContent(), " \t");
         if ($content && "\n" === $content[0]) {
             $content = substr($content, 1);
         }
         $nextToken->setContent($content);
     }
     if ($prevToken->isWhitespace() && $nextToken->isWhitespace()) {
         $tokens->overrideAt($nextIndex, array(T_WHITESPACE, $prevToken->getContent() . $nextToken->getContent()));
         $prevToken->clear();
     }
 }