4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Formatter;
14 use Psy\Util\Docblock;
15 use Symfony\Component\Console\Formatter\OutputFormatter;
18 * A pretty-printer for docblocks.
20 class DocblockFormatter implements Formatter
22 private static $vectorParamTemplates = array(
30 * @param \Reflector $reflector
32 * @return string Formatted docblock
34 public static function format(\Reflector $reflector)
36 $docblock = new Docblock($reflector);
39 if (!empty($docblock->desc)) {
40 $chunks[] = '<comment>Description:</comment>';
41 $chunks[] = self::indent(OutputFormatter::escape($docblock->desc), ' ');
45 if (!empty($docblock->tags)) {
46 foreach ($docblock::$vectors as $name => $vector) {
47 if (isset($docblock->tags[$name])) {
48 $chunks[] = sprintf('<comment>%s:</comment>', self::inflect($name));
49 $chunks[] = self::formatVector($vector, $docblock->tags[$name]);
54 $tags = self::formatTags(array_keys($docblock::$vectors), $docblock->tags);
61 return rtrim(implode("\n", $chunks));
65 * Format a docblock vector, for example, `@throws`, `@param`, or `@return`.
67 * @see DocBlock::$vectors
69 * @param array $vector
74 private static function formatVector(array $vector, array $lines)
76 $template = array(' ');
77 foreach ($vector as $type) {
79 foreach ($lines as $line) {
80 $chunk = $line[$type];
81 $cur = empty($chunk) ? 0 : strlen($chunk) + 1;
87 $template[] = self::getVectorParamTemplate($type, $max);
89 $template = implode(' ', $template);
91 return implode("\n", array_map(function ($line) use ($template) {
92 $escaped = array_map(array('Symfony\Component\Console\Formatter\OutputFormatter', 'escape'), $line);
94 return rtrim(vsprintf($template, $escaped));
99 * Format docblock tags.
101 * @param array $skip Tags to exclude
102 * @param array $tags Tags to format
104 * @return string formatted tags
106 private static function formatTags(array $skip, array $tags)
110 foreach ($tags as $name => $values) {
111 if (in_array($name, $skip)) {
115 foreach ($values as $value) {
116 $chunks[] = sprintf('<comment>%s%s</comment> %s', self::inflect($name), empty($value) ? '' : ':', OutputFormatter::escape($value));
122 return implode("\n", $chunks);
126 * Get a docblock vector template.
128 * @param string $type Vector type
129 * @param int $max Pad width
133 private static function getVectorParamTemplate($type, $max)
135 if (!isset(self::$vectorParamTemplates[$type])) {
136 return sprintf('%%-%ds', $max);
139 return sprintf('<%s>%%-%ds</%s>', self::$vectorParamTemplates[$type], $max, self::$vectorParamTemplates[$type]);
145 * @param string $text String to indent
146 * @param string $indent (default: ' ')
150 private static function indent($text, $indent = ' ')
152 return $indent . str_replace("\n", "\n" . $indent, $text);
156 * Convert underscored or whitespace separated words into sentence case.
158 * @param string $text
162 private static function inflect($text)
164 $words = trim(preg_replace('/[\s_-]+/', ' ', preg_replace('/([a-z])([A-Z])/', '$1 $2', $text)));
166 return implode(' ', array_map('ucfirst', explode(' ', $words)));