Skip to content

Commit 951f887

Browse files
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
1 parent 1e60814 commit 951f887

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/wp-includes/Text/Diff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ function _check($from_lines, $to_lines)
276276

277277
$prevtype = null;
278278
foreach ($this->_edits as $edit) {
279-
if ($edit instanceof $prevtype) {
279+
if ($prevtype !== null && $edit instanceof $prevtype) {
280280
trigger_error("Edit sequence is non-optimal", E_USER_ERROR);
281281
}
282282
$prevtype = get_class($edit);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* Tests for WP native customizations added to the Text_Diff::check() method.
5+
*
6+
* @group diff
7+
*
8+
* @covers Text_Diff::_check
9+
*/
10+
final class Text_Diff_Check_Test extends WP_UnitTestCase {
11+
12+
const FILE_A = array(
13+
'Line 1',
14+
'Line 2',
15+
'Line 3',
16+
);
17+
18+
const FILE_B = array(
19+
'Line 11',
20+
'Line 2',
21+
'Line 13',
22+
);
23+
24+
public static function set_up_before_class() {
25+
require_once ABSPATH . 'wp-includes/Text/Diff.php';
26+
}
27+
28+
/**
29+
* Disable WP specific set up as it is not needed.
30+
*/
31+
public function set_up() {}
32+
33+
public function test_check_passes_when_passed_same_input() {
34+
$diff = new Text_Diff( 'auto', array( self::FILE_A, self::FILE_B ) );
35+
$this->assertTrue( $diff->_check( self::FILE_A, self::FILE_B ) );
36+
}
37+
}

0 commit comments

Comments
 (0)