Skip to content

Commit efc7e3b

Browse files
authored
Merge pull request #108 from JBlond/php-diff-97-2
Add statistics function for #97
2 parents ff0bb29 + 54c0e03 commit efc7e3b

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-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['equal']; ?><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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,38 @@ 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+
'replaced' => 0,
238+
];
239+
240+
foreach ($this->getGroupedOpCodes() as $chunk) {
241+
foreach ($chunk as [$string, $one, $two, $three, $four]) {
242+
switch ($string) {
243+
case 'delete':
244+
$return['deleted'] += $two - $one;
245+
break;
246+
case 'insert':
247+
$return['inserted'] += $four - $three;
248+
break;
249+
case 'replace':
250+
$return['replaced'] += $two - $one;
251+
break;
252+
}
253+
}
254+
}
255+
256+
$return['equal'] = count($this->old) - $return['replaced'] - $return['deleted'];
257+
258+
return $return;
259+
}
226260
}

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+
'equal' => 10,
46+
'replaced' => 0,
47+
],
48+
$similarity->getDifference()
49+
);
50+
}
3451
}

0 commit comments

Comments
 (0)