-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathrunner.php
376 lines (319 loc) · 9.4 KB
/
runner.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
namespace WP_Parser;
use phpDocumentor\Reflection\BaseReflector;
use phpDocumentor\Reflection\ClassReflector\MethodReflector;
use phpDocumentor\Reflection\ClassReflector\PropertyReflector;
use phpDocumentor\Reflection\FunctionReflector;
use phpDocumentor\Reflection\FunctionReflector\ArgumentReflector;
use phpDocumentor\Reflection\ReflectionAbstract;
/**
* @param string $directory
*
* @return array|\WP_Error
*/
function get_wp_files( $directory ) {
$iterableFiles = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator( $directory )
);
$files = array();
try {
foreach ( $iterableFiles as $file ) {
if ( 'php' !== $file->getExtension() ) {
continue;
}
$files[] = $file->getPathname();
}
} catch ( \UnexpectedValueException $exc ) {
return new \WP_Error(
'unexpected_value_exception',
sprintf( 'Directory [%s] contained a directory we can not recurse into', $directory )
);
}
return $files;
}
/**
* @param array $files
* @param string $root
*
* @return array
*/
function parse_files( $files, $root ) {
$output = array();
foreach ( $files as $filename ) {
$file = new File_Reflector( $filename );
$path = ltrim( substr( $filename, strlen( $root ) ), DIRECTORY_SEPARATOR );
$file->setFilename( $path );
$file->process();
// TODO proper exporter
$out = array(
'file' => export_docblock( $file ),
'path' => str_replace( DIRECTORY_SEPARATOR, '/', $file->getFilename() ),
'root' => $root,
);
if ( ! empty( $file->uses ) ) {
$out['uses'] = export_uses( $file->uses );
}
foreach ( $file->getIncludes() as $include ) {
$out['includes'][] = array(
'name' => $include->getName(),
'line' => $include->getLineNumber(),
'type' => $include->getType(),
);
}
foreach ( $file->getConstants() as $constant ) {
$out['constants'][] = array(
'name' => $constant->getShortName(),
'line' => $constant->getLineNumber(),
'value' => $constant->getValue(),
);
}
if ( ! empty( $file->uses['hooks'] ) ) {
$out['hooks'] = export_hooks( $file->uses['hooks'] );
}
foreach ( $file->getFunctions() as $function ) {
$func = array(
'name' => $function->getShortName(),
'namespace' => $function->getNamespace(),
'aliases' => $function->getNamespaceAliases(),
'line' => $function->getLineNumber(),
'end_line' => $function->getNode()->getAttribute( 'endLine' ),
'arguments' => export_arguments( $function->getArguments() ),
'doc' => export_docblock( $function ),
'hooks' => array(),
);
if ( ! empty( $function->uses ) ) {
$func['uses'] = export_uses( $function->uses );
if ( ! empty( $function->uses['hooks'] ) ) {
$func['hooks'] = export_hooks( $function->uses['hooks'] );
}
}
$out['functions'][] = $func;
}
foreach ( $file->getClasses() as $class ) {
$class_data = array(
'name' => $class->getShortName(),
'namespace' => $class->getNamespace(),
'line' => $class->getLineNumber(),
'end_line' => $class->getNode()->getAttribute( 'endLine' ),
'final' => $class->isFinal(),
'abstract' => $class->isAbstract(),
'extends' => $class->getParentClass(),
'implements' => $class->getInterfaces(),
'properties' => export_properties( $class->getProperties() ),
'methods' => export_methods( $class->getMethods() ),
'doc' => export_docblock( $class ),
);
$out['classes'][] = $class_data;
}
$output[] = $out;
}
return $output;
}
/**
* @param BaseReflector|ReflectionAbstract $element
*
* @return array
*/
function export_docblock( $element ) {
$docblock = $element->getDocBlock();
if ( ! $docblock ) {
return array(
'raw' => '',
'summary' => '',
'description' => '',
'raw_description' => '',
'tags' => array(),
);
}
// Extract the raw doc comment
if ( $element instanceof BaseReflector ) {
$raw_doc = (string) $element->getNode()->getDocComment();
} elseif ( $element instanceof File_Reflector && isset( $element->doc_comment ) ) {
$raw_doc = (string) $element->doc_comment->getText();
} else {
$raw_doc = '';
}
$output = array(
'raw' => $raw_doc,
'summary' => $docblock->getShortDescription(),
'description' => $docblock->getLongDescription()->getFormattedContents(),
'raw_description' => $docblock->getLongDescription()->getContents(),
'tags' => array(),
);
foreach ( $docblock->getTags() as $tag ) {
$tag_data = array(
'name' => $tag->getName(),
'content' => format_description( $tag->getDescription() ),
);
if ( method_exists( $tag, 'getTypes' ) ) {
$tag_data['types'] = $tag->getTypes();
}
if ( method_exists( $tag, 'getVariableName' ) ) {
$tag_data['variable'] = $tag->getVariableName();
}
if ( method_exists( $tag, 'getReference' ) ) {
$tag_data['refers'] = $tag->getReference();
}
if ( 'since' == $tag->getName() && method_exists( $tag, 'getVersion' ) ) {
// Version string.
$version = $tag->getVersion();
if ( ! empty( $version ) ) {
$tag_data['content'] = $version;
}
// Description string.
if ( method_exists( $tag, 'getDescription' ) ) {
$description = format_description( $tag->getDescription() );
if ( ! empty( $description ) ) {
$tag_data['description'] = $description;
}
}
}
$output['tags'][] = $tag_data;
}
return $output;
}
/**
* @param Hook_Reflector[] $hooks
*
* @return array
*/
function export_hooks( array $hooks ) {
$out = array();
foreach ( $hooks as $hook ) {
$out[] = array(
'name' => $hook->getName(),
'line' => $hook->getLineNumber(),
'end_line' => $hook->getNode()->getAttribute( 'endLine' ),
'type' => $hook->getType(),
'arguments' => $hook->getArgs(),
'doc' => export_docblock( $hook ),
);
}
return $out;
}
/**
* @param ArgumentReflector[] $arguments
*
* @return array
*/
function export_arguments( array $arguments ) {
$output = array();
foreach ( $arguments as $argument ) {
$output[] = array(
'name' => $argument->getName(),
'default' => $argument->getDefault(),
'type' => $argument->getType(),
);
}
return $output;
}
/**
* @param PropertyReflector[] $properties
*
* @return array
*/
function export_properties( array $properties ) {
$out = array();
foreach ( $properties as $property ) {
$out[] = array(
'name' => $property->getName(),
'line' => $property->getLineNumber(),
'end_line' => $property->getNode()->getAttribute( 'endLine' ),
'default' => $property->getDefault(),
// 'final' => $property->isFinal(),
'static' => $property->isStatic(),
'visibility' => $property->getVisibility(),
'doc' => export_docblock( $property ),
);
}
return $out;
}
/**
* @param MethodReflector[] $methods
*
* @return array
*/
function export_methods( array $methods ) {
$output = array();
foreach ( $methods as $method ) {
$method_data = array(
'name' => $method->getShortName(),
'namespace' => $method->getNamespace(),
'aliases' => $method->getNamespaceAliases(),
'line' => $method->getLineNumber(),
'end_line' => $method->getNode()->getAttribute( 'endLine' ),
'final' => $method->isFinal(),
'abstract' => $method->isAbstract(),
'static' => $method->isStatic(),
'visibility' => $method->getVisibility(),
'arguments' => export_arguments( $method->getArguments() ),
'doc' => export_docblock( $method ),
);
if ( ! empty( $method->uses ) ) {
$method_data['uses'] = export_uses( $method->uses );
if ( ! empty( $method->uses['hooks'] ) ) {
$method_data['hooks'] = export_hooks( $method->uses['hooks'] );
}
}
$output[] = $method_data;
}
return $output;
}
/**
* Export the list of elements used by a file or structure.
*
* @param array $uses {
* @type Function_Call_Reflector[] $functions The functions called.
* }
*
* @return array
*/
function export_uses( array $uses ) {
$out = array();
// Ignore hooks here, they are exported separately.
unset( $uses['hooks'] );
foreach ( $uses as $type => $used_elements ) {
/** @var MethodReflector|FunctionReflector $element */
foreach ( $used_elements as $element ) {
$name = $element->getName();
switch ( $type ) {
case 'methods':
$out[ $type ][] = array(
'name' => $name[1],
'class' => $name[0],
'static' => $element->isStatic(),
'line' => $element->getLineNumber(),
'end_line' => $element->getNode()->getAttribute( 'endLine' ),
);
break;
default:
case 'functions':
$out[ $type ][] = array(
'name' => $name,
'line' => $element->getLineNumber(),
'end_line' => $element->getNode()->getAttribute( 'endLine' ),
);
if ( '_deprecated_file' === $name || '_deprecated_function' === $name || '_deprecated_argument' === $name ) {
$arguments = $element->getNode()->args;
$out[ $type ][0]['deprecation_version'] = $arguments[1]->value->value;
}
break;
}
}
}
return $out;
}
/**
* Format the given description with Markdown.
*
* @param string $description Description.
* @return string Description as Markdown if the Parsedown class exists, otherwise return
* the given description text.
*/
function format_description( $description ) {
if ( class_exists( 'Parsedown' ) ) {
$parsedown = \Parsedown::instance();
$description = $parsedown->line( $description );
}
return $description;
}