Skip to content
PHP Ecosystem
Explore

LineDiffer

API reference › Diff › LineDiffer

final classPHPForge\Diff\LineDiffer
ImplementsDifferInterface
Sourcesrc/LineDiffer.php

Computes the line edit scripts of git diff with default settings, except on inputs that reach Git’s cost limit.

Ports the pipeline of Git’s xdiff library: lines are interned, the common head and tail are trimmed, lines without a counterpart and runs of over-popular lines are discarded before the alignment, the remaining lines are aligned with the linear-space middle-snake refinement of Myers’ algorithm, and every group of changes is slid to its canonical position, scored by the indent heuristic. The cost-limiting heuristics Git applies to very expensive diffs are not ported, so the alignment always runs to completion. Memory stays linear in the input size; because discarded lines never anchor the alignment, the edit script is not always the shortest one.

See also: https://doi.org/10.1007/BF01840446 (Eugene W. Myers, “An O(ND) Difference Algorithm and Its Variations”.), https://github.com/git/git/tree/master/xdiff (Git’s xdiff library.).

MethodDescriptionDefined by
diff()Returns the edit script that transforms $old into $new, as git diff produces it below Git’s cost limit.LineDiffer
public function diff(string $old, string $new): list<PHPForge\Diff\DiffLine>

Returns the edit script that transforms $old into $new, as git diff produces it below Git’s cost limit.

Parameters

ParameterTypeDescription
$oldstringOriginal input, split into lines on "\n".
$newstringTarget input, split into lines on "\n".

Returns

list<DiffLine>: Edit script; its Context and Remove lines in order reproduce the lines of $old, and its Context and Add lines in order reproduce the lines of $new.