@@ -37,34 +37,34 @@ discard block |
||
37 | 37 | const DELETED = 1; |
38 | 38 | const INSERTED = 2; |
39 | 39 | |
40 | - /** |
|
41 | - * @var boolean |
|
42 | - */ |
|
40 | + /** |
|
41 | + * @var boolean |
|
42 | + */ |
|
43 | 43 | private $compareCharacters = false; |
44 | 44 | |
45 | - /** |
|
46 | - * @var string |
|
47 | - */ |
|
45 | + /** |
|
46 | + * @var string |
|
47 | + */ |
|
48 | 48 | private $string1; |
49 | 49 | |
50 | - /** |
|
51 | - * @var string |
|
52 | - */ |
|
50 | + /** |
|
51 | + * @var string |
|
52 | + */ |
|
53 | 53 | private $string2; |
54 | 54 | |
55 | - /** |
|
56 | - * @var string|string[] |
|
57 | - */ |
|
55 | + /** |
|
56 | + * @var string|string[] |
|
57 | + */ |
|
58 | 58 | private $sequence1 = ''; |
59 | 59 | |
60 | - /** |
|
61 | - * @var string|string[] |
|
62 | - */ |
|
60 | + /** |
|
61 | + * @var string|string[] |
|
62 | + */ |
|
63 | 63 | private $sequence2 = ''; |
64 | 64 | |
65 | - /** |
|
66 | - * @var boolean |
|
67 | - */ |
|
65 | + /** |
|
66 | + * @var boolean |
|
67 | + */ |
|
68 | 68 | private $disposed = false; |
69 | 69 | |
70 | 70 | public function __construct(string $string1, string $string2) |
@@ -73,13 +73,13 @@ discard block |
||
73 | 73 | $this->string2 = $string2; |
74 | 74 | } |
75 | 75 | |
76 | - /** |
|
77 | - * Sets whether to compare single characters. Default is to |
|
78 | - * compare only lines. |
|
79 | - * |
|
80 | - * @param bool $compare |
|
81 | - * @return Diff |
|
82 | - */ |
|
76 | + /** |
|
77 | + * Sets whether to compare single characters. Default is to |
|
78 | + * compare only lines. |
|
79 | + * |
|
80 | + * @param bool $compare |
|
81 | + * @return Diff |
|
82 | + */ |
|
83 | 83 | public function setCompareCharacters(bool $compare=true) : Diff |
84 | 84 | { |
85 | 85 | $this->compareCharacters = $compare; |
@@ -87,19 +87,19 @@ discard block |
||
87 | 87 | return $this; |
88 | 88 | } |
89 | 89 | |
90 | - /** |
|
91 | - * Returns the diff for two strings. The return value is an array, each of |
|
92 | - * whose values is an array containing two values: a line (or character, if |
|
93 | - * $compareCharacters is true), and one of the constants DIFF::UNMODIFIED (the |
|
94 | - * line or character is in both strings), DIFF::DELETED (the line or character |
|
95 | - * is only in the first string), and DIFF::INSERTED (the line or character is |
|
96 | - * only in the second string). |
|
97 | - * |
|
98 | - * @param string $string1 |
|
99 | - * @param string $string2 |
|
100 | - * @param bool $compareCharacters Whether to compare single characters (compares lines otherwise) |
|
101 | - * @return Diff |
|
102 | - */ |
|
90 | + /** |
|
91 | + * Returns the diff for two strings. The return value is an array, each of |
|
92 | + * whose values is an array containing two values: a line (or character, if |
|
93 | + * $compareCharacters is true), and one of the constants DIFF::UNMODIFIED (the |
|
94 | + * line or character is in both strings), DIFF::DELETED (the line or character |
|
95 | + * is only in the first string), and DIFF::INSERTED (the line or character is |
|
96 | + * only in the second string). |
|
97 | + * |
|
98 | + * @param string $string1 |
|
99 | + * @param string $string2 |
|
100 | + * @param bool $compareCharacters Whether to compare single characters (compares lines otherwise) |
|
101 | + * @return Diff |
|
102 | + */ |
|
103 | 103 | public static function compareStrings(string $string1, string $string2, bool $compareCharacters = false) : Diff |
104 | 104 | { |
105 | 105 | $diff = new Diff($string1, $string2); |
@@ -107,16 +107,16 @@ discard block |
||
107 | 107 | return $diff; |
108 | 108 | } |
109 | 109 | |
110 | - /** |
|
111 | - * Like {@see Diff::compare()}, but returns the diff for two files. |
|
112 | - * |
|
113 | - * @param string $file1 |
|
114 | - * @param string $file2 |
|
115 | - * @param bool $compareCharacters Whether to compare single characters (compares lines otherwise) |
|
116 | - * @return Diff |
|
117 | - * |
|
118 | - * @throws FileHelper_Exception If one of the files cannot be found or opened. |
|
119 | - */ |
|
110 | + /** |
|
111 | + * Like {@see Diff::compare()}, but returns the diff for two files. |
|
112 | + * |
|
113 | + * @param string $file1 |
|
114 | + * @param string $file2 |
|
115 | + * @param bool $compareCharacters Whether to compare single characters (compares lines otherwise) |
|
116 | + * @return Diff |
|
117 | + * |
|
118 | + * @throws FileHelper_Exception If one of the files cannot be found or opened. |
|
119 | + */ |
|
120 | 120 | public static function compareFiles(string $file1, string $file2, bool $compareCharacters = false) : Diff |
121 | 121 | { |
122 | 122 | return self::compareStrings( |
@@ -126,49 +126,49 @@ discard block |
||
126 | 126 | ); |
127 | 127 | } |
128 | 128 | |
129 | - /** |
|
130 | - * Creates an instance of the styler class, which |
|
131 | - * is used to access the CSS used for the syntax |
|
132 | - * highlighting in the HTML renderers. |
|
133 | - * |
|
134 | - * @return Styler |
|
135 | - */ |
|
129 | + /** |
|
130 | + * Creates an instance of the styler class, which |
|
131 | + * is used to access the CSS used for the syntax |
|
132 | + * highlighting in the HTML renderers. |
|
133 | + * |
|
134 | + * @return Styler |
|
135 | + */ |
|
136 | 136 | public static function createStyler() : Styler |
137 | 137 | { |
138 | 138 | return new Styler(); |
139 | 139 | } |
140 | 140 | |
141 | - /** |
|
142 | - * Retrieves the raw array that contains the diff definitions |
|
143 | - * for the two strings. |
|
144 | - * |
|
145 | - * For example, comparing the following strings: |
|
146 | - * |
|
147 | - * Hello word |
|
148 | - * Hello world |
|
149 | - * |
|
150 | - * Will return the following array: |
|
151 | - * |
|
152 | - * <pre> |
|
153 | - * Array( |
|
154 | - * [0] => Array |
|
155 | - * ( |
|
156 | - * [0] => Hello word |
|
157 | - * [1] => 1 |
|
158 | - * ) |
|
159 | - * [1] => Array |
|
160 | - * ( |
|
161 | - * [0] => Hello world |
|
162 | - * [1] => 2 |
|
163 | - * ) |
|
164 | - * ) |
|
165 | - * </pre> |
|
166 | - * |
|
167 | - * Where the second entry in the sub-array is the status |
|
168 | - * code, e.g. Diff::DELETED, Diff::INSERTED. |
|
169 | - * |
|
170 | - * @return array<int,array<int,int|string>> |
|
171 | - */ |
|
141 | + /** |
|
142 | + * Retrieves the raw array that contains the diff definitions |
|
143 | + * for the two strings. |
|
144 | + * |
|
145 | + * For example, comparing the following strings: |
|
146 | + * |
|
147 | + * Hello word |
|
148 | + * Hello world |
|
149 | + * |
|
150 | + * Will return the following array: |
|
151 | + * |
|
152 | + * <pre> |
|
153 | + * Array( |
|
154 | + * [0] => Array |
|
155 | + * ( |
|
156 | + * [0] => Hello word |
|
157 | + * [1] => 1 |
|
158 | + * ) |
|
159 | + * [1] => Array |
|
160 | + * ( |
|
161 | + * [0] => Hello world |
|
162 | + * [1] => 2 |
|
163 | + * ) |
|
164 | + * ) |
|
165 | + * </pre> |
|
166 | + * |
|
167 | + * Where the second entry in the sub-array is the status |
|
168 | + * code, e.g. Diff::DELETED, Diff::INSERTED. |
|
169 | + * |
|
170 | + * @return array<int,array<int,int|string>> |
|
171 | + */ |
|
172 | 172 | public function toArray() : array |
173 | 173 | { |
174 | 174 | if($this->disposed) |
@@ -242,13 +242,13 @@ discard block |
||
242 | 242 | return $diff; |
243 | 243 | } |
244 | 244 | |
245 | - /** |
|
246 | - * Splits the string into individual characters. |
|
247 | - * |
|
248 | - * @param string $string |
|
249 | - * @throws DiffException |
|
250 | - * @return string[] |
|
251 | - */ |
|
245 | + /** |
|
246 | + * Splits the string into individual characters. |
|
247 | + * |
|
248 | + * @param string $string |
|
249 | + * @throws DiffException |
|
250 | + * @return string[] |
|
251 | + */ |
|
252 | 252 | private function splitString(string $string) : array |
253 | 253 | { |
254 | 254 | $split = preg_split('/\R/', $string); |
@@ -265,15 +265,15 @@ discard block |
||
265 | 265 | ); |
266 | 266 | } |
267 | 267 | |
268 | - /** |
|
269 | - * Returns the table of longest common subsequence lengths for |
|
270 | - * the specified sequences. |
|
271 | - * |
|
272 | - * @param int $start |
|
273 | - * @param int $end1 |
|
274 | - * @param int $end2 |
|
275 | - * @return array<int,array<int,int>> |
|
276 | - */ |
|
268 | + /** |
|
269 | + * Returns the table of longest common subsequence lengths for |
|
270 | + * the specified sequences. |
|
271 | + * |
|
272 | + * @param int $start |
|
273 | + * @param int $end1 |
|
274 | + * @param int $end2 |
|
275 | + * @return array<int,array<int,int>> |
|
276 | + */ |
|
277 | 277 | private function computeTable(int $start, int $end1, int $end2) : array |
278 | 278 | { |
279 | 279 | // determine the lengths to be compared |
@@ -307,14 +307,14 @@ discard block |
||
307 | 307 | return $table; |
308 | 308 | } |
309 | 309 | |
310 | - /** |
|
311 | - * Returns the partial diff for the specificed sequences, in reverse order. |
|
312 | - * |
|
313 | - * @param int $start |
|
314 | - * @param int $end1 |
|
315 | - * @param int $end2 |
|
316 | - * @return array<int,array<int,int|string>> |
|
317 | - */ |
|
310 | + /** |
|
311 | + * Returns the partial diff for the specificed sequences, in reverse order. |
|
312 | + * |
|
313 | + * @param int $start |
|
314 | + * @param int $end1 |
|
315 | + * @param int $end2 |
|
316 | + * @return array<int,array<int,int|string>> |
|
317 | + */ |
|
318 | 318 | private function generatePartialDiff(int $start, int $end1, int $end2) : array |
319 | 319 | { |
320 | 320 | // compute the table of longest common subsequence lengths |
@@ -361,13 +361,13 @@ discard block |
||
361 | 361 | return $diff; |
362 | 362 | } |
363 | 363 | |
364 | - /** |
|
365 | - * Returns a diff as a string, where unmodified lines are prefixed by ' ', |
|
366 | - * deletions are prefixed by '- ', and insertions are prefixed by '+ '. |
|
367 | - * |
|
368 | - * @param string $separator |
|
369 | - * @return string |
|
370 | - */ |
|
364 | + /** |
|
365 | + * Returns a diff as a string, where unmodified lines are prefixed by ' ', |
|
366 | + * deletions are prefixed by '- ', and insertions are prefixed by '+ '. |
|
367 | + * |
|
368 | + * @param string $separator |
|
369 | + * @return string |
|
370 | + */ |
|
371 | 371 | public function toString(string $separator = "\n") : string |
372 | 372 | { |
373 | 373 | $renderer = new PlainText($this); |
@@ -376,14 +376,14 @@ discard block |
||
376 | 376 | return $renderer->render(); |
377 | 377 | } |
378 | 378 | |
379 | - /** |
|
380 | - * Returns a diff as an HTML string, where unmodified lines are contained |
|
381 | - * within 'span' elements, deletions are contained within 'del' elements, and |
|
382 | - * insertions are contained within 'ins' elements. |
|
383 | - * |
|
384 | - * @param string $separator |
|
385 | - * @return string |
|
386 | - */ |
|
379 | + /** |
|
380 | + * Returns a diff as an HTML string, where unmodified lines are contained |
|
381 | + * within 'span' elements, deletions are contained within 'del' elements, and |
|
382 | + * insertions are contained within 'ins' elements. |
|
383 | + * |
|
384 | + * @param string $separator |
|
385 | + * @return string |
|
386 | + */ |
|
387 | 387 | public function toHTML(string $separator = '<br>') : string |
388 | 388 | { |
389 | 389 | $renderer = new HTML($this); |
@@ -392,13 +392,13 @@ discard block |
||
392 | 392 | return $renderer->render(); |
393 | 393 | } |
394 | 394 | |
395 | - /** |
|
396 | - * Returns a diff as an HTML table. |
|
397 | - * |
|
398 | - * @param string $indentation |
|
399 | - * @param string $separator |
|
400 | - * @return string |
|
401 | - */ |
|
395 | + /** |
|
396 | + * Returns a diff as an HTML table. |
|
397 | + * |
|
398 | + * @param string $indentation |
|
399 | + * @param string $separator |
|
400 | + * @return string |
|
401 | + */ |
|
402 | 402 | public function toHTMLTable(string $indentation = '', string $separator = '<br>') : string |
403 | 403 | { |
404 | 404 | $renderer = new HTMLTable($this); |
@@ -408,12 +408,12 @@ discard block |
||
408 | 408 | return $renderer->render(); |
409 | 409 | } |
410 | 410 | |
411 | - /** |
|
412 | - * Disposes of the diff by clearing the stored strings, |
|
413 | - * to free memory until the class is destructed. |
|
414 | - * |
|
415 | - * @return Diff |
|
416 | - */ |
|
411 | + /** |
|
412 | + * Disposes of the diff by clearing the stored strings, |
|
413 | + * to free memory until the class is destructed. |
|
414 | + * |
|
415 | + * @return Diff |
|
416 | + */ |
|
417 | 417 | public function dispose() : Diff |
418 | 418 | { |
419 | 419 | $this->string1 = ''; |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | * @param bool $compare |
81 | 81 | * @return Diff |
82 | 82 | */ |
83 | - public function setCompareCharacters(bool $compare=true) : Diff |
|
83 | + public function setCompareCharacters(bool $compare = true) : Diff |
|
84 | 84 | { |
85 | 85 | $this->compareCharacters = $compare; |
86 | 86 | |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | */ |
172 | 172 | public function toArray() : array |
173 | 173 | { |
174 | - if($this->disposed) |
|
174 | + if ($this->disposed) |
|
175 | 175 | { |
176 | 176 | throw new DiffException( |
177 | 177 | 'The diff has been disposed.', |
@@ -204,14 +204,14 @@ discard block |
||
204 | 204 | // skip any common prefix |
205 | 205 | while ($start <= $end1 && $start <= $end2 && $this->sequence1[$start] == $this->sequence2[$start]) |
206 | 206 | { |
207 | - $start ++; |
|
207 | + $start++; |
|
208 | 208 | } |
209 | 209 | |
210 | 210 | // skip any common suffix |
211 | 211 | while ($end1 >= $start && $end2 >= $start && $this->sequence1[$end1] == $this->sequence2[$end2]) |
212 | 212 | { |
213 | - $end1 --; |
|
214 | - $end2 --; |
|
213 | + $end1--; |
|
214 | + $end2--; |
|
215 | 215 | } |
216 | 216 | |
217 | 217 | // generate the partial diff |
@@ -220,7 +220,7 @@ discard block |
||
220 | 220 | // generate the full diff |
221 | 221 | $diff = array(); |
222 | 222 | |
223 | - for ($index = 0; $index < $start; $index ++) |
|
223 | + for ($index = 0; $index < $start; $index++) |
|
224 | 224 | { |
225 | 225 | $diff[] = array($this->sequence1[$index], self::UNMODIFIED); |
226 | 226 | } |
@@ -230,7 +230,7 @@ discard block |
||
230 | 230 | $diff[] = array_pop($partialDiff); |
231 | 231 | } |
232 | 232 | |
233 | - for ($index = $end1 + 1; $index < $totalSequence; $index ++) |
|
233 | + for ($index = $end1 + 1; $index < $totalSequence; $index++) |
|
234 | 234 | { |
235 | 235 | $diff[] = array($this->sequence1[$index], self::UNMODIFIED); |
236 | 236 | } |
@@ -253,7 +253,7 @@ discard block |
||
253 | 253 | { |
254 | 254 | $split = preg_split('/\R/', $string); |
255 | 255 | |
256 | - if(is_array($split)) |
|
256 | + if (is_array($split)) |
|
257 | 257 | { |
258 | 258 | return $split; |
259 | 259 | } |
@@ -284,19 +284,19 @@ discard block |
||
284 | 284 | $table = array(array_fill(0, $length2 + 1, 0)); |
285 | 285 | |
286 | 286 | // loop over the rows |
287 | - for ($index1 = 1; $index1 <= $length1; $index1 ++){ |
|
287 | + for ($index1 = 1; $index1 <= $length1; $index1++) { |
|
288 | 288 | |
289 | 289 | // create the new row |
290 | 290 | $table[$index1] = array(0); |
291 | 291 | |
292 | 292 | // loop over the columns |
293 | - for ($index2 = 1; $index2 <= $length2; $index2 ++){ |
|
293 | + for ($index2 = 1; $index2 <= $length2; $index2++) { |
|
294 | 294 | |
295 | 295 | // store the longest common subsequence length |
296 | 296 | if ($this->sequence1[$index1 + $start - 1] |
297 | - == $this->sequence2[$index2 + $start - 1]){ |
|
297 | + == $this->sequence2[$index2 + $start - 1]) { |
|
298 | 298 | $table[$index1][$index2] = $table[$index1 - 1][$index2 - 1] + 1; |
299 | - }else{ |
|
299 | + } else { |
|
300 | 300 | $table[$index1][$index2] = |
301 | 301 | max($table[$index1 - 1][$index2], $table[$index1][$index2 - 1]); |
302 | 302 | } |
@@ -335,11 +335,11 @@ discard block |
||
335 | 335 | $index1 > 0 && $index2 > 0 |
336 | 336 | && $this->sequence1[$index1 + $start - 1] |
337 | 337 | == $this->sequence2[$index2 + $start - 1] |
338 | - ){ |
|
338 | + ) { |
|
339 | 339 | // update the diff and the indices |
340 | 340 | $diff[] = array($this->sequence1[$index1 + $start - 1], self::UNMODIFIED); |
341 | - $index1 --; |
|
342 | - $index2 --; |
|
341 | + $index1--; |
|
342 | + $index2--; |
|
343 | 343 | } |
344 | 344 | elseif ( |
345 | 345 | $index2 > 0 |
@@ -347,13 +347,13 @@ discard block |
||
347 | 347 | ) { |
348 | 348 | // update the diff and the indices |
349 | 349 | $diff[] = array($this->sequence2[$index2 + $start - 1], self::INSERTED); |
350 | - $index2 --; |
|
350 | + $index2--; |
|
351 | 351 | } |
352 | 352 | else |
353 | 353 | { |
354 | 354 | // update the diff and the indices |
355 | 355 | $diff[] = array($this->sequence1[$index1 + $start - 1], self::DELETED); |
356 | - $index1 --; |
|
356 | + $index1--; |
|
357 | 357 | } |
358 | 358 | } |
359 | 359 |
@@ -191,8 +191,7 @@ discard block |
||
191 | 191 | $end1 = strlen($this->string1) - 1; |
192 | 192 | $end2 = strlen($this->string2) - 1; |
193 | 193 | $totalSequence = strlen($this->sequence1); |
194 | - } |
|
195 | - else |
|
194 | + } else |
|
196 | 195 | { |
197 | 196 | $this->sequence1 = $this->splitString($this->string1); |
198 | 197 | $this->sequence2 = $this->splitString($this->string2); |
@@ -296,7 +295,7 @@ discard block |
||
296 | 295 | if ($this->sequence1[$index1 + $start - 1] |
297 | 296 | == $this->sequence2[$index2 + $start - 1]){ |
298 | 297 | $table[$index1][$index2] = $table[$index1 - 1][$index2 - 1] + 1; |
299 | - }else{ |
|
298 | + } else{ |
|
300 | 299 | $table[$index1][$index2] = |
301 | 300 | max($table[$index1 - 1][$index2], $table[$index1][$index2 - 1]); |
302 | 301 | } |
@@ -340,16 +339,14 @@ discard block |
||
340 | 339 | $diff[] = array($this->sequence1[$index1 + $start - 1], self::UNMODIFIED); |
341 | 340 | $index1 --; |
342 | 341 | $index2 --; |
343 | - } |
|
344 | - elseif ( |
|
342 | + } elseif ( |
|
345 | 343 | $index2 > 0 |
346 | 344 | && $table[$index1][$index2] == $table[$index1][$index2 - 1] |
347 | 345 | ) { |
348 | 346 | // update the diff and the indices |
349 | 347 | $diff[] = array($this->sequence2[$index2 + $start - 1], self::INSERTED); |
350 | 348 | $index2 --; |
351 | - } |
|
352 | - else |
|
349 | + } else |
|
353 | 350 | { |
354 | 351 | // update the diff and the indices |
355 | 352 | $diff[] = array($this->sequence1[$index1 + $start - 1], self::DELETED); |