Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Differ.php

function Differ::diffToArray

1 call to Differ::diffToArray()
Differ::diff in vendor/sebastian/diff/src/Differ.php

File

vendor/sebastian/diff/src/Differ.php, line 52

Class

Differ

Namespace

SebastianBergmann\Diff

Code

public function diffToArray(array|string $from, array|string $to, ?LongestCommonSubsequenceCalculator $lcs = null) : array {
    if (is_string($from)) {
        $from = $this->splitStringByLines($from);
    }
    if (is_string($to)) {
        $to = $this->splitStringByLines($to);
    }
    [
        $from,
        $to,
        $start,
        $end,
    ] = self::getArrayDiffParted($from, $to);
    if ($lcs === null) {
        $lcs = $this->selectLcsImplementation($from, $to);
    }
    $common = $lcs->calculate(array_values($from), array_values($to));
    $diff = [];
    foreach ($start as $token) {
        $diff[] = [
            $token,
            self::OLD,
        ];
    }
    reset($from);
    reset($to);
    foreach ($common as $token) {
        while (($fromToken = reset($from)) !== $token) {
            $diff[] = [
                array_shift($from),
                self::REMOVED,
            ];
        }
        while (($toToken = reset($to)) !== $token) {
            $diff[] = [
                array_shift($to),
                self::ADDED,
            ];
        }
        $diff[] = [
            $token,
            self::OLD,
        ];
        array_shift($from);
        array_shift($to);
    }
    while (($token = array_shift($from)) !== null) {
        $diff[] = [
            $token,
            self::REMOVED,
        ];
    }
    while (($token = array_shift($to)) !== null) {
        $diff[] = [
            $token,
            self::ADDED,
        ];
    }
    foreach ($end as $token) {
        $diff[] = [
            $token,
            self::OLD,
        ];
    }
    if ($this->detectUnmatchedLineEndings($diff)) {
        array_unshift($diff, [
            "#Warning: Strings contain different line endings!\n",
            self::DIFF_LINE_END_WARNING,
        ]);
    }
    return $diff;
}
RSS feed
Powered by Drupal