Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | 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
 *
200 lars 6
 * (c) 2012-2023 Justin Hileman
148 lars 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
    private static function formatVector(array $vector, array $lines): string
73
    {
74
        $template = [' '];
75
        foreach ($vector as $type) {
76
            $max = 0;
77
            foreach ($lines as $line) {
78
                $chunk = $line[$type];
79
                $cur = empty($chunk) ? 0 : \strlen($chunk) + 1;
80
                if ($cur > $max) {
81
                    $max = $cur;
82
                }
83
            }
84
 
85
            $template[] = self::getVectorParamTemplate($type, $max);
86
        }
87
        $template = \implode(' ', $template);
88
 
89
        return \implode("\n", \array_map(function ($line) use ($template) {
90
            $escaped = \array_map(function ($l) {
91
                if ($l === null) {
92
                    return '';
93
                }
94
 
95
                return OutputFormatter::escape($l);
96
            }, $line);
97
 
98
            return \rtrim(\vsprintf($template, $escaped));
99
        }, $lines));
100
    }
101
 
102
    /**
103
     * Format docblock tags.
104
     *
105
     * @param array $skip Tags to exclude
106
     * @param array $tags Tags to format
107
     *
108
     * @return string formatted tags
109
     */
110
    private static function formatTags(array $skip, array $tags): string
111
    {
112
        $chunks = [];
113
 
114
        foreach ($tags as $name => $values) {
115
            if (\in_array($name, $skip)) {
116
                continue;
117
            }
118
 
119
            foreach ($values as $value) {
120
                $chunks[] = \sprintf('<comment>%s%s</comment> %s', self::inflect($name), empty($value) ? '' : ':', OutputFormatter::escape($value));
121
            }
122
 
123
            $chunks[] = '';
124
        }
125
 
126
        return \implode("\n", $chunks);
127
    }
128
 
129
    /**
130
     * Get a docblock vector template.
131
     *
132
     * @param string $type Vector type
133
     * @param int    $max  Pad width
134
     */
135
    private static function getVectorParamTemplate(string $type, int $max): string
136
    {
137
        if (!isset(self::$vectorParamTemplates[$type])) {
138
            return \sprintf('%%-%ds', $max);
139
        }
140
 
141
        return \sprintf('<%s>%%-%ds</%s>', self::$vectorParamTemplates[$type], $max, self::$vectorParamTemplates[$type]);
142
    }
143
 
144
    /**
145
     * Indent a string.
146
     *
147
     * @param string $text   String to indent
148
     * @param string $indent (default: '  ')
149
     */
150
    private static function indent(string $text, string $indent = '  '): string
151
    {
152
        return $indent.\str_replace("\n", "\n".$indent, $text);
153
    }
154
 
155
    /**
156
     * Convert underscored or whitespace separated words into sentence case.
157
     *
158
     * @param string $text
159
     */
160
    private static function inflect(string $text): string
161
    {
162
        $words = \trim(\preg_replace('/[\s_-]+/', ' ', \preg_replace('/([a-z])([A-Z])/', '$1 $2', $text)));
163
 
164
        return \implode(' ', \array_map('ucfirst', \explode(' ', $words)));
165
    }
166
}