57
57
if ( ! is_array ( $ token ) || ! isset ( $ token [1 ] ) ) {
58
58
continue ;
59
59
}
60
- if ( ! in_array ( $ token [1 ], array ( 'apply_filters ' , 'do_action ' ) ) ) {
60
+ if ( ! in_array ( ltrim ( $ token [1 ], '\\' ) , array ( 'apply_filters ' , 'do_action ' ) ) ) {
61
61
continue ;
62
62
}
63
63
92
92
}
93
93
94
94
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 )
98
98
99
99
) {
100
100
if ( ! isset ( $ filters [ $ hook ] ) ) {
108
108
$ filters [ $ hook ]['files ' ][] = $ dir . '/ ' . $ file ->getFilename () . ': ' . $ token [2 ];
109
109
$ filters [ $ hook ]['base_dirs ' ][ $ main_dir ] = true ;
110
110
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
+
111
131
if ( $ comment ) {
112
132
$ 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 ' ] ) ) {
114
135
$ filters [ $ hook ] = array_merge ( $ docblock , $ filters [ $ hook ] );
115
136
}
116
137
}
@@ -216,12 +237,58 @@ function parse_docblock( $raw_comment ) {
216
237
$ index .= PHP_EOL ;
217
238
218
239
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
+
219
248
$ doc .= "## Parameters \n" ;
249
+ $ first = false ;
250
+ $ count = 0 ;
220
251
foreach ( (array ) $ data ['param ' ] as $ param ) {
252
+ $ count += 1 ;
221
253
$ 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 ;
223
288
}
289
+ $ signature .= PHP_EOL . '); ' ;
224
290
291
+ $ doc = '```php ' . PHP_EOL . $ signature . PHP_EOL . '``` ' . PHP_EOL . $ doc ;
225
292
$ doc .= PHP_EOL . PHP_EOL ;
226
293
}
227
294
@@ -242,4 +309,4 @@ function parse_docblock( $raw_comment ) {
242
309
$ index
243
310
);
244
311
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