Skip to content

Commit 9131af6

Browse files
committed
feat: add differ option: ignoreLineEnding
Signed-off-by: Jack Cherng <[email protected]>
1 parent 74e1878 commit 9131af6

File tree

6 files changed

+119
-6
lines changed

6 files changed

+119
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ $differOptions = [
7878
'context' => 3,
7979
// ignore case difference
8080
'ignoreCase' => false,
81+
// ignore line ending difference
82+
'ignoreLineEnding' => false,
8183
// ignore whitespace difference
8284
'ignoreWhitespace' => false,
8385
];

example/demo_base.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
'context' => 1,
1919
// ignore case difference
2020
'ignoreCase' => false,
21+
// ignore line ending difference
22+
'ignoreLineEnding' => false,
2123
// ignore whitespace difference
2224
'ignoreWhitespace' => false,
2325
];

src/Differ.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,12 @@ final class Differ
111111
// show how many neighbor lines
112112
// Differ::CONTEXT_ALL can be used to show the whole file
113113
'context' => 3,
114-
// ignore case difference
115-
'ignoreWhitespace' => false,
116114
// ignore whitespace difference
117115
'ignoreCase' => false,
116+
// ignore line ending difference
117+
'ignoreLineEnding' => false,
118+
// ignore case difference
119+
'ignoreWhitespace' => false,
118120
];
119121

120122
/**
@@ -312,8 +314,14 @@ public function getGroupedOpcodes(): array
312314
return $this->groupedOpcodes;
313315
}
314316

315-
$old = $this->old;
316-
$new = $this->new;
317+
if ($this->options['ignoreLineEnding']) {
318+
$old = array_map([$this, 'removeLineEnding'], $this->old);
319+
$new = array_map([$this, 'removeLineEnding'], $this->new);
320+
} else {
321+
$old = $this->old;
322+
$new = $this->new;
323+
}
324+
317325
$this->getGroupedOpcodesPre($old, $new);
318326

319327
$opcodes = $this->sequenceMatcher
@@ -338,8 +346,14 @@ public function getGroupedOpcodesGnu(): array
338346
return $this->groupedOpcodesGnu;
339347
}
340348

341-
$old = $this->old;
342-
$new = $this->new;
349+
if ($this->options['ignoreLineEnding']) {
350+
$old = array_map([$this, 'removeLineEnding'], $this->old);
351+
$new = array_map([$this, 'removeLineEnding'], $this->new);
352+
} else {
353+
$old = $this->old;
354+
$new = $this->new;
355+
}
356+
343357
$this->getGroupedOpcodesGnuPre($old, $new);
344358

345359
$opcodes = $this->sequenceMatcher
@@ -462,6 +476,16 @@ private function getGroupedOpcodesGnuPost(array &$opcodes): void
462476
$this->getGroupedOpcodesPost($opcodes);
463477
}
464478

479+
/**
480+
* Remove line ending characters at the end of the string.
481+
*
482+
* @param string $str the string
483+
*/
484+
private function removeLineEnding(string $str): string
485+
{
486+
return rtrim($str, "\r\n");
487+
}
488+
465489
/**
466490
* Claim this class has settled down and we could calculate cached
467491
* properties by current properties.

tests/IgnoreLineEndingTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jfcherng\Diff\Test;
6+
7+
use Jfcherng\Diff\DiffHelper;
8+
use Jfcherng\Diff\Renderer\RendererConstant;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/**
12+
* @internal
13+
*
14+
* @coversNothing
15+
*/
16+
final class IgnoreLineEndingTest extends TestCase
17+
{
18+
/**
19+
* @return string[][]
20+
*/
21+
public function provideIgnoreLineEndingTrue(): array
22+
{
23+
return [
24+
[
25+
file_get_contents(__DIR__ . '/data/ignore_line_ending/old_1.txt'),
26+
file_get_contents(__DIR__ . '/data/ignore_line_ending/new_1.txt'),
27+
<<<'DIFF'
28+
DIFF
29+
],
30+
];
31+
}
32+
33+
/**
34+
* @return string[][]
35+
*/
36+
public function provideIgnoreLineEndingFalse(): array
37+
{
38+
return [
39+
[
40+
file_get_contents(__DIR__ . '/data/ignore_line_ending/old_1.txt'),
41+
file_get_contents(__DIR__ . '/data/ignore_line_ending/new_1.txt'),
42+
<<<"DIFF"
43+
@@ -1,2 +1,2 @@
44+
-line 1\r
45+
-line 2\r
46+
+line 1
47+
+line 2
48+
49+
DIFF
50+
],
51+
];
52+
}
53+
54+
/**
55+
* @dataProvider provideIgnoreLineEndingTrue
56+
*/
57+
public function testIgnoreLineEndingTrue(string $old, string $new, string $expectedDiff): void
58+
{
59+
$diff = DiffHelper::calculate($old, $new, 'Unified', [
60+
'ignoreLineEnding' => true,
61+
], [
62+
'cliColorization' => RendererConstant::CLI_COLOR_DISABLE,
63+
]);
64+
65+
static::assertSame($expectedDiff, $diff);
66+
}
67+
68+
/**
69+
* @dataProvider provideIgnoreLineEndingFalse
70+
*/
71+
public function testIgnoreLineEndingFalse(string $old, string $new, string $expectedDiff): void
72+
{
73+
$diff = DiffHelper::calculate($old, $new, 'Unified', [
74+
'ignoreLineEnding' => false,
75+
], [
76+
'cliColorization' => RendererConstant::CLI_COLOR_DISABLE,
77+
]);
78+
79+
static::assertSame($expectedDiff, $diff);
80+
}
81+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
line 1
2+
line 2
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
line 1
2+
line 2

0 commit comments

Comments
 (0)