/
DocComment.php
245 lines (205 loc) · 8 KB
/
DocComment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
namespace Psalm;
use PhpParser\Comment\Doc;
use Psalm\Exception\DocblockParseException;
use Psalm\Internal\Scanner\DocblockParser;
use Psalm\Internal\Scanner\ParsedDocblock;
use function array_filter;
use function explode;
use function implode;
use function in_array;
use function min;
use function preg_match;
use function preg_match_all;
use function preg_replace;
use function rtrim;
use function str_repeat;
use function str_replace;
use function strlen;
use function strpos;
use function strspn;
use function substr;
use function trim;
use const PREG_SET_ORDER;
class DocComment
{
public const PSALM_ANNOTATIONS = [
'return', 'param', 'template', 'var', 'type',
'template-covariant', 'property', 'property-read', 'property-write', 'method',
'assert', 'assert-if-true', 'assert-if-false', 'suppress',
'ignore-nullable-return', 'override-property-visibility',
'override-method-visibility', 'seal-properties', 'seal-methods',
'generator-return', 'ignore-falsable-return', 'variadic', 'pure',
'ignore-variable-method', 'ignore-variable-property', 'internal',
'taint-sink', 'taint-source', 'assert-untainted', 'scope-this',
'mutation-free', 'external-mutation-free', 'immutable', 'readonly',
'allow-private-mutation', 'readonly-allow-private-mutation',
'yield', 'trace', 'import-type', 'flow', 'taint-specialize', 'taint-escape',
'taint-unescape', 'self-out', 'consistent-constructor', 'stub-override',
'require-extends', 'require-implements', 'param-out', 'ignore-var',
'consistent-templates', 'if-this-is', 'this-out'
];
/**
* Parse a docblock comment into its parts.
*
* Taken from advanced api docmaker, which was taken from
* https://github.com/facebook/libphutil/blob/master/src/parser/docblock/PhutilDocblockParser.php
*
* @return array Array of the main comment and specials
*
* @psalm-return array{description:string, specials:array<string, array<int, string>>}
* @psalm-suppress PossiblyUnusedMethod
*
* @deprecated use parsePreservingLength instead, going to be removed in Psalm 5
*
* @psalm-pure
*/
public static function parse(string $docblock, ?int $line_number = null, bool $preserve_format = false): array
{
// Strip off comments.
$docblock = trim($docblock);
$docblock = preg_replace('@^/\*\*@', '', $docblock);
$docblock = preg_replace('@\*/$@', '', $docblock);
$docblock = preg_replace('@^[ \t]*\*@m', '', $docblock);
// Normalize multi-line @specials.
$lines = explode("\n", $docblock);
$line_map = [];
$last = false;
foreach ($lines as $k => $line) {
if (preg_match('/^\s?@\w/i', $line)) {
$last = $k;
} elseif (preg_match('/^\s*$/', $line)) {
$last = false;
} elseif ($last !== false) {
$old_last_line = $lines[$last];
$lines[$last] = rtrim($old_last_line)
. ($preserve_format || trim($old_last_line) === '@return' ? "\n" . $line : ' ' . trim($line));
if ($line_number) {
$old_line_number = $line_map[$old_last_line];
unset($line_map[$old_last_line]);
$line_map[$lines[$last]] = $old_line_number;
}
unset($lines[$k]);
}
if ($line_number) {
$line_map[$line] = $line_number++;
}
}
$special = [];
if ($preserve_format) {
foreach ($lines as $m => $line) {
if (preg_match('/^\s?@([\w\-:]+)[\t ]*(.*)$/sm', $line, $matches)) {
[$full_match, $type, $data] = $matches;
$docblock = str_replace($full_match, '', $docblock);
if (empty($special[$type])) {
$special[$type] = [];
}
$line_number = $line_map && isset($line_map[$full_match]) ? $line_map[$full_match] : $m;
$special[$type][$line_number] = rtrim($data);
}
}
} else {
$docblock = implode("\n", $lines);
// Parse @specials.
if (preg_match_all('/^\s?@([\w\-:]+)[\t ]*([^\n]*)/m', $docblock, $matches, PREG_SET_ORDER)) {
$docblock = preg_replace('/^\s?@([\w\-:]+)\s*([^\n]*)/m', '', $docblock);
foreach ($matches as $m => $match) {
[$_, $type, $data] = $match;
if (empty($special[$type])) {
$special[$type] = [];
}
$line_number = $line_map && isset($line_map[$_]) ? $line_map[$_] : $m;
$special[$type][$line_number] = $data;
}
}
}
$docblock = str_replace("\t", ' ', $docblock);
// Smush the whole docblock to the left edge.
$min_indent = 80;
$indent = 0;
foreach (array_filter(explode("\n", $docblock)) as $line) {
for ($ii = 0, $iiMax = strlen($line); $ii < $iiMax; ++$ii) {
if ($line[$ii] !== ' ') {
break;
}
++$indent;
}
$min_indent = min($indent, $min_indent);
}
$docblock = preg_replace('/^' . str_repeat(' ', $min_indent) . '/m', '', $docblock);
$docblock = rtrim($docblock);
// Trim any empty lines off the front, but leave the indent level if there
// is one.
$docblock = preg_replace('/^\s*\n/', '', $docblock);
foreach ($special as $special_key => $_) {
if (strpos($special_key, 'psalm-') === 0) {
$special_key = substr($special_key, 6);
if (!in_array(
$special_key,
self::PSALM_ANNOTATIONS,
true
)) {
throw new DocblockParseException('Unrecognised annotation @psalm-' . $special_key);
}
}
}
return [
'description' => $docblock,
'specials' => $special,
];
}
/**
* Parse a docblock comment into its parts.
*/
public static function parsePreservingLength(Doc $docblock): ParsedDocblock
{
$parsed_docblock = DocblockParser::parse(
$docblock->getText(),
$docblock->getStartFilePos()
);
foreach ($parsed_docblock->tags as $special_key => $_) {
if (strpos($special_key, 'psalm-') === 0) {
$special_key = substr($special_key, 6);
if (!in_array(
$special_key,
self::PSALM_ANNOTATIONS,
true
)) {
throw new DocblockParseException('Unrecognised annotation @psalm-' . $special_key);
}
}
}
return $parsed_docblock;
}
/**
* @psalm-pure
* @return array<int,string>
*/
public static function parseSuppressList(string $suppress_entry): array
{
preg_match(
'/
(?(DEFINE)
# either a single issue or comma separated list of issues
(?<issue_list> (?&issue) \s* , \s* (?&issue_list) | (?&issue) )
# definition of a single issue
(?<issue> [A-Za-z0-9_-]+ )
)
^ (?P<issues> (?&issue_list) ) (?P<description> .* ) $
/xm',
$suppress_entry,
$matches
);
if (!isset($matches['issues'])) {
return [];
}
$issue_offset = 0;
$ret = [];
foreach (explode(',', $matches['issues']) as $suppressed_issue) {
$issue_offset += strspn($suppressed_issue, "\t\n\f\r ");
$ret[$issue_offset] = trim($suppressed_issue);
$issue_offset += strlen($suppressed_issue) + 1;
}
return $ret;
}
}