コード例 #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();
 }
コード例 #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_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;
 }
コード例 #3
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;
 }
コード例 #4
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();
 }
コード例 #5
0
 /**
  * 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);
     }
 }
コード例 #6
0
 /**
  * @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;
 }
コード例 #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;
 }
コード例 #8
0
ファイル: Action.php プロジェクト: robo47/php-manipulator
 /**
  * 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();
 }
コード例 #9
0
 /**
  * @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();
     }
 }
コード例 #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();
     }
 }
コード例 #11
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();
     }
 }
コード例 #12
0
 /**
  * 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();
 }
コード例 #14
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;
 }
コード例 #15
0
 /**
  * Remove trailing spaces
  *
  * @param \PHP\Manipulator\TokenContainer $container
  * @param mixed $params
  */
 public function run(TokenContainer $container)
 {
     $newlineDetector = new NewlineDetector();
     $code = $container->toString();
     $defaultBreak = $newlineDetector->getNewlineFromContainer($container);
     $code = preg_split('~(\\r\\n|\\n|\\r)~', $code);
     $code = array_map('rtrim', $code);
     $code = implode($defaultBreak, $code);
     if (true === $this->getOption('removeEmptyLinesAtFileEnd')) {
         $code = rtrim($code);
     }
     $container->updateFromCode($code);
 }
コード例 #16
0
ファイル: Util.php プロジェクト: robo47/php-manipulator
 /**
  * Compare Containers
  *
  * Returns string-presentation of both containers next to each other
  *
  * @param \PHP\Manipulator\TokenContainer $expected
  * @param \PHP\Manipulator\TokenContainer $secod
  */
 public static function compareContainers(TokenContainer $expectedContainer, TokenContainer $actualContainer, $strict)
 {
     $expectedIterator = new \ArrayIterator($expectedContainer->toArray());
     $actualIterator = new \ArrayIterator($actualContainer->toArray());
     $values = array();
     $longest = 0;
     while ($actualIterator->valid() || $expectedIterator->valid()) {
         $expected = '';
         $actual = '';
         $missmatch = true;
         if ($expectedIterator->valid()) {
             $expected = (string) self::dumpToken($expectedIterator->current(), false);
         }
         if ($actualIterator->valid()) {
             $actual = (string) self::dumpToken($actualIterator->current(), false);
         }
         if ($actualIterator->valid() && $expectedIterator->valid()) {
             $constraint = new TokensMatchConstraint($expectedIterator->current(), $strict);
             $missmatch = !$constraint->evaluate($actualIterator->current(), '', true);
         }
         $values[] = array('actual' => $actual, 'expected' => $expected, 'missmatch' => $missmatch);
         if (strlen($actual) > $longest) {
             $longest = strlen($actual);
         }
         if (strlen($expected) > $longest) {
             $longest = strlen($expected);
         }
         $expectedIterator->next();
         $actualIterator->next();
     }
     $comparision = '    ';
     $comparision .= str_pad('Tokens: ' . count($expectedContainer), $longest + 2, ' ', STR_PAD_BOTH);
     $comparision .= ' | ';
     $comparision .= str_pad('Tokens: ' . count($actualContainer), $longest + 2, ' ', STR_PAD_BOTH);
     $comparision .= PHP_EOL;
     $comparision .= PHP_EOL;
     $i = 0;
     foreach ($values as $val) {
         if (true === $val['missmatch']) {
             $comparision .= '####### NEXT IS DIFFERENT ## ' . PHP_EOL;
         }
         $comparision .= str_pad($i . ') ', 4, ' ');
         $comparision .= str_pad($val['expected'], $longest + 2, ' ');
         $comparision .= ' | ';
         $comparision .= str_pad($val['actual'], $longest + 2, ' ');
         $comparision .= PHP_EOL;
         $i++;
     }
     $comparision = rtrim($comparision);
     return $comparision;
 }
コード例 #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();
 }
コード例 #18
0
 /**
  * @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;
 }
コード例 #19
0
 /**
  * @param array $tokens
  */
 protected function _handleTokens(TokenContainer $container, array $tokens)
 {
     foreach ($tokens as $start) {
         if ($container->contains($start)) {
             $result = $this->findTokens('IncludeAndRequire', $start, $container);
             $tokens = $result->getTokens();
             foreach ($tokens as $token) {
                 if ($container->contains($token)) {
                     $container->removeToken($token);
                 }
             }
         }
     }
 }
コード例 #20
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_CONSTANT_ENCAPSED_STRING)) {
             if (!$this->_containsEscapeSequence($token)) {
                 $value = $token->getValue();
                 $token->setValue(str_replace('"', '\'', $value));
             }
         }
         $iterator->next();
     }
     $container->retokenize();
 }
コード例 #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'));
 }
コード例 #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();
     }
 }
コード例 #24
0
 /**
  * @param string $file
  * @throws Exception If file does not exist or is not readable
  */
 public function __construct($file)
 {
     $this->_file = $file;
     if (!file_exists($file) || !is_file($file) || !is_readable($file)) {
         throw new \Exception('Unable to open file for reading: ' . $file);
     }
     parent::__construct(file_get_contents($file));
 }
コード例 #25
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();
 }
コード例 #26
0
ファイル: Iterator.php プロジェクト: robo47/php-manipulator
 /**
  * @param \PHP\Manipulator\Token $token
  * @return \PHP\Manipulator\TokenContainer\Iterator *Provides Fluent Interface*
  */
 public function seekToToken(Token $token)
 {
     if ($this->valid() && $this->current() === $token) {
         return $this;
     }
     $key = $this->_container->getOffsetByToken($token);
     $this->seek($key);
     return $this;
 }
コード例 #27
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;
 }
コード例 #28
0
 /**
  * @param \PHP\Manipulator\TokenContainer $container
  * @param array $argumentTokens
  */
 protected function _parseSingleArgument(array $argumentTokens, $container)
 {
     foreach ($argumentTokens as $token) {
         if ('=' === $token->getValue()) {
             break;
         }
         if ($this->isType($token, array(T_STRING, T_ARRAY, T_NS_SEPARATOR))) {
             $container->removeToken($token);
         }
     }
 }
コード例 #29
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();
 }
コード例 #30
0
 /**
  * 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();
 }