11
11
use jblond \Diff \Renderer \Text \Context ;
12
12
use jblond \Diff \Renderer \Text \Unified ;
13
13
use jblond \Diff \SequenceMatcher ;
14
+ use jblond \Diff \Similarity ;
14
15
use OutOfRangeException ;
15
16
16
17
/**
@@ -79,6 +80,10 @@ class Diff
79
80
* @var bool True when compared versions are identical, False otherwise.
80
81
*/
81
82
private $ identical ;
83
+ /**
84
+ * @var float Similarity ratio of the two sequences.
85
+ */
86
+ private $ similarity ;
82
87
83
88
/**
84
89
* The constructor.
@@ -92,9 +97,9 @@ class Diff
92
97
* When a keyName matches the name of a default option, that option's value will be overridden by the key's value.
93
98
* Any other keyName (and it's value) can be added as an option, but will not be used if not implemented.
94
99
*
95
- * @param string|array $version1 Data to compare to.
96
- * @param string|array $version2 Data to compare.
97
- * @param array $options User defined option values.
100
+ * @param string|array $version1 Data to compare to.
101
+ * @param string|array $version2 Data to compare.
102
+ * @param array $options User defined option values.
98
103
*
99
104
* @see Diff::$defaultOptions
100
105
*
@@ -116,7 +121,7 @@ public function __construct($version1, $version2, array $options = [])
116
121
* 0 If the type is 'array'
117
122
* 1 if the type is 'string'
118
123
*
119
- * @param mixed $var Variable to get type from.
124
+ * @param mixed $var Variable to get type from.
120
125
*
121
126
* @return int Number indicating the type of the variable. 0 for array type and 1 for string type.
122
127
* @throws InvalidArgumentException When the type isn't 'array' or 'string'.
@@ -137,7 +142,7 @@ public function getArgumentType($var): int
137
142
/**
138
143
* Set the options to be used by the sequence matcher, called by this class.
139
144
*
140
- * @param array $options User defined option names and values.
145
+ * @param array $options User defined option names and values.
141
146
*
142
147
* @see Diff::$defaultOptions
143
148
*
@@ -174,8 +179,8 @@ public function getVersion2(): array
174
179
/**
175
180
* Render a diff-view using a rendering class and get its results.
176
181
*
177
- * @param object|Context|Unified|UnifiedHtml|Inline|SideBySide $renderer An instance of the rendering object,
178
- * used for generating the diff-view.
182
+ * @param object|Context|Unified|UnifiedHtml|Inline|SideBySide $renderer An instance of the rendering object,
183
+ * used for generating the diff-view.
179
184
*
180
185
* @return mixed The generated diff-view. The type of the return value depends on the applied renderer.
181
186
*/
@@ -196,10 +201,10 @@ public function render(object $renderer)
196
201
* If the arguments for both parameters are omitted, the entire array will be returned.
197
202
* If the argument for the second parameter is omitted, the element defined as start will be returned.
198
203
*
199
- * @param array $array The source array.
200
- * @param int $start The first element of the range to get.
201
- * @param int|null $end The last element of the range to get.
202
- * If not supplied, only the element at start will be returned.
204
+ * @param array $array The source array.
205
+ * @param int $start The first element of the range to get.
206
+ * @param int|null $end The last element of the range to get.
207
+ * If not supplied, only the element at start will be returned.
203
208
*
204
209
* @return array Array containing all of the elements of the specified range.
205
210
* @throws OutOfRangeException When the value of start or end are invalid to define a range.
@@ -265,4 +270,25 @@ public function getGroupedOpCodes(): array
265
270
266
271
return $ this ->groupedCodes ;
267
272
}
273
+
274
+ /**
275
+ * Get the similarity ratio of the two sequences.
276
+ *
277
+ * Once calculated, the results are cached in the diff class instance.
278
+ *
279
+ * @param int $method Calculation method.
280
+ *
281
+ * @return float Similarity ratio.
282
+ */
283
+ public function getSimilarity ($ method = Similarity::CALC_DEFAULT ): float
284
+ {
285
+ if ($ this ->similarity !== null ) {
286
+ return $ this ->similarity ;
287
+ }
288
+
289
+ $ similarity = new Similarity ($ this ->version1 , $ this ->version2 , $ this ->options );
290
+ $ this ->similarity = $ similarity ->getSimilarity ($ method );
291
+
292
+ return $ this ->similarity ;
293
+ }
268
294
}
0 commit comments