Skip to content

Commit ea7a9b4

Browse files
Junade Alisebastianbergmann
authored andcommitted
Extracted line count and line splitting functions from body of diffToArray function.
1 parent 892e9f1 commit ea7a9b4

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

src/Differ.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,11 @@ private function getDiffBufferElement($diff, $i, $newChunk, $buffer)
196196
*/
197197
public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
198198
{
199-
preg_match_all('(\r\n|\r|\n)', $from, $fromMatches);
200-
preg_match_all('(\r\n|\r|\n)', $to, $toMatches);
199+
$fromMatches = $this->getNewLineMatches($from);
200+
$toMatches = $this->getNewLineMatches($to);
201201

202-
if (is_string($from)) {
203-
$from = preg_split('(\r\n|\r|\n)', $from);
204-
}
205-
206-
if (is_string($to)) {
207-
$to = preg_split('(\r\n|\r|\n)', $to);
208-
}
202+
$from = $this->splitStringByLines($from);
203+
$to = $this->splitStringByLines($to);
209204

210205
$start = array();
211206
$end = array();
@@ -287,6 +282,31 @@ public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
287282
return $diff;
288283
}
289284

285+
/**
286+
* @param $string
287+
*
288+
* @return mixed
289+
*/
290+
private function getNewLineMatches($string) {
291+
preg_match_all('(\r\n|\r|\n)', $string, $stringMatches);
292+
return $stringMatches;
293+
294+
}
295+
296+
/**
297+
* Checks if input is string, if so it will split it line-by-life.
298+
* @param $input
299+
*
300+
* @return array
301+
*/
302+
private function splitStringByLines($input) {
303+
if (is_string($input)) {
304+
return preg_split('(\r\n|\r|\n)', $input);
305+
}
306+
307+
return $input;
308+
}
309+
290310
/**
291311
* @param array $from
292312
* @param array $to

0 commit comments

Comments
 (0)