Skip to content

Commit 5834450

Browse files
Closes #135
1 parent a2b6d09 commit 5834450

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
name: CI
88

99
env:
10-
COMPOSER_ROOT_VERSION: 8.0.x-dev
10+
COMPOSER_ROOT_VERSION: 8.1.x-dev
1111
PHP_VERSION: 8.4
1212

1313
permissions:

ChangeLog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles.
44

5+
## [8.1.0] - 2026-MM-DD
6+
7+
### Added
8+
9+
* [#135](https://github.com/sebastianbergmann/diff/issues/135): Add `$contextLines` constructor parameter on `UnifiedDiffOutputBuilder`
10+
511
## [8.0.0] - 2026-02-06
612

713
### Removed
@@ -163,6 +169,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt
163169

164170
* This component is no longer supported on PHP 5.6
165171

172+
[8.1.0]: https://github.com/sebastianbergmann/diff/compare/8.0.0...main
166173
[8.0.0]: https://github.com/sebastianbergmann/diff/compare/7.0...8.0.0
167174
[7.0.0]: https://github.com/sebastianbergmann/diff/compare/6.0.2...7.0.0
168175
[6.0.2]: https://github.com/sebastianbergmann/diff/compare/6.0.1...6.0.2

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
},
4646
"extra": {
4747
"branch-alias": {
48-
"dev-main": "8.0-dev"
48+
"dev-main": "8.1-dev"
4949
}
5050
}
5151
}

src/Output/UnifiedDiffOutputBuilder.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ final class UnifiedDiffOutputBuilder extends AbstractChunkOutputBuilder
3636
/**
3737
* @var positive-int
3838
*/
39-
private int $contextLines = 3;
39+
private int $contextLines;
4040
private string $header;
4141
private bool $addLineNumbers;
4242

43-
public function __construct(string $header = "--- Original\n+++ New\n", bool $addLineNumbers = false)
43+
/**
44+
* @param positive-int $contextLines
45+
*/
46+
public function __construct(string $header = "--- Original\n+++ New\n", bool $addLineNumbers = false, int $contextLines = 3)
4447
{
4548
$this->header = $header;
4649
$this->addLineNumbers = $addLineNumbers;
50+
$this->contextLines = $contextLines;
4751
}
4852

4953
public function getDiff(array $diff): string

tests/Output/UnifiedDiffOutputBuilderTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,28 @@ public function testEmptyDiffProducesEmptyOutput(string $from, string $to): void
115115

116116
$this->assertEmpty($output);
117117
}
118+
119+
public function testCustomContextLinesCanBeUsed(): void
120+
{
121+
$from = "line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nline10\n";
122+
$to = "line1\nline2\nline3\nline4\nLINE5\nline6\nline7\nline8\nline9\nline10\n";
123+
124+
$differ3 = new Differ(new UnifiedDiffOutputBuilder('', false, 3));
125+
$differ1 = new Differ(new UnifiedDiffOutputBuilder('', false, 1));
126+
$differ5 = new Differ(new UnifiedDiffOutputBuilder('', false, 5));
127+
128+
$diff3 = $differ3->diff($from, $to);
129+
$diff1 = $differ1->diff($from, $to);
130+
$diff5 = $differ5->diff($from, $to);
131+
132+
$this->assertStringContainsString(' line2', $diff3);
133+
$this->assertStringNotContainsString(' line1', $diff3);
134+
135+
$this->assertStringContainsString(' line4', $diff1);
136+
$this->assertStringNotContainsString(' line3', $diff1);
137+
$this->assertStringNotContainsString(' line7', $diff1);
138+
139+
$this->assertStringContainsString(' line1', $diff5);
140+
$this->assertStringContainsString(' line9', $diff5);
141+
}
118142
}

0 commit comments

Comments
 (0)