Skip to content

Commit 6b006e9

Browse files
committed
Compat: Replace warning-suppression in _wp_can_use_pcre_u()
This patch replaces the use of the problematic error-suppresssion operator with a specific error-handler to catch and report Unicode PCRE support without raising the related issues of error-suppression: notably conflating errors and failing to prevent completely the logging of the warnings. In this case, the WPCS rule against using error-suppression was actually helpful in pointing out the risk, but the code was left in place with an “ignore” comment to silence the violation; this patch addresses the risk and removes the need for the comment. Developed in #9576 Discussed in https://core.trac.wordpress.org/ticket/63865 Follow-up to: [45611]. Props dmsnell. Fixes #63865. git-svn-id: https://develop.svn.wordpress.org/trunk@60694 602fd350-edb4-49c9-b593-d223f7449a82
1 parent da59c19 commit 6b006e9

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/wp-includes/compat.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,28 @@ function _wp_can_use_pcre_u( $set = null ) {
4848
}
4949

5050
if ( 'reset' === $utf8_pcre ) {
51-
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- intentional error generated to detect PCRE/u support.
52-
$utf8_pcre = @preg_match( '/^./u', 'a' );
51+
$utf8_pcre = true;
52+
53+
set_error_handler(
54+
function ( $errno, $errstr ) use ( &$utf8_pcre ) {
55+
if ( str_starts_with( $errstr, 'preg_match():' ) ) {
56+
$utf8_pcre = false;
57+
return true;
58+
}
59+
60+
return false;
61+
},
62+
E_WARNING
63+
);
64+
65+
/*
66+
* Attempt to compile a PCRE pattern with the PCRE_UTF8 flag. For
67+
* systems lacking Unicode support this will trigger a warning
68+
* during compilation, which the error handler will intercept.
69+
*/
70+
preg_match( '//u', '' );
71+
72+
restore_error_handler();
5373
}
5474

5575
return $utf8_pcre;

0 commit comments

Comments
 (0)