1
1
<?php
2
2
/*
3
- * This file is part of the Diff package .
3
+ * This file is part of sebastian/diff .
4
4
*
5
5
* (c) Sebastian Bergmann <[email protected] >
6
6
*
@@ -41,32 +41,26 @@ public function __construct($header = "--- Original\n+++ New\n", $showNonDiffLin
41
41
/**
42
42
* Returns the diff between two arrays or strings as string.
43
43
*
44
- * @param array|string $from
45
- * @param array|string $to
44
+ * @param array|string $from
45
+ * @param array|string $to
46
46
* @param LongestCommonSubsequence $lcs
47
47
*
48
48
* @return string
49
49
*/
50
50
public function diff ($ from , $ to , LongestCommonSubsequence $ lcs = null )
51
51
{
52
- $ from = $ this ->validateDiffInput ($ from );
53
-
54
- $ to = $ this ->validateDiffInput ($ to );
55
-
56
- $ diff = $ this ->diffToArray ($ from , $ to , $ lcs );
57
-
58
- $ old = $ this ->checkIfDiffInOld ($ diff );
59
-
52
+ $ from = $ this ->validateDiffInput ($ from );
53
+ $ to = $ this ->validateDiffInput ($ to );
54
+ $ diff = $ this ->diffToArray ($ from , $ to , $ lcs );
55
+ $ old = $ this ->checkIfDiffInOld ($ diff );
60
56
$ start = isset ($ old [0 ]) ? $ old [0 ] : 0 ;
61
- $ end = count ($ diff );
57
+ $ end = \ count ($ diff );
62
58
63
- if ($ tmp = array_search ($ end , $ old )) {
59
+ if ($ tmp = \ array_search ($ end , $ old )) {
64
60
$ end = $ tmp ;
65
61
}
66
62
67
- $ buffer = $ this ->getBuffer ($ diff , $ old , $ start , $ end );
68
-
69
- return $ buffer ;
63
+ return $ this ->getBuffer ($ diff , $ old , $ start , $ end );
70
64
}
71
65
72
66
/**
@@ -78,11 +72,11 @@ public function diff($from, $to, LongestCommonSubsequence $lcs = null)
78
72
*/
79
73
private function validateDiffInput ($ input )
80
74
{
81
- if ( ! is_array ($ input ) && ! is_string ($ input )) {
82
- return (string )$ input ;
83
- } else {
84
- return $ input ;
75
+ if (!\is_array ($ input ) && !\is_string ($ input )) {
76
+ return (string ) $ input ;
85
77
}
78
+
79
+ return $ input ;
86
80
}
87
81
88
82
/**
@@ -93,7 +87,7 @@ private function validateDiffInput($input)
93
87
*
94
88
* @return array
95
89
*/
96
- private function checkIfDiffInOld (Array $ diff )
90
+ private function checkIfDiffInOld (array $ diff )
97
91
{
98
92
$ inOld = false ;
99
93
$ i = 0 ;
@@ -188,8 +182,8 @@ private function getDiffBufferElement($diff, $i, $newChunk, $buffer)
188
182
* - 1: ADDED: $token was added to $from
189
183
* - 0: OLD: $token is not changed in $to
190
184
*
191
- * @param array|string $from
192
- * @param array|string $to
185
+ * @param array|string $from
186
+ * @param array|string $to
193
187
* @param LongestCommonSubsequence $lcs
194
188
*
195
189
* @return array
@@ -198,15 +192,13 @@ public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
198
192
{
199
193
$ fromMatches = $ this ->getNewLineMatches ($ from );
200
194
$ toMatches = $ this ->getNewLineMatches ($ to );
201
-
202
- $ from = $ this ->splitStringByLines ($ from );
203
- $ to = $ this ->splitStringByLines ($ to );
204
-
205
- $ start = array ();
206
- $ end = array ();
207
- $ fromLength = count ($ from );
208
- $ toLength = count ($ to );
209
- $ length = min ($ fromLength , $ toLength );
195
+ $ from = $ this ->splitStringByLines ($ from );
196
+ $ to = $ this ->splitStringByLines ($ to );
197
+ $ start = array ();
198
+ $ end = array ();
199
+ $ fromLength = \count ($ from );
200
+ $ toLength = \count ($ to );
201
+ $ length = \min ($ fromLength , $ toLength );
210
202
211
203
$ this ->adjustDiffStartPoint ($ length , $ from , $ to );
212
204
$ this ->adjustDiffEndPoint ($ length , $ from , $ to , $ end , $ fromLength , $ toLength );
@@ -215,7 +207,7 @@ public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
215
207
$ lcs = $ this ->selectLcsImplementation ($ from , $ to );
216
208
}
217
209
218
- $ common = $ lcs ->calculate (array_values ($ from ), array_values ($ to ));
210
+ $ common = $ lcs ->calculate (\ array_values ($ from ), \ array_values ($ to ));
219
211
$ diff = array ();
220
212
221
213
if ($ this ->detectUnmatchedLineEndings ($ fromMatches , $ toMatches )) {
@@ -229,29 +221,29 @@ public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
229
221
$ diff [] = array ($ token , 0 /* OLD */ );
230
222
}
231
223
232
- reset ($ from );
233
- reset ($ to );
224
+ \ reset ($ from );
225
+ \ reset ($ to );
234
226
235
227
foreach ($ common as $ token ) {
236
- while ((( $ fromToken = reset ($ from )) !== $ token) ) {
237
- $ diff [] = array (array_shift ($ from ), 2 /* REMOVED */ );
228
+ while (($ fromToken = \ reset ($ from )) !== $ token ) {
229
+ $ diff [] = array (\ array_shift ($ from ), 2 /* REMOVED */ );
238
230
}
239
231
240
- while ((( $ toToken = reset ($ to )) !== $ token) ) {
241
- $ diff [] = array (array_shift ($ to ), 1 /* ADDED */ );
232
+ while (($ toToken = \ reset ($ to )) !== $ token ) {
233
+ $ diff [] = array (\ array_shift ($ to ), 1 /* ADDED */ );
242
234
}
243
235
244
236
$ diff [] = array ($ token , 0 /* OLD */ );
245
237
246
- array_shift ($ from );
247
- array_shift ($ to );
238
+ \ array_shift ($ from );
239
+ \ array_shift ($ to );
248
240
}
249
241
250
- while (($ token = array_shift ($ from )) !== null ) {
242
+ while (($ token = \ array_shift ($ from )) !== null ) {
251
243
$ diff [] = array ($ token , 2 /* REMOVED */ );
252
244
}
253
245
254
- while (($ token = array_shift ($ to )) !== null ) {
246
+ while (($ token = \ array_shift ($ to )) !== null ) {
255
247
$ diff [] = array ($ token , 1 /* ADDED */ );
256
248
}
257
249
@@ -271,10 +263,9 @@ public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
271
263
*/
272
264
private function getNewLineMatches ($ string )
273
265
{
274
- preg_match_all ('(\r\n|\r|\n) ' , $ string , $ stringMatches );
266
+ \ preg_match_all ('(\r\n|\r|\n) ' , $ string , $ stringMatches );
275
267
276
268
return $ stringMatches ;
277
-
278
269
}
279
270
280
271
/**
@@ -286,8 +277,8 @@ private function getNewLineMatches($string)
286
277
*/
287
278
private function splitStringByLines ($ input )
288
279
{
289
- if (is_string ($ input )) {
290
- return preg_split ('(\r\n|\r|\n) ' , $ input );
280
+ if (\ is_string ($ input )) {
281
+ return \ preg_split ('(\r\n|\r|\n) ' , $ input );
291
282
}
292
283
293
284
return $ input ;
@@ -324,23 +315,24 @@ private function selectLcsImplementation(array $from, array $to)
324
315
*/
325
316
private function calculateEstimatedFootprint (array $ from , array $ to )
326
317
{
327
- $ itemSize = PHP_INT_SIZE == 4 ? 76 : 144 ;
318
+ $ itemSize = PHP_INT_SIZE === 4 ? 76 : 144 ;
328
319
329
- return $ itemSize * pow (min (count ($ from ), count ($ to )), 2 );
320
+ return $ itemSize * \ pow (\ min (\ count ($ from ), \ count ($ to )), 2 );
330
321
}
331
322
332
323
/**
333
324
* Adjust start point and removes common from/to lines.
334
325
*
335
- * @param $length
336
- * @param $from
337
- * @param $to
326
+ * @param int $length
327
+ * @param array $from
328
+ * @param array $to
338
329
*/
339
- private function adjustDiffStartPoint (&$ length , &$ from , &$ to )
330
+ private function adjustDiffStartPoint (&$ length , array &$ from , array &$ to )
340
331
{
341
332
for ($ i = 0 ; $ i < $ length ; ++$ i ) {
342
333
if ($ from [$ i ] === $ to [$ i ]) {
343
334
$ start [] = $ from [$ i ];
335
+
344
336
unset($ from [$ i ], $ to [$ i ]);
345
337
} else {
346
338
break ;
@@ -353,18 +345,18 @@ private function adjustDiffStartPoint(&$length, &$from, &$to)
353
345
/**
354
346
* Adjusts end point and removes common from/to lines.
355
347
*
356
- * @param $length
357
- * @param $from
358
- * @param $to
359
- * @param $end
360
- * @param $fromLength
361
- * @param $toLength
348
+ * @param int $length
349
+ * @param array $from
350
+ * @param array $to
351
+ * @param array $end
352
+ * @param int $fromLength
353
+ * @param int $toLength
362
354
*/
363
- private function adjustDiffEndPoint (&$ length , &$ from , &$ to , $ end , $ fromLength , $ toLength )
355
+ private function adjustDiffEndPoint (&$ length , array &$ from , array &$ to , array $ end , $ fromLength , $ toLength )
364
356
{
365
357
for ($ i = 1 ; $ i < $ length ; ++$ i ) {
366
358
if ($ from [$ fromLength - $ i ] === $ to [$ toLength - $ i ]) {
367
- array_unshift ($ end , $ from [$ fromLength - $ i ]);
359
+ \ array_unshift ($ end , $ from [$ fromLength - $ i ]);
368
360
unset($ from [$ fromLength - $ i ], $ to [$ toLength - $ i ]);
369
361
} else {
370
362
break ;
@@ -375,15 +367,16 @@ private function adjustDiffEndPoint(&$length, &$from, &$to, $end, $fromLength, $
375
367
/**
376
368
* Returns true if line ends don't match on fromMatches and toMatches.
377
369
*
378
- * @param $fromMatches
379
- * @param $toMatches
370
+ * @param array $fromMatches
371
+ * @param array $toMatches
380
372
*
381
373
* @return bool
382
374
*/
383
- private function detectUnmatchedLineEndings ($ fromMatches , $ toMatches )
375
+ private function detectUnmatchedLineEndings (array $ fromMatches , array $ toMatches )
384
376
{
385
- return isset ($ fromMatches [0 ]) && $ toMatches [0 ] &&
386
- count ($ fromMatches [0 ]) === count ($ toMatches [0 ]) &&
377
+ return isset ($ fromMatches [0 ]) &&
378
+ $ toMatches [0 ] &&
379
+ \count ($ fromMatches [0 ]) === \count ($ toMatches [0 ]) &&
387
380
$ fromMatches [0 ] !== $ toMatches [0 ];
388
381
}
389
382
}
0 commit comments