Skip to content

Commit 36157a2

Browse files
Feat/Add getStatistics method to Diff class (#55)
1 parent e778ab3 commit 36157a2

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

src/Diff.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Overtrue\LaravelVersionable;
44

55
use Illuminate\Support\Arr;
6+
use Illuminate\Support\Collection;
7+
use Jfcherng\Diff\Differ;
68
use Jfcherng\Diff\DiffHelper;
79

810
/**
@@ -92,4 +94,34 @@ public function render(?string $renderer = null, array $differOptions = [], arra
9294

9395
return $diff;
9496
}
97+
98+
public function getStatistics(array $differOptions = []): array
99+
{
100+
if (empty($differOptions)) {
101+
$differOptions = $this->differOptions;
102+
}
103+
104+
$oldContents = $this->fromVersion->contents;
105+
$newContents = $this->toVersion->contents;
106+
107+
$diffStats = new Collection;
108+
109+
foreach ($oldContents as $key => $value) {
110+
if ($newContents[$key] !== $oldContents[$key]) {
111+
$diffStats->push(
112+
(new Differ(
113+
explode("\n", $newContents[$key]),
114+
explode("\n", $oldContents[$key]),
115+
))->getStatistics()
116+
);
117+
}
118+
}
119+
120+
return [
121+
'inserted' => $diffStats->sum('inserted'),
122+
'deleted' => $diffStats->sum('deleted'),
123+
'unmodified' => $diffStats->sum('unmodified'),
124+
'changedRatio' => $diffStats->sum('changedRatio'),
125+
];
126+
}
95127
}

tests/DiffTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,79 @@ public function test_diff_to_side_by_side_html()
140140
);
141141
}
142142

143+
/**
144+
* @test
145+
*/
146+
public function it_can_return_the_diff_statistics()
147+
{
148+
$old = new Version(['contents' => ['title' => 'example title', 'content' => 'example content']]);
149+
150+
// we are modifying everything
151+
$new = new Version(['contents' => ['title' => 'changing the title', 'content' => 'changing the content']]);
152+
153+
$stats = (new Diff($new, $old))->getStatistics();
154+
155+
$this->assertIsArray($stats);
156+
$this->assertArrayHasKey('inserted', $stats);
157+
$this->assertArrayHasKey('deleted', $stats);
158+
$this->assertArrayHasKey('unmodified', $stats);
159+
160+
$this->assertEquals(2, $stats['inserted']); // two new lines inserted
161+
$this->assertEquals(2, $stats['deleted']); // two lines deleted
162+
$this->assertEquals(0, $stats['unmodified']); // modified everything
163+
}
164+
165+
/**
166+
* @test
167+
*/
168+
public function it_can_return_correct_statistics_for_new_line_insertions()
169+
{
170+
$old = new Version(['contents' => ['title' => 'example title', 'content' => 'example content']]);
171+
172+
// we are adding two new lines
173+
$new = new Version(['contents' => ['content' => "example content\n adding new line \n another new line"]]);
174+
175+
$stats = (new Diff($new, $old))->getStatistics();
176+
177+
$this->assertEquals(2, $stats['inserted']); // two new lines inserted
178+
$this->assertEquals(0, $stats['deleted']); // no deletions
179+
$this->assertEquals(1, $stats['unmodified']); // one line unmodified
180+
}
181+
182+
/**
183+
* @test
184+
*/
185+
public function it_can_return_correct_statistics_for_deleted_lines()
186+
{
187+
$old = new Version(['contents' => ['title' => 'example title', 'content' => "example content\n adding new line \n another new line"]]);
188+
189+
// we are removing last two lines
190+
$new = new Version(['contents' => ['content' => 'example content']]);
191+
192+
$stats = (new Diff($new, $old))->getStatistics();
193+
194+
$this->assertEquals(0, $stats['inserted']); // no insertions
195+
$this->assertEquals(2, $stats['deleted']); // two lines deleted
196+
$this->assertEquals(1, $stats['unmodified']); // one line unmodified
197+
}
198+
199+
/**
200+
* @test
201+
*/
202+
public function it_can_return_correct_statistics_for_unmodified_lines()
203+
{
204+
$old = new Version(['contents' => ['title' => 'example title', 'content' => "example content\n adding new line \n another new line"]]);
205+
206+
// we are just removing the last line
207+
$new = new Version(['contents' => ['content' => "example content\n adding new line "]]);
208+
209+
$stats = (new Diff($new, $old))->getStatistics();
210+
211+
$this->assertEquals(0, $stats['inserted']); // no insertions
212+
$this->assertEquals(1, $stats['deleted']); // one line deleted
213+
$this->assertEquals(2, $stats['unmodified']); // two lines unmodified
214+
}
215+
143216
public function test_diff_nested_array_to_array()
144217
{
145218
$oldContent = [

0 commit comments

Comments
 (0)