/**
  * 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);
     }
 }
Exemple #2
0
 /**
  * @param \PHP\Manipulator\Token $caseToken
  * @return boolean
  */
 protected function _isCasePreceededByBreak(Token $caseToken)
 {
     $iterator = $this->_container->getReverseIterator();
     $iterator->seekToToken($caseToken);
     // @todo add/test T_CLOSE_TAG, T_OPEN_TAG, T_INLINE_HTML (important for alternate syntax ?)
     return $this->isPrecededByTokenType($iterator, T_BREAK);
 }
 /**
  * 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();
 }
 /**
  * @covers \PHP\Manipulator\TokenContainer::getReverseIterator
  */
 public function testGetReverseIterator()
 {
     $container = new TokenContainer();
     $iterator = $container->getReverseIterator();
     $this->assertInstanceOf('PHP\\Manipulator\\TokenContainer\\ReverseIterator', $iterator);
 }