Skip to content

Commit 556bf85

Browse files
committed
Only prepare error constants once
1 parent 5245d69 commit 556bf85

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/WP_CLI/SearchReplacer.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,19 @@ public function clear_log_data() {
169169
* @return string Error constant name.
170170
*/
171171
private function preg_error_message( $error ) {
172-
$constants = get_defined_constants( true );
173-
if ( ! array_key_exists( 'pcre', $constants ) ) {
174-
return '<unknown error>';
172+
static $error_names = null;
173+
174+
if ( null === $error_names ) {
175+
$definitions = get_defined_constants( true );
176+
$pcre_constants = array_key_exists( 'pcre', $definitions )
177+
? $definitions['pcre']
178+
: array();
179+
$error_names = array_flip( $pcre_constants );
175180
}
176181

177-
$names = array_flip( $constants['pcre'] );
178-
return isset( $names[ $error ] ) ? $names[ $error ] : '<unknown error>';
182+
return isset( $error_names[ $error ] )
183+
? $error_names[ $error ]
184+
: '<unknown error>';
179185
}
180186
}
181187

0 commit comments

Comments
 (0)