Skip to content

Commit 614a2f8

Browse files
committed
Better global namespace removal, plus a diff helper
1 parent 4457b18 commit 614a2f8

2 files changed

Lines changed: 50 additions & 17 deletions

File tree

‎lib/runner.php‎

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,25 +135,30 @@ function parse_files( $files, $root ) {
135135
throw $e;
136136
}
137137

138-
/*
139-
* nikic/php-parser in version 3 started adding a namespace prefix
140-
* at the start of global names, but this is different than how the
141-
* documentation was previously generated. this removes those prefixes.
142-
*/
143-
array_walk_recursive(
144-
$output,
145-
static function ( &$value ) {
146-
if ( ! is_string( $value ) || strlen( $value ) < 2 || '\\' !== $value[0] ) {
147-
return;
148-
}
138+
if ( true ) {
139+
/*
140+
* nikic/php-parser in version 3 started adding a namespace prefix
141+
* at the start of global names, but this is different than how the
142+
* documentation was previously generated. this removes those prefixes.
143+
*/
144+
array_walk_recursive(
145+
$output,
146+
static function( &$value ) {
147+
if ( is_string( $value ) ) {
148+
// "\wp_kses()" -> "wp_kses()"
149+
$without_global_namespace = preg_replace(
150+
'~(^|\p{Z})\\\\([A-Z_a-z\x80-\xFF][0-9A-Z_a-z\x80-\xFF]*)(\p{Z}|$)~',
151+
'$1$2$3',
152+
$value,
153+
);
149154

150-
// "\wp_kses()" -> "wp_kses()"
151-
$next_slash = strpos( $value, '\\', 1 );
152-
if ( false === $next_slash ) {
153-
$value = substr( $value, 1 );
155+
if ( $value !== $without_global_namespace ) {
156+
$value = $without_global_namespace;
157+
}
158+
}
154159
}
155-
}
156-
);
160+
);
161+
}
157162

158163
return $output;
159164
}

‎prep-diff.php‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
$output = json_decode( file_get_contents( 'php://stdin' ), true );
4+
5+
array_walk_recursive(
6+
$output,
7+
static function( &$value, $key ) {
8+
if ( 'line' === $key || 'end_line' === $key ) {
9+
$value = 0;
10+
return;
11+
}
12+
13+
if ( is_string( $value ) ) {
14+
// "\wp_kses()" -> "wp_kses()"
15+
$without_global_namespace = preg_replace(
16+
'~(^|\p{Z})\\\\([A-Z_a-z\x80-\xFF][0-9A-Z_a-z\x80-\xFF]*)(\p{Z}|$)~',
17+
'$1$2$3',
18+
$value,
19+
);
20+
21+
if ( $value !== $without_global_namespace ) {
22+
$value = $without_global_namespace;
23+
}
24+
}
25+
}
26+
);
27+
28+
echo json_encode( $output, JSON_PRETTY_PRINT );

0 commit comments

Comments
 (0)