Skip to content

Commit f497539

Browse files
committed
Tidy codes
Signed-off-by: Jack Cherng <[email protected]>
1 parent caec927 commit f497539

File tree

6 files changed

+66
-89
lines changed

6 files changed

+66
-89
lines changed

src/Renderer/AbstractRenderer.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -154,24 +154,13 @@ final public function renderArray(array $differArray): string
154154
* @param Differ $differ the differ object
155155
*/
156156
abstract protected function renderWoker(Differ $differ): string;
157-
157+
158158
/**
159-
* The worker for array render.
159+
* The real worker for self::renderArray().
160160
*
161161
* @param array $differArray the differ array
162-
*
163-
* @return string
164162
*/
165163
abstract protected function renderArrayWoker(array $differArray): string;
166-
167-
/**
168-
* Woker's base function.
169-
*
170-
* @param array $changes the changes array
171-
*
172-
* @return string
173-
*/
174-
abstract protected function baseWoker(array $changes): string;
175164

176165
/**
177166
* Update the Language object.

src/Renderer/Html/AbstractHtml.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,29 @@ public function getChanges(Differ $differ): array
127127
return $changes;
128128
}
129129

130+
/**
131+
* {@inheritdoc}
132+
*/
133+
protected function renderWoker(Differ $differ): string
134+
{
135+
return $this->redererChanges($this->getChanges($differ));
136+
}
137+
138+
/**
139+
* {@inheritdoc}
140+
*/
141+
protected function renderArrayWoker(array $differArray): string
142+
{
143+
return $this->redererChanges($this->ensureChangesUseIntTag($differArray));
144+
}
145+
146+
/**
147+
* Render the array of changes.
148+
*
149+
* @param array $changes the changes
150+
*/
151+
abstract protected function redererChanges(array $changes): string;
152+
130153
/**
131154
* Renderer the changed extent.
132155
*
@@ -284,4 +307,40 @@ function (array $matches): string {
284307
$string
285308
);
286309
}
310+
311+
/**
312+
* Make sure the "changes" array uses int "tag".
313+
*
314+
* Internally, we would like always int form for better performance.
315+
*
316+
* @param array $changes the changes
317+
*/
318+
protected function ensureChangesUseIntTag(array $changes): array
319+
{
320+
if (empty($changes)) {
321+
return [];
322+
}
323+
324+
$isTagInt = true;
325+
foreach ($changes as $blocks) {
326+
foreach ($blocks as $change) {
327+
$isTagInt = \is_int($change['tag']);
328+
329+
break 2;
330+
}
331+
}
332+
333+
if (!$isTagInt) {
334+
// convert string tags into their int forms
335+
foreach ($changes as &$blocks) {
336+
foreach ($blocks as &$change) {
337+
$change['tag'] = SequenceMatcher::opStrToInt($change['tag']);
338+
}
339+
}
340+
341+
unset($blocks, $change);
342+
}
343+
344+
return $changes;
345+
}
287346
}

src/Renderer/Html/Inline.php

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

55
namespace Jfcherng\Diff\Renderer\Html;
66

7-
use Jfcherng\Diff\Differ;
87
use Jfcherng\Diff\SequenceMatcher;
98

109
/**
@@ -23,32 +22,12 @@ final class Inline extends AbstractHtml
2322
/**
2423
* {@inheritdoc}
2524
*/
26-
protected function renderWoker(Differ $differ): string
27-
{
28-
$changes = $this->getChanges($differ);
29-
30-
return $this->baseWoker($changes);
31-
}
32-
33-
/**
34-
* {@inheritdoc}
35-
*/
36-
protected function renderArrayWoker(array $differArray): string
37-
{
38-
$changes = $differArray;
39-
40-
return $this->baseWoker($changes);
41-
}
42-
43-
/**
44-
* {@inheritdoc}
45-
*/
46-
protected function baseWoker(array $changes): string
25+
protected function redererChanges(array $changes): string
4726
{
4827
if (empty($changes)) {
4928
return $this->getResultForIdenticals();
5029
}
51-
30+
5231
$wrapperClasses = \array_merge(
5332
$this->options['wrapperClasses'],
5433
['diff', 'diff-html', 'diff-inline']

src/Renderer/Html/Json.php

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Jfcherng\Diff\Renderer\Html;
66

7-
use Jfcherng\Diff\Differ;
87
use Jfcherng\Diff\SequenceMatcher;
98

109
/**
@@ -36,10 +35,8 @@ public function getResultForIdenticalsDefault(): string
3635
/**
3736
* {@inheritdoc}
3837
*/
39-
protected function renderWoker(Differ $differ): string
38+
protected function redererChanges(array $changes): string
4039
{
41-
$changes = $this->getChanges($differ);
42-
4340
if ($this->options['outputTagAsString']) {
4441
$this->convertTagToString($changes);
4542
}
@@ -50,22 +47,6 @@ protected function renderWoker(Differ $differ): string
5047
);
5148
}
5249

53-
/**
54-
* {@inheritdoc}
55-
*/
56-
public function renderArrayWoker(array $differArray): string
57-
{
58-
return '';
59-
}
60-
61-
/**
62-
* {@inheritdoc}
63-
*/
64-
public function baseWoker(array $changes): string
65-
{
66-
return '';
67-
}
68-
6950
/**
7051
* Convert tags of changes to their string form for better readability.
7152
*

src/Renderer/Html/SideBySide.php

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Jfcherng\Diff\Renderer\Html;
66

7-
use Jfcherng\Diff\Differ;
87
use Jfcherng\Diff\SequenceMatcher;
98

109
/**
@@ -23,27 +22,7 @@ final class SideBySide extends AbstractHtml
2322
/**
2423
* {@inheritdoc}
2524
*/
26-
protected function renderWoker(Differ $differ): string
27-
{
28-
$changes = $this->getChanges($differ);
29-
30-
return $this->baseWoker($changes);
31-
}
32-
33-
/**
34-
* {@inheritdoc}
35-
*/
36-
protected function renderArrayWoker(array $differArray): string
37-
{
38-
$changes = $differArray;
39-
40-
return $this->baseWoker($changes);
41-
}
42-
43-
/**
44-
* {@inheritdoc}
45-
*/
46-
protected function baseWoker(array $changes): string
25+
protected function redererChanges(array $changes): string
4726
{
4827
if (empty($changes)) {
4928
return $this->getResultForIdenticals();

src/Renderer/Text/AbstractText.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,6 @@ public function renderArrayWoker(array $differArray): string
3232
{
3333
throw new UnsupportedFunctionException(__METHOD__);
3434

35-
return '';
36-
}
37-
38-
/**
39-
* {@inheritdoc}
40-
*/
41-
public function baseWoker(array $changes): string
42-
{
43-
throw new UnsupportedFunctionException(__METHOD__);
44-
45-
return '';
35+
return ''; // make IDE not complain
4636
}
4737
}

0 commit comments

Comments
 (0)