From 951f887f8a874e9c183bb990b0aa767d3ebb835b Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Thu, 19 Sep 2024 20:20:30 +0000 Subject: [PATCH] External Libraries: Skip instanceof check when null in Text_Diff::_check(). On the first `foreach` loop in Text_Diff::_check()`, `$prevtype` is `null`. As `instanceof` requires the class name term to be an object or string, a fatal error is thrown: >Fatal error: Uncaught Error: Class name must be a valid object or a string on line 279 This change: * Adds a simple test for the `Text_Diff::_check()` method, which is how the bug was discovered as the test could never pass with the code as-is. * Adds a defensive guard to protect against the fatal. It checks if `$prevtype` is not `null` as a pre-condition to for checking the instance. This bugfix also resolves the failing test. Follow-up to [49194], [7747]. Props jrf, hellofromTonya. See #62083. git-svn-id: https://develop.svn.wordpress.org/trunk@59070 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/Text/Diff.php | 2 +- .../tests/diff/Text_Diff_Check_Test.php | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/tests/diff/Text_Diff_Check_Test.php diff --git a/src/wp-includes/Text/Diff.php b/src/wp-includes/Text/Diff.php index eee4e4f8ea531..b3c63afc5f859 100644 --- a/src/wp-includes/Text/Diff.php +++ b/src/wp-includes/Text/Diff.php @@ -276,7 +276,7 @@ function _check($from_lines, $to_lines) $prevtype = null; foreach ($this->_edits as $edit) { - if ($edit instanceof $prevtype) { + if ($prevtype !== null && $edit instanceof $prevtype) { trigger_error("Edit sequence is non-optimal", E_USER_ERROR); } $prevtype = get_class($edit); diff --git a/tests/phpunit/tests/diff/Text_Diff_Check_Test.php b/tests/phpunit/tests/diff/Text_Diff_Check_Test.php new file mode 100644 index 0000000000000..dec8193ad0896 --- /dev/null +++ b/tests/phpunit/tests/diff/Text_Diff_Check_Test.php @@ -0,0 +1,37 @@ +assertTrue( $diff->_check( self::FILE_A, self::FILE_B ) ); + } +}