Skip to content

Commit ed1f204

Browse files
committed
Add statistics function for #97
1 parent ff0bb29 commit ed1f204

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed

example/example.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,13 @@ function changeCSS(cssFile, cssLinkIndex) {
7272
<h2>Informational</h2>
7373
Between the two versions, there's a
7474
<?php
75+
$stats = $diff->getStatistics();
7576
echo round($diff->getSimilarity(), 2) * 100;
76-
?>% match.
77+
?>% match.<br>
78+
Inserted lines: <?php echo $stats['inserted']; ?><br>
79+
Deleted lines: <?php echo $stats['deleted']; ?><br>
80+
Not modified lines: <?php echo $stats['notModified']; ?><br>
81+
Lines with replacement: <?php echo $stats['replaced']; ?><br>
7782
</aside>
7883
<hr>
7984

lib/jblond/Diff.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,15 @@ public function getSimilarity(int $method = Similarity::CALC_DEFAULT): float
287287

288288
return $this->similarity;
289289
}
290+
291+
/**
292+
* Get diff statistics
293+
*
294+
* @return array
295+
*/
296+
public function getStatistics(): array
297+
{
298+
$similarity = new Similarity($this->version1, $this->version2, $this->options);
299+
return $similarity->getDifference();
300+
}
290301
}

lib/jblond/Diff/Similarity.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,39 @@ private function ratioReduce(int $sum, array $triple): int
223223
{
224224
return $sum + ($triple[count($triple) - 1]);
225225
}
226+
227+
/**
228+
* Get diff statistics
229+
*
230+
* @return array
231+
*/
232+
public function getDifference(): array
233+
{
234+
$return = [
235+
'inserted' => 0,
236+
'deleted' => 0,
237+
'notModified' => 0,
238+
'replaced' => 0,
239+
];
240+
241+
foreach ($this->getGroupedOpCodes() as $chunk) {
242+
foreach ($chunk as [$string, $one, $two, $three, $four]) {
243+
switch ($string) {
244+
case 'delete':
245+
$return['deleted'] += $two - $one;
246+
break;
247+
case 'insert':
248+
$return['inserted'] += $four - $three;
249+
break;
250+
case 'replace':
251+
$return['replaced'] += $two - $one;
252+
break;
253+
}
254+
}
255+
}
256+
257+
$return['notModified'] = count($this->old) - $return['replaced'] - $return['deleted'];
258+
259+
return $return;
260+
}
226261
}

tests/Diff/SequenceMatcherTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Tests\Diff;
44

5-
use jblond\Diff\ConstantsInterface;
65
use jblond\Diff\SequenceMatcher;
76
use PHPUnit\Framework\TestCase;
87

tests/Diff/SimilarityTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,21 @@ public function testGetSimilarity(): void
3131
$this->assertEquals(2 / 3, $similarity->getSimilarity(Similarity::CALC_FAST));
3232
$this->assertEquals(2 / 3, $similarity->getSimilarity(Similarity::CALC_FASTEST));
3333
}
34+
35+
/**
36+
* Test the statistics function
37+
*/
38+
public function testGetDifference(): void
39+
{
40+
$similarity = new Similarity(range(0, 10), range(1, 23));
41+
$this->assertEquals(
42+
[
43+
'inserted' => 13,
44+
'deleted' => 1,
45+
'notModified' => 10,
46+
'replaced' => 0,
47+
],
48+
$similarity->getDifference()
49+
);
50+
}
3451
}

0 commit comments

Comments
 (0)