/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokensOrg) { $content = $tokensOrg->generateCode(); // replace all <? with <?php to replace all short open tags even without short_open_tag option enabled $newContent = preg_replace('/<\\?(\\s|$)/', '<?php$1', $content, -1, $count); if (!$count) { return; } /* the following code is magic to revert previous replacements which should NOT be replaced, for example incorrectly replacing * > echo '<? '; * with * > echo '<?php '; */ $tokens = Tokens::fromCode($newContent); $tokensOldContent = ''; $tokensOldContentLength = 0; foreach ($tokens as $token) { if ($token->isGivenKind(T_OPEN_TAG)) { $tokenContent = $token->getContent(); if ('<?php' !== substr($content, $tokensOldContentLength, 5)) { $tokenContent = '<? '; } $tokensOldContent .= $tokenContent; $tokensOldContentLength += strlen($tokenContent); continue; } if ($token->isGivenKind(array(T_COMMENT, T_DOC_COMMENT, T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE, T_STRING))) { $tokenContent = ''; $tokenContentLength = 0; $parts = explode('<?php', $token->getContent()); $iLast = count($parts) - 1; foreach ($parts as $i => $part) { $tokenContent .= $part; $tokenContentLength += strlen($part); if ($i !== $iLast) { if ('<?php' === substr($content, $tokensOldContentLength + $tokenContentLength, 5)) { $tokenContent .= '<?php'; $tokenContentLength += 5; } else { $tokenContent .= '<?'; $tokenContentLength += 2; } } } $token->setContent($tokenContent); } $tokensOldContent .= $token->getContent(); $tokensOldContentLength += strlen($token->getContent()); } $tokensOrg->overrideRange(0, $tokensOrg->count() - 1, $tokens); }
/** * Look for group of placeholders, and provide vertical alignment. * * @param Tokens $tokens * * @return string */ protected function replacePlaceholder(Tokens $tokens) { $tmpCode = $tokens->generateCode(); for ($j = 0; $j <= $this->deepestLevel; ++$j) { $placeholder = sprintf(self::ALIGNABLE_PLACEHOLDER, $j); if (false === strpos($tmpCode, $placeholder)) { continue; } $lines = explode("\n", $tmpCode); $linesWithPlaceholder = array(); $blockSize = 0; $linesWithPlaceholder[$blockSize] = array(); foreach ($lines as $index => $line) { if (substr_count($line, $placeholder) > 0) { $linesWithPlaceholder[$blockSize][] = $index; } else { ++$blockSize; $linesWithPlaceholder[$blockSize] = array(); } } $i = 0; foreach ($linesWithPlaceholder as $group) { if (1 === count($group)) { continue; } ++$i; $rightmostSymbol = 0; foreach ($group as $index) { $rightmostSymbol = max($rightmostSymbol, strpos(utf8_decode($lines[$index]), $placeholder)); } foreach ($group as $index) { $line = $lines[$index]; $currentSymbol = strpos(utf8_decode($line), $placeholder); $delta = abs($rightmostSymbol - $currentSymbol); if ($delta > 0) { $line = str_replace($placeholder, str_repeat(' ', $delta) . $placeholder, $line); $lines[$index] = $line; } } } $tmpCode = str_replace($placeholder, '', implode("\n", $lines)); } return $tmpCode; }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { // [Structure] Use the linefeed character (0x0A) to end lines $tokens->setCode(str_replace("\r\n", "\n", $tokens->generateCode())); }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { $tokens->setCode(preg_replace('/^\\h+$/m', '', $tokens->generateCode())); }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { // [Structure] Don't add trailing spaces at the end of non-blank lines $tokens->setCode(preg_replace('/(?<=\\S)[ \\t]+$/m', '', $tokens->generateCode())); }