/**
  * @covers PHP\Manipulator\ClosureFactory::getTypeAndValueClosure
  */
 public function testGetTypeAndValueClosure()
 {
     $closure = ClosureFactory::getTypeAndValueClosure(T_WHITESPACE, 'foo');
     $this->assertValidTokenMatchingClosure($closure);
     $this->assertFalse($closure(new Token('  ', T_WHITESPACE)));
     $this->assertTrue($closure(new Token('foo', T_WHITESPACE)));
     $this->assertFalse($closure(new Token('blub', T_STRING)));
     $this->assertFalse($closure(new Token('foo', T_STRING)));
 }
示例#2
0
 /**
  * Run Action
  *
  * @param \PHP\Manipulator\TokenContainer $container
  */
 public function run(TokenContainer $container, $params = null)
 {
     $iterator = $container->getIterator();
     $helper = new NewlineDetector();
     $this->_defaultLineBreak = $helper->getNewlineFromContainer($container);
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->isType($token, T_SWITCH)) {
             if ($this->getOption('spaceAfterSwitch')) {
                 $iterator->next();
                 if ($this->isType($iterator->current(), T_WHITESPACE)) {
                     $iterator->current()->setValue(' ');
                 } else {
                     $whitespaceToken = new Token(' ', T_WHITESPACE);
                     $container->insertTokenAfter($token, $whitespaceToken);
                 }
                 $iterator->update($token);
             }
             if ($this->getOption('spaceAfterSwitchVariable')) {
                 $nextOpeningBrace = $this->getNextMatchingToken($iterator, ClosureFactory::getTypeAndValueClosure(null, '('));
                 if (null !== $nextOpeningBrace) {
                     $iterator->seekToToken($nextOpeningBrace);
                     $matchingBrace = $this->getMatchingBrace($iterator);
                     if (null !== $matchingBrace) {
                         $iterator->seekToToken($matchingBrace);
                         $iterator->next();
                         if ($this->isType($iterator->current(), T_WHITESPACE)) {
                             $iterator->current()->setValue(' ');
                         } else {
                             $whitespaceToken = new Token(' ', T_WHITESPACE);
                             $container->insertTokenBefore($iterator->current(), $whitespaceToken);
                         }
                     }
                 }
                 $iterator->update($token);
             }
         }
         if ($this->isOpeningCurlyBrace($token)) {
             $iterator->next();
             if (!$this->isType($iterator->current(), T_WHITESPACE)) {
                 $whitespaceToken = new Token($this->_defaultLineBreak, T_WHITESPACE);
                 $container->insertTokenBefore($iterator->current(), $whitespaceToken);
             } elseif (!$this->evaluateConstraint('ContainsNewline', $iterator->current())) {
                 $iterator->current()->setValue($this->_defaultLineBreak . $iterator->current()->getValue());
             }
             $iterator->update($token);
         }
         if ($this->isClosingCurlyBrace($token)) {
             $iterator->previous();
             if (!$this->isType($iterator->current(), T_WHITESPACE)) {
                 $whitespaceToken = new Token($this->_defaultLineBreak, T_WHITESPACE);
                 $container->insertTokenAfter($iterator->current(), $whitespaceToken);
             } elseif (!$this->evaluateConstraint('ContainsNewline', $iterator->current())) {
                 $iterator->current()->setValue($iterator->current()->getValue() . $this->_defaultLineBreak);
             }
             $iterator->update($token);
         }
         if ($this->isType($token, T_CASE)) {
             $iterator->next();
             if (!$this->isType($iterator->current(), T_WHITESPACE)) {
                 $whitespaceToken = new Token(' ', T_WHITESPACE);
                 $container->insertTokenBefore($iterator->current(), $whitespaceToken);
             } elseif (!$this->evaluateConstraint('ContainsNewline', $iterator->current())) {
                 $iterator->current()->setValue(' ');
             }
             $iterator->update($token);
             $iterator->previous();
             if (!$this->isType($iterator->current(), T_WHITESPACE)) {
                 $whitespaceToken = new Token($this->_defaultLineBreak, T_WHITESPACE);
                 $container->insertTokenAfter($iterator->current(), $whitespaceToken);
             } elseif (!$this->evaluateConstraint('ContainsNewline', $iterator->current())) {
                 $iterator->current()->setValue($iterator->current()->getValue() . $this->_defaultLineBreak);
             }
             $iterator->update($token);
         }
         $iterator->next();
     }
     $container->retokenize();
 }
 /**
  * @param \PHP\Manipulator\TokenContainer\Iterator $iterator
  */
 protected function _handleSpaceBeforeAndAfterExpressions(Iterator $iterator)
 {
     $start = $iterator->current();
     // Adding spaces for expressions
     $openingBrace = $this->getNextMatchingToken($iterator, ClosureFactory::getTypeAndValueClosure(null, '('));
     if (null !== $openingBrace) {
         $iterator->seekToToken($openingBrace);
         $iterator->next();
         $foundToken = $iterator->current();
         if ($this->isType($foundToken, T_WHITESPACE)) {
             $foundToken->setValue($this->getOption('spaceBeforeIfExpression'));
         } else {
             $whitespaceToken = new Token($this->getOption('spaceBeforeIfExpression'), T_WHITESPACE);
             $this->_container->insertTokenBefore($foundToken, $whitespaceToken);
         }
         $iterator->update($openingBrace);
         // insert space before expression
         $closingBrace = $this->getMatchingBrace($iterator);
         if (null !== $closingBrace) {
             $iterator->seekToToken($closingBrace);
             $iterator->previous();
             $foundToken = $iterator->current();
             if ($this->isType($foundToken, T_WHITESPACE)) {
                 $foundToken->setValue($this->getOption('spaceAfterIfExpression'));
             } else {
                 $whitespaceToken = new Token($this->getOption('spaceAfterIfExpression'), T_WHITESPACE);
                 $this->_container->insertTokenAfter($foundToken, $whitespaceToken);
             }
         } else {
             throw new \Exception('f**k something went wrong');
         }
         // insert space after expression
         $iterator->update($start);
     }
 }
示例#4
0
 /**
  * @return array
  */
 public function isFollowedProvider()
 {
     $data = array();
     $path = '/AHelper/isFollowed/';
     $container = $this->getContainerFromFixture($path . 'input0.php');
     #0
     $data[] = array($container->getIterator()->seekToToken($container[21]), ClosureFactory::getIsTypeClosure(array(T_ECHO)), ClosureFactory::getIsTypeClosure(array(T_WHITESPACE, T_COMMENT, T_DOC_COMMENT, null)), $container[25], true);
     #1
     $data[] = array($container->getIterator()->seekToToken($container[21]), ClosureFactory::getIsTypeClosure(array(T_ECHO)), ClosureFactory::getIsTypeClosure(array(T_WHITESPACE, T_COMMENT, T_DOC_COMMENT)), null, false);
     #2
     $data[] = array($container->getIterator()->seekToToken($container[25]), ClosureFactory::getIsTypeClosure(array(T_FOR)), ClosureFactory::getIsTypeClosure(array(T_WHITESPACE, T_COMMENT, T_DOC_COMMENT, null)), null, false);
     return $data;
 }
示例#5
0
 /**
  * @param \PHP\Manipulator\TokenContainer\Iterator $iterator
  * @param \Closure $closure
  * @param array $allowedTypes
  * @return boolean
  * @todo ugly name
  */
 public function isPrecededByTokenMatchedByClosure(Iterator $iterator, \Closure $closure, array $allowedTypes = array(T_WHITESPACE, T_COMMENT, T_DOC_COMMENT))
 {
     return $this->isPreceded($iterator, $closure, ClosureFactory::getIsTypeClosure($allowedTypes));
 }