Exemplo n.º 1
0
 /**
  * Run Action
  *
  * @param \PHP\Manipulator\TokenContainer $container
  */
 public function run(TokenContainer $container)
 {
     $iterator = $container->getIterator();
     $waitingForIf = false;
     $replaceTokens = array();
     $allowedTypes = array(T_IF, T_ELSE, T_WHITESPACE);
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->isType($token, T_ELSE)) {
             $waitingForIf = true;
             $replaceTokens = array();
         }
         if (true === $waitingForIf && !$this->isType($token, $allowedTypes)) {
             $waitingForIf = false;
         } else {
             $replaceTokens[] = $token;
         }
         if (true === $waitingForIf && $this->isType($token, T_IF)) {
             $waitingForIf = false;
             $token = array_pop($replaceTokens);
             $token->setType(T_ELSEIF);
             $token->setValue('elseif');
             $container->removeTokens($replaceTokens);
             $replaceTokens = array();
         }
         $iterator->next();
     }
     $container->retokenize();
 }
 /**
  * Remove unneded ?> from the file-end
  *
  * @param \PHP\Manipulator\TokenContainer $container
  * @param mixed $params
  */
 public function run(TokenContainer $container)
 {
     $stripWhitespaceFromEnd = $this->getOption('stripWhitespaceFromEnd');
     $iterator = $container->getReverseIterator();
     $helper = new NewlineDetector("\n");
     while ($iterator->valid()) {
         $token = $iterator->current();
         if (!$this->_isNotAllowedTag($token)) {
             break;
         } elseif ($this->isType($token, T_CLOSE_TAG)) {
             if ($this->evaluateConstraint('EndsWithNewline', $token)) {
                 $newline = $helper->getNewlineFromToken($token);
                 $token->setType(T_WHITESPACE);
                 $token->setValue($newline);
             } else {
                 $container->removeToken($token);
             }
             break;
         }
         $iterator->next();
     }
     $container->retokenize();
     if (true === $stripWhitespaceFromEnd) {
         $this->runAction('RemoveWhitespaceFromEnd', $container);
     }
 }
Exemplo n.º 3
0
 /**
  * Unindents all Code
  *
  * @param \PHP\Manipulator\TokenContainer $container
  * @param mixed $params
  */
 public function run(TokenContainer $container)
 {
     $iterator = $container->getIterator();
     $regexWhitespace = '[\\t ]{1,}';
     $regexNotWhitespace = '[^\\t^ ]{1,}';
     $linebreak = '\\n|\\r\\n|\\r';
     $previousToken = null;
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->isType($token, T_WHITESPACE)) {
             $value = $token->getValue();
             // Single-line-Comments include a Linebreak at the end, so the whitespace not begins with a linebreak
             if ($this->evaluateConstraint('IsSinglelineComment', $previousToken, T_COMMENT)) {
                 $value = preg_replace('~' . $regexWhitespace . '$~', '\\1', $value);
             }
             $value = preg_replace('~(' . $linebreak . ')' . $regexWhitespace . '$~', '\\1', $value);
             $value = preg_replace('~(' . $linebreak . ')' . $regexWhitespace . $regexNotWhitespace . '(.*?)(' . $linebreak . ')~m', '\\1\\2', $value);
             $value = preg_replace('~(' . $linebreak . ')' . $regexWhitespace . $regexNotWhitespace . '(.*?)$~m', '\\1\\2', $value);
             $token->setValue($value);
         } elseif ($this->evaluateConstraint('IsMultilineComment', $token)) {
             $this->manipulateToken('RemoveCommentIndention', $token);
         }
         $previousToken = $token;
         $iterator->next();
     }
     $container->retokenize();
 }
Exemplo n.º 4
0
 /**
  * Run Action
  *
  * @param \PHP\Manipulator\TokenContainer $container
  */
 public function run(TokenContainer $container)
 {
     $this->_container = $container;
     $iterator = $container->getIterator();
     $lastElse = null;
     $noOtherTokens = true;
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->isType($token, T_ELSE)) {
             $lastElse = $token;
             $noOtherTokens = true;
         }
         if (null !== $lastElse && !$this->_isAllowedTokenInsideEmptyElse($token)) {
             $noOtherTokens = false;
         }
         if ($this->_isEndElse($token) && true === $noOtherTokens && null !== $lastElse) {
             $start = $lastElse;
             $end = $token;
             $previous = $container->getPreviousToken($start);
             if ($this->isType($end, T_ENDIF)) {
                 $end = $container->getPreviousToken($end);
             }
             $container->removeTokensFromTo($start, $end);
             $iterator->update($previous);
             $lastElse = null;
         }
         $iterator->next();
     }
     $container->retokenize();
 }
Exemplo n.º 5
0
 /**
  * Run Action
  *
  * @param \PHP\Manipulator\TokenContainer $container
  */
 public function run(TokenContainer $container, $params = null)
 {
     $iterator = $container->getIterator();
     while ($iterator->valid()) {
         $token = $iterator->current();
         $iterator->next();
     }
     $container->retokenize();
 }
 /**
  * Remove ErrorControlOperators (@)
  *
  * @param \PHP\Manipulator\TokenContainer $container
  * @param mixed $params
  */
 public function run(TokenContainer $container)
 {
     $iterator = $container->getIterator();
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->evaluateConstraint('IsErrorControlOperator', $token)) {
             $container->removeToken($token);
         }
         $iterator->next();
     }
     $container->retokenize();
 }
 /**
  * Run Action
  *
  * @param \PHP\Manipulator\TokenContainer $container
  */
 public function run(TokenContainer $container, $params = null)
 {
     $iterator = $container->getIterator();
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->isType($token, T_DOC_COMMENT)) {
             $this->manipulateToken('RemoveLeadingAndTrailingEmptyLinesInPhpdoc', $token);
         }
         $iterator->next();
     }
     $container->retokenize();
 }
Exemplo n.º 8
0
 /**
  * @param \PHP\Manipulator\TokenContainer $container
  * @param mixed $params
  */
 public function run(TokenContainer $container)
 {
     $iterator = $container->getIterator();
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->isType($token, T_OPEN_TAG)) {
             $token->setValue(str_replace('<%', '<?php', $token->getValue()));
         } elseif ($this->isType($token, T_OPEN_TAG_WITH_ECHO)) {
             $token->setValue(str_replace('<%=', '<?php echo ', $token->getValue()));
         }
         $iterator->next();
     }
     $container->retokenize();
 }
 /**
  * Run Action
  *
  * @param \PHP\Manipulator\TokenContainer $container
  */
 public function run(TokenContainer $container)
 {
     $iterator = $container->getIterator();
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->isType($token, T_CONSTANT_ENCAPSED_STRING)) {
             if (!$this->_containsEscapeSequence($token)) {
                 $value = $token->getValue();
                 $token->setValue(str_replace('"', '\'', $value));
             }
         }
         $iterator->next();
     }
     $container->retokenize();
 }
Exemplo n.º 10
0
 /**
  * Make all Constants uppercase
  *
  * @param \PHP\Manipulator\TokenContainer $container
  * @param mixed $params
  */
 public function run(TokenContainer $container)
 {
     $iterator = $container->getIterator();
     $this->_container = $container;
     $this->_setNext($iterator);
     while ($iterator->valid()) {
         $token = $iterator->current();
         $this->_checkCurrentToken($token);
         if ($this->_isConstant($iterator)) {
             $token->setValue(strtoupper($token->getValue()));
         }
         $this->_setNext($iterator);
         $iterator->next();
     }
     $container->retokenize();
 }
Exemplo n.º 11
0
 /**
  * Run Action
  *
  * @param \PHP\Manipulator\TokenContainer $container
  */
 public function run(TokenContainer $container)
 {
     $this->_container = $container;
     $iterator = $container->getIterator();
     $insideClassOrInterface = false;
     $classLevel = null;
     $level = 0;
     $insideMethod = false;
     $methodLevel = null;
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->isOpeningCurlyBrace($token)) {
             $level++;
         }
         if ($this->isClosingCurlyBrace($token)) {
             $level--;
             if ($classLevel === $level && true === $insideClassOrInterface) {
                 $insideClassOrInterface = false;
                 $classLevel = null;
                 if (true === $insideMethod) {
                     $insideMethod = false;
                     $methodLevel = null;
                 }
             }
         }
         if ($this->isType($token, array(T_CLASS, T_INTERFACE))) {
             $insideClassOrInterface = true;
             $classLevel = $level;
         }
         if (true === $insideClassOrInterface && false === $insideMethod) {
             if ($this->isType($token, T_FUNCTION)) {
                 $insideMethod = true;
                 if (!$this->isPrecededByTokenType($iterator, array(T_PUBLIC, T_PRIVATE, T_PROTECTED))) {
                     $token = $iterator->current();
                     $publicToken = new Token('public', T_PUBLIC);
                     $whitespaceToken = new Token(' ', T_WHITESPACE);
                     $this->_container->insertTokensBefore($token, array($publicToken, $whitespaceToken));
                     $iterator->update($token);
                 }
             }
         }
         $iterator->next();
     }
     $container->retokenize();
 }
Exemplo n.º 12
0
 /**
  * Run Action
  *
  * @param \PHP\Manipulator\TokenContainer $container
  */
 public function run(TokenContainer $container)
 {
     $iterator = $container->getIterator();
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->isType($token, T_ELSEIF)) {
             $token->setType(T_ELSE);
             $token->setValue('else');
             $whitespaceToken = new Token(' ', T_WHITESPACE);
             $ifToken = new Token('if', T_IF);
             $container->insertTokenAfter($token, $whitespaceToken);
             $container->insertTokenAfter($whitespaceToken, $ifToken);
             $iterator = $container->getIterator();
             $iterator->seekToToken($ifToken);
         }
         $iterator->next();
     }
     $container->retokenize();
 }
 /**
  * Run Action
  *
  * @param \PHP\Manipulator\TokenContainer $container
  */
 public function run(TokenContainer $container)
 {
     $iterator = $container->getIterator();
     $this->_classStack = new SplStack();
     $classname = null;
     while ($iterator->valid()) {
         $token = $iterator->current();
         $this->_checkLevel($token);
         if ($this->isType($token, T_CLASS)) {
             $this->_classStack->push($this->_level);
             $iterator->next();
             while ($iterator->valid()) {
                 $token = $iterator->current();
                 $this->_checkLevel($token);
                 if ($this->isType($token, T_STRING)) {
                     $classname = $token->getValue();
                     break;
                 }
                 $iterator->next();
             }
         }
         if (!$this->_classStack->isEmpty()) {
             if ($this->isType($token, T_FUNCTION)) {
                 while ($iterator->valid()) {
                     $token = $iterator->current();
                     $this->_checkLevel($token);
                     if ($this->isType($token, T_STRING)) {
                         if (strtolower($token->getValue()) === strtolower($classname)) {
                             $token->setValue('__construct');
                         }
                         break;
                     }
                     $iterator->next();
                 }
             }
         }
         $iterator->next();
     }
     $container->retokenize();
 }
Exemplo n.º 14
0
 /**
  * Remove php-code
  *
  * @param \PHP\Manipulator\TokenContainer $container
  * @param mixed $params
  */
 public function run(TokenContainer $container)
 {
     $iterator = $container->getIterator();
     $open = false;
     $this->_deleteList = array();
     $allowedTokens = array(T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO);
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->isType($token, $allowedTokens)) {
             $open = true;
         }
         if ($this->_shoudDelete($open)) {
             $this->_deleteList[] = $token;
         }
         if ($this->isType($token, T_CLOSE_TAG)) {
             $open = false;
         }
         $iterator->next();
     }
     $container->removeTokens($this->_deleteList);
     $container->retokenize();
 }
 /**
  * Remove Whitespace from the end
  *
  * @param \PHP\Manipulator\TokenContainer $container
  * @param mixed $params
  */
 public function run(TokenContainer $container)
 {
     $iterator = $container->getReverseIterator();
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->isType($token, T_WHITESPACE)) {
             $container->removeToken($token);
         } elseif ($this->isType($token, T_INLINE_HTML)) {
             if ($this->evaluateConstraint('ContainsOnlyWhitespace', $token)) {
                 $container->removeToken($token);
             } else {
                 $token->setValue(rtrim($token->getValue()));
                 break;
             }
         } else {
             $token->setValue(rtrim($token->getValue()));
             break;
         }
         $iterator->next();
     }
     $container->retokenize();
 }
Exemplo n.º 16
0
 /**
  * Format casts
  *
  * @param \PHP\Manipulator\TokenContainer $container
  * @param array $params
  */
 public function run(TokenContainer $container)
 {
     $iterator = $container->getIterator();
     $searchedTokens = $this->getOption('searchedTokens');
     $whitespace = $this->getOption('whitespaceBehindCasts');
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->isType($token, array_keys($searchedTokens))) {
             $token->setValue($searchedTokens[$token->getType()]);
             $next = $iterator->getNext();
             if ($this->isType($next, T_WHITESPACE)) {
                 if ($next->getValue() != $whitespace) {
                     $next->setValue($this->getOption('whitespaceBehindCasts'));
                 }
             } elseif (!empty($whitespace)) {
                 $container->insertTokenAfter($token, new Token($whitespace, T_WHITESPACE));
                 $iterator->update($token);
             }
         }
         $iterator->next();
     }
     $container->retokenize();
 }
Exemplo n.º 17
0
 /**
  * @covers \PHP\Manipulator\TokenContainer::retokenize
  */
 public function testRetokenize()
 {
     $token0 = Token::factory(array(0 => T_OPEN_TAG, 1 => "<?php\n"));
     $token1 = Token::factory(array(0 => T_WHITESPACE, 1 => " \n \n \n"));
     $token2 = Token::factory(array(0 => T_WHITESPACE, 1 => " \t \n "));
     $token3 = Token::factory(array(0 => T_CLOSE_TAG, 1 => "?>"));
     $container = new TokenContainer(array($token0, $token1, $token2, $token3));
     $container->retokenize();
     $this->assertCount(3, $container);
     $this->assertEquals("<?php\n \n \n \n \t \n ?>", $container->toString());
 }