-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentor.php
More file actions
351 lines (291 loc) · 7.61 KB
/
Copy pathDocumentor.php
File metadata and controls
351 lines (291 loc) · 7.61 KB
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
<?php
/**
* Documentor
*
* @author SolveBeam <info@solvebeam.com>
* @copyright 2005-2022 SolveBeam
* @license GPL-3.0-or-later
* @package SolveBeam\WordPress\Documentor
*/
namespace SolveBeam\WordPress\Documentor;
use PhpParser\Node;
use PhpParser\NodeFinder;
use PhpParser\NodeTraverser;
use PhpParser\ParserFactory;
use PhpParser\NodeVisitor\NodeConnectingVisitor;
use Symfony\Component\Filesystem\Filesystem;
/**
* Documentor
*
* @author Remco Tolsma
* @version 1.0.0
* @since 1.0.0
*/
class Documentor {
/**
* Hooks.
*
* @var Hook[]
*/
private $hooks;
/**
* Prefixes.
*
* @var string[]
*/
public $prefixes;
/**
* Document type 'actions' or 'filters'.
*
* @var string
*/
public $type;
/**
* Relative path.
*
* @var string
*/
public $relative;
/**
* Construct documentor.
*/
public function __construct() {
$this->hooks = [];
$this->prefixes = [];
}
/**
* Get hooks.
*
* @return array
*/
public function get_hooks() {
return $this->hooks;
}
/**
* Get actions.
*
* @return array
*/
public function get_actions() {
return \array_filter(
$this->hooks,
fn($hook) => $hook->is_action()
);
}
/**
* Get filters.
*
* @return array
*/
public function get_filters() {
return \array_filter(
$this->hooks,
fn($hook) => $hook->is_filter()
);
}
/**
* Get relative path.
*
* @param \SplFileInfo $file File.
* @return string
*/
public function relative( \SplFileInfo $file ) {
$filesystem = new Filesystem();
$end = $file;
$start = new \SplFileInfo( $this->relative );
return \trim( $filesystem->makePathRelative( $end->getRealPath(), $start->getRealPath() ), '/' );
}
/**
* Check if the specified tag name should be parsed.
*
* @param string $name Tag name.
* @return bool True if should parse, false otherwise.
*/
private function should_parse_tag( $name ) {
if ( 0 === \count( $this->prefixes ) ) {
return true;
}
foreach ( $this->prefixes as $prefix ) {
if ( \str_starts_with( $name, $prefix ) ) {
return true;
}
}
return false;
}
/**
* Get statement doc document.
*
* @param Node $statement Statement.
* @return string|null
*/
private function get_statement_doc_comment( Node $statement ) {
$doc_comment = $statement->getDocComment();
if ( null !== $doc_comment ) {
return $doc_comment;
}
$parent = $statement->getAttribute( 'parent' );
/**
* This handles a assignment before a hook function call:
*
* ```php
* $some_variable = apply_filters( 'my_plugin_filter_1', $first_param, $second_param );
* ```
*
* @link https://github.com/solvebeam/wp-hooks-documentor/issues/11
*/
if ( $parent instanceof \PhpParser\Node\Expr\Assign ) {
return $this->get_statement_doc_comment( $parent );
}
/**
* This handles a cast before a hook function call:
*
* ```php
* $should_do = (bool) apply_filters( 'should_we_do_it', true, $some_value );
* ```
*
* @link https://github.com/solvebeam/wp-hooks-documentor/issues/18
*/
if ( $parent instanceof \PhpParser\Node\Expr\Cast ) {
return $this->get_statement_doc_comment( $parent );
}
/**
* This handles a hook function call in a `if` statement (`\PhpParser\Node\Stmt\If_`):
*
* ```php
* if ( (bool) apply_filters( 'some_condition_filter', $some_condition, $some_other_parameter ) )
* ```
*
* And also a `return` statement (`PhpParser\Node\Stmt\Return_`):
*
* ```php
* return apply_filters( 'test_issue_13_file_exclude_2', $first_param, $second_param );
* ```
*/
if ( $parent instanceof \PhpParser\Node\Stmt ) {
return $this->get_statement_doc_comment( $parent );
}
return null;
}
/**
* Parse.
*
* @throws \Exception Throws exception when parsing fails.
* @param \SplFileInfo $file File.
*/
public function parse( $file ) {
$parser_factory = new ParserFactory();
$parser = $parser_factory->createForNewestSupportedVersion();
$node_finder = new NodeFinder();
$traverser = new NodeTraverser();
$traverser->addVisitor( new NodeConnectingVisitor() );
$traverser->addVisitor( new NamespaceResolver() );
$tag_printer = new TagPrinter();
$changelog_factory = new ChangelogFactory();
/**
* File.
*/
$contents = $file->getContents();
$statements = $parser->parse( $contents );
$statements = $traverser->traverse( $statements );
$statements = $node_finder->find(
$statements,
function( Node $node ) {
if ( ! $node instanceof Node\Expr\FuncCall ) {
return false;
}
/**
* Function call can be a name or an expression, for example: `$callback()`.
*
* @link https://github.com/nikic/PHP-Parser/blob/v4.10.4/lib/PhpParser/Node/Expr/FuncCall.php#L10-L11
*/
if ( ! $node->name instanceof Node\Name ) {
return false;
}
return \in_array(
\strval( $node->name ),
[
'apply_filters',
'apply_filters_ref_array',
'apply_filters_deprecated',
'do_action',
'do_action_ref_array',
'do_action_deprecated',
],
true
);
}
);
foreach ( $statements as $statement ) {
$tag_arg = \array_shift( $statement->args );
if ( null === $tag_arg ) {
throw new \Exception( 'Tag argument missing from hook call.' );
}
$doc_comment = $this->get_statement_doc_comment( $statement );
$doc_block = null;
if ( null !== $doc_comment ) {
$doc_block_factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance();
$context_factory = new \phpDocumentor\Reflection\Types\ContextFactory();
$context = $context_factory->createForNamespace( \strval( $statement->getAttribute( 'namespace' ) ), $file->getContents() );
$doc_block = $doc_block_factory->create( (string) $doc_comment, $context );
if ( $doc_block->hasTag( 'ignore' ) ) {
continue;
}
}
try {
$tag_name = $tag_printer->print( $tag_arg->value );
} catch ( \Exception $exception ) {
throw new \Exception(
\sprintf(
'Could not convert tag argument value to a name in %s.',
$file . '#' . $statement->getStartLine()
),
0,
$exception
);
}
if ( ! $this->should_parse_tag( $tag_name ) ) {
continue;
}
$tag = new Tag( $tag_name, $tag_arg );
$arguments = [];
foreach ( $statement->args as $arg ) {
$argument = new Argument( $arg );
$arguments[] = $argument;
}
$hook = new Hook( $file, $statement, $tag, $arguments );
$hook->set_doc_comment( $doc_comment );
$hook->set_doc_block( $doc_block );
if ( null !== $doc_block ) {
$hook->set_changelog( $changelog_factory->create( $doc_block ) );
foreach ( $hook->get_arguments() as $argument ) {
$arg = $argument->get_php_parser_argument();
$param_tags = \array_filter(
$doc_block->getTagsByName( 'param' ),
function( $tag ) use ( $arg ) {
/**
* Documentor can only match named expression to a tag, currently no support for:
*
* ```php
* do_action_ref_array( $hook, $v['args'] );
* ```
*
* @link https://github.com/WordPress/WordPress/blob/5.7/wp-cron.php#L129-L138
* @link https://github.com/nikic/PHP-Parser/blob/v4.10.4/lib/PhpParser/Node/Expr/ArrayDimFetch.php
* @link https://github.com/nikic/PHP-Parser/blob/v4.10.4/lib/PhpParser/Node/Expr/Variable.php#L9-L10
*/
if ( ! \property_exists( $arg->value, 'name' ) ) {
return false;
}
return $tag->getVariableName() === $arg->value->name;
}
);
$param_tag = \reset( $param_tags );
if ( false !== $param_tag ) {
$argument->set_param_tag( $param_tag );
}
}
}
$this->hooks[] = $hook;
}
}
}