Skip to content

Commit 36ccefc

Browse files
committed
Improve hooks extractor script
1 parent 247a8ba commit 36ccefc

File tree

1 file changed

+74
-7
lines changed

1 file changed

+74
-7
lines changed

bin/extract-hooks.php

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
if ( ! is_array( $token ) || ! isset( $token[1] ) ) {
5858
continue;
5959
}
60-
if ( ! in_array( $token[1], array( 'apply_filters', 'do_action' ) ) ) {
60+
if ( ! in_array( ltrim( $token[1], '\\' ), array( 'apply_filters', 'do_action' ) ) ) {
6161
continue;
6262
}
6363

@@ -92,9 +92,9 @@
9292
}
9393

9494
if (
95-
$hook
96-
&& ! in_array( $hook, $ignore_filters )
97-
&& ! preg_match( '/^(activitypub_)/', $hook )
95+
$hook
96+
&& ! in_array( $hook, $ignore_filters )
97+
&& ! preg_match( '/^(activitypub_)/', $hook )
9898

9999
) {
100100
if ( ! isset( $filters[ $hook ] ) ) {
@@ -108,9 +108,30 @@
108108
$filters[ $hook ]['files'][] = $dir . '/' . $file->getFilename() . ':' . $token[2];
109109
$filters[ $hook ]['base_dirs'][ $main_dir ] = true;
110110

111+
if ( ! $comment ) {
112+
$comment = '/**' . PHP_EOL;
113+
// generate a fake doccomment if it's missing.
114+
for ( $j = $i + 1; $j < $i + 10; $j++ ) {
115+
if ( ! isset( $tokens[ $j ] ) ) {
116+
break;
117+
}
118+
119+
if ( ! is_array( $tokens[ $j ] ) ) {
120+
continue;
121+
}
122+
123+
if ( T_VARIABLE === $tokens[ $j ][0] ) {
124+
$comment .= ' * @param unknown ' . $tokens[ $j ][1] . PHP_EOL;
125+
}
126+
}
127+
128+
$comment .= '*/';
129+
}
130+
111131
if ( $comment ) {
112132
$docblock = parse_docblock( $comment );
113-
if ( ! empty( $docblock['comment'] ) && ! preg_match( '#^Documented in#i', $docblock['comment'] ) ) {
133+
134+
if ( ( ! empty( $docblock['comment'] ) && ! preg_match( '#^Documented in#i', $docblock['comment'] ) ) || ! empty( $docblock['param'] ) ) {
114135
$filters[ $hook ] = array_merge( $docblock, $filters[ $hook ] );
115136
}
116137
}
@@ -216,12 +237,58 @@ function parse_docblock( $raw_comment ) {
216237
$index .= PHP_EOL;
217238

218239
if ( ! empty( $data['param'] ) ) {
240+
if ( 'do_action' === $data['type'] ) {
241+
$signature = 'add_action(';
242+
} else {
243+
$signature = 'add_filter(';
244+
}
245+
$signature .= PHP_EOL . ' \'' . $hook . '\',';
246+
$signature .= PHP_EOL . ' function (';
247+
219248
$doc .= "## Parameters\n";
249+
$first = false;
250+
$count = 0;
220251
foreach ( (array) $data['param'] as $param ) {
252+
$count += 1;
221253
$p = explode( ' ', $param, 3 );
222-
$doc .= "\n- {$p[0]} `{$p[1]}` {$p[2]}";
254+
if ( '\\' === substr( $p[0], 0, 1 ) ) {
255+
$p[0] = substr( $p[0], 1 );
256+
} elseif ( ! in_array( $p[0], array( 'int', 'string', 'bool', 'array', 'unknown' ) ) ) {
257+
$p[0] = 'Friends\\' . $p[0];
258+
}
259+
if ( ! $first ) {
260+
$first = $p[1];
261+
}
262+
if ( 'unknown' === $p[0] ) {
263+
$doc .= "\n- `{$p[1]}`";
264+
$signature .= "\n {$p[1]},";
265+
} else {
266+
$doc .= "\n- *`{$p[0]}`* `{$p[1]}`";
267+
if ( isset( $p[2] ) ) {
268+
$doc .= ' ' . $p[2];
269+
}
270+
$signature .= "\n {$p[0]} {$p[1]},";
271+
}
272+
}
273+
if ( 1 === $count ) {
274+
$signature = str_replace( 'function (' . PHP_EOL . ' ', 'function ( ', substr( $signature, 0, -1 ) );
275+
$signature .= ' ) {';
276+
} else {
277+
$signature = substr( $signature, 0, -1 ) . PHP_EOL . ' ) {';
278+
}
279+
$signature .= PHP_EOL . ' // Your code here';
280+
if ( 'do_action' !== $data['type'] ) {
281+
$signature .= PHP_EOL . ' return ' . $first . ';';
282+
}
283+
$signature .= PHP_EOL . ' }';
284+
if ( $count > 1 ) {
285+
$signature .= ',';
286+
$signature .= PHP_EOL . ' 10,';
287+
$signature .= PHP_EOL . ' ' . $count;
223288
}
289+
$signature .= PHP_EOL . ');';
224290

291+
$doc = '```php' . PHP_EOL . $signature . PHP_EOL . '```' . PHP_EOL . $doc;
225292
$doc .= PHP_EOL . PHP_EOL;
226293
}
227294

@@ -242,4 +309,4 @@ function parse_docblock( $raw_comment ) {
242309
$index
243310
);
244311

245-
echo 'Genearated ' . count( $filters ) . ' hooks documentation files.' . PHP_EOL;
312+
echo 'Genearated ' . count( $filters ) . ' hooks documentation files in ' . realpath( $docs ) . PHP_EOL; // phpcs:ignore

0 commit comments

Comments
 (0)