Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
6
 * (c) 2012-2022 Justin Hileman
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Psy\Formatter;
13
 
14
use Psy\Util\Docblock;
15
use Symfony\Component\Console\Formatter\OutputFormatter;
16
 
17
/**
18
 * A pretty-printer for docblocks.
19
 */
20
class DocblockFormatter implements ReflectorFormatter
21
{
22
    private static $vectorParamTemplates = [
23
        'type' => 'info',
24
        'var'  => 'strong',
25
    ];
26
 
27
    /**
28
     * Format a docblock.
29
     *
30
     * @param \Reflector $reflector
31
     *
32
     * @return string Formatted docblock
33
     */
34
    public static function format(\Reflector $reflector): string
35
    {
36
        $docblock = new Docblock($reflector);
37
        $chunks = [];
38
 
39
        if (!empty($docblock->desc)) {
40
            $chunks[] = '<comment>Description:</comment>';
41
            $chunks[] = self::indent(OutputFormatter::escape($docblock->desc), '  ');
42
            $chunks[] = '';
43
        }
44
 
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]);
50
                    $chunks[] = '';
51
                }
52
            }
53
 
54
            $tags = self::formatTags(\array_keys($docblock::$vectors), $docblock->tags);
55
            if (!empty($tags)) {
56
                $chunks[] = $tags;
57
                $chunks[] = '';
58
            }
59
        }
60
 
61
        return \rtrim(\implode("\n", $chunks));
62
    }
63
 
64
    /**
65
     * Format a docblock vector, for example, `@throws`, `@param`, or `@return`.
66
     *
67
     * @see DocBlock::$vectors
68
     *
69
     * @param array $vector
70
     * @param array $lines
71
     *
72
     * @return string
73
     */
74
    private static function formatVector(array $vector, array $lines): string
75
    {
76
        $template = [' '];
77
        foreach ($vector as $type) {
78
            $max = 0;
79
            foreach ($lines as $line) {
80
                $chunk = $line[$type];
81
                $cur = empty($chunk) ? 0 : \strlen($chunk) + 1;
82
                if ($cur > $max) {
83
                    $max = $cur;
84
                }
85
            }
86
 
87
            $template[] = self::getVectorParamTemplate($type, $max);
88
        }
89
        $template = \implode(' ', $template);
90
 
91
        return \implode("\n", \array_map(function ($line) use ($template) {
92
            $escaped = \array_map(function ($l) {
93
                if ($l === null) {
94
                    return '';
95
                }
96
 
97
                return OutputFormatter::escape($l);
98
            }, $line);
99
 
100
            return \rtrim(\vsprintf($template, $escaped));
101
        }, $lines));
102
    }
103
 
104
    /**
105
     * Format docblock tags.
106
     *
107
     * @param array $skip Tags to exclude
108
     * @param array $tags Tags to format
109
     *
110
     * @return string formatted tags
111
     */
112
    private static function formatTags(array $skip, array $tags): string
113
    {
114
        $chunks = [];
115
 
116
        foreach ($tags as $name => $values) {
117
            if (\in_array($name, $skip)) {
118
                continue;
119
            }
120
 
121
            foreach ($values as $value) {
122
                $chunks[] = \sprintf('<comment>%s%s</comment> %s', self::inflect($name), empty($value) ? '' : ':', OutputFormatter::escape($value));
123
            }
124
 
125
            $chunks[] = '';
126
        }
127
 
128
        return \implode("\n", $chunks);
129
    }
130
 
131
    /**
132
     * Get a docblock vector template.
133
     *
134
     * @param string $type Vector type
135
     * @param int    $max  Pad width
136
     *
137
     * @return string
138
     */
139
    private static function getVectorParamTemplate(string $type, int $max): string
140
    {
141
        if (!isset(self::$vectorParamTemplates[$type])) {
142
            return \sprintf('%%-%ds', $max);
143
        }
144
 
145
        return \sprintf('<%s>%%-%ds</%s>', self::$vectorParamTemplates[$type], $max, self::$vectorParamTemplates[$type]);
146
    }
147
 
148
    /**
149
     * Indent a string.
150
     *
151
     * @param string $text   String to indent
152
     * @param string $indent (default: '  ')
153
     *
154
     * @return string
155
     */
156
    private static function indent(string $text, string $indent = '  '): string
157
    {
158
        return $indent.\str_replace("\n", "\n".$indent, $text);
159
    }
160
 
161
    /**
162
     * Convert underscored or whitespace separated words into sentence case.
163
     *
164
     * @param string $text
165
     *
166
     * @return string
167
     */
168
    private static function inflect(string $text): string
169
    {
170
        $words = \trim(\preg_replace('/[\s_-]+/', ' ', \preg_replace('/([a-z])([A-Z])/', '$1 $2', $text)));
171
 
172
        return \implode(' ', \array_map('ucfirst', \explode(' ', $words)));
173
    }
174
}