# LineDiffer

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

[API reference](/docs/api/index.md) › [Diff](/docs/extensions/diff/reference/index.md) › LineDiffer

| final class | `PHPForge\Diff\LineDiffer` |
| --- | --- |
| Implements | [`DifferInterface`](/docs/extensions/diff/reference/differinterface/index.md) |
| Source | [`src/LineDiffer.php`](https://github.com/php-forge/diff/blob/main/src/LineDiffer.php#L37) |

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.).

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`diff()`](#method-diff) | Returns the edit script that transforms `$old` into `$new`, as `git diff` produces it below Git's cost limit. | `LineDiffer` |

## Method details

### diff() <span id="method-diff"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$old` | `string` | Original input, split into lines on `"\n"`. |
| `$new` | `string` | Target input, split into lines on `"\n"`. |

**Returns**

`list<`[`DiffLine`](/docs/extensions/diff/reference/diffline/index.md)`>`: 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`.