/**
  * 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);
 }
 /**
  * Removes all Comments
  *
  * @param \PHP\Manipulator\TokenContainer $container
  * @param mixed $params
  */
 public function run(TokenContainer $container)
 {
     $helper = new NewlineDetector();
     $newline = $helper->getNewlineFromContainer($container);
     $iterator = $container->getIterator();
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->_isCommentAndShouldBeRemoved($token)) {
             if ($this->evaluateConstraint('IsSinglelineComment', $token)) {
                 $token->setType(T_WHITESPACE);
                 $token->setValue($newline);
             } else {
                 $container->removeToken($token);
             }
         }
         $iterator->next();
     }
     $container->retokenize();
 }
 /**
  * @param \PHP\Manipulator\TokenContainer $container
  * @param mixed $params
  */
 public function run(TokenContainer $container)
 {
     $newlineDetector = new NewlineDetector();
     $iterator = $container->getIterator();
     $maxEmptyLines = $this->getOption('maxEmptyLines');
     $defaultBreak = $newlineDetector->getNewlineFromContainer($container);
     $previous = null;
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->isType($token, T_WHITESPACE)) {
             if (null !== $previous && $this->evaluateConstraint('IsSinglelineComment', $previous)) {
                 $maxEmptyLines = $this->getOption('maxEmptyLines') - 1;
             } else {
                 $maxEmptyLines = $this->getOption('maxEmptyLines');
             }
             $pattern = '~(((\\r\\n|\\r|\\n)([\\t| ]{0,})){' . ($maxEmptyLines + 1) . ',}([\\t| ]{0,}))~';
             $replace = str_repeat($defaultBreak, $maxEmptyLines) . '\\4';
             $value = preg_replace($pattern, $replace, $token->getValue());
             $token->setValue($value);
         }
         $previous = $token;
         $iterator->next();
     }
 }
 /**
  * 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();
 }
 /**
  * Run Action
  *
  * @param \PHP\Manipulator\TokenContainer $container
  */
 public function run(TokenContainer $container)
 {
     $this->_container = $container;
     $helper = new NewlineDetector();
     $this->_defaultLineBreak = $helper->getNewlineFromContainer($container);
     $iterator = $container->getIterator();
     $this->_reset();
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->isType($token, T_IF)) {
             $this->_ifStack->push($this->_level);
             $this->_format($iterator);
             $this->_handleSpaceBeforeAndAfterExpressions($iterator);
         }
         if ($this->isType($token, T_ELSEIF)) {
             $this->_elseifStack->push($this->_level);
             $this->_format($iterator);
             $this->_handleSpaceBeforeAndAfterExpressions($iterator);
         }
         if ($this->isType($token, T_ELSE)) {
             $this->_elseStack->push($this->_level);
             $this->_format($iterator);
         }
         if ($this->isOpeningCurlyBrace($token)) {
             $this->_level++;
             $this->_applyBreaksAfterCurlyBraces($iterator);
             if (true === $this->getOption('breakAfterIf') && $this->_stackHasLevelMatchingItem($this->_ifStack) - 1) {
                 $this->_addLineBreakBeforeCurrentToken($iterator);
             }
             if (true === $this->getOption('breakAfterElseif') && $this->_stackHasLevelMatchingItem($this->_elseifStack) - 1) {
                 $this->_addLineBreakBeforeCurrentToken($iterator);
             }
             if (true === $this->getOption('breakAfterElse') && $this->_stackHasLevelMatchingItem($this->_elseStack) - 1) {
                 $this->_addLineBreakBeforeCurrentToken($iterator);
             }
         }
         if ($this->isClosingCurlyBrace($token)) {
             $this->_level--;
             if (!$this->isPrecededByTokenType($iterator, T_WHITESPACE)) {
                 if ($this->_shouldInsertBreakBeforeCurrentCurlyBrace()) {
                     $newToken = new Token($this->_defaultLineBreak, T_WHITESPACE);
                     $this->_container->insertTokenBefore($token, $newToken);
                     $iterator->update($token);
                 }
             }
             if (true === $this->getOption('breakBeforeCurlyBraceOfElse') && $this->isFollowedByTokenType($iterator, T_ELSE)) {
                 $this->_addLineBreakBeforeCurrentToken($iterator);
             }
             if (true === $this->getOption('breakBeforeCurlyBraceOfElseif') && $this->isFollowedByTokenType($iterator, T_ELSEIF)) {
                 $this->_addLineBreakBeforeCurrentToken($iterator);
             }
             if (true === $this->getOption('breakBeforeElse') && $this->isFollowedByTokenType($iterator, T_ELSE)) {
                 $this->_addLineBreakAfterCurrentToken($iterator);
             }
             if (true === $this->getOption('breakBeforeElseif') && $this->isFollowedByTokenType($iterator, T_ELSEIF)) {
                 $this->_addLineBreakAfterCurrentToken($iterator);
             }
             if ($this->_stackHasLevelMatchingItem($this->_ifStack)) {
                 $this->_ifStack->pop();
             }
             if ($this->_stackHasLevelMatchingItem($this->_elseifStack)) {
                 $this->_elseifStack->pop();
             }
             if ($this->_stackHasLevelMatchingItem($this->_elseStack)) {
                 $this->_elseStack->pop();
             }
         }
         $iterator->next();
     }
     $container->retokenize();
 }
 /**
  * @dataProvider containerProvider
  * @covers \PHP\Manipulator\Helper\NewlineDetector::getNewlineFromContainer
  * @covers \PHP\Manipulator\Helper\NewlineDetector::<protected>
  */
 public function testGetNewlineFromContainer($container, $defaultNewline, $expectedNewline)
 {
     $detector = new NewlineDetector($defaultNewline);
     $actualNewline = $detector->getNewlineFromContainer($container);
     $this->assertEquals($expectedNewline, $actualNewline);
 }