Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Aug 21, 2024
1 parent 7da63ab commit 6af04a1
Showing 1 changed file with 7 additions and 64 deletions.
71 changes: 7 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,73 +46,16 @@ The code above yields the output below:
+bar
```

There are three output builders available in this package:
The `UnifiedDiffOutputBuilder` used in the example above generates output in "unified diff"
format and is used by PHPUnit, for example.

#### UnifiedDiffOutputBuilder
The `StrictUnifiedDiffOutputBuilder` generates output in "strict unified diff" format with
hunks, similar to `diff -u` and compatible with `patch` or `git apply`.

This is default builder, which generates the output close to udiff and is used by PHPUnit.
The `DiffOnlyOutputBuilder` generates output that only contains the lines that differ.

```php
<?php

use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;

$builder = new UnifiedDiffOutputBuilder(
"--- Original\n+++ New\n", // custom header
false // do not add line numbers to the diff
);

$differ = new Differ($builder);
print $differ->diff('foo', 'bar');
```

#### StrictUnifiedDiffOutputBuilder

Generates (strict) Unified diff's (unidiffs) with hunks,
similar to `diff -u` and compatible with `patch` and `git apply`.

```php
<?php

use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\StrictUnifiedDiffOutputBuilder;

$builder = new StrictUnifiedDiffOutputBuilder([
'collapseRanges' => true, // ranges of length one are rendered with the trailing `,1`
'commonLineThreshold' => 6, // number of same lines before ending a new hunk and creating a new one (if needed)
'contextLines' => 3, // like `diff: -u, -U NUM, --unified[=NUM]`, for patch/git apply compatibility best to keep at least @ 3
'fromFile' => '',
'fromFileDate' => null,
'toFile' => '',
'toFileDate' => null,
]);

$differ = new Differ($builder);
print $differ->diff('foo', 'bar');
```

#### DiffOnlyOutputBuilder

Output only the lines that differ.

```php
<?php

use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\DiffOnlyOutputBuilder;

$builder = new DiffOnlyOutputBuilder(
"--- Original\n+++ New\n"
);

$differ = new Differ($builder);
print $differ->diff('foo', 'bar');
```

#### DiffOutputBuilderInterface

You can pass any output builder to the `Differ` class as longs as it implements the `DiffOutputBuilderInterface`.
If none of these three output builders match your use case then you can implement
`DiffOutputBuilderInterface` to generate custom output.

#### Parsing diff

Expand Down

0 comments on commit 6af04a1

Please sign in to comment.