/
PhpDocParser.php
349 lines (297 loc) · 8.75 KB
/
PhpDocParser.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
namespace luya\admin\openapi\phpdoc;
use luya\helpers\StringHelper;
use Reflector;
use Yii;
/**
* Create Parser Object from reflection class to read PhpDocs.
*
* @author Basil Suter <git@nadar.io>
* @since 3.2.0
*/
class PhpDocParser
{
/**
* @var PhpDocReflection
*/
public $reflection;
public $rows = [];
public function __construct(Reflector $reflection)
{
$this->reflection = new PhpDocReflection($reflection);
$this->rows = $this->parseDocBlockToArrays($this->reflection);
Yii::debug("PhpDocParser for file '{$reflection->name}'", __METHOD__);
}
protected function parseDocBlockToArrays(PhpDocReflection $reflection)
{
$rows = [
'texts' => [],
'return' => [], // @return <type> <description>
'author' => [],
'param' => [], // @param <type> $firstname <description>
'deprecated' => [],
'see' => [],
'link' => [],
'since' => [],
'var' => [],
'property' => [], // @property <type> $firstname <description>,
'uses' => [], // @uses <type> <description>,
'method' => [], // @method <returnType> <actionName> <description>
];
foreach (explode(PHP_EOL, $reflection->getDocComment()) as $row) {
$row = ltrim($row);
if (in_array($row, ["/**", "/*", "*/"])) {
continue;
}
$row = ltrim($row, "* ");
if (substr($row, 0, 1) == '@') {
if (StringHelper::startsWith($row, '@param') || StringHelper::startsWith($row, '@property')) {
preg_match("/^(@[a-z]+)\s+([^\s]+)\s+([^\s]+)\s*(.*)$/", $row, $matches, 0, 0);
unset($matches[0]);
} elseif (StringHelper::startsWith($row, '@method')) {
preg_match("/^(@[a-z]+)\s+([^\s]+)\s+([a-zA-Z]+)\((.*)\)\s+(.*)$/", $row, $matches, 0, 0);
unset($matches[0]);
} else {
preg_match("/^(@[a-z]+)\s+([^\s]+)\s*(.*)$/", $row, $matches, 0, 0);
unset($matches[0]);
}
if (isset($matches[1])) {
$rows[substr($matches[1], 1)][] = array_values($matches);
}
} else {
$rows['texts'][] = $row;
}
}
return $rows;
}
/**
* Returns all PhpDoc @method definitions
*
* @return PhpDocMethod[] An array where the key is the methodName like `actionIndex`
* @since 3.3.1
*/
public function getMethods()
{
$methods = [];
foreach ($this->rows['method'] as $method) {
$phpDocMethod = new PhpDocMethod($this, $method);
$methods[$phpDocMethod->getMethodName()] = $phpDocMethod;
}
return $methods;
}
/**
* Get a PhpDoc @method definition by its name
*
* @param string $name The name of the action which is defined, for example `actionIndex`
* @return PhpDocMethod
* @since 3.3.1
*/
public function getMethod($name)
{
return $this->getMethods()[$name] ?? false;
}
/**
* Get all @property
*
* @return PhpDocParam[]
*/
public function getProperties()
{
$properties = [];
foreach ($this->rows['property'] as $param) {
$properties[] = new PhpDocParam($this, $param);
}
return $properties;
}
/**
*
*
* @param string $paramName
* @return PhpDocParam
*/
public function getProperty($propertyName)
{
$properties = $this->rows['property'];
foreach ($properties as $p) {
if (isset($p[2]) && ltrim(strtolower($p[2]), '$') == strtolower($propertyName)) {
return new PhpDocParam($this, $p);
}
}
return new PhpDocParam($this, []);
}
/**
* Get all @param
*
* @return PhpDocParam[]
*/
public function getParams()
{
$params = [];
foreach ($this->rows['param'] as $param) {
$params[] = new PhpDocParam($this, $param);
}
return $params;
}
/**
* Get uses
*
* @return PhpDocUses[]
* @since 3.3.0
*/
public function getUses()
{
$uses = [];
foreach ($this->rows['uses'] as $use) {
$uses[] = new PhpDocUses($this, $use);
}
return $uses;
}
/**
*
*
* @param string $paramName
* @return PhpDocParam
*/
public function getParam($paramName)
{
$params = $this->rows['param'];
foreach ($params as $p) {
if (isset($p[2]) && ltrim(strtolower($p[2]), '$') == strtolower($paramName)) {
return new PhpDocParam($this, $p);
}
}
return new PhpDocParam($this, []);
}
/**
* Return types to a single type.
*
* Assuming `boolean|string|array` it will return the `boolean` unless $preferred is configured.
*
* @param string $schema
* @param string $preferred
* @return string
*/
public function typesTotype($schema, $preferred = null)
{
$values = explode("|", $schema);
if (count($values) > 1) {
return $this->normalizeTypes(current($values));
}
return $schema;
}
/**
* Ensure types are always the same not in short form.
*
* @param string $type
* @return string
*/
public function normalizeTypes($type)
{
$type = strtolower((string) $type);
// fix not supported short form types
if ($type == 'bool') {
$type = 'boolean';
}
if ($type == 'int') {
$type = 'integer';
}
// convert types to number
if (in_array($type, ['float', 'double', 'int32', 'int64'])) {
$type = 'number';
}
// ensure uncovered types
if (!in_array($type, ['array', 'boolean', 'integer', 'number', 'object', 'string'])) {
return 'string';
}
return $type;
}
/**
* Return
*
* @return PhpDocReturn
*/
public function getReturn()
{
$params = $this->rows['return'];
// as it should have only 1 return!
return new PhpDocReturn($this, empty($params) ? [] : current($params));
}
public function getShortSummary()
{
return current($this->rows['texts']);
}
public function getLongDescription()
{
$content = [];
foreach ($this->rows['texts'] as $key => $row) {
if ($key == 0) {
continue;
}
if ($key == 1 && empty($row)) {
continue;
}
$content[] = $row;
}
return implode(PHP_EOL, $content);
}
/**
* Returns all use statements from a class file.
*
* + luya\cms\models\NavContainer
* + luya\cms\models\NavItemModule
* + luya\base\DynamicModel as FooBar
*
* @return array
*/
public function getUseClasses()
{
$file = $this->reflection->getFileName();
$tokens = \PhpToken::tokenize(file_get_contents($file));
$parts = [];
$startCapture = 0;
foreach ($tokens as $token) {
if (is_array($token)) {
if ($token[0] == T_DOC_COMMENT) {
break;
}
if ($token[0] == T_USE) {
// use starts
$startCapture = $startCapture + 1;
}
if ($startCapture > 0) {
$parts[$startCapture][] = $token->text;
}
}
}
foreach ($parts as $k => $part) {
$parts[$k] = str_replace("use ", "", trim(implode("", $part)));
}
return $parts;
}
/**
* Search for a given class inside the use statement and return the fully qualified path.
*
* @param string $className Search for `User` should return `app\models\User`
* @return string
*/
public function ensureClassName($className)
{
$className = strtolower($className);
foreach ($this->getUseClasses() as $name) {
if (StringHelper::contains(' as ', strtolower($name))) {
$items = explode(" as ", $name);
$aliasName = end($items);
if (trim($aliasName) == $className) {
return $name;
}
} else {
$items = explode("\\", strtolower($name));
$lastItem = end($items);
if (trim($lastItem) == $className) {
return $name;
}
}
}
return false;
}
}