Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
3.45% |
1 / 29 |
|
14.29% |
1 / 7 |
CRAP | |
0.00% |
0 / 1 |
| Diff | |
3.57% |
1 / 28 |
|
14.29% |
1 / 7 |
245.54 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getEdits | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| reverse | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| isEmpty | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| lcs | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| orig | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| closing | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3) |
| 4 | * |
| 5 | * Copyright © 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org> |
| 6 | * You may copy this code freely under the conditions of the GPL. |
| 7 | * |
| 8 | * @license GPL-2.0-or-later |
| 9 | * @file |
| 10 | * @ingroup DifferenceEngine |
| 11 | */ |
| 12 | |
| 13 | namespace Wikimedia\Diff; |
| 14 | |
| 15 | /** |
| 16 | * Class representing a 'diff' between two sequences of strings. |
| 17 | * @newable |
| 18 | * @ingroup DifferenceEngine |
| 19 | */ |
| 20 | class Diff { |
| 21 | |
| 22 | /** |
| 23 | * @var DiffOp[] |
| 24 | */ |
| 25 | public $edits; |
| 26 | |
| 27 | /** |
| 28 | * @var int If this diff complexity is exceeded, a ComplexityException is thrown |
| 29 | * 0 means no limit. |
| 30 | */ |
| 31 | protected $bailoutComplexity = 0; |
| 32 | |
| 33 | /** |
| 34 | * Computes diff between sequences of strings. |
| 35 | * |
| 36 | * @stable to call |
| 37 | * @todo Don't do work in the constructor, use a service to create diffs instead (T257472). |
| 38 | * |
| 39 | * @param string[] $from_lines An array of strings. |
| 40 | * Typically these are lines from a file. |
| 41 | * @param string[] $to_lines An array of strings. |
| 42 | * @throws ComplexityException |
| 43 | */ |
| 44 | public function __construct( $from_lines, $to_lines ) { |
| 45 | $eng = new DiffEngine; |
| 46 | $eng->setBailoutComplexity( $this->bailoutComplexity ); |
| 47 | $this->edits = $eng->diff( $from_lines, $to_lines ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @return DiffOp[] |
| 52 | */ |
| 53 | public function getEdits() { |
| 54 | return $this->edits; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Compute reversed Diff. |
| 59 | * |
| 60 | * SYNOPSIS: |
| 61 | * |
| 62 | * $diff = new Diff($lines1, $lines2); |
| 63 | * $rev = $diff->reverse(); |
| 64 | * |
| 65 | * @return self A Diff object representing the inverse of the |
| 66 | * original diff. |
| 67 | */ |
| 68 | public function reverse() { |
| 69 | $rev = $this; |
| 70 | $rev->edits = []; |
| 71 | /** @var DiffOp $edit */ |
| 72 | foreach ( $this->edits as $edit ) { |
| 73 | $rev->edits[] = $edit->reverse(); |
| 74 | } |
| 75 | |
| 76 | return $rev; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Check for empty diff. |
| 81 | * |
| 82 | * @return bool True if two sequences were identical. |
| 83 | */ |
| 84 | public function isEmpty() { |
| 85 | foreach ( $this->edits as $edit ) { |
| 86 | if ( $edit->type != 'copy' ) { |
| 87 | return false; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Compute the length of the Longest Common Subsequence (LCS). |
| 96 | * |
| 97 | * This is mostly for diagnostic purposed. |
| 98 | * |
| 99 | * @return int The length of the LCS. |
| 100 | */ |
| 101 | public function lcs() { |
| 102 | $lcs = 0; |
| 103 | foreach ( $this->edits as $edit ) { |
| 104 | if ( $edit->type == 'copy' ) { |
| 105 | $lcs += count( $edit->orig ); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return $lcs; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Get the original set of lines. |
| 114 | * |
| 115 | * This reconstructs the $from_lines parameter passed to the |
| 116 | * constructor. |
| 117 | * |
| 118 | * @return string[] The original sequence of strings. |
| 119 | */ |
| 120 | public function orig() { |
| 121 | $lines = []; |
| 122 | |
| 123 | foreach ( $this->edits as $edit ) { |
| 124 | if ( $edit->orig ) { |
| 125 | array_splice( $lines, count( $lines ), 0, $edit->orig ); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return $lines; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Get the closing set of lines. |
| 134 | * |
| 135 | * This reconstructs the $to_lines parameter passed to the |
| 136 | * constructor. |
| 137 | * |
| 138 | * @return string[] The sequence of strings. |
| 139 | */ |
| 140 | public function closing() { |
| 141 | $lines = []; |
| 142 | |
| 143 | foreach ( $this->edits as $edit ) { |
| 144 | if ( $edit->closing ) { |
| 145 | array_splice( $lines, count( $lines ), 0, $edit->closing ); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return $lines; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** @deprecated class alias since 1.41 */ |
| 154 | class_alias( Diff::class, 'Diff' ); |