Skip to content

Commit ba570f8

Browse files
committed
Make Differ work in a DI way in Renderer internally
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
1 parent 177146e commit ba570f8

File tree

9 files changed

+63
-61
lines changed

9 files changed

+63
-61
lines changed

UPGRADING/UPGRADING_v6.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
## Upgrading to v6
22

3+
4+
### For Simple User
5+
6+
If you only use the `DiffHelper` and built-in `Renderer`s,
7+
there is no breaking change for you so you do not have to do anything.
8+
9+
10+
### External Breaking Changes
11+
312
- `Diff` has been renamed to `Differ`.
13+
It's easy to adapt to this by changing the class name.
14+
415
- Now a `Renderer` has `render()` API, but a `Differ` does not.
5-
If you are only using the `DiffHelper`, there should be nothing changed for you.
6-
But if you combine those classes by yourself, it should be wrote like below.
16+
But if you use those classes by yourself, it should be written like below.
717

818
```php
919
use Jfcherng\Diff\Differ;
@@ -13,3 +23,13 @@
1323
$renderer = RendererFactory::make($template, $templateOptions);
1424
$result = $renderer->render($differ); // <-- this has been changed
1525
```
26+
27+
- If you call `Differ::getGroupedOpcodes()` by yourself,
28+
you must call `Differ::finalize()` before it.
29+
30+
31+
### Internal Breaking Changes
32+
33+
- Now a `Renderer` should implement `protected function renderWoker(Differ $differ): string`
34+
rather than previous `public function render(): string`. Note that `$this->diff` no longer
35+
works in `Renderer`s as they are now injected as the parameter of `Renderer::renderWoker()`.

src/Differ.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ final class Differ
5252

5353
/**
5454
* @var int the result of comparing the old and the new with the spaceship operator
55+
* -1 means old < new, 0 means old == new, 1 means old > new
5556
*/
5657
private $oldNewComparison = 0;
5758

src/Renderer/AbstractRenderer.php

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ abstract class AbstractRenderer implements RendererInterface
2525
*/
2626
const IS_HTML_TEMPLATE = true;
2727

28-
/**
29-
* @var Differ the instance of the Differ class that this renderer is generating the rendered diff for
30-
*/
31-
protected $differ;
32-
3328
/**
3429
* @var Language the language translation object
3530
*/
@@ -72,20 +67,6 @@ public function __construct(array $options = [])
7267
$this->setOptions($options);
7368
}
7469

75-
/**
76-
* Set the Differ object.
77-
*
78-
* @param Differ $differ the Differ object
79-
*
80-
* @return self
81-
*/
82-
public function setDiff(Differ $differ): self
83-
{
84-
$this->differ = $differ;
85-
86-
return $this;
87-
}
88-
8970
/**
9071
* Set the options of the renderer to those supplied in the passed in array.
9172
* Options are merged with the default to ensure that there aren't any missing
@@ -106,16 +87,6 @@ public function setOptions(array $options): self
10687
return $this;
10788
}
10889

109-
/**
110-
* Get the Differ object.
111-
*
112-
* @return Differ the Differ object
113-
*/
114-
public function getDiffer(): Differ
115-
{
116-
return $this->differ;
117-
}
118-
11990
/**
12091
* Get the options.
12192
*
@@ -141,20 +112,22 @@ public static function getIdenticalResult(): string
141112
*/
142113
public function render(Differ $differ): string
143114
{
144-
$this->differ = $differ->finalize();
115+
$differ->finalize();
145116

146117
// the "no difference" situation may happen frequently
147-
return $this->differ->getOldNewComparison() === 0
118+
return $differ->getOldNewComparison() === 0
148119
? static::getIdenticalResult()
149-
: $this->renderWoker();
120+
: $this->renderWoker($differ);
150121
}
151122

152123
/**
153124
* The real worker for self::render().
154125
*
126+
* @param Differ $differ the differ object
127+
*
155128
* @return string
156129
*/
157-
abstract protected function renderWoker(): string;
130+
abstract protected function renderWoker(Differ $differ): string;
158131

159132
/**
160133
* Update the Language object.

src/Renderer/Html/AbstractHtml.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Jfcherng\Diff\Renderer\Html;
66

7+
use Jfcherng\Diff\Differ;
78
use Jfcherng\Diff\Factory\LineRendererFactory;
89
use Jfcherng\Diff\Renderer\AbstractRenderer;
910
use Jfcherng\Diff\Renderer\Html\LineRenderer\AbstractLineRenderer;
@@ -38,23 +39,23 @@ abstract class AbstractHtml extends AbstractRenderer
3839
*
3940
* @return array an array of the generated changes, suitable for presentation in HTML
4041
*/
41-
public function getChanges(): array
42+
public function getChanges(Differ $differ): array
4243
{
4344
$lineRenderer = LineRendererFactory::make(
4445
$this->options['detailLevel'],
45-
$this->differ->getOptions(),
46+
$differ->getOptions(),
4647
$this->options
4748
);
4849

4950
// As we'll be modifying old & new to include our change markers,
5051
// we need to get the contents and store them here. That way
5152
// we're not going to destroy the original data
52-
$old = $this->differ->getOld();
53-
$new = $this->differ->getNew();
53+
$old = $differ->getOld();
54+
$new = $differ->getNew();
5455

5556
$changes = [];
5657

57-
foreach ($this->differ->getGroupedOpcodes() as $opcodes) {
58+
foreach ($differ->getGroupedOpcodes() as $opcodes) {
5859
$blocks = [];
5960
$lastTag = SequenceMatcher::OP_NOP;
6061
$lastBlock = 0;

src/Renderer/Html/Inline.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Jfcherng\Diff\Renderer\Html;
66

7+
use Jfcherng\Diff\Differ;
78
use Jfcherng\Diff\SequenceMatcher;
89

910
/**
@@ -22,9 +23,9 @@ final class Inline extends AbstractHtml
2223
/**
2324
* {@inheritdoc}
2425
*/
25-
protected function renderWoker(): string
26+
protected function renderWoker(Differ $differ): string
2627
{
27-
$changes = $this->getChanges();
28+
$changes = $this->getChanges($differ);
2829

2930
if (empty($changes)) {
3031
return self::getIdenticalResult();

src/Renderer/Html/Json.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Jfcherng\Diff\Renderer\Html;
66

7+
use Jfcherng\Diff\Differ;
78
use Jfcherng\Diff\SequenceMatcher;
89

910
/**
@@ -35,9 +36,9 @@ public static function getIdenticalResult(): string
3536
/**
3637
* {@inheritdoc}
3738
*/
38-
protected function renderWoker(): string
39+
protected function renderWoker(Differ $differ): string
3940
{
40-
$changes = $this->getChanges();
41+
$changes = $this->getChanges($differ);
4142

4243
if ($this->options['outputTagAsString']) {
4344
$this->convertTagToString($changes);

src/Renderer/Html/SideBySide.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Jfcherng\Diff\Renderer\Html;
66

7+
use Jfcherng\Diff\Differ;
78
use Jfcherng\Diff\SequenceMatcher;
89

910
/**
@@ -22,9 +23,9 @@ final class SideBySide extends AbstractHtml
2223
/**
2324
* {@inheritdoc}
2425
*/
25-
protected function renderWoker(): string
26+
protected function renderWoker(Differ $differ): string
2627
{
27-
$changes = $this->getChanges();
28+
$changes = $this->getChanges($differ);
2829

2930
if (empty($changes)) {
3031
return self::getIdenticalResult();

src/Renderer/Text/Context.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Jfcherng\Diff\Renderer\Text;
66

7+
use Jfcherng\Diff\Differ;
78
use Jfcherng\Diff\SequenceMatcher;
89

910
/**
@@ -34,11 +35,11 @@ final class Context extends AbstractText
3435
/**
3536
* {@inheritdoc}
3637
*/
37-
protected function renderWoker(): string
38+
protected function renderWoker(Differ $differ): string
3839
{
3940
$ret = '';
4041

41-
foreach ($this->differ->getGroupedOpcodes() as $opcodes) {
42+
foreach ($differ->getGroupedOpcodes() as $opcodes) {
4243
$lastItem = \count($opcodes) - 1;
4344

4445
$i1 = $opcodes[0][1];
@@ -49,9 +50,9 @@ protected function renderWoker(): string
4950
$ret .=
5051
"***************\n" .
5152
$this->renderBlockHeader('*', $i1, $i2) .
52-
$this->renderBlockOld($opcodes) .
53+
$this->renderBlockOld($opcodes, $differ) .
5354
$this->renderBlockHeader('-', $j1, $j2) .
54-
$this->renderBlockNew($opcodes);
55+
$this->renderBlockNew($opcodes, $differ);
5556
}
5657

5758
return $ret;
@@ -77,11 +78,12 @@ protected function renderBlockHeader(string $delimiter, int $a1, int $a2): strin
7778
/**
7879
* Render the old block.
7980
*
80-
* @param array $opcodes the opcodes
81+
* @param array $opcodes the opcodes
82+
* @param Differ $differ the differ object
8183
*
8284
* @return string
8385
*/
84-
protected function renderBlockOld(array $opcodes): string
86+
protected function renderBlockOld(array $opcodes, Differ $differ): string
8587
{
8688
$ret = '';
8789

@@ -92,7 +94,7 @@ protected function renderBlockOld(array $opcodes): string
9294

9395
$ret .= $this->renderContext(
9496
self::TAG_MAP[$tag],
95-
$this->differ->getOld($i1, $i2)
97+
$differ->getOld($i1, $i2)
9698
);
9799
}
98100

@@ -102,11 +104,12 @@ protected function renderBlockOld(array $opcodes): string
102104
/**
103105
* Render the new block.
104106
*
105-
* @param array $opcodes the opcodes
107+
* @param array $opcodes the opcodes
108+
* @param Differ $differ the differ object
106109
*
107110
* @return string
108111
*/
109-
protected function renderBlockNew(array $opcodes): string
112+
protected function renderBlockNew(array $opcodes, Differ $differ): string
110113
{
111114
$ret = '';
112115

@@ -117,7 +120,7 @@ protected function renderBlockNew(array $opcodes): string
117120

118121
$ret .= $this->renderContext(
119122
self::TAG_MAP[$tag],
120-
$this->differ->getNew($j1, $j2)
123+
$differ->getNew($j1, $j2)
121124
);
122125
}
123126

src/Renderer/Text/Unified.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Jfcherng\Diff\Renderer\Text;
66

7+
use Jfcherng\Diff\Differ;
78
use Jfcherng\Diff\SequenceMatcher;
89

910
/**
@@ -24,11 +25,11 @@ final class Unified extends AbstractText
2425
/**
2526
* {@inheritdoc}
2627
*/
27-
protected function renderWoker(): string
28+
protected function renderWoker(Differ $differ): string
2829
{
2930
$ret = '';
3031

31-
foreach ($this->differ->getGroupedOpcodes() as $opcodes) {
32+
foreach ($differ->getGroupedOpcodes() as $opcodes) {
3233
$lastItem = \count($opcodes) - 1;
3334

3435
$i1 = $opcodes[0][1];
@@ -44,17 +45,17 @@ protected function renderWoker(): string
4445

4546
foreach ($opcodes as [$tag, $i1, $i2, $j1, $j2]) {
4647
if ($tag === SequenceMatcher::OP_EQ) {
47-
$ret .= $this->renderContext(' ', $this->differ->getOld($i1, $i2));
48+
$ret .= $this->renderContext(' ', $differ->getOld($i1, $i2));
4849

4950
continue;
5051
}
5152

5253
if ($tag & (SequenceMatcher::OP_REP | SequenceMatcher::OP_DEL)) {
53-
$ret .= $this->renderContext('-', $this->differ->getOld($i1, $i2));
54+
$ret .= $this->renderContext('-', $differ->getOld($i1, $i2));
5455
}
5556

5657
if ($tag & (SequenceMatcher::OP_REP | SequenceMatcher::OP_INS)) {
57-
$ret .= $this->renderContext('+', $this->differ->getNew($j1, $j2));
58+
$ret .= $this->renderContext('+', $differ->getNew($j1, $j2));
5859
}
5960
}
6061
}

0 commit comments

Comments
 (0)