Skip to content

Commit 9fef36e

Browse files
committed
feat: add new option: ignoreLineEnding
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
1 parent 6ab3a36 commit 9fef36e

File tree

6 files changed

+74
-10
lines changed

6 files changed

+74
-10
lines changed

src/SequenceMatcher.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ final class SequenceMatcher
9090
private array $options = [];
9191

9292
private static array $defaultOptions = [
93-
'ignoreWhitespace' => false,
9493
'ignoreCase' => false,
94+
'ignoreLineEnding' => false,
95+
'ignoreWhitespace' => false,
9596
];
9697

9798
private array $matchingBlocks = [];
@@ -125,7 +126,11 @@ public function __construct(array $a, array $b, ?\Closure $junkCallback = null,
125126
*/
126127
public function setOptions(array $options): static
127128
{
128-
$needRerunChainB = $this->isAnyOptionChanged($this->options, $options, ['ignoreCase', 'ignoreWhitespace']);
129+
$needRerunChainB = $this->isAnyOptionChanged(
130+
$this->options,
131+
$options,
132+
['ignoreCase', 'ignoreLineEnding', 'ignoreWhitespace'],
133+
);
129134

130135
$this->options = $options + self::$defaultOptions;
131136

@@ -641,6 +646,10 @@ private function processLineWithOptions(string $line): string
641646
$line = strtolower($line);
642647
}
643648

649+
if ($this->options['ignoreLineEnding']) {
650+
$line = rtrim($line, "\r\n");
651+
}
652+
644653
return $line;
645654
}
646655

tests/SequenceMatcherTest.php

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,11 @@ public function testGetOpcodes(array $old, array $new, array $expected): void
234234
}
235235

236236
/**
237-
* Data provider for SequenceMatcher::getGroupedOpcodes with "ignoreWhitespaces".
237+
* Data provider for SequenceMatcher::getGroupedOpcodes with "ignoreWhitespace".
238238
*
239239
* @return array the data provider
240240
*/
241-
public static function getGroupedOpcodesIgnoreWhitespacesDataProvider(): array
241+
public static function getGroupedOpcodesIgnoreWhitespaceDataProvider(): array
242242
{
243243
return [
244244
[
@@ -318,8 +318,8 @@ function foo()
318318
],
319319
],
320320
[
321-
file_get_contents(__DIR__ . '/data/WorkerCommandA.php'),
322-
file_get_contents(__DIR__ . '/data/WorkerCommandB.php'),
321+
file_get_contents(__DIR__ . '/data/ignore_whitespace/old_1.php'),
322+
file_get_contents(__DIR__ . '/data/ignore_whitespace/new_1.php'),
323323
[
324324
[
325325
[SequenceMatcher::OP_DEL, 217, 222, 217, 217],
@@ -330,15 +330,42 @@ function foo()
330330
}
331331

332332
/**
333-
* Test the SequenceMatcher::getOpcodes with "ignoreWhitespaces".
333+
* Data provider for SequenceMatcher::getGroupedOpcodes with "ignoreLineEnding".
334+
*
335+
* @return array the data provider
336+
*/
337+
public static function getGroupedOpcodesIgnoreLineEndingDataProvider(): array
338+
{
339+
return [
340+
[
341+
file_get_contents(__DIR__ . '/data/ignore_line_ending/old_1.txt'),
342+
file_get_contents(__DIR__ . '/data/ignore_line_ending/new_1.txt'),
343+
[],
344+
true,
345+
],
346+
[
347+
file_get_contents(__DIR__ . '/data/ignore_line_ending/old_1.txt'),
348+
file_get_contents(__DIR__ . '/data/ignore_line_ending/new_1.txt'),
349+
[
350+
[
351+
[SequenceMatcher::OP_REP, 0, 2, 0, 2],
352+
],
353+
],
354+
false,
355+
],
356+
];
357+
}
358+
359+
/**
360+
* Test the SequenceMatcher::getOpcodes with "ignoreWhitespace".
334361
*
335362
* @covers \Jfcherng\Diff\SequenceMatcher::getOpcodes
336363
*
337364
* @param string $old the old
338365
* @param string $new the new
339366
* @param array $expected the expected
340367
*/
341-
#[DataProvider('getGroupedOpcodesIgnoreWhitespacesDataProvider')]
368+
#[DataProvider('getGroupedOpcodesIgnoreWhitespaceDataProvider')]
342369
public function testGetOpcodesIgnoreWhitespaces(string $old, string $new, array $expected): void
343370
{
344371
$this->sm->setSequences(explode("\n", $old), explode("\n", $new));
@@ -347,6 +374,30 @@ public function testGetOpcodesIgnoreWhitespaces(string $old, string $new, array
347374
static::assertSame($expected, $this->sm->getGroupedOpcodes(0));
348375
}
349376

377+
/**
378+
* Test the SequenceMatcher::getOpcodes with "ignoreLineEnding".
379+
*
380+
* @covers \Jfcherng\Diff\SequenceMatcher::getOpcodes
381+
*
382+
* @dataProvider getGroupedOpcodesIgnoreLineEndingDataProvider
383+
*
384+
* @param string $old the old
385+
* @param string $new the new
386+
* @param array $expected the expected
387+
* @param array $ignoreLineEnding should ignore line ending
388+
*/
389+
public function testGetOpcodesIgnoreLineEnding(
390+
string $old,
391+
string $new,
392+
array $expected,
393+
bool $ignoreLineEnding
394+
): void {
395+
$this->sm->setSequences(explode("\n", $old), explode("\n", $new));
396+
$this->sm->setOptions(['ignoreLineEnding' => $ignoreLineEnding]);
397+
398+
static::assertSame($expected, $this->sm->getGroupedOpcodes(0));
399+
}
400+
350401
/**
351402
* @see https://github.com/jfcherng/php-sequence-matcher/issues/2
352403
*/
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

tests/data/WorkerCommandB.php renamed to tests/data/ignore_whitespace/new_1.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private function runWorker(
157157
/** @var Registry $registry */
158158
$registry = $container->getByType(Registry::class);
159159

160-
// todo collectErrors (from Analyser)
160+
// collectErrors (from Analyser)
161161
$in->on('data', static function (array $json) use ($fileAnalyser, $registry, $out, $analysedFiles): void {
162162
$action = $json['action'];
163163
if ($action !== 'analyse') {

tests/data/WorkerCommandA.php renamed to tests/data/ignore_whitespace/old_1.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private function runWorker(
157157
/** @var Registry $registry */
158158
$registry = $container->getByType(Registry::class);
159159

160-
// todo collectErrors (from Analyser)
160+
// collectErrors (from Analyser)
161161
$in->on('data', static function (array $json) use ($fileAnalyser, $registry, $out, $analysedFiles): void {
162162
$action = $json['action'];
163163
if ($action !== 'analyse') {

0 commit comments

Comments
 (0)