/** * Cleanup a whitespace token. * * @param Token $token */ private function fixWhitespace(Token $token) { $content = $token->getContent(); // if there is more than one new line in the whitespace, then we need to fix it if (substr_count($content, "\n") > 1) { // the final bit of the whitespace must be the next statement's indentation $lines = Utils::splitLines($content); $token->setContent("\n" . end($lines)); } }
/** * Create a new docblock instance. * * @param string $content */ public function __construct($content) { foreach (Utils::splitLines($content) as $line) { $this->lines[] = new Line($line); } }
/** * Fix a given docblock. * * @param string $content * * @return string */ private function fixDocBlock($content) { $lines = Utils::splitLines($content); $l = count($lines); for ($i = 0; $i < $l; ++$i) { $items = array(); $matches = $this->getMatches($lines[$i]); if (null === $matches) { continue; } $current = $i; $items[] = $matches; while ($matches = $this->getMatches($lines[++$i], true)) { $items[] = $matches; } // compute the max length of the tag, hint and variables $tagMax = 0; $hintMax = 0; $varMax = 0; foreach ($items as $item) { if (null === $item['tag']) { continue; } $tagMax = max($tagMax, strlen($item['tag'])); $hintMax = max($hintMax, strlen($item['hint'])); $varMax = max($varMax, strlen($item['var'])); } $currTag = null; // update foreach ($items as $j => $item) { if (null === $item['tag']) { if ($item['desc'][0] === '@') { $lines[$current + $j] = $item['indent'] . ' * ' . $item['desc'] . "\n"; continue; } $line = $item['indent'] . ' * ' . str_repeat(' ', $tagMax + $hintMax + $varMax + ('param' === $currTag ? 3 : 2)) . $item['desc'] . "\n"; $lines[$current + $j] = $line; continue; } $currTag = $item['tag']; $line = $item['indent'] . ' * @' . $item['tag'] . str_repeat(' ', $tagMax - strlen($item['tag']) + 1) . $item['hint']; if (!empty($item['var'])) { $line .= str_repeat(' ', $hintMax - strlen($item['hint']) + 1) . $item['var'] . (!empty($item['desc']) ? str_repeat(' ', $varMax - strlen($item['var']) + 1) . $item['desc'] . "\n" : "\n"); } elseif (!empty($item['desc'])) { $line .= str_repeat(' ', $hintMax - strlen($item['hint']) + 1) . $item['desc'] . "\n"; } else { $line .= "\n"; } $lines[$current + $j] = $line; } } return implode($lines); }
/** * @dataProvider provideSplitLinesCases */ public function testSplitLines(array $expected, $input) { $this->assertSame($expected, Utils::splitLines($input)); }