11<?php
22/*
3- * This file is part of the Diff package .
3+ * This file is part of sebastian/diff .
44 *
55 * (c) Sebastian Bergmann <sebastian@phpunit.de>
66 *
@@ -41,32 +41,26 @@ public function __construct($header = "--- Original\n+++ New\n", $showNonDiffLin
4141 /**
4242 * Returns the diff between two arrays or strings as string.
4343 *
44- * @param array|string $from
45- * @param array|string $to
44+ * @param array|string $from
45+ * @param array|string $to
4646 * @param LongestCommonSubsequence $lcs
4747 *
4848 * @return string
4949 */
5050 public function diff ($ from , $ to , LongestCommonSubsequence $ lcs = null )
5151 {
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 );
6056 $ start = isset ($ old [0 ]) ? $ old [0 ] : 0 ;
61- $ end = count ($ diff );
57+ $ end = \ count ($ diff );
6258
63- if ($ tmp = array_search ($ end , $ old )) {
59+ if ($ tmp = \ array_search ($ end , $ old )) {
6460 $ end = $ tmp ;
6561 }
6662
67- $ buffer = $ this ->getBuffer ($ diff , $ old , $ start , $ end );
68-
69- return $ buffer ;
63+ return $ this ->getBuffer ($ diff , $ old , $ start , $ end );
7064 }
7165
7266 /**
@@ -78,11 +72,11 @@ public function diff($from, $to, LongestCommonSubsequence $lcs = null)
7872 */
7973 private function validateDiffInput ($ input )
8074 {
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 ;
8577 }
78+
79+ return $ input ;
8680 }
8781
8882 /**
@@ -93,7 +87,7 @@ private function validateDiffInput($input)
9387 *
9488 * @return array
9589 */
96- private function checkIfDiffInOld (Array $ diff )
90+ private function checkIfDiffInOld (array $ diff )
9791 {
9892 $ inOld = false ;
9993 $ i = 0 ;
@@ -188,8 +182,8 @@ private function getDiffBufferElement($diff, $i, $newChunk, $buffer)
188182 * - 1: ADDED: $token was added to $from
189183 * - 0: OLD: $token is not changed in $to
190184 *
191- * @param array|string $from
192- * @param array|string $to
185+ * @param array|string $from
186+ * @param array|string $to
193187 * @param LongestCommonSubsequence $lcs
194188 *
195189 * @return array
@@ -198,15 +192,13 @@ public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
198192 {
199193 $ fromMatches = $ this ->getNewLineMatches ($ from );
200194 $ 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 );
210202
211203 $ this ->adjustDiffStartPoint ($ length , $ from , $ to );
212204 $ this ->adjustDiffEndPoint ($ length , $ from , $ to , $ end , $ fromLength , $ toLength );
@@ -215,7 +207,7 @@ public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
215207 $ lcs = $ this ->selectLcsImplementation ($ from , $ to );
216208 }
217209
218- $ common = $ lcs ->calculate (array_values ($ from ), array_values ($ to ));
210+ $ common = $ lcs ->calculate (\ array_values ($ from ), \ array_values ($ to ));
219211 $ diff = array ();
220212
221213 if ($ this ->detectUnmatchedLineEndings ($ fromMatches , $ toMatches )) {
@@ -229,29 +221,29 @@ public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
229221 $ diff [] = array ($ token , 0 /* OLD */ );
230222 }
231223
232- reset ($ from );
233- reset ($ to );
224+ \ reset ($ from );
225+ \ reset ($ to );
234226
235227 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 */ );
238230 }
239231
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 */ );
242234 }
243235
244236 $ diff [] = array ($ token , 0 /* OLD */ );
245237
246- array_shift ($ from );
247- array_shift ($ to );
238+ \ array_shift ($ from );
239+ \ array_shift ($ to );
248240 }
249241
250- while (($ token = array_shift ($ from )) !== null ) {
242+ while (($ token = \ array_shift ($ from )) !== null ) {
251243 $ diff [] = array ($ token , 2 /* REMOVED */ );
252244 }
253245
254- while (($ token = array_shift ($ to )) !== null ) {
246+ while (($ token = \ array_shift ($ to )) !== null ) {
255247 $ diff [] = array ($ token , 1 /* ADDED */ );
256248 }
257249
@@ -271,10 +263,9 @@ public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
271263 */
272264 private function getNewLineMatches ($ string )
273265 {
274- preg_match_all ('(\r\n|\r|\n) ' , $ string , $ stringMatches );
266+ \ preg_match_all ('(\r\n|\r|\n) ' , $ string , $ stringMatches );
275267
276268 return $ stringMatches ;
277-
278269 }
279270
280271 /**
@@ -286,8 +277,8 @@ private function getNewLineMatches($string)
286277 */
287278 private function splitStringByLines ($ input )
288279 {
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 );
291282 }
292283
293284 return $ input ;
@@ -324,23 +315,24 @@ private function selectLcsImplementation(array $from, array $to)
324315 */
325316 private function calculateEstimatedFootprint (array $ from , array $ to )
326317 {
327- $ itemSize = PHP_INT_SIZE == 4 ? 76 : 144 ;
318+ $ itemSize = PHP_INT_SIZE === 4 ? 76 : 144 ;
328319
329- return $ itemSize * pow (min (count ($ from ), count ($ to )), 2 );
320+ return $ itemSize * \ pow (\ min (\ count ($ from ), \ count ($ to )), 2 );
330321 }
331322
332323 /**
333324 * Adjust start point and removes common from/to lines.
334325 *
335- * @param $length
336- * @param $from
337- * @param $to
326+ * @param int $length
327+ * @param array $from
328+ * @param array $to
338329 */
339- private function adjustDiffStartPoint (&$ length , &$ from , &$ to )
330+ private function adjustDiffStartPoint (&$ length , array &$ from , array &$ to )
340331 {
341332 for ($ i = 0 ; $ i < $ length ; ++$ i ) {
342333 if ($ from [$ i ] === $ to [$ i ]) {
343334 $ start [] = $ from [$ i ];
335+
344336 unset($ from [$ i ], $ to [$ i ]);
345337 } else {
346338 break ;
@@ -353,18 +345,18 @@ private function adjustDiffStartPoint(&$length, &$from, &$to)
353345 /**
354346 * Adjusts end point and removes common from/to lines.
355347 *
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
362354 */
363- private function adjustDiffEndPoint (&$ length , &$ from , &$ to , $ end , $ fromLength , $ toLength )
355+ private function adjustDiffEndPoint (&$ length , array &$ from , array &$ to , array $ end , $ fromLength , $ toLength )
364356 {
365357 for ($ i = 1 ; $ i < $ length ; ++$ i ) {
366358 if ($ from [$ fromLength - $ i ] === $ to [$ toLength - $ i ]) {
367- array_unshift ($ end , $ from [$ fromLength - $ i ]);
359+ \ array_unshift ($ end , $ from [$ fromLength - $ i ]);
368360 unset($ from [$ fromLength - $ i ], $ to [$ toLength - $ i ]);
369361 } else {
370362 break ;
@@ -375,15 +367,16 @@ private function adjustDiffEndPoint(&$length, &$from, &$to, $end, $fromLength, $
375367 /**
376368 * Returns true if line ends don't match on fromMatches and toMatches.
377369 *
378- * @param $fromMatches
379- * @param $toMatches
370+ * @param array $fromMatches
371+ * @param array $toMatches
380372 *
381373 * @return bool
382374 */
383- private function detectUnmatchedLineEndings ($ fromMatches , $ toMatches )
375+ private function detectUnmatchedLineEndings (array $ fromMatches , array $ toMatches )
384376 {
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 ]) &&
387380 $ fromMatches [0 ] !== $ toMatches [0 ];
388381 }
389382}
0 commit comments