Skip to content

Commit 892e9f1

Browse files
Junade Alisebastianbergmann
authored andcommitted
Extracted getBuffer function (generating the patch) from diff function.
1 parent 04243c3 commit 892e9f1

File tree

1 file changed

+62
-26
lines changed

1 file changed

+62
-26
lines changed

src/Differ.php

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public function diff($from, $to, LongestCommonSubsequence $lcs = null)
5353

5454
$to = $this->validateDiffInput($to);
5555

56-
$buffer = $this->header;
57-
$diff = $this->diffToArray($from, $to, $lcs);
56+
$diff = $this->diffToArray($from, $to, $lcs);
5857

5958
$old = $this->checkIfDiffInOld($diff);
6059

@@ -65,30 +64,7 @@ public function diff($from, $to, LongestCommonSubsequence $lcs = null)
6564
$end = $tmp;
6665
}
6766

68-
$newChunk = true;
69-
70-
for ($i = $start; $i < $end; $i++) {
71-
if (isset($old[$i])) {
72-
$buffer .= "\n";
73-
$newChunk = true;
74-
$i = $old[$i];
75-
}
76-
77-
if ($newChunk) {
78-
if ($this->showNonDiffLines === true) {
79-
$buffer .= "@@ @@\n";
80-
}
81-
$newChunk = false;
82-
}
83-
84-
if ($diff[$i][1] === 1 /* ADDED */) {
85-
$buffer .= '+' . $diff[$i][0] . "\n";
86-
} elseif ($diff[$i][1] === 2 /* REMOVED */) {
87-
$buffer .= '-' . $diff[$i][0] . "\n";
88-
} elseif ($this->showNonDiffLines === true) {
89-
$buffer .= ' ' . $diff[$i][0] . "\n";
90-
}
91-
}
67+
$buffer = $this->getBuffer($diff, $old, $start, $end);
9268

9369
return $buffer;
9470
}
@@ -112,6 +88,7 @@ private function validateDiffInput($input)
11288
/**
11389
* Takes input of the diff array and returns the old array.
11490
* Iterates through diff line by line,
91+
*
11592
* @param array $diff
11693
*
11794
* @return array
@@ -141,6 +118,65 @@ private function checkIfDiffInOld(Array $diff)
141118
return $old;
142119
}
143120

121+
/**
122+
* Generates buffer in string format, returning the patch.
123+
*
124+
* @param $diff
125+
* @param $old
126+
* @param $start
127+
* @param $end
128+
*
129+
* @return string
130+
*/
131+
private function getBuffer($diff, $old, $start, $end)
132+
{
133+
$newChunk = true;
134+
$buffer = $this->header;
135+
136+
for ($i = $start; $i < $end; $i++) {
137+
if (isset($old[$i])) {
138+
$buffer .= "\n";
139+
$newChunk = true;
140+
$i = $old[$i];
141+
}
142+
143+
$buffer = $this->getDiffBufferElement($diff, $i, $newChunk, $buffer);
144+
145+
$newChunk = false;
146+
}
147+
148+
return $buffer;
149+
}
150+
151+
/**
152+
* Gets individual buffer element.
153+
*
154+
* @param $diff
155+
* @param $i
156+
* @param $newChunk
157+
* @param $buffer
158+
*
159+
* @return string
160+
*/
161+
private function getDiffBufferElement($diff, $i, $newChunk, $buffer)
162+
{
163+
if ($newChunk) {
164+
if ($this->showNonDiffLines === true) {
165+
$buffer .= "@@ @@\n";
166+
}
167+
}
168+
169+
if ($diff[$i][1] === 1 /* ADDED */) {
170+
$buffer .= '+' . $diff[$i][0] . "\n";
171+
} elseif ($diff[$i][1] === 2 /* REMOVED */) {
172+
$buffer .= '-' . $diff[$i][0] . "\n";
173+
} elseif ($this->showNonDiffLines === true) {
174+
$buffer .= ' ' . $diff[$i][0] . "\n";
175+
}
176+
177+
return $buffer;
178+
}
179+
144180
/**
145181
* Returns the diff between two arrays or strings as array.
146182
*

0 commit comments

Comments
 (0)