@@ -42,14 +42,14 @@ discard block |
||
42 | 42 | private string $string2; |
43 | 43 | private bool $disposed = false; |
44 | 44 | |
45 | - /** |
|
46 | - * @var string|string[] |
|
47 | - */ |
|
45 | + /** |
|
46 | + * @var string|string[] |
|
47 | + */ |
|
48 | 48 | private $sequence1 = ''; |
49 | 49 | |
50 | - /** |
|
51 | - * @var string|string[] |
|
52 | - */ |
|
50 | + /** |
|
51 | + * @var string|string[] |
|
52 | + */ |
|
53 | 53 | private $sequence2 = ''; |
54 | 54 | |
55 | 55 | public function __construct(string $string1, string $string2) |
@@ -58,13 +58,13 @@ discard block |
||
58 | 58 | $this->string2 = $string2; |
59 | 59 | } |
60 | 60 | |
61 | - /** |
|
62 | - * Sets whether to compare single characters. Default is to |
|
63 | - * compare only lines. |
|
64 | - * |
|
65 | - * @param bool $compare |
|
66 | - * @return Diff |
|
67 | - */ |
|
61 | + /** |
|
62 | + * Sets whether to compare single characters. Default is to |
|
63 | + * compare only lines. |
|
64 | + * |
|
65 | + * @param bool $compare |
|
66 | + * @return Diff |
|
67 | + */ |
|
68 | 68 | public function setCompareCharacters(bool $compare=true) : Diff |
69 | 69 | { |
70 | 70 | $this->compareCharacters = $compare; |
@@ -72,19 +72,19 @@ discard block |
||
72 | 72 | return $this; |
73 | 73 | } |
74 | 74 | |
75 | - /** |
|
76 | - * Returns the diff for two strings. The return value is an array, each of |
|
77 | - * whose values are an array containing two values: a line (or character, if |
|
78 | - * $compareCharacters is true), and one of the constants DIFF::UNMODIFIED (the |
|
79 | - * line or character is in both strings), DIFF::DELETED (the line or character |
|
80 | - * is only in the first string), and DIFF::INSERTED (the line or character is |
|
81 | - * only in the second string). |
|
82 | - * |
|
83 | - * @param string $string1 |
|
84 | - * @param string $string2 |
|
85 | - * @param bool $compareCharacters Whether to compare single characters (compares lines otherwise) |
|
86 | - * @return Diff |
|
87 | - */ |
|
75 | + /** |
|
76 | + * Returns the diff for two strings. The return value is an array, each of |
|
77 | + * whose values are an array containing two values: a line (or character, if |
|
78 | + * $compareCharacters is true), and one of the constants DIFF::UNMODIFIED (the |
|
79 | + * line or character is in both strings), DIFF::DELETED (the line or character |
|
80 | + * is only in the first string), and DIFF::INSERTED (the line or character is |
|
81 | + * only in the second string). |
|
82 | + * |
|
83 | + * @param string $string1 |
|
84 | + * @param string $string2 |
|
85 | + * @param bool $compareCharacters Whether to compare single characters (compares lines otherwise) |
|
86 | + * @return Diff |
|
87 | + */ |
|
88 | 88 | public static function compareStrings(string $string1, string $string2, bool $compareCharacters = false) : Diff |
89 | 89 | { |
90 | 90 | $diff = new Diff($string1, $string2); |
@@ -92,16 +92,16 @@ discard block |
||
92 | 92 | return $diff; |
93 | 93 | } |
94 | 94 | |
95 | - /** |
|
96 | - * Like {@see Diff::compare()}, but returns the diff for two files. |
|
97 | - * |
|
98 | - * @param string $file1 |
|
99 | - * @param string $file2 |
|
100 | - * @param bool $compareCharacters Whether to compare single characters (compares lines otherwise) |
|
101 | - * @return Diff |
|
102 | - * |
|
103 | - * @throws FileHelper_Exception If one of the files cannot be found or opened. |
|
104 | - */ |
|
95 | + /** |
|
96 | + * Like {@see Diff::compare()}, but returns the diff for two files. |
|
97 | + * |
|
98 | + * @param string $file1 |
|
99 | + * @param string $file2 |
|
100 | + * @param bool $compareCharacters Whether to compare single characters (compares lines otherwise) |
|
101 | + * @return Diff |
|
102 | + * |
|
103 | + * @throws FileHelper_Exception If one of the files cannot be found or opened. |
|
104 | + */ |
|
105 | 105 | public static function compareFiles(string $file1, string $file2, bool $compareCharacters = false) : Diff |
106 | 106 | { |
107 | 107 | return self::compareStrings( |
@@ -111,13 +111,13 @@ discard block |
||
111 | 111 | ); |
112 | 112 | } |
113 | 113 | |
114 | - /** |
|
115 | - * Creates an instance of the styler class, which |
|
116 | - * is used to access the CSS used for the syntax |
|
117 | - * highlighting in the HTML renderers. |
|
118 | - * |
|
119 | - * @return Styler |
|
120 | - */ |
|
114 | + /** |
|
115 | + * Creates an instance of the styler class, which |
|
116 | + * is used to access the CSS used for the syntax |
|
117 | + * highlighting in the HTML renderers. |
|
118 | + * |
|
119 | + * @return Styler |
|
120 | + */ |
|
121 | 121 | public static function createStyler() : Styler |
122 | 122 | { |
123 | 123 | return new Styler(); |
@@ -225,13 +225,13 @@ discard block |
||
225 | 225 | return $diff; |
226 | 226 | } |
227 | 227 | |
228 | - /** |
|
229 | - * Splits the string into individual lines. |
|
230 | - * |
|
231 | - * @param string $string |
|
232 | - * @throws DiffException |
|
233 | - * @return string[] |
|
234 | - */ |
|
228 | + /** |
|
229 | + * Splits the string into individual lines. |
|
230 | + * |
|
231 | + * @param string $string |
|
232 | + * @throws DiffException |
|
233 | + * @return string[] |
|
234 | + */ |
|
235 | 235 | public static function splitLines(string $string) : array |
236 | 236 | { |
237 | 237 | $split = preg_split('/\R/', $string); |
@@ -271,15 +271,15 @@ discard block |
||
271 | 271 | ); |
272 | 272 | } |
273 | 273 | |
274 | - /** |
|
275 | - * Returns the table of the longest common subsequence lengths |
|
276 | - * for the specified sequences. |
|
277 | - * |
|
278 | - * @param int $start |
|
279 | - * @param int $end1 |
|
280 | - * @param int $end2 |
|
281 | - * @return array<int,array<int,int>> |
|
282 | - */ |
|
274 | + /** |
|
275 | + * Returns the table of the longest common subsequence lengths |
|
276 | + * for the specified sequences. |
|
277 | + * |
|
278 | + * @param int $start |
|
279 | + * @param int $end1 |
|
280 | + * @param int $end2 |
|
281 | + * @return array<int,array<int,int>> |
|
282 | + */ |
|
283 | 283 | private function computeTable(int $start, int $end1, int $end2) : array |
284 | 284 | { |
285 | 285 | // determine the lengths to be compared |
@@ -313,14 +313,14 @@ discard block |
||
313 | 313 | return $table; |
314 | 314 | } |
315 | 315 | |
316 | - /** |
|
317 | - * Returns the partial diff for the specified sequences, in reverse order. |
|
318 | - * |
|
319 | - * @param int $start |
|
320 | - * @param int $end1 |
|
321 | - * @param int $end2 |
|
322 | - * @return array<int,array<int,int|string>> |
|
323 | - */ |
|
316 | + /** |
|
317 | + * Returns the partial diff for the specified sequences, in reverse order. |
|
318 | + * |
|
319 | + * @param int $start |
|
320 | + * @param int $end1 |
|
321 | + * @param int $end2 |
|
322 | + * @return array<int,array<int,int|string>> |
|
323 | + */ |
|
324 | 324 | private function generatePartialDiff(int $start, int $end1, int $end2) : array |
325 | 325 | { |
326 | 326 | // compute the table of the longest common subsequence lengths |
@@ -367,13 +367,13 @@ discard block |
||
367 | 367 | return $diff; |
368 | 368 | } |
369 | 369 | |
370 | - /** |
|
371 | - * Returns a diff as a string, where unmodified lines are prefixed by ' ', |
|
372 | - * deletions are prefixed by '- ', and insertions are prefixed by '+ '. |
|
373 | - * |
|
374 | - * @param string $separator |
|
375 | - * @return string |
|
376 | - */ |
|
370 | + /** |
|
371 | + * Returns a diff as a string, where unmodified lines are prefixed by ' ', |
|
372 | + * deletions are prefixed by '- ', and insertions are prefixed by '+ '. |
|
373 | + * |
|
374 | + * @param string $separator |
|
375 | + * @return string |
|
376 | + */ |
|
377 | 377 | public function toString(string $separator = "\n") : string |
378 | 378 | { |
379 | 379 | $renderer = new PlainText($this); |
@@ -382,14 +382,14 @@ discard block |
||
382 | 382 | return $renderer->render(); |
383 | 383 | } |
384 | 384 | |
385 | - /** |
|
386 | - * Returns a diff as an HTML string, where unmodified lines are contained |
|
387 | - * within 'span' elements, deletions are contained within 'del' elements, and |
|
388 | - * insertions are contained within 'ins' elements. |
|
389 | - * |
|
390 | - * @param string $separator |
|
391 | - * @return string |
|
392 | - */ |
|
385 | + /** |
|
386 | + * Returns a diff as an HTML string, where unmodified lines are contained |
|
387 | + * within 'span' elements, deletions are contained within 'del' elements, and |
|
388 | + * insertions are contained within 'ins' elements. |
|
389 | + * |
|
390 | + * @param string $separator |
|
391 | + * @return string |
|
392 | + */ |
|
393 | 393 | public function toHTML(string $separator = '<br>') : string |
394 | 394 | { |
395 | 395 | $renderer = new HTML($this); |
@@ -398,13 +398,13 @@ discard block |
||
398 | 398 | return $renderer->render(); |
399 | 399 | } |
400 | 400 | |
401 | - /** |
|
402 | - * Returns a diff as an HTML table. |
|
403 | - * |
|
404 | - * @param string $indentation |
|
405 | - * @param string $separator |
|
406 | - * @return string |
|
407 | - */ |
|
401 | + /** |
|
402 | + * Returns a diff as an HTML table. |
|
403 | + * |
|
404 | + * @param string $indentation |
|
405 | + * @param string $separator |
|
406 | + * @return string |
|
407 | + */ |
|
408 | 408 | public function toHTMLTable(string $indentation = '', string $separator = '<br>') : string |
409 | 409 | { |
410 | 410 | $renderer = new HTMLTable($this); |
@@ -414,12 +414,12 @@ discard block |
||
414 | 414 | return $renderer->render(); |
415 | 415 | } |
416 | 416 | |
417 | - /** |
|
418 | - * Disposes of the diff by clearing the stored strings, |
|
419 | - * to free memory until the class is destructed. |
|
420 | - * |
|
421 | - * @return Diff |
|
422 | - */ |
|
417 | + /** |
|
418 | + * Disposes of the diff by clearing the stored strings, |
|
419 | + * to free memory until the class is destructed. |
|
420 | + * |
|
421 | + * @return Diff |
|
422 | + */ |
|
423 | 423 | public function dispose() : Diff |
424 | 424 | { |
425 | 425 | $this->string1 = ''; |
@@ -173,8 +173,7 @@ discard block |
||
173 | 173 | { |
174 | 174 | $this->sequence1 = self::splitCharacters($this->string1); |
175 | 175 | $this->sequence2 = self::splitCharacters($this->string2); |
176 | - } |
|
177 | - else |
|
176 | + } else |
|
178 | 177 | { |
179 | 178 | $this->sequence1 = self::splitLines($this->string1); |
180 | 179 | $this->sequence2 = self::splitLines($this->string2); |
@@ -302,7 +301,7 @@ discard block |
||
302 | 301 | if ($this->sequence1[$index1 + $start - 1] |
303 | 302 | === $this->sequence2[$index2 + $start - 1]){ |
304 | 303 | $table[$index1][$index2] = $table[$index1 - 1][$index2 - 1] + 1; |
305 | - }else{ |
|
304 | + } else{ |
|
306 | 305 | $table[$index1][$index2] = |
307 | 306 | max($table[$index1 - 1][$index2], $table[$index1][$index2 - 1]); |
308 | 307 | } |
@@ -346,16 +345,14 @@ discard block |
||
346 | 345 | $diff[] = array($this->sequence1[$index1 + $start - 1], self::UNMODIFIED); |
347 | 346 | $index1 --; |
348 | 347 | $index2 --; |
349 | - } |
|
350 | - elseif ( |
|
348 | + } elseif ( |
|
351 | 349 | $index2 > 0 |
352 | 350 | && $table[$index1][$index2] === $table[$index1][$index2 - 1] |
353 | 351 | ) { |
354 | 352 | // update the diff and the indices |
355 | 353 | $diff[] = array($this->sequence2[$index2 + $start - 1], self::INSERTED); |
356 | 354 | $index2 --; |
357 | - } |
|
358 | - else |
|
355 | + } else |
|
359 | 356 | { |
360 | 357 | // update the diff and the indices |
361 | 358 | $diff[] = array($this->sequence1[$index1 + $start - 1], self::DELETED); |