Skip to content

Commit 90c5d3d

Browse files
authored
Merge pull request #78 from JBlond/similarity
Add similarity calculation.
2 parents aedefa8 + 3e4bbe6 commit 90c5d3d

File tree

5 files changed

+540
-321
lines changed

5 files changed

+540
-321
lines changed

example/example.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ function changeCSS(cssFile, cssLinkIndex) {
6868
<a href="example.php?inlineMarking=4">None</a>
6969
</aside>
7070
<hr>
71+
<aside>
72+
<h2>Informational</h2>
73+
Between the two versions, there's a <?php echo round($diff->getSimilarity(),2) * 100; ?>% match.
74+
</aside>
75+
<hr>
7176

7277
<h2>HTML Side by Side Diff</h2>
7378

lib/jblond/Diff.php

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use jblond\Diff\Renderer\Text\Context;
1212
use jblond\Diff\Renderer\Text\Unified;
1313
use jblond\Diff\SequenceMatcher;
14+
use jblond\Diff\Similarity;
1415
use OutOfRangeException;
1516

1617
/**
@@ -79,6 +80,10 @@ class Diff
7980
* @var bool True when compared versions are identical, False otherwise.
8081
*/
8182
private $identical;
83+
/**
84+
* @var float Similarity ratio of the two sequences.
85+
*/
86+
private $similarity;
8287

8388
/**
8489
* The constructor.
@@ -92,9 +97,9 @@ class Diff
9297
* When a keyName matches the name of a default option, that option's value will be overridden by the key's value.
9398
* Any other keyName (and it's value) can be added as an option, but will not be used if not implemented.
9499
*
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.
98103
*
99104
* @see Diff::$defaultOptions
100105
*
@@ -116,7 +121,7 @@ public function __construct($version1, $version2, array $options = [])
116121
* 0 If the type is 'array'
117122
* 1 if the type is 'string'
118123
*
119-
* @param mixed $var Variable to get type from.
124+
* @param mixed $var Variable to get type from.
120125
*
121126
* @return int Number indicating the type of the variable. 0 for array type and 1 for string type.
122127
* @throws InvalidArgumentException When the type isn't 'array' or 'string'.
@@ -137,7 +142,7 @@ public function getArgumentType($var): int
137142
/**
138143
* Set the options to be used by the sequence matcher, called by this class.
139144
*
140-
* @param array $options User defined option names and values.
145+
* @param array $options User defined option names and values.
141146
*
142147
* @see Diff::$defaultOptions
143148
*
@@ -174,8 +179,8 @@ public function getVersion2(): array
174179
/**
175180
* Render a diff-view using a rendering class and get its results.
176181
*
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.
179184
*
180185
* @return mixed The generated diff-view. The type of the return value depends on the applied renderer.
181186
*/
@@ -196,10 +201,10 @@ public function render(object $renderer)
196201
* If the arguments for both parameters are omitted, the entire array will be returned.
197202
* If the argument for the second parameter is omitted, the element defined as start will be returned.
198203
*
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.
203208
*
204209
* @return array Array containing all of the elements of the specified range.
205210
* @throws OutOfRangeException When the value of start or end are invalid to define a range.
@@ -265,4 +270,25 @@ public function getGroupedOpCodes(): array
265270

266271
return $this->groupedCodes;
267272
}
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+
}
268294
}

0 commit comments

Comments
 (0)