/**
  * @param \PHP\Manipulator\TokenContainer $other
  * @param  string $description Additional information about the test
  * @param  bool $returnResult Whether to return a result or throw an exception
  * @return boolean
  */
 public function evaluate($other, $description = '', $returnResult = FALSE)
 {
     if (!$other instanceof TokenContainer) {
         throw \PHPUnit_Util_InvalidArgumentHelper::factory(1, 'PHP\\Manipulator\\TokenContainer');
     }
     $expectedIterator = $this->_expectedContainer->getIterator();
     $actualIterator = $other->getIterator();
     $i = 0;
     while ($expectedIterator->valid() && $actualIterator->valid()) {
         $expectedToken = $expectedIterator->current();
         /* @var $expectedToken PHP\Manipulator\Token */
         $actualToken = $actualIterator->current();
         /* @var $actualToken PHP\Manipulator\Token */
         if (!$actualToken->equals($expectedToken, $this->_strict)) {
             if ($returnResult) {
                 return FALSE;
             }
             $this->fail($other, $description);
         }
         $i++;
         $expectedIterator->next();
         $actualIterator->next();
     }
     if ($expectedIterator->valid() || $actualIterator->valid()) {
         if ($returnResult) {
             return FALSE;
         }
         $this->fail($other, $description);
     }
     return true;
 }
Exemplo n.º 2
0
 /**
  * Finds tokens
  *
  * @param \PHP\Manipulator\Token $token
  * @param \PHP\Manipulator\TokenContainer $container
  * @param mixed $params
  * @return \PHP\Manipulator\TokenFinder\Result
  */
 public function find(Token $token, TokenContainer $container, $params = null)
 {
     if (!$this->isType($token, T_SWITCH)) {
         throw new \Exception('Starttoken is not T_SWITCH');
     }
     $result = new Result();
     $iterator = $container->getIterator();
     $iterator->seekToToken($token);
     $level = 0;
     $inside = false;
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->isOpeningCurlyBrace($token)) {
             if (0 === $level) {
                 $inside = true;
             }
             $level++;
         }
         if ($this->isClosingCurlyBrace($token)) {
             $level--;
         }
         $result->addToken($token);
         if ($inside && 0 === $level) {
             break;
         }
         $iterator->next();
     }
     return $result;
 }
Exemplo n.º 3
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.º 4
0
 /**
  * Finds tokens
  *
  * @param \PHP\Manipulator\Token $token
  * @param \PHP\Manipulator\TokenContainer $container
  * @param mixed $params
  * @return \PHP\Manipulator\TokenFinder\Result
  */
 public function find(Token $token, TokenContainer $container, $params = null)
 {
     if (!$this->isType($token, T_FUNCTION)) {
         throw new \Exception('Starttoken is not T_FUNCTION');
     }
     $iterator = $container->getIterator();
     $iterator->seekToToken($token);
     if ($this->_includeMethodProperties($params) && !$this->_includePhpDoc($params)) {
         $this->_seekToMethodProperties($iterator);
     }
     if ($this->_includePhpDoc($params)) {
         $this->_seekToPhpdoc($iterator, $params);
     }
     $this->_inside = false;
     $this->_level = 0;
     $this->_end = false;
     $result = new Result();
     while ($iterator->valid() && false === $this->_end) {
         $result->addToken($iterator->current());
         $this->_checkLevel($iterator);
         $this->_checkBreak($iterator);
         $iterator->next();
     }
     return $result;
 }
Exemplo n.º 5
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();
 }
Exemplo n.º 6
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.º 7
0
 /**
  * Finds tokens
  *
  * @param \PHP\Manipulator\Token $token
  * @param \PHP\Manipulator\TokenContainer $container
  * @param mixed $params
  * @return \PHP\Manipulator\TokenFinder\Result
  */
 public function find(Token $token, TokenContainer $container, $params = null)
 {
     $iterator = $container->getIterator();
     while ($iterator->valid()) {
         $iterator->next();
     }
     return false;
 }
Exemplo n.º 8
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();
 }
Exemplo n.º 9
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();
 }
Exemplo n.º 10
0
 /**
  * Replace var $foo; with public $foo;
  *
  * @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_VAR)) {
             $token->setType(T_PUBLIC)->setValue('public');
         }
         $iterator->next();
     }
 }
 /**
  * @param \PHP\Manipulator\TokenContainer $container
  * @param array $tokens
  * @param string $whitespace
  */
 public function run(TokenContainer $container, array $tokens, array $whitespace)
 {
     $this->_container = $container;
     $iterator = $container->getIterator();
     while ($iterator->valid()) {
         if (in_array($iterator->current(), $tokens)) {
             $this->setWhitespace($iterator, $whitespace);
         }
         $iterator->next();
     }
 }
Exemplo n.º 12
0
 /**
  * @param \PHP\Manipulator\TokenContainer $container
  * @param mixed $params
  */
 public function run(TokenContainer $container)
 {
     $newline = $this->getOption('newline');
     $iterator = $container->getIterator();
     while ($iterator->valid()) {
         $token = $iterator->current();
         $value = preg_replace('~(\\r\\n|\\n|\\r)~', $newline, $token->getValue());
         $token->setValue($value);
         $iterator->next();
     }
 }
 /**
  * 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();
 }
 /**
  * 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();
 }
Exemplo n.º 15
0
 /**
  * Dump Container
  *
  * @param \PHP\Manipulator\TokenContainer $container
  * @return string
  */
 public static function dumpContainer(TokenContainer $container)
 {
     $dump = '';
     $iterator = $container->getIterator();
     $dump .= str_pad('Token', 28, ' ', STR_PAD_RIGHT) . '| ' . str_pad('LEN', 4, ' ', STR_PAD_LEFT) . ' | ' . str_pad('LINE', 4, ' ', STR_PAD_LEFT) . ' | VALUE' . PHP_EOL . PHP_EOL;
     while ($iterator->valid()) {
         $token = $iterator->current();
         $dump .= Util::dumpToken($token) . PHP_EOL;
         $iterator->next();
     }
     return trim($dump);
 }
Exemplo n.º 16
0
 /**
  * @param \PHP\Manipulator\Token $token
  * @return string
  */
 public function getNewlineFromContainer(TokenContainer $container)
 {
     $iterator = $container->getIterator();
     while ($iterator->valid()) {
         $newline = $this->_getNewlineFromToken($iterator->current());
         if (false !== $newline) {
             return $newline;
         }
         $iterator->next();
     }
     return $this->_defaultNewline;
 }
Exemplo n.º 17
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();
 }
Exemplo n.º 18
0
 /**
  * @param \PHP\Manipulator\Token $token
  */
 public function _checkAndChangeIndentionLevelIncreasment(Token $token)
 {
     if ($this->isColon($token)) {
         $iterator = $this->_container->getIterator();
         $iterator->seekToToken($token);
         $allowedTypes = array(null, T_STRING, T_WHITESPACE, T_COMMENT, T_DOC_COMMENT);
         if ($this->isPrecededByTokenType($iterator, T_IF, $allowedTypes)) {
             $this->_indentionLevel++;
         }
     }
     if ($this->_isIndentionLevelIncreasment($token)) {
         $this->_indentionLevel++;
     }
 }
 /**
  * @param \PHP\Manipulator\TokenContainer $container
  * @param integer $startOffset
  * @param integer $endOffset
  * @return array
  */
 protected function _getTokensFromStartToEnd(TokenContainer $container, $startOffset, $endOffset)
 {
     $iterator = $container->getIterator();
     $iterator->seek($startOffset);
     $tokens = array();
     while ($iterator->valid()) {
         $tokens[] = $iterator->current();
         if ($iterator->key() === $endOffset) {
             break;
         }
         $iterator->next();
     }
     return $tokens;
 }
 /**
  * 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.º 21
0
 /**
  * @param \PHP\Manipulator\TokenContainer $container
  * @param mixed $params
  */
 public function run(TokenContainer $container)
 {
     $iterator = $container->getIterator();
     $operatorTokens = array();
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->evaluateConstraint('IsOperator', $token)) {
             $operatorTokens[] = $token;
         }
         $iterator->next();
     }
     $setWhitespaceAfterToken = new SetWhitespaceAfterToken();
     $setWhitespaceAfterToken->run($container, $operatorTokens, $this->getOption('afterOperator'));
     $setWhitespaceBeforeToken = new SetWhitespaceBeforeToken();
     $setWhitespaceBeforeToken->run($container, $operatorTokens, $this->getOption('beforeOperator'));
 }
Exemplo n.º 22
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();
 }
 /**
  * Replace boolean and (AND)/or (OR) with logical and (&&)/or (||)
  *
  * @param \PHP\Manipulator\TokenContainer $container
  * @param mixed $params
  */
 public function run(TokenContainer $container)
 {
     $iterator = $container->getIterator();
     $and = '&&';
     $or = '||';
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->_isLogicalAndAndShouldBeReplaced($token)) {
             $token->setValue($and);
             $token->setType(T_BOOLEAN_AND);
         } elseif ($this->_isLogicalOrAndShouldBeReplaced($token)) {
             $token->setValue($or);
             $token->setType(T_BOOLEAN_OR);
         }
         $iterator->next();
     }
 }
Exemplo n.º 24
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.º 25
0
 /**
  * Finds tokens
  *
  * @param \PHP\Manipulator\Token $token
  * @param \PHP\Manipulator\TokenContainer $container
  * @param mixed $params
  * @return \PHP\Manipulator\TokenFinder\Result
  */
 public function find(Token $token, TokenContainer $container, $params = null)
 {
     $allowedTokens = array(T_INCLUDE, T_INCLUDE_ONCE, T_REQUIRE, T_REQUIRE_ONCE);
     if (!$this->isType($token, $allowedTokens)) {
         $message = 'Start-token is not one of T_INCLUDE, T_INCLUDE_ONCE, T_REQUIRE, T_REQUIRE_ONCE';
         throw new \Exception($message);
     }
     $result = new Result();
     $iterator = $container->getIterator();
     $iterator->seekToToken($token);
     while ($iterator->valid()) {
         $token = $iterator->current();
         $result->addToken($token);
         if ($this->isSemicolon($token)) {
             break;
         }
         $iterator->next();
     }
     return $result;
 }
 /**
  * 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.º 27
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();
 }
 /**
  * Replace boolean and (AND)/or (OR) with logical and (&&)/or (||)
  *
  * @param \PHP\Manipulator\TokenContainer $container
  * @param mixed $params
  */
 public function run(TokenContainer $container)
 {
     $iterator = $container->getIterator();
     if ($this->getOption('uppercase')) {
         $and = 'AND';
         $or = 'OR';
     } else {
         $and = 'and';
         $or = 'or';
     }
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->_isBooleanAndAndShouldBeReplaced($token)) {
             $token->setValue($and);
             $token->setType(T_LOGICAL_AND);
         } elseif ($this->_isBooleanOrAndShouldBeReplaced($token)) {
             $token->setValue($or);
             $token->setType(T_LOGICAL_OR);
         }
         $iterator->next();
     }
 }
Exemplo n.º 29
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.º 30
0
 /**
  * @param \PHP\Manipulator\TokenContainer $container
  * @param \PHP\Manipulator\Token $token
  */
 protected function _parseFunctionArguments(TokenContainer $container, Token $startToken)
 {
     $iterator = $container->getIterator();
     $iterator->seekToToken($startToken);
     $indentionLevel = 0;
     $inside = false;
     $arguments = array();
     $argumentTokens = array();
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->isOpeningBrace($token)) {
             $indentionLevel++;
         } elseif ($this->isClosingBrace($token)) {
             $indentionLevel--;
         }
         // next argument
         if ($this->isComma($token)) {
             $arguments[] = $argumentTokens;
             $argumentTokens = array();
         }
         if ($indentionLevel > 0) {
             $argumentTokens[] = $token;
             $inside = true;
         } elseif ($indentionLevel === 0 && true === $inside) {
             // break if we are at the end of the arguments
             break;
         }
         $iterator->next();
     }
     // add last argument
     if (!empty($argumentTokens)) {
         $arguments[] = $argumentTokens;
     }
     foreach ($arguments as $argument) {
         $this->_parseSingleArgument($argument, $container);
     }
 }