Skip to content

Commit 0394a74

Browse files
committed
Tidy Codes
Signed-off-by: Jack Cherng <[email protected]>
1 parent 7093932 commit 0394a74

File tree

4 files changed

+137
-109
lines changed

4 files changed

+137
-109
lines changed

src/Renderer/Html/AbstractHtml.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function getResultForIdenticalsDefault(): string
4747
*
4848
* @param Differ $differ the differ object
4949
*
50-
* @return array an array of the generated changes, suitable for presentation in HTML
50+
* @return array generated changes, suitable for presentation in HTML
5151
*/
5252
public function getChanges(Differ $differ): array
5353
{
@@ -60,10 +60,10 @@ public function getChanges(Differ $differ): array
6060
$old = $differ->getOld();
6161
$new = $differ->getNew();
6262

63-
$changes = [];
63+
$hunks = [];
6464

6565
foreach ($differ->getGroupedOpcodes() as $opcodes) {
66-
$blocks = [];
66+
$hunk = [];
6767
$lastTag = SequenceMatcher::OP_NOP;
6868
$lastBlock = 0;
6969

@@ -78,8 +78,8 @@ public function getChanges(Differ $differ): array
7878
}
7979

8080
if ($tag !== $lastTag) {
81-
$blocks[] = $this->getDefaultBlock($tag, $i1, $j1);
82-
$lastBlock = \count($blocks) - 1;
81+
$hunk[] = $this->getDefaultBlock($tag, $i1, $j1);
82+
$lastBlock = \count($hunk) - 1;
8383
}
8484

8585
$lastTag = $tag;
@@ -89,9 +89,9 @@ public function getChanges(Differ $differ): array
8989
// the old and the new may not be exactly the same
9090
// because of ignoreCase, ignoreWhitespace, etc
9191
$lines = \array_slice($old, $i1, $i2 - $i1);
92-
$blocks[$lastBlock]['old']['lines'] += $this->formatLines($lines);
92+
$hunk[$lastBlock]['old']['lines'] += $this->formatLines($lines);
9393
$lines = \array_slice($new, $j1, $j2 - $j1);
94-
$blocks[$lastBlock]['new']['lines'] += $this->formatLines($lines);
94+
$hunk[$lastBlock]['new']['lines'] += $this->formatLines($lines);
9595

9696
continue;
9797
}
@@ -105,7 +105,7 @@ public function getChanges(Differ $differ): array
105105
$lines
106106
);
107107

108-
$blocks[$lastBlock]['old']['lines'] += $lines;
108+
$hunk[$lastBlock]['old']['lines'] += $lines;
109109
}
110110

111111
if ($tag & (SequenceMatcher::OP_REP | SequenceMatcher::OP_INS)) {
@@ -117,14 +117,14 @@ public function getChanges(Differ $differ): array
117117
$lines
118118
);
119119

120-
$blocks[$lastBlock]['new']['lines'] += $lines;
120+
$hunk[$lastBlock]['new']['lines'] += $lines;
121121
}
122122
}
123123

124-
$changes[] = $blocks;
124+
$hunks[] = $hunk;
125125
}
126126

127-
return $changes;
127+
return $hunks;
128128
}
129129

130130
/**
@@ -219,10 +219,12 @@ protected function formatLines(array $lines): array
219219
*/
220220
return \explode(
221221
RendererConstant::IMPLODE_DELIMITER,
222-
$this->formatStringFromLines(\implode(
223-
RendererConstant::IMPLODE_DELIMITER,
224-
$lines
225-
))
222+
$this->formatStringFromLines(
223+
\implode(
224+
RendererConstant::IMPLODE_DELIMITER,
225+
$lines
226+
)
227+
)
226228
);
227229
}
228230

src/Renderer/Html/Inline.php

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,11 @@ protected function redererChanges(array $changes): string
3333
['diff', 'diff-html', 'diff-inline']
3434
);
3535

36-
$html = '<table class="' . \implode(' ', $wrapperClasses) . '">';
37-
38-
$html .= $this->renderTableHeader();
39-
40-
foreach ($changes as $i => $blocks) {
41-
if ($i > 0 && $this->options['separateBlock']) {
42-
$html .= $this->renderTableSeparateBlock();
43-
}
44-
45-
foreach ($blocks as $change) {
46-
$html .= $this->renderTableBlock($change);
47-
}
48-
}
49-
50-
return $html . '</table>';
36+
return
37+
'<table class="' . \implode(' ', $wrapperClasses) . '">' .
38+
$this->renderTableHeader() .
39+
$this->renderTableHunks($changes) .
40+
'</table>';
5141
}
5242

5343
/**
@@ -89,49 +79,71 @@ protected function renderTableSeparateBlock(): string
8979
'</tbody>';
9080
}
9181

82+
/**
83+
* Renderer table hunks.
84+
*
85+
* @param array $hunks each hunk has many blocks
86+
*/
87+
protected function renderTableHunks(array $hunks): string
88+
{
89+
$html = '';
90+
91+
foreach ($hunks as $i => $hunk) {
92+
if ($i > 0 && $this->options['separateBlock']) {
93+
$html .= $this->renderTableSeparateBlock();
94+
}
95+
96+
foreach ($hunk as $block) {
97+
$html .= $this->renderTableBlock($block);
98+
}
99+
}
100+
101+
return $html;
102+
}
103+
92104
/**
93105
* Renderer the table block.
94106
*
95-
* @param array $change the change
107+
* @param array $block the block
96108
*/
97-
protected function renderTableBlock(array $change): string
109+
protected function renderTableBlock(array $block): string
98110
{
99111
static $callbacks = [
100-
SequenceMatcher::OP_EQ => 'renderTableEqual',
101-
SequenceMatcher::OP_INS => 'renderTableInsert',
102-
SequenceMatcher::OP_DEL => 'renderTableDelete',
103-
SequenceMatcher::OP_REP => 'renderTableReplace',
112+
SequenceMatcher::OP_EQ => 'renderTableBlockEqual',
113+
SequenceMatcher::OP_INS => 'renderTableBlockInsert',
114+
SequenceMatcher::OP_DEL => 'renderTableBlockDelete',
115+
SequenceMatcher::OP_REP => 'renderTableBlockReplace',
104116
];
105117

106118
return
107-
'<tbody class="change change-' . self::TAG_CLASS_MAP[$change['tag']] . '">' .
108-
$this->{$callbacks[$change['tag']]}($change) .
119+
'<tbody class="change change-' . self::TAG_CLASS_MAP[$block['tag']] . '">' .
120+
$this->{$callbacks[$block['tag']]}($block) .
109121
'</tbody>';
110122
}
111123

112124
/**
113125
* Renderer the table block: equal.
114126
*
115-
* @param array $change the change
127+
* @param array $block the block
116128
*/
117-
protected function renderTableEqual(array $change): string
129+
protected function renderTableBlockEqual(array $block): string
118130
{
119131
$html = '';
120132

121133
// note that although we are in a OP_EQ situation,
122134
// the old and the new may not be exactly the same
123135
// because of ignoreCase, ignoreWhitespace, etc
124-
foreach ($change['old']['lines'] as $no => $oldLine) {
125-
// hmm... but this is a inline renderer
126-
// we could only pick a line from the old or the new to show
127-
$oldLineNum = $change['old']['offset'] + $no + 1;
128-
$newLineNum = $change['new']['offset'] + $no + 1;
136+
foreach ($block['new']['lines'] as $no => $newLine) {
137+
// hmm... but there is only space for one line
138+
// we could only pick either the old or the new to show
139+
$oldLineNum = $block['old']['offset'] + $no + 1;
140+
$newLineNum = $block['new']['offset'] + $no + 1;
129141

130142
$html .=
131143
'<tr data-type="=">' .
132144
$this->renderLineNumberColumns($oldLineNum, $newLineNum) .
133145
'<th class="sign"></th>' .
134-
'<td class="old">' . $oldLine . '</td>' .
146+
'<td class="new">' . $newLine . '</td>' .
135147
'</tr>';
136148
}
137149

@@ -141,14 +153,14 @@ protected function renderTableEqual(array $change): string
141153
/**
142154
* Renderer the table block: insert.
143155
*
144-
* @param array $change the change
156+
* @param array $block the block
145157
*/
146-
protected function renderTableInsert(array $change): string
158+
protected function renderTableBlockInsert(array $block): string
147159
{
148160
$html = '';
149161

150-
foreach ($change['new']['lines'] as $no => $newLine) {
151-
$newLineNum = $change['new']['offset'] + $no + 1;
162+
foreach ($block['new']['lines'] as $no => $newLine) {
163+
$newLineNum = $block['new']['offset'] + $no + 1;
152164

153165
$html .=
154166
'<tr data-type="+">' .
@@ -164,14 +176,14 @@ protected function renderTableInsert(array $change): string
164176
/**
165177
* Renderer the table block: delete.
166178
*
167-
* @param array $change the change
179+
* @param array $block the block
168180
*/
169-
protected function renderTableDelete(array $change): string
181+
protected function renderTableBlockDelete(array $block): string
170182
{
171183
$html = '';
172184

173-
foreach ($change['old']['lines'] as $no => $oldLine) {
174-
$oldLineNum = $change['old']['offset'] + $no + 1;
185+
foreach ($block['old']['lines'] as $no => $oldLine) {
186+
$oldLineNum = $block['old']['offset'] + $no + 1;
175187

176188
$html .=
177189
'<tr data-type="-">' .
@@ -187,14 +199,14 @@ protected function renderTableDelete(array $change): string
187199
/**
188200
* Renderer the table block: replace.
189201
*
190-
* @param array $change the change
202+
* @param array $block the block
191203
*/
192-
protected function renderTableReplace(array $change): string
204+
protected function renderTableBlockReplace(array $block): string
193205
{
194206
$html = '';
195207

196-
foreach ($change['old']['lines'] as $no => $oldLine) {
197-
$oldLineNum = $change['old']['offset'] + $no + 1;
208+
foreach ($block['old']['lines'] as $no => $oldLine) {
209+
$oldLineNum = $block['old']['offset'] + $no + 1;
198210

199211
$html .=
200212
'<tr data-type="-">' .
@@ -204,8 +216,8 @@ protected function renderTableReplace(array $change): string
204216
'</tr>';
205217
}
206218

207-
foreach ($change['new']['lines'] as $no => $newLine) {
208-
$newLineNum = $change['new']['offset'] + $no + 1;
219+
foreach ($block['new']['lines'] as $no => $newLine) {
220+
$newLineNum = $block['new']['offset'] + $no + 1;
209221

210222
$html .=
211223
'<tr data-type="+">' .

0 commit comments

Comments
 (0)