Skip to content
PHP Ecosystem
Explore

UnifiedDiffRenderer

API reference › Diff › UnifiedDiffRenderer

final readonly classPHPForge\Diff\UnifiedDiffRenderer
Sourcesrc/UnifiedDiffRenderer.php

Renders the output of git diff --unified=<context>, without its diff --git and index lines, for two texts compared after normalizing their line endings to "\n".

The injected differ receives the lines without their terminators and a carriage return in place of a missing final newline, so a last line without "\n" differs from the same line with one, as in Git, and a differ that weighs indentation, such as LineDiffer, sees the layout of the original text. The rendered lines are taken back from the texts by position, so any DifferInterface implementation can be injected.

See also: https://www.gnu.org/software/diffutils/manual/html_node/Unified-Format.html.

MethodDescriptionDefined by
__construct()Creates a renderer.UnifiedDiffRenderer
render()Renders the unified diff that transforms $old into $new.UnifiedDiffRenderer
public function __construct(PHPForge\Diff\DifferInterface $differ = new \PHPForge\Diff\LineDiffer())

Creates a renderer.

Parameters

ParameterTypeDescription
$differDifferInterfaceDiffer that computes the edit scripts.
public function render(string $old, string $new, string $fromLabel, string $toLabel, int $context = 3): string

Renders the unified diff that transforms $old into $new.

Parameters

ParameterTypeDescription
$oldstringOriginal text.
$newstringTarget text.
$fromLabelstringLabel written after --- .
$toLabelstringLabel written after +++ .
$contextintNumber of unchanged lines shown around each change; negative values count as 0.

Returns

string: Unified diff without a trailing newline, or an empty string when both texts are identical after line-ending normalization.

Throws

  • LogicException: when the differ returns a script that does not reproduce its inputs.