class Diff

Same name in other branches
  1. 9 core/lib/Drupal/Component/Diff/Diff.php \Drupal\Component\Diff\Diff
  2. 8.9.x core/lib/Drupal/Component/Diff/Diff.php \Drupal\Component\Diff\Diff
  3. 11.x core/lib/Drupal/Component/Diff/Diff.php \Drupal\Component\Diff\Diff

Class representing a 'diff' between two sequences of strings.

Component code originally taken from https://www.drupal.org/project/diff which was itself based on the PHP diff engine for phpwiki. The original code in phpwiki was copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org> and licensed under GPL.

Hierarchy

  • class \Drupal\Component\Diff\Diff

Expanded class hierarchy of Diff

3 files declare their use of Diff
AssertConfigTrait.php in core/tests/Drupal/KernelTests/AssertConfigTrait.php
ConfigManager.php in core/lib/Drupal/Core/Config/ConfigManager.php
DiffFormatterTest.php in core/tests/Drupal/Tests/Component/Diff/DiffFormatterTest.php
15 string references to 'Diff'
CKEditor5UpdateCodeBlockConfigurationTest::testUpdateCodeBlockConfigurationPostUpdate in core/modules/ckeditor5/tests/src/Functional/Update/CKEditor5UpdateCodeBlockConfigurationTest.php
Ensure default configuration for the CKEditor 5 codeBlock plugin is added.
CKEditor5UpdateCodeBlockConfigurationTest::testUpdateCodeBlockConfigurationPostUpdate in core/modules/ckeditor5/tests/src/Functional/Update/CKEditor5UpdateCodeBlockConfigurationTest.php
Ensure default configuration for the CKEditor 5 codeBlock plugin is added.
ckeditor5_editor_presave in core/modules/ckeditor5/ckeditor5.module
Implements hook_ENTITY_TYPE_presave() for editor entities.
ckeditor5_editor_presave in core/modules/ckeditor5/ckeditor5.module
Implements hook_ENTITY_TYPE_presave() for editor entities.
CodeBlock::defaultConfiguration in core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/CodeBlock.php

... See full list

File

core/lib/Drupal/Component/Diff/Diff.php, line 15

Namespace

Drupal\Component\Diff
View source
class Diff {
    
    /**
     * The list of differences as an array of diff operations.
     *
     * @var \Drupal\Component\Diff\Engine\DiffOp[]
     */
    protected $edits;
    
    /**
     * Constructor.
     * Computes diff between sequences of strings.
     *
     * @param array $from_lines
     *   An array of strings.
     *   (Typically these are lines from a file.)
     * @param array $to_lines
     *   An array of strings.
     */
    public function __construct($from_lines, $to_lines) {
        $diffOpBuilder = new DiffOpOutputBuilder();
        $differ = new Differ($diffOpBuilder);
        $this->edits = $diffOpBuilder->toOpsArray($differ->diffToArray($from_lines, $to_lines));
    }
    
    /**
     * Compute reversed Diff.
     *
     * SYNOPSIS:
     *
     *  $diff = new Diff($lines1, $lines2);
     *  $rev = $diff->reverse();
     * @return object
     *   A Diff object representing the inverse of the original diff.
     *
     * @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
     */
    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;
    }
    
    /**
     * Check for empty diff.
     *
     * @return bool True iff two sequences were identical.
     *
     * @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
     */
    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;
    }
    
    /**
     * Compute the length of the Longest Common Subsequence (LCS).
     *
     * This is mostly for diagnostic purposed.
     *
     * @return int The length of the LCS.
     *
     * @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
     */
    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;
    }
    
    /**
     * Gets the original set of lines.
     *
     * This reconstructs the $from_lines parameter passed to the
     * constructor.
     *
     * @return array The original sequence of strings.
     *
     * @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
     */
    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;
    }
    
    /**
     * Gets the closing set of lines.
     *
     * This reconstructs the $to_lines parameter passed to the
     * constructor.
     *
     * @return array The sequence of strings.
     *
     * @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
     */
    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;
    }
    
    /**
     * Check a Diff for validity.
     *
     * This is here only for debugging purposes.
     *
     * @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
     */
    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);
    }
    
    /**
     * Gets the list of differences as an array of diff operations.
     *
     * @return \Drupal\Component\Diff\Engine\DiffOp[]
     *   The list of differences as an array of diff operations.
     */
    public function getEdits() {
        return $this->edits;
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
Diff::$edits protected property The list of differences as an array of diff operations.
Diff::check Deprecated public function Check a Diff for validity.
Diff::closing Deprecated public function Gets the closing set of lines. 1
Diff::getEdits public function Gets the list of differences as an array of diff operations.
Diff::isEmpty Deprecated public function Check for empty diff.
Diff::lcs Deprecated public function Compute the length of the Longest Common Subsequence (LCS).
Diff::orig Deprecated public function Gets the original set of lines. 1
Diff::reverse Deprecated public function Compute reversed Diff.
Diff::__construct public function Constructor.
Computes diff between sequences of strings.
1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.