Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Component/Diff/Diff.php
- 9 core/lib/Drupal/Component/Diff/Diff.php
Namespace
Drupal\Component\Diff
File
core/lib/Drupal/Component/Diff/Diff.php
View source
<?php
namespace Drupal\Component\Diff;
use SebastianBergmann\Diff\Differ;
class Diff {
protected $edits;
public function __construct($from_lines, $to_lines) {
$diffOpBuilder = new DiffOpOutputBuilder();
$differ = new Differ($diffOpBuilder);
$this->edits = $diffOpBuilder
->toOpsArray($differ
->diffToArray($from_lines, $to_lines));
}
public function reverse() {
@trigger_error(__METHOD__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3337942', E_USER_DEPRECATED);
$rev = $this;
$rev->edits = [];
foreach ($this->edits as $edit) {
$rev->edits[] = $edit
->reverse();
}
return $rev;
}
public function isEmpty() {
@trigger_error(__METHOD__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3337942', E_USER_DEPRECATED);
foreach ($this->edits as $edit) {
if ($edit->type != 'copy') {
return FALSE;
}
}
return TRUE;
}
public function lcs() {
@trigger_error(__METHOD__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3337942', E_USER_DEPRECATED);
$lcs = 0;
foreach ($this->edits as $edit) {
if ($edit->type == 'copy') {
$lcs += sizeof($edit->orig);
}
}
return $lcs;
}
public function orig() {
@trigger_error(__METHOD__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3337942', E_USER_DEPRECATED);
$lines = [];
foreach ($this->edits as $edit) {
if ($edit->orig) {
array_splice($lines, sizeof($lines), 0, $edit->orig);
}
}
return $lines;
}
public function closing() {
@trigger_error(__METHOD__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3337942', E_USER_DEPRECATED);
$lines = [];
foreach ($this->edits as $edit) {
if ($edit->closing) {
array_splice($lines, sizeof($lines), 0, $edit->closing);
}
}
return $lines;
}
public function check($from_lines, $to_lines) {
@trigger_error(__METHOD__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3337942', E_USER_DEPRECATED);
if (serialize($from_lines) != serialize($this
->orig())) {
trigger_error("Reconstructed original doesn't match", E_USER_ERROR);
}
if (serialize($to_lines) != serialize($this
->closing())) {
trigger_error("Reconstructed closing doesn't match", E_USER_ERROR);
}
$rev = $this
->reverse();
if (serialize($to_lines) != serialize($rev
->orig())) {
trigger_error("Reversed original doesn't match", E_USER_ERROR);
}
if (serialize($from_lines) != serialize($rev
->closing())) {
trigger_error("Reversed closing doesn't match", E_USER_ERROR);
}
$prevtype = 'none';
foreach ($this->edits as $edit) {
if ($prevtype == $edit->type) {
trigger_error("Edit sequence is non-optimal", E_USER_ERROR);
}
$prevtype = $edit->type;
}
$lcs = $this
->lcs();
trigger_error('Diff okay: LCS = ' . $lcs, E_USER_NOTICE);
}
public function getEdits() {
return $this->edits;
}
}
Classes
Name |
Description |
Diff |
Class representing a 'diff' between two sequences of strings. |