Skip to content

Commit 2c69dc2

Browse files
Junade Alisebastianbergmann
authored andcommitted
Extracted line split functions and extracted start/end adjustment functions.
1 parent 7bd30b3 commit 2c69dc2

File tree

1 file changed

+54
-23
lines changed

1 file changed

+54
-23
lines changed

src/Differ.php

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -197,36 +197,19 @@ private function getDiffBufferElement($diff, $i, $newChunk, $buffer)
197197
public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
198198
{
199199
$fromMatches = $this->getNewLineMatches($from);
200-
$toMatches = $this->getNewLineMatches($to);
200+
$toMatches = $this->getNewLineMatches($to);
201201

202202
$from = $this->splitStringByLines($from);
203-
$to = $this->splitStringByLines($to);
203+
$to = $this->splitStringByLines($to);
204204

205205
$start = array();
206206
$end = array();
207207
$fromLength = count($from);
208208
$toLength = count($to);
209209
$length = min($fromLength, $toLength);
210210

211-
for ($i = 0; $i < $length; ++$i) {
212-
if ($from[$i] === $to[$i]) {
213-
$start[] = $from[$i];
214-
unset($from[$i], $to[$i]);
215-
} else {
216-
break;
217-
}
218-
}
219-
220-
$length -= $i;
221-
222-
for ($i = 1; $i < $length; ++$i) {
223-
if ($from[$fromLength - $i] === $to[$toLength - $i]) {
224-
array_unshift($end, $from[$fromLength - $i]);
225-
unset($from[$fromLength - $i], $to[$toLength - $i]);
226-
} else {
227-
break;
228-
}
229-
}
211+
$this->adjustDiffStartPoint($length, $from, $to);
212+
$this->adjustDiffEndPoint($length, $from, $to, $end, $fromLength, $toLength);
230213

231214
if ($lcs === null) {
232215
$lcs = $this->selectLcsImplementation($from, $to);
@@ -284,23 +267,28 @@ public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
284267

285268
/**
286269
* Get new strings denoting new lines from a given string.
270+
*
287271
* @param $string
288272
*
289273
* @return mixed
290274
*/
291-
private function getNewLineMatches($string) {
275+
private function getNewLineMatches($string)
276+
{
292277
preg_match_all('(\r\n|\r|\n)', $string, $stringMatches);
278+
293279
return $stringMatches;
294280

295281
}
296282

297283
/**
298284
* Checks if input is string, if so it will split it line-by-life.
285+
*
299286
* @param $input
300287
*
301288
* @return array
302289
*/
303-
private function splitStringByLines($input) {
290+
private function splitStringByLines($input)
291+
{
304292
if (is_string($input)) {
305293
return preg_split('(\r\n|\r|\n)', $input);
306294
}
@@ -343,4 +331,47 @@ private function calculateEstimatedFootprint(array $from, array $to)
343331

344332
return $itemSize * pow(min(count($from), count($to)), 2);
345333
}
334+
335+
/**
336+
* Adjust start point and removes common from/to lines.
337+
*
338+
* @param $length
339+
* @param $from
340+
* @param $to
341+
*/
342+
private function adjustDiffStartPoint(&$length, &$from, &$to)
343+
{
344+
for ($i = 0; $i < $length; ++$i) {
345+
if ($from[$i] === $to[$i]) {
346+
$start[] = $from[$i];
347+
unset($from[$i], $to[$i]);
348+
} else {
349+
break;
350+
}
351+
}
352+
353+
$length -= $i;
354+
}
355+
356+
/**
357+
* Adjusts end point and removes common from/to lines.
358+
*
359+
* @param $length
360+
* @param $from
361+
* @param $to
362+
* @param $end
363+
* @param $fromLength
364+
* @param $toLength
365+
*/
366+
private function adjustDiffEndPoint(&$length, &$from, &$to, $end, $fromLength, $toLength)
367+
{
368+
for ($i = 1; $i < $length; ++$i) {
369+
if ($from[$fromLength - $i] === $to[$toLength - $i]) {
370+
array_unshift($end, $from[$fromLength - $i]);
371+
unset($from[$fromLength - $i], $to[$toLength - $i]);
372+
} else {
373+
break;
374+
}
375+
}
376+
}
346377
}

0 commit comments

Comments
 (0)