Skip to content

Commit

Permalink
Merge pull request #78 from JBlond/similarity
Browse files Browse the repository at this point in the history
Add similarity calculation.
  • Loading branch information
JBlond authored Nov 18, 2020
2 parents aedefa8 + 3e4bbe6 commit 90c5d3d
Show file tree
Hide file tree
Showing 5 changed files with 540 additions and 321 deletions.
5 changes: 5 additions & 0 deletions example/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ function changeCSS(cssFile, cssLinkIndex) {
<a href="example.php?inlineMarking=4">None</a>
</aside>
<hr>
<aside>
<h2>Informational</h2>
Between the two versions, there's a <?php echo round($diff->getSimilarity(),2) * 100; ?>% match.
</aside>
<hr>

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

Expand Down
48 changes: 37 additions & 11 deletions lib/jblond/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use jblond\Diff\Renderer\Text\Context;
use jblond\Diff\Renderer\Text\Unified;
use jblond\Diff\SequenceMatcher;
use jblond\Diff\Similarity;
use OutOfRangeException;

/**
Expand Down Expand Up @@ -79,6 +80,10 @@ class Diff
* @var bool True when compared versions are identical, False otherwise.
*/
private $identical;
/**
* @var float Similarity ratio of the two sequences.
*/
private $similarity;

/**
* The constructor.
Expand All @@ -92,9 +97,9 @@ class Diff
* When a keyName matches the name of a default option, that option's value will be overridden by the key's value.
* Any other keyName (and it's value) can be added as an option, but will not be used if not implemented.
*
* @param string|array $version1 Data to compare to.
* @param string|array $version2 Data to compare.
* @param array $options User defined option values.
* @param string|array $version1 Data to compare to.
* @param string|array $version2 Data to compare.
* @param array $options User defined option values.
*
* @see Diff::$defaultOptions
*
Expand All @@ -116,7 +121,7 @@ public function __construct($version1, $version2, array $options = [])
* 0 If the type is 'array'
* 1 if the type is 'string'
*
* @param mixed $var Variable to get type from.
* @param mixed $var Variable to get type from.
*
* @return int Number indicating the type of the variable. 0 for array type and 1 for string type.
* @throws InvalidArgumentException When the type isn't 'array' or 'string'.
Expand All @@ -137,7 +142,7 @@ public function getArgumentType($var): int
/**
* Set the options to be used by the sequence matcher, called by this class.
*
* @param array $options User defined option names and values.
* @param array $options User defined option names and values.
*
* @see Diff::$defaultOptions
*
Expand Down Expand Up @@ -174,8 +179,8 @@ public function getVersion2(): array
/**
* Render a diff-view using a rendering class and get its results.
*
* @param object|Context|Unified|UnifiedHtml|Inline|SideBySide $renderer An instance of the rendering object,
* used for generating the diff-view.
* @param object|Context|Unified|UnifiedHtml|Inline|SideBySide $renderer An instance of the rendering object,
* used for generating the diff-view.
*
* @return mixed The generated diff-view. The type of the return value depends on the applied renderer.
*/
Expand All @@ -196,10 +201,10 @@ public function render(object $renderer)
* If the arguments for both parameters are omitted, the entire array will be returned.
* If the argument for the second parameter is omitted, the element defined as start will be returned.
*
* @param array $array The source array.
* @param int $start The first element of the range to get.
* @param int|null $end The last element of the range to get.
* If not supplied, only the element at start will be returned.
* @param array $array The source array.
* @param int $start The first element of the range to get.
* @param int|null $end The last element of the range to get.
* If not supplied, only the element at start will be returned.
*
* @return array Array containing all of the elements of the specified range.
* @throws OutOfRangeException When the value of start or end are invalid to define a range.
Expand Down Expand Up @@ -265,4 +270,25 @@ public function getGroupedOpCodes(): array

return $this->groupedCodes;
}

/**
* Get the similarity ratio of the two sequences.
*
* Once calculated, the results are cached in the diff class instance.
*
* @param int $method Calculation method.
*
* @return float Similarity ratio.
*/
public function getSimilarity($method = Similarity::CALC_DEFAULT): float
{
if ($this->similarity !== null) {
return $this->similarity;
}

$similarity = new Similarity($this->version1, $this->version2, $this->options);
$this->similarity = $similarity->getSimilarity($method);

return $this->similarity;
}
}
Loading

0 comments on commit 90c5d3d

Please sign in to comment.