wppaste
WordPress

WP_Text_Diff_Renderer_Table::_changed( array $orig, array $final ): string

Since
2.6.0
Source
wp-includes/class-wp-text-diff-renderer-table.php:287
Process changed lines to do word-by-word diffs for extra highlighting.

Description

(TRAC style) sometimes these lines can actually be deleted or added rows.
We do additional processing to figure that out

Compatibility

WordPress
since 2.6.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$origarray
$finalarray

Return value

string

Performance profile

How much work a call to WP_Text_Diff_Renderer_Table::_changed() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
22–24

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 167.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Text_Diff_Renderer_Table::_changed() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always22–24->interleave_changed_lines(), array_keys()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev16722–2417
8.516722–2417
8.416722–241713 fewer instructions than PHP 8.3
8.318022–2417
8.218022–24174 fewer instructions than PHP 8.1
8.118426–2817
7.418426–2817

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 6

Source code

	public function _changed( $orig, $final ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.finalFound		$r = ''; 		/*		 * Does the aforementioned additional processing:		 * *_matches tell what rows are "the same" in orig and final. Those pairs will be diffed to get word changes.		 * - match is numeric: an index in other column.		 * - match is 'X': no match. It is a new row.		 * *_rows are column vectors for the orig column and the final column.		 * - row >= 0: an index of the $orig or $final array.		 * - row < 0: a blank row for that column.		 */		list($orig_matches, $final_matches, $orig_rows, $final_rows) = $this->interleave_changed_lines( $orig, $final ); 		// These will hold the word changes as determined by an inline diff.		$orig_diffs  = array();		$final_diffs = array(); 		// Compute word diffs for each matched pair using the inline diff.		foreach ( $orig_matches as $o => $f ) {			if ( is_numeric( $o ) && is_numeric( $f ) ) {				$text_diff = new Text_Diff( 'auto', array( array( $orig[ $o ] ), array( $final[ $f ] ) ) );				$renderer  = new $this->inline_diff_renderer();				$diff      = $renderer->render( $text_diff ); 				// If they're too different, don't include any <ins> or <del>'s.				if ( preg_match_all( '!(<ins>.*?</ins>|<del>.*?</del>)!', $diff, $diff_matches ) ) {					// Length of all text between <ins> or <del>.					$stripped_matches = strlen( strip_tags( implode( ' ', $diff_matches[0] ) ) );					/*					 * Since we count length of text between <ins> or <del> (instead of picking just one),					 * we double the length of chars not in those tags.					 */					$stripped_diff = strlen( strip_tags( $diff ) ) * 2 - $stripped_matches;					$diff_ratio    = $stripped_matches / $stripped_diff;					if ( $diff_ratio > $this->_diff_threshold ) {						continue; // Too different. Don't save diffs.					}				} 				// Un-inline the diffs by removing <del> or <ins>.				$orig_diffs[ $o ]  = preg_replace( '|<ins>.*?</ins>|', '', $diff );				$final_diffs[ $f ] = preg_replace( '|<del>.*?</del>|', '', $diff );			}		} 		foreach ( array_keys( $orig_rows ) as $row ) {			// Both columns have blanks. Ignore them.			if ( $orig_rows[ $row ] < 0 && $final_rows[ $row ] < 0 ) {				continue;			} 			// If we have a word based diff, use it. Otherwise, use the normal line.			if ( isset( $orig_diffs[ $orig_rows[ $row ] ] ) ) {				$orig_line = $orig_diffs[ $orig_rows[ $row ] ];			} elseif ( isset( $orig[ $orig_rows[ $row ] ] ) ) {				$orig_line = htmlspecialchars( $orig[ $orig_rows[ $row ] ] );			} else {				$orig_line = '';			} 			if ( isset( $final_diffs[ $final_rows[ $row ] ] ) ) {				$final_line = $final_diffs[ $final_rows[ $row ] ];			} elseif ( isset( $final[ $final_rows[ $row ] ] ) ) {				$final_line = htmlspecialchars( $final[ $final_rows[ $row ] ] );			} else {				$final_line = '';			} 			if ( $orig_rows[ $row ] < 0 ) { // Orig is blank. This is really an added row.				$r .= $this->_added( array( $final_line ), false );			} elseif ( $final_rows[ $row ] < 0 ) { // Final is blank. This is really a deleted row.				$r .= $this->_deleted( array( $orig_line ), false );			} else { // A true changed row.				if ( $this->_show_split_view ) {					$r .= '<tr>' . $this->deletedLine( $orig_line ) . $this->addedLine( $final_line ) . "</tr>\n";				} else {					$r .= '<tr>' . $this->deletedLine( $orig_line ) . '</tr><tr>' . $this->addedLine( $final_line ) . "</tr>\n";				}			}

Changelog

Introduced in 2.6.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/class-wp-text-diff-renderer-table.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.