@@ -41,44 +41,22 @@ 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- if (!is_array ($ from ) && !is_string ($ from )) {
53- $ from = (string ) $ from ;
54- }
52+ $ from = $ this ->validateDiffInput ($ from );
5553
56- if (!is_array ($ to ) && !is_string ($ to )) {
57- $ to = (string ) $ to ;
58- }
54+ $ to = $ this ->validateDiffInput ($ to );
5955
6056 $ buffer = $ this ->header ;
6157 $ diff = $ this ->diffToArray ($ from , $ to , $ lcs );
6258
63- $ inOld = false ;
64- $ i = 0 ;
65- $ old = array ();
66-
67- foreach ($ diff as $ line ) {
68- if ($ line [1 ] === 0 /* OLD */ ) {
69- if ($ inOld === false ) {
70- $ inOld = $ i ;
71- }
72- } elseif ($ inOld !== false ) {
73- if (($ i - $ inOld ) > 5 ) {
74- $ old [$ inOld ] = $ i - 1 ;
75- }
76-
77- $ inOld = false ;
78- }
79-
80- ++$ i ;
81- }
59+ $ old = $ this ->checkIfDiffInOld ($ diff );
8260
8361 $ start = isset ($ old [0 ]) ? $ old [0 ] : 0 ;
8462 $ end = count ($ diff );
@@ -91,7 +69,7 @@ public function diff($from, $to, LongestCommonSubsequence $lcs = null)
9169
9270 for ($ i = $ start ; $ i < $ end ; $ i ++) {
9371 if (isset ($ old [$ i ])) {
94- $ buffer .= "\n" ;
72+ $ buffer .= "\n" ;
9573 $ newChunk = true ;
9674 $ i = $ old [$ i ];
9775 }
@@ -115,6 +93,54 @@ public function diff($from, $to, LongestCommonSubsequence $lcs = null)
11593 return $ buffer ;
11694 }
11795
96+ /**
97+ * Casts variable to string if it is not a string or array.
98+ *
99+ * @param $input
100+ *
101+ * @return string
102+ */
103+ private function validateDiffInput ($ input )
104+ {
105+ if ( ! is_array ($ input ) && ! is_string ($ input )) {
106+ return (string )$ input ;
107+ } else {
108+ return $ input ;
109+ }
110+ }
111+
112+ /**
113+ * Takes input of the diff array and returns the old array.
114+ * Iterates through diff line by line,
115+ * @param array $diff
116+ *
117+ * @return array
118+ */
119+ private function checkIfDiffInOld (Array $ diff )
120+ {
121+ $ inOld = false ;
122+ $ i = 0 ;
123+ $ old = array ();
124+
125+ foreach ($ diff as $ line ) {
126+ if ($ line [1 ] === 0 /* OLD */ ) {
127+ if ($ inOld === false ) {
128+ $ inOld = $ i ;
129+ }
130+ } elseif ($ inOld !== false ) {
131+ if (($ i - $ inOld ) > 5 ) {
132+ $ old [$ inOld ] = $ i - 1 ;
133+ }
134+
135+ $ inOld = false ;
136+ }
137+
138+ ++$ i ;
139+ }
140+
141+ return $ old ;
142+ }
143+
118144 /**
119145 * Returns the diff between two arrays or strings as array.
120146 *
@@ -126,8 +152,8 @@ public function diff($from, $to, LongestCommonSubsequence $lcs = null)
126152 * - 1: ADDED: $token was added to $from
127153 * - 0: OLD: $token is not changed in $to
128154 *
129- * @param array|string $from
130- * @param array|string $to
155+ * @param array|string $from
156+ * @param array|string $to
131157 * @param LongestCommonSubsequence $lcs
132158 *
133159 * @return array
@@ -180,9 +206,11 @@ public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
180206
181207 if (isset ($ fromMatches [0 ]) && $ toMatches [0 ] &&
182208 count ($ fromMatches [0 ]) === count ($ toMatches [0 ]) &&
183- $ fromMatches [0 ] !== $ toMatches [0 ]) {
209+ $ fromMatches [0 ] !== $ toMatches [0 ]
210+ ) {
184211 $ diff [] = array (
185- '#Warning: Strings contain different line endings! ' , 0
212+ '#Warning: Strings contain different line endings! ' ,
213+ 0
186214 );
187215 }
188216
0 commit comments