Code Modernization: Pass an appropriate error level to `trigger_error()` in `_doing_it_wrong()` and related functions:

* `_deprecated_function()`
* `_deprecated_argument()`
* `_deprecated_constructor()`
* `_deprecated_file()`

The error level passed is `E_USER_DEPRECATED` for the deprecated function group and `E_USER_NOTICE` for `_doing_it_wrong()`.

Props jrf.
Fixes #36561.

git-svn-id: https://develop.svn.wordpress.org/trunk@46625 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2019-11-01 00:39:04 +00:00
parent 3623ab0ad8
commit 89621877f8
3 changed files with 167 additions and 30 deletions

View File

@ -4548,6 +4548,7 @@ function absint( $maybeint ) {
*
* @since 2.5.0
* @since 5.4.0 This function is no longer marked as "private".
* @since 5.4.0 The error type is now classified as E_USER_DEPRECATED (used to default to E_USER_NOTICE).
*
* @param string $function The function that was called.
* @param string $version The version of WordPress that deprecated the function.
@ -4576,17 +4577,47 @@ function _deprecated_function( $function, $version, $replacement = null ) {
if ( WP_DEBUG && apply_filters( 'deprecated_function_trigger_error', true ) ) {
if ( function_exists( '__' ) ) {
if ( ! is_null( $replacement ) ) {
/* translators: 1: PHP function name, 2: Version number, 3: Alternative function name. */
trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ), $function, $version, $replacement ) );
trigger_error(
sprintf(
/* translators: 1: PHP function name, 2: Version number, 3: Alternative function name. */
__( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ),
$function,
$version,
$replacement
),
E_USER_DEPRECATED
);
} else {
/* translators: 1: PHP function name, 2: Version number. */
trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.' ), $function, $version ) );
trigger_error(
sprintf(
/* translators: 1: PHP function name, 2: Version number. */
__( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.' ),
$function,
$version
),
E_USER_DEPRECATED
);
}
} else {
if ( ! is_null( $replacement ) ) {
trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', $function, $version, $replacement ) );
trigger_error(
sprintf(
'%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.',
$function,
$version,
$replacement
),
E_USER_DEPRECATED
);
} else {
trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', $function, $version ) );
trigger_error(
sprintf(
'%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.',
$function,
$version
),
E_USER_DEPRECATED
);
}
}
}
@ -4605,6 +4636,7 @@ function _deprecated_function( $function, $version, $replacement = null ) {
* @since 4.3.0
* @since 4.5.0 Added the `$parent_class` parameter.
* @since 5.4.0 This function is no longer marked as "private".
* @since 5.4.0 The error type is now classified as E_USER_DEPRECATED (used to default to E_USER_NOTICE).
*
* @param string $class The class containing the deprecated constructor.
* @param string $version The version of WordPress that deprecated the function.
@ -4645,7 +4677,8 @@ function _deprecated_constructor( $class, $version, $parent_class = '' ) {
$parent_class,
$version,
'<pre>__construct()</pre>'
)
),
E_USER_DEPRECATED
);
} else {
trigger_error(
@ -4655,7 +4688,8 @@ function _deprecated_constructor( $class, $version, $parent_class = '' ) {
$class,
$version,
'<pre>__construct()</pre>'
)
),
E_USER_DEPRECATED
);
}
} else {
@ -4667,7 +4701,8 @@ function _deprecated_constructor( $class, $version, $parent_class = '' ) {
$parent_class,
$version,
'<pre>__construct()</pre>'
)
),
E_USER_DEPRECATED
);
} else {
trigger_error(
@ -4676,7 +4711,8 @@ function _deprecated_constructor( $class, $version, $parent_class = '' ) {
$class,
$version,
'<pre>__construct()</pre>'
)
),
E_USER_DEPRECATED
);
}
}
@ -4697,6 +4733,7 @@ function _deprecated_constructor( $class, $version, $parent_class = '' ) {
*
* @since 2.5.0
* @since 5.4.0 This function is no longer marked as "private".
* @since 5.4.0 The error type is now classified as E_USER_DEPRECATED (used to default to E_USER_NOTICE).
*
* @param string $file The file that was included.
* @param string $version The version of WordPress that deprecated the file.
@ -4727,19 +4764,50 @@ function _deprecated_file( $file, $version, $replacement = null, $message = '' )
*/
if ( WP_DEBUG && apply_filters( 'deprecated_file_trigger_error', true ) ) {
$message = empty( $message ) ? '' : ' ' . $message;
if ( function_exists( '__' ) ) {
if ( ! is_null( $replacement ) ) {
/* translators: 1: PHP file name, 2: Version number, 3: Alternative file name. */
trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ), $file, $version, $replacement ) . $message );
trigger_error(
sprintf(
/* translators: 1: PHP file name, 2: Version number, 3: Alternative file name. */
__( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ),
$file,
$version,
$replacement
) . $message,
E_USER_DEPRECATED
);
} else {
/* translators: 1: PHP file name, 2: Version number. */
trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.' ), $file, $version ) . $message );
trigger_error(
sprintf(
/* translators: 1: PHP file name, 2: Version number. */
__( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.' ),
$file,
$version
) . $message,
E_USER_DEPRECATED
);
}
} else {
if ( ! is_null( $replacement ) ) {
trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', $file, $version, $replacement ) . $message );
trigger_error(
sprintf(
'%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.',
$file,
$version,
$replacement
) . $message,
E_USER_DEPRECATED
);
} else {
trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', $file, $version ) . $message );
trigger_error(
sprintf(
'%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.',
$file,
$version
) . $message,
E_USER_DEPRECATED
);
}
}
}
@ -4764,6 +4832,7 @@ function _deprecated_file( $file, $version, $replacement = null, $message = '' )
*
* @since 3.0.0
* @since 5.4.0 This function is no longer marked as "private".
* @since 5.4.0 The error type is now classified as E_USER_DEPRECATED (used to default to E_USER_NOTICE).
*
* @param string $function The function that was called.
* @param string $version The version of WordPress that deprecated the argument used.
@ -4792,17 +4861,47 @@ function _deprecated_argument( $function, $version, $message = null ) {
if ( WP_DEBUG && apply_filters( 'deprecated_argument_trigger_error', true ) ) {
if ( function_exists( '__' ) ) {
if ( ! is_null( $message ) ) {
/* translators: 1: PHP function name, 2: Version number, 3: Optional message regarding the change. */
trigger_error( sprintf( __( '%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s! %3$s' ), $function, $version, $message ) );
trigger_error(
sprintf(
/* translators: 1: PHP function name, 2: Version number, 3: Optional message regarding the change. */
__( '%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s! %3$s' ),
$function,
$version,
$message
),
E_USER_DEPRECATED
);
} else {
/* translators: 1: PHP function name, 2: Version number. */
trigger_error( sprintf( __( '%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s with no alternative available.' ), $function, $version ) );
trigger_error(
sprintf(
/* translators: 1: PHP function name, 2: Version number. */
__( '%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s with no alternative available.' ),
$function,
$version
),
E_USER_DEPRECATED
);
}
} else {
if ( ! is_null( $message ) ) {
trigger_error( sprintf( '%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s! %3$s', $function, $version, $message ) );
trigger_error(
sprintf(
'%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s! %3$s',
$function,
$version,
$message
),
E_USER_DEPRECATED
);
} else {
trigger_error( sprintf( '%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s with no alternative available.', $function, $version ) );
trigger_error(
sprintf(
'%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s with no alternative available.',
$function,
$version
),
E_USER_DEPRECATED
);
}
}
}
@ -4820,6 +4919,7 @@ function _deprecated_argument( $function, $version, $message = null ) {
* functions, and so generally does not need to be called directly.
*
* @since 4.6.0
* @since 5.4.0 The error type is now classified as E_USER_DEPRECATED (used to default to E_USER_NOTICE).
* @access private
*
* @param string $hook The hook that was used.
@ -4850,12 +4950,28 @@ function _deprecated_hook( $hook, $version, $replacement = null, $message = null
*/
if ( WP_DEBUG && apply_filters( 'deprecated_hook_trigger_error', true ) ) {
$message = empty( $message ) ? '' : ' ' . $message;
if ( ! is_null( $replacement ) ) {
/* translators: 1: WordPress hook name, 2: Version number, 3: Alternative hook name. */
trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ), $hook, $version, $replacement ) . $message );
trigger_error(
sprintf(
/* translators: 1: WordPress hook name, 2: Version number, 3: Alternative hook name. */
__( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ),
$hook,
$version,
$replacement
) . $message,
E_USER_DEPRECATED
);
} else {
/* translators: 1: WordPress hook name, 2: Version number. */
trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.' ), $hook, $version ) . $message );
trigger_error(
sprintf(
/* translators: 1: WordPress hook name, 2: Version number. */
__( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.' ),
$hook,
$version
) . $message,
E_USER_DEPRECATED
);
}
}
}
@ -4908,24 +5024,44 @@ function _doing_it_wrong( $function, $message, $version ) {
/* translators: %s: Version number. */
$version = sprintf( __( '(This message was added in version %s.)' ), $version );
}
$message .= ' ' . sprintf(
/* translators: %s: Documentation URL. */
__( 'Please see <a href="%s">Debugging in WordPress</a> for more information.' ),
__( 'https://wordpress.org/support/article/debugging-in-wordpress/' )
);
/* translators: Developer debugging message. 1: PHP function name, 2: Explanatory message, 3: Version information message. */
trigger_error( sprintf( __( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s' ), $function, $message, $version ) );
trigger_error(
sprintf(
/* translators: Developer debugging message. 1: PHP function name, 2: Explanatory message, 3: Version information message. */
__( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s' ),
$function,
$message,
$version
),
E_USER_NOTICE
);
} else {
if ( is_null( $version ) ) {
$version = '';
} else {
$version = sprintf( '(This message was added in version %s.)', $version );
}
$message .= sprintf(
' Please see <a href="%s">Debugging in WordPress</a> for more information.',
'https://wordpress.org/support/article/debugging-in-wordpress/'
);
trigger_error( sprintf( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s', $function, $message, $version ) );
trigger_error(
sprintf(
'%1$s was called <strong>incorrectly</strong>. %2$s %3$s',
$function,
$message,
$version
),
E_USER_NOTICE
);
}
}
}

View File

@ -5,6 +5,7 @@ if ( class_exists( 'PHPUnit\Runner\Version' ) && version_compare( PHPUnit\Runner
class_alias( 'PHPUnit\Framework\TestCase', 'PHPUnit_Framework_TestCase' );
class_alias( 'PHPUnit\Framework\Exception', 'PHPUnit_Framework_Exception' );
class_alias( 'PHPUnit\Framework\ExpectationFailedException', 'PHPUnit_Framework_ExpectationFailedException' );
class_alias( 'PHPUnit\Framework\Error\Deprecated', 'PHPUnit_Framework_Error_Deprecated' );
class_alias( 'PHPUnit\Framework\Error\Notice', 'PHPUnit_Framework_Error_Notice' );
class_alias( 'PHPUnit\Framework\Error\Warning', 'PHPUnit_Framework_Error_Warning' );
class_alias( 'PHPUnit\Framework\Test', 'PHPUnit_Framework_Test' );

View File

@ -180,7 +180,7 @@ EOT;
}
/**
* @expectedException PHPUnit_Framework_Error_Notice
* @expectedException PHPUnit_Framework_Error_Deprecated
*/
function test_json_encode_decode() {
require_once( ABSPATH . WPINC . '/class-json.php' );