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

Commit af0ab94

Browse files
SpacePossumsebastianbergmann
authored andcommitted
Add an empty line before every header afther the first.
1 parent 4e0c886 commit af0ab94

File tree

2 files changed

+59
-45
lines changed

2 files changed

+59
-45
lines changed

src/Differ.php

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

140136
return $buffer;
141137
}
142138

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)
139+
private function getDiffBufferElement($diff, $i, $newChunk, $buffer)
153140
{
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";
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";
158150
} elseif ($this->showNonDiffLines === true) {
159-
$buffer .= ' ' . $diff[$diffIndex][0] . "\n";
151+
$buffer .= ' ' . $diff[$i][0] . "\n";
160152
}
161153

162154
return $buffer;
163155
}
164156

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-
183157
/**
184158
* Returns the diff between two arrays or strings as array.
185159
*

tests/DifferTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,46 @@ public function textProvider()
304304
"A\nB",
305305
"A1\nB",
306306
],
307+
[
308+
<<<EOF
309+
--- Original
310+
+++ New
311+
@@ @@
312+
a
313+
-b
314+
+p
315+
316+
@@ @@
317+
i
318+
-j
319+
+w
320+
k
321+
322+
EOF
323+
,
324+
"a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk",
325+
"a\np\nc\nd\ne\nf\ng\nh\ni\nw\nk",
326+
],
327+
[
328+
<<<EOF
329+
--- Original
330+
+++ New
331+
@@ @@
332+
a
333+
-b
334+
+p
335+
336+
@@ @@
337+
i
338+
-j
339+
+w
340+
k
341+
342+
EOF
343+
,
344+
"a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk",
345+
"a\np\nc\nd\ne\nf\ng\nh\ni\nw\nk",
346+
],
307347
];
308348
}
309349

0 commit comments

Comments
 (0)