@@ -41,44 +41,22 @@ 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
- if (!is_array ($ from ) && !is_string ($ from )) {
53
- $ from = (string ) $ from ;
54
- }
52
+ $ from = $ this ->validateDiffInput ($ from );
55
53
56
- if (!is_array ($ to ) && !is_string ($ to )) {
57
- $ to = (string ) $ to ;
58
- }
54
+ $ to = $ this ->validateDiffInput ($ to );
59
55
60
56
$ buffer = $ this ->header ;
61
57
$ diff = $ this ->diffToArray ($ from , $ to , $ lcs );
62
58
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 );
82
60
83
61
$ start = isset ($ old [0 ]) ? $ old [0 ] : 0 ;
84
62
$ end = count ($ diff );
@@ -91,7 +69,7 @@ public function diff($from, $to, LongestCommonSubsequence $lcs = null)
91
69
92
70
for ($ i = $ start ; $ i < $ end ; $ i ++) {
93
71
if (isset ($ old [$ i ])) {
94
- $ buffer .= "\n" ;
72
+ $ buffer .= "\n" ;
95
73
$ newChunk = true ;
96
74
$ i = $ old [$ i ];
97
75
}
@@ -115,6 +93,54 @@ public function diff($from, $to, LongestCommonSubsequence $lcs = null)
115
93
return $ buffer ;
116
94
}
117
95
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
+
118
144
/**
119
145
* Returns the diff between two arrays or strings as array.
120
146
*
@@ -126,8 +152,8 @@ public function diff($from, $to, LongestCommonSubsequence $lcs = null)
126
152
* - 1: ADDED: $token was added to $from
127
153
* - 0: OLD: $token is not changed in $to
128
154
*
129
- * @param array|string $from
130
- * @param array|string $to
155
+ * @param array|string $from
156
+ * @param array|string $to
131
157
* @param LongestCommonSubsequence $lcs
132
158
*
133
159
* @return array
@@ -180,9 +206,11 @@ public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
180
206
181
207
if (isset ($ fromMatches [0 ]) && $ toMatches [0 ] &&
182
208
count ($ fromMatches [0 ]) === count ($ toMatches [0 ]) &&
183
- $ fromMatches [0 ] !== $ toMatches [0 ]) {
209
+ $ fromMatches [0 ] !== $ toMatches [0 ]
210
+ ) {
184
211
$ diff [] = array (
185
- '#Warning: Strings contain different line endings! ' , 0
212
+ '#Warning: Strings contain different line endings! ' ,
213
+ 0
186
214
);
187
215
}
188
216
0 commit comments