@@ -197,36 +197,19 @@ private function getDiffBufferElement($diff, $i, $newChunk, $buffer)
197
197
public function diffToArray ($ from , $ to , LongestCommonSubsequence $ lcs = null )
198
198
{
199
199
$ fromMatches = $ this ->getNewLineMatches ($ from );
200
- $ toMatches = $ this ->getNewLineMatches ($ to );
200
+ $ toMatches = $ this ->getNewLineMatches ($ to );
201
201
202
202
$ from = $ this ->splitStringByLines ($ from );
203
- $ to = $ this ->splitStringByLines ($ to );
203
+ $ to = $ this ->splitStringByLines ($ to );
204
204
205
205
$ start = array ();
206
206
$ end = array ();
207
207
$ fromLength = count ($ from );
208
208
$ toLength = count ($ to );
209
209
$ length = min ($ fromLength , $ toLength );
210
210
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 );
230
213
231
214
if ($ lcs === null ) {
232
215
$ lcs = $ this ->selectLcsImplementation ($ from , $ to );
@@ -284,23 +267,28 @@ public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
284
267
285
268
/**
286
269
* Get new strings denoting new lines from a given string.
270
+ *
287
271
* @param $string
288
272
*
289
273
* @return mixed
290
274
*/
291
- private function getNewLineMatches ($ string ) {
275
+ private function getNewLineMatches ($ string )
276
+ {
292
277
preg_match_all ('(\r\n|\r|\n) ' , $ string , $ stringMatches );
278
+
293
279
return $ stringMatches ;
294
280
295
281
}
296
282
297
283
/**
298
284
* Checks if input is string, if so it will split it line-by-life.
285
+ *
299
286
* @param $input
300
287
*
301
288
* @return array
302
289
*/
303
- private function splitStringByLines ($ input ) {
290
+ private function splitStringByLines ($ input )
291
+ {
304
292
if (is_string ($ input )) {
305
293
return preg_split ('(\r\n|\r|\n) ' , $ input );
306
294
}
@@ -343,4 +331,47 @@ private function calculateEstimatedFootprint(array $from, array $to)
343
331
344
332
return $ itemSize * pow (min (count ($ from ), count ($ to )), 2 );
345
333
}
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
+ }
346
377
}
0 commit comments