UnifiedDiffRenderer
API reference › Diff › UnifiedDiffRenderer
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.
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Creates a renderer. | UnifiedDiffRenderer |
render() | Renders the unified diff that transforms $old into $new. | UnifiedDiffRenderer |
Method details
Section titled “Method details”__construct()
Section titled “__construct() ”public function __construct(PHPForge\Diff\DifferInterface $differ = new \PHPForge\Diff\LineDiffer())Creates a renderer.
Parameters
| Parameter | Type | Description |
|---|---|---|
$differ | DifferInterface | Differ that computes the edit scripts. |
render()
Section titled “render() ”public function render(string $old, string $new, string $fromLabel, string $toLabel, int $context = 3): stringRenders the unified diff that transforms $old into $new.
Parameters
| Parameter | Type | Description |
|---|---|---|
$old | string | Original text. |
$new | string | Target text. |
$fromLabel | string | Label written after --- . |
$toLabel | string | Label written after +++ . |
$context | int | Number 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.