Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit 6227cf8

Browse files
SpacePossumsebastianbergmann
authored andcommitted
1
1 parent af0ab94 commit 6227cf8

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed

src/Differ.php

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,41 +119,67 @@ private function checkIfDiffInOld(array $diff)
119119
*
120120
* @return string
121121
*/
122-
private function getBuffer($diff, $old, $start, $end)
122+
private function getBuffer(array $diff, array $old, $start, $end)
123123
{
124-
$newChunk = true;
125-
$buffer = $this->header;
124+
$buffer = $this->header;
125+
126+
if (!isset($old[$start])) {
127+
$buffer = $this->getDiffBufferElementNew($diff, $buffer, $start);
128+
++$start;
129+
}
130+
126131
for ($i = $start; $i < $end; $i++) {
127132
if (isset($old[$i])) {
128-
$buffer .= "\n";
129-
$newChunk = true;
130-
$i = $old[$i];
133+
$i = $old[$i];
134+
$buffer = $this->getDiffBufferElementNew($diff, $buffer, $i);
135+
} else {
136+
$buffer = $this->getDiffBufferElement($diff, $buffer, $i);
131137
}
132-
$buffer = $this->getDiffBufferElement($diff, $i, $newChunk, $buffer);
133-
$newChunk = false;
134138
}
135139

136140
return $buffer;
137141
}
138142

139-
private function getDiffBufferElement($diff, $i, $newChunk, $buffer)
143+
/**
144+
* Gets individual buffer element.
145+
*
146+
* @param array $diff
147+
* @param string $buffer
148+
* @param int $diffIndex
149+
*
150+
* @return string
151+
*/
152+
private function getDiffBufferElement(array $diff, $buffer, $diffIndex)
140153
{
141-
if ($newChunk) {
142-
if ($this->showNonDiffLines === true) {
143-
$buffer .= "@@ @@\n";
144-
}
145-
}
146-
if ($diff[$i][1] === 1 /* ADDED */) {
147-
$buffer .= '+' . $diff[$i][0] . "\n";
148-
} elseif ($diff[$i][1] === 2 /* REMOVED */) {
149-
$buffer .= '-' . $diff[$i][0] . "\n";
154+
if ($diff[$diffIndex][1] === 1 /* ADDED */) {
155+
$buffer .= '+' . $diff[$diffIndex][0] . "\n";
156+
} elseif ($diff[$diffIndex][1] === 2 /* REMOVED */) {
157+
$buffer .= '-' . $diff[$diffIndex][0] . "\n";
150158
} elseif ($this->showNonDiffLines === true) {
151-
$buffer .= ' ' . $diff[$i][0] . "\n";
159+
$buffer .= ' ' . $diff[$diffIndex][0] . "\n";
152160
}
153161

154162
return $buffer;
155163
}
156164

165+
/**
166+
* Gets individual buffer element with opening.
167+
*
168+
* @param array $diff
169+
* @param string $buffer
170+
* @param int $diffIndex
171+
*
172+
* @return string
173+
*/
174+
private function getDiffBufferElementNew(array $diff, $buffer, $diffIndex)
175+
{
176+
if ($this->showNonDiffLines === true) {
177+
$buffer .= "@@ @@\n";
178+
}
179+
180+
return $this->getDiffBufferElement($diff, $buffer, $diffIndex);
181+
}
182+
157183
/**
158184
* Returns the diff between two arrays or strings as array.
159185
*

tests/DifferTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,6 @@ public function textProvider()
312312
a
313313
-b
314314
+p
315-
316315
@@ @@
317316
i
318317
-j
@@ -332,7 +331,6 @@ public function textProvider()
332331
a
333332
-b
334333
+p
335-
336334
@@ @@
337335
i
338336
-j

0 commit comments

Comments
 (0)