Skip to content

Commit 0a30b27

Browse files
committed
Renderer::render() rather than Diff::render()
A renderer renders, but a diff does not. Signed-off-by: Jack Cherng <jfcherng@gmail.com>
1 parent 37beef1 commit 0a30b27

File tree

12 files changed

+95
-49
lines changed

12 files changed

+95
-49
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ $result = DiffHelper::calculate($old, $new, $template);
9191
// custom usage
9292
$diff = new Diff(explode("\n", $old), explode("\n", $new), $diffOptions);
9393
$renderer = RendererFactory::make($template, $templateOptions); // or your own renderers
94-
$result = $diff->render($renderer);
94+
$result = $renderer->render($diff);
9595
```
9696

9797

UPGRADING/UPGRADING_v6.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Upgrading to v6
2+
3+
- Now `Renderer` has `::render()` API, but a `Diff` does not.
4+
If you are only using the `DiffHelper`, there should be nothing changed for you.
5+
But if you combine those classes by yourself, it should be wrote like below.
6+
7+
```php
8+
use Jfcherng\Diff\Diff;
9+
use Jfcherng\Diff\Factory\RendererFactory;
10+
11+
$diff = new Diff(explode("\n", $old), explode("\n", $new), $diffOptions);
12+
$renderer = RendererFactory::make($template, $templateOptions);
13+
$result = $renderer->render($diff); // <-- this has been changed
14+
```

src/Diff.php

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ final class Diff
2121
* @var array cached properties and their default values
2222
*/
2323
private const CACHED_PROPERTIES = [
24-
'groupedCodes' => null,
24+
'groupedCodes' => [],
25+
'oldNewComparison' => 0,
2526
];
2627

2728
/**
@@ -50,9 +51,14 @@ final class Diff
5051
private $sequenceMatcher;
5152

5253
/**
53-
* @var null|array array containing the generated opcodes for the differences between the two items
54+
* @var int the result of comparing the old and the new with the spaceship operator
5455
*/
55-
private $groupedCodes;
56+
private $oldNewComparison = 0;
57+
58+
/**
59+
* @var array array containing the generated opcodes for the differences between the two items
60+
*/
61+
private $groupedCodes = [];
5662

5763
/**
5864
* @var array associative array of the default options available for the diff class and their default value
@@ -186,6 +192,16 @@ public function getOptions(): array
186192
return $this->options;
187193
}
188194

195+
/**
196+
* Compare the old and the new with the spaceship operator.
197+
*
198+
* @return int
199+
*/
200+
public function getOldNewComparison(): int
201+
{
202+
return $this->oldNewComparison;
203+
}
204+
189205
/**
190206
* Get the singleton.
191207
*
@@ -208,45 +224,32 @@ public static function getInstance(): self
208224
*/
209225
public function getGroupedOpcodes(): array
210226
{
211-
$this->finalize();
227+
if (!empty($this->groupedCodes)) {
228+
return $this->groupedCodes;
229+
}
212230

213-
return $this->groupedCodes = $this->groupedCodes ??
214-
$this->sequenceMatcher->getGroupedOpcodes($this->options['context']);
231+
return $this->groupedCodes = $this->sequenceMatcher
232+
->getGroupedOpcodes($this->options['context']);
215233
}
216234

217235
/**
218-
* Render a diff using the supplied rendering class and return it.
236+
* Claim this class has settled down which means properties will not
237+
* be changed before doing diff calculations.
219238
*
220-
* @param AbstractRenderer $renderer an instance of the rendering object to use for generating the diff
239+
* Properties will be re-propagated to other classes. This method must be
240+
* re-called after any property changed before doing calculations.
221241
*
222-
* @return string the generated diff
223-
*/
224-
public function render(AbstractRenderer $renderer): string
225-
{
226-
$this->finalize();
227-
228-
$renderer->setDiff($this);
229-
230-
// the "no difference" situation may happen frequently
231-
// let's save some calculation if possible
232-
return $this->old === $this->new
233-
? $renderer::getIdenticalResult()
234-
: $renderer->render();
235-
}
236-
237-
/**
238-
* Claim this class is all set.
239-
*
240-
* Properties will be propagated to other classes. You must re-call
241-
* this method after any property changed before doing calculation.
242+
* This method is called in AbstractRenderer::render() automatically.
242243
*
243244
* @return self
244245
*/
245-
private function finalize(): self
246+
public function finalize(): self
246247
{
247248
if ($this->isCacheDirty) {
248249
$this->resetCachedResults();
249250

251+
$this->oldNewComparison = $this->old <=> $this->new;
252+
250253
$this->sequenceMatcher
251254
->setOptions($this->options)
252255
->setSequences($this->old, $this->new);

src/DiffHelper.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,13 @@ public static function calculate(
132132
\is_string($old) && ($old = \explode("\n", $old));
133133
\is_string($new) && ($new = \explode("\n", $new));
134134

135-
return Diff::getInstance()
136-
->setOldNew($old, $new)
137-
->setOptions($diffOptions)
138-
->render(RendererFactory::getInstance($template)->setOptions($templateOptions));
135+
return RendererFactory::getInstance($template)
136+
->setOptions($templateOptions)
137+
->render(
138+
Diff::getInstance()
139+
->setOldNew($old, $new)
140+
->setOptions($diffOptions)
141+
);
139142
}
140143

141144
/**

src/Renderer/AbstractRenderer.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,26 @@ public static function getIdenticalResult(): string
136136
return '';
137137
}
138138

139+
/**
140+
* {@inheritdoc}
141+
*/
142+
public function render(Diff $diff): string
143+
{
144+
$this->diff = $diff->finalize();
145+
146+
// the "no difference" situation may happen frequently
147+
return $this->diff->getOldNewComparison() === 0
148+
? static::getIdenticalResult()
149+
: $this->renderWoker();
150+
}
151+
152+
/**
153+
* The real worker for self::render().
154+
*
155+
* @return string
156+
*/
157+
abstract protected function renderWoker(): string;
158+
139159
/**
140160
* Update the Language object.
141161
*

src/Renderer/Html/Inline.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final class Inline extends AbstractHtml
2222
/**
2323
* {@inheritdoc}
2424
*/
25-
public function render(): string
25+
protected function renderWoker(): string
2626
{
2727
$changes = $this->getChanges();
2828

src/Renderer/Html/Json.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ final class Json extends AbstractHtml
2727
/**
2828
* {@inheritdoc}
2929
*/
30-
public function render(): string
30+
public static function getIdenticalResult(): string
31+
{
32+
return '[]';
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
protected function renderWoker(): string
3139
{
3240
$changes = $this->getChanges();
3341

@@ -41,14 +49,6 @@ public function render(): string
4149
);
4250
}
4351

44-
/**
45-
* {@inheritdoc}
46-
*/
47-
public static function getIdenticalResult(): string
48-
{
49-
return '[]';
50-
}
51-
5252
/**
5353
* Convert tags of changes to their string form for better readability.
5454
*

src/Renderer/Html/SideBySide.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final class SideBySide extends AbstractHtml
2222
/**
2323
* {@inheritdoc}
2424
*/
25-
public function render(): string
25+
protected function renderWoker(): string
2626
{
2727
$changes = $this->getChanges();
2828

src/Renderer/RendererInterface.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Jfcherng\Diff\Renderer;
66

7+
use Jfcherng\Diff\Diff;
8+
79
/**
810
* Renderer Interface.
911
*/
@@ -12,7 +14,9 @@ interface RendererInterface
1214
/**
1315
* Render and return diff.
1416
*
17+
* @param Diff $diff the diff object to be rendered
18+
*
1519
* @return string
1620
*/
17-
public function render(): string;
21+
public function render(Diff $diff): string;
1822
}

src/Renderer/Text/Context.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ final class Context extends AbstractText
3434
/**
3535
* {@inheritdoc}
3636
*/
37-
public function render(): string
37+
protected function renderWoker(): string
3838
{
3939
$ret = '';
4040

0 commit comments

Comments
 (0)