You are here

public function Differ::diff in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/sebastian/diff/src/Differ.php \SebastianBergmann\Diff\Differ::diff()

Returns the diff between two arrays or strings as string.

Parameters

array|string $from:

array|string $to:

LongestCommonSubsequence $lcs:

Return value

string

File

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

Class

Differ
Diff implementation.

Namespace

SebastianBergmann\Diff

Code

public function diff($from, $to, LongestCommonSubsequence $lcs = null) {
  if (!is_array($from) && !is_string($from)) {
    $from = (string) $from;
  }
  if (!is_array($to) && !is_string($to)) {
    $to = (string) $to;
  }
  $buffer = $this->header;
  $diff = $this
    ->diffToArray($from, $to, $lcs);
  $inOld = false;
  $i = 0;
  $old = array();
  foreach ($diff as $line) {
    if ($line[1] === 0) {
      if ($inOld === false) {
        $inOld = $i;
      }
    }
    elseif ($inOld !== false) {
      if ($i - $inOld > 5) {
        $old[$inOld] = $i - 1;
      }
      $inOld = false;
    }
    ++$i;
  }
  $start = isset($old[0]) ? $old[0] : 0;
  $end = count($diff);
  if ($tmp = array_search($end, $old)) {
    $end = $tmp;
  }
  $newChunk = true;
  for ($i = $start; $i < $end; $i++) {
    if (isset($old[$i])) {
      $buffer .= "\n";
      $newChunk = true;
      $i = $old[$i];
    }
    if ($newChunk) {
      $buffer .= "@@ @@\n";
      $newChunk = false;
    }
    if ($diff[$i][1] === 1) {
      $buffer .= '+' . $diff[$i][0] . "\n";
    }
    elseif ($diff[$i][1] === 2) {
      $buffer .= '-' . $diff[$i][0] . "\n";
    }
    else {
      $buffer .= ' ' . $diff[$i][0] . "\n";
    }
  }
  return $buffer;
}