Skip to content

Commit

Permalink
External Libraries: Skip instanceof check when null in Text_Diff::_ch…
Browse files Browse the repository at this point in the history
…eck().

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
  • Loading branch information
hellofromtonya committed Sep 19, 2024
1 parent 1e60814 commit 951f887
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/wp-includes/Text/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions tests/phpunit/tests/diff/Text_Diff_Check_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* Tests for WP native customizations added to the Text_Diff::check() method.
*
* @group diff
*
* @covers Text_Diff::_check
*/
final class Text_Diff_Check_Test extends WP_UnitTestCase {

const FILE_A = array(
'Line 1',
'Line 2',
'Line 3',
);

const FILE_B = array(
'Line 11',
'Line 2',
'Line 13',
);

public static function set_up_before_class() {
require_once ABSPATH . 'wp-includes/Text/Diff.php';
}

/**
* Disable WP specific set up as it is not needed.
*/
public function set_up() {}

public function test_check_passes_when_passed_same_input() {
$diff = new Text_Diff( 'auto', array( self::FILE_A, self::FILE_B ) );
$this->assertTrue( $diff->_check( self::FILE_A, self::FILE_B ) );
}
}

0 comments on commit 951f887

Please sign in to comment.