/** * 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); } }
/** * 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); }
/** * @param \PHP\Manipulator\Token $token * @param mixed $params (unused) */ public function manipulate(Token $token, $params = null) { $helper = new NewlineDetector(); $newline = $helper->getNewlineFromToken($token); $lines = new ArrayObject(preg_split('~(\\r\\n|\\r|\\n)~', $token->getValue())); if (count($lines) >= 3) { // delete empty lines from begin $this->_iterateLines($lines); // delete empty lines from end $lines = new ArrayObject(array_reverse($lines->getArrayCopy())); $this->_iterateLines($lines); $token->setValue(implode($newline, array_reverse($lines->getArrayCopy()))); } }
/** * @param \PHP\Manipulator\Token $token * @param mixed $params */ public function manipulate(Token $token, $params = null) { if (!$this->evaluateConstraint('IsMultilineComment', $token)) { throw new \Exception('Token is no Multiline-comment'); } $this->manipulateToken('RemoveCommentIndention', $token); $value = preg_split('~(\\r\\n|\\n|\\r)~', $token->getValue()); $newValue = ''; $helper = new NewlineDetector(); $newline = $helper->getNewlineFromToken($token); foreach ($value as $line) { // removes */ and * and /** and /** $newValue .= '//' . preg_replace('~^(\\*\\/|\\*|\\/\\*\\*|\\/\\*){1,}(.*?)$~', '\\2', $line) . $newline; } $token->setType(T_COMMENT); $token->setValue($newValue); }
/** * 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(); }
/** * Manipulate Token * * @param \PHP\Manipulator\Token $token * @param mixed $params */ public function manipulate(Token $token, $params = null) { $regexNewline = '(\\n|\\r\\n|\\r)'; $indention = $params; $value = $token->getValue(); $lines = preg_split('~' . $regexNewline . '~', $value); $helper = new NewlineDetector(); $newline = $helper->getNewlineFromToken($token); $first = true; $value = ''; foreach ($lines as $key => $line) { if ($first) { $first = false; } else { $temp = trim($line); if (!empty($temp)) { $lines[$key] = $indention . ' ' . $line; } } } $token->setValue(implode($newline, $lines)); }
/** * @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(); }
/** * @covers \PHP\Manipulator\Helper\NewlineDetector::setDefaultNewline * @covers \PHP\Manipulator\Helper\NewlineDetector::getDefaultNewline */ public function testSetDefaultNewlineGetDefaultNewline() { $detector = new NewlineDetector(); $detector->setDefaultNewline("baa"); $this->assertEquals("baa", $detector->getDefaultNewline()); }