Code Modernization: Fix trigger_error() with E_USER_ERROR deprecation in Text_Diff::_check().

PHP 8.4 deprecates the use of `trigger_errror()` with `E_USER_ERROR` as the error level, as there are a number of gotchas to this way of creating a `Fatal Error` (`finally` blocks not executing, destructors not executing). The recommended replacements are either to use exceptions or to do a hard `exit`.

This is an unmaintained external dependency; thus, the fix is made in the WP specific copy of the dependency.

Now, there were basically three options:
* Silence the deprecation until PHP 9.0 and delay properly solving this until then.
    This would lead to an awkward solution, as prior to PHP 8.0, error silencing would apply to all errors, while, as of PHP 8.0, it will no longer apply to fatal errors.
    It also would only buy us some time and wouldn't actually solve anything.
* Use `exit($status)`.
    This would make the code untestable and would disable handling of these errors via custom error handlers, which makes this an undesirable solution.
* Throw an exception.
    This makes for the most elegant solution with the least BC-breaking impact.

The third option is implemented which:
* Introduces a new `Text_Exception` class.
* Starts using that in the `Text_Diff::_check()` method in all applicable places.
* Adds tests for the first two error conditions.

References:
* https://wiki.php.net/rfc/deprecations_php_8_4#deprecate_passing_e_user_error_to_trigger_error
* https://www.php.net/manual/en/migration80.incompatible.php

Follow-up to [59070], [52978], [7747].

Props jrf.
See #62061.
Built from https://develop.svn.wordpress.org/trunk@59105


git-svn-id: http://core.svn.wordpress.org/trunk@58501 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
hellofromTonya 2024-09-27 17:53:19 +00:00
parent c41e36b3c4
commit 65d27642af
4 changed files with 19 additions and 6 deletions

View File

@ -260,24 +260,24 @@ class Text_Diff {
function _check($from_lines, $to_lines) function _check($from_lines, $to_lines)
{ {
if (serialize($from_lines) != serialize($this->getOriginal())) { if (serialize($from_lines) != serialize($this->getOriginal())) {
trigger_error("Reconstructed original does not match", E_USER_ERROR); throw new Text_Exception("Reconstructed original does not match");
} }
if (serialize($to_lines) != serialize($this->getFinal())) { if (serialize($to_lines) != serialize($this->getFinal())) {
trigger_error("Reconstructed final does not match", E_USER_ERROR); throw new Text_Exception("Reconstructed final does not match");
} }
$rev = $this->reverse(); $rev = $this->reverse();
if (serialize($to_lines) != serialize($rev->getOriginal())) { if (serialize($to_lines) != serialize($rev->getOriginal())) {
trigger_error("Reversed original does not match", E_USER_ERROR); throw new Text_Exception("Reversed original does not match");
} }
if (serialize($from_lines) != serialize($rev->getFinal())) { if (serialize($from_lines) != serialize($rev->getFinal())) {
trigger_error("Reversed final does not match", E_USER_ERROR); throw new Text_Exception("Reversed final does not match");
} }
$prevtype = null; $prevtype = null;
foreach ($this->_edits as $edit) { foreach ($this->_edits as $edit) {
if ($prevtype !== null && $edit instanceof $prevtype) { if ($prevtype !== null && $edit instanceof $prevtype) {
trigger_error("Edit sequence is non-optimal", E_USER_ERROR); throw new Text_Exception("Edit sequence is non-optimal");
} }
$prevtype = get_class($edit); $prevtype = get_class($edit);
} }

View File

@ -0,0 +1,11 @@
<?php
/**
* Exception for errors from the Text_Diff package.
*
* {@internal This is a WP native addition to the external Text_Diff package.}
*
* @package WordPress
* @subpackage Text_Diff
*/
class Text_Exception extends Exception {}

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.7-alpha-59104'; $wp_version = '6.7-alpha-59105';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

View File

@ -15,6 +15,8 @@ if ( ! class_exists( 'Text_Diff', false ) ) {
require ABSPATH . WPINC . '/Text/Diff/Renderer.php'; require ABSPATH . WPINC . '/Text/Diff/Renderer.php';
/** Text_Diff_Renderer_inline class */ /** Text_Diff_Renderer_inline class */
require ABSPATH . WPINC . '/Text/Diff/Renderer/inline.php'; require ABSPATH . WPINC . '/Text/Diff/Renderer/inline.php';
/** Text_Exception class */
require ABSPATH . WPINC . '/Text/Exception.php';
} }
require ABSPATH . WPINC . '/class-wp-text-diff-renderer-table.php'; require ABSPATH . WPINC . '/class-wp-text-diff-renderer-table.php';