Skip to content

Commit 40ebaec

Browse files
dgsebastianbergmann
authored andcommitted
Parser: isset($match['startrange']) was always true
1 parent 9e2ea44 commit 40ebaec

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/Parser.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private function parseFileDiff(Diff $diff, array $lines): void
7474
$diffLines = [];
7575

7676
foreach ($lines as $line) {
77-
if (preg_match('/^@@\s+-(?P<start>\d+)(?:,\s*(?P<startrange>\d+))?\s+\+(?P<end>\d+)(?:,\s*(?P<endrange>\d+))?\s+@@/', $line, $match)) {
77+
if (preg_match('/^@@\s+-(?P<start>\d+)(?:,\s*(?P<startrange>\d+))?\s+\+(?P<end>\d+)(?:,\s*(?P<endrange>\d+))?\s+@@/', $line, $match, PREG_UNMATCHED_AS_NULL)) {
7878
$chunk = new Chunk(
7979
(int) $match['start'],
8080
isset($match['startrange']) ? max(0, (int) $match['startrange']) : 1,

tests/ParserTest.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ public function testParseWithRange(): void
201201
--- a/Test.txt
202202
+++ b/Test.txt
203203
@@ -49,0 +49,0 @@
204+
@@ -50 +50 @@
205+
A
206+
-B
204207
END;
205208
$diffs = $this->parser->parse($content);
206209
$this->assertContainsOnlyInstancesOf(Diff::class, $diffs);
@@ -209,13 +212,19 @@ public function testParseWithRange(): void
209212
$chunks = $diffs[0]->getChunks();
210213

211214
$this->assertContainsOnlyInstancesOf(Chunk::class, $chunks);
212-
$this->assertCount(1, $chunks);
215+
$this->assertCount(2, $chunks);
213216

214217
$chunk = $chunks[0];
215218
$this->assertSame(49, $chunk->getStart());
216219
$this->assertSame(49, $chunk->getEnd());
217220
$this->assertSame(0, $chunk->getStartRange());
218221
$this->assertSame(0, $chunk->getEndRange());
222+
223+
$chunk = $chunks[1];
224+
$this->assertSame(50, $chunk->getStart());
225+
$this->assertSame(50, $chunk->getEnd());
226+
$this->assertSame(1, $chunk->getStartRange());
227+
$this->assertSame(1, $chunk->getEndRange());
219228
}
220229

221230
/**

0 commit comments

Comments
 (0)