Skip to content

Commit 802a7cb

Browse files
committed
Tidy codes
Signed-off-by: Jack Cherng <[email protected]>
1 parent 52045f3 commit 802a7cb

File tree

2 files changed

+69
-74
lines changed

2 files changed

+69
-74
lines changed

src/Renderer/Html/Inline.php

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,19 @@ protected function redererChanges(array $changes): string
5555
*/
5656
protected function renderTableHeader(): string
5757
{
58-
$colspan = (!$this->options['lineNumbers'] ? ' colspan="2"' : '');
58+
$colspan = $this->options['lineNumbers'] ? '' : ' colspan="2"';
5959

6060
return
6161
'<thead>' .
6262
'<tr>' .
63-
($this->options['lineNumbers'] ?
64-
'<th>' . $this->_('old_version') . '</th>' .
65-
'<th>' . $this->_('new_version') . '</th>' .
66-
'<th></th>'
67-
: ''
63+
(
64+
$this->options['lineNumbers']
65+
?
66+
'<th>' . $this->_('old_version') . '</th>' .
67+
'<th>' . $this->_('new_version') . '</th>' .
68+
'<th></th>'
69+
:
70+
''
6871
) .
6972
'<th' . $colspan . '>' . $this->_('differences') . '</th>' .
7073
'</tr>' .
@@ -76,7 +79,7 @@ protected function renderTableHeader(): string
7679
*/
7780
protected function renderTableSeparateBlock(): string
7881
{
79-
$colspan = (!$this->options['lineNumbers'] ? '2' : '4');
82+
$colspan = $this->options['lineNumbers'] ? '4' : '2';
8083

8184
return
8285
'<tbody class="skipped">' .
@@ -126,11 +129,7 @@ protected function renderTableEqual(array $change): string
126129

127130
$html .=
128131
'<tr data-type="=">' .
129-
($this->options['lineNumbers'] ?
130-
'<th class="n-old">' . $oldLineNum . '</th>' .
131-
'<th class="n-old">' . $newLineNum . '</th>'
132-
: ''
133-
) .
132+
$this->renderLineNumberColumns($oldLineNum, $newLineNum) .
134133
'<th class="sign"></th>' .
135134
'<td class="old">' . $oldLine . '</td>' .
136135
'</tr>';
@@ -153,11 +152,7 @@ protected function renderTableInsert(array $change): string
153152

154153
$html .=
155154
'<tr data-type="+">' .
156-
($this->options['lineNumbers'] ?
157-
'<th></th>' .
158-
'<th class="n-new">' . $newLineNum . '</th>'
159-
: ''
160-
) .
155+
$this->renderLineNumberColumns(null, $newLineNum) .
161156
'<th class="sign ins">+</th>' .
162157
'<td class="new">' . $newLine . '</td>' .
163158
'</tr>';
@@ -180,11 +175,7 @@ protected function renderTableDelete(array $change): string
180175

181176
$html .=
182177
'<tr data-type="-">' .
183-
($this->options['lineNumbers'] ?
184-
'<th class="n-old">' . $oldLineNum . '</th>' .
185-
'<th></th>'
186-
: ''
187-
) .
178+
$this->renderLineNumberColumns($oldLineNum, null) .
188179
'<th class="sign del">-</th>' .
189180
'<td class="old">' . $oldLine . '</td>' .
190181
'</tr>';
@@ -207,11 +198,7 @@ protected function renderTableReplace(array $change): string
207198

208199
$html .=
209200
'<tr data-type="-">' .
210-
($this->options['lineNumbers'] ?
211-
'<th class="n-old">' . $oldLineNum . '</th>' .
212-
'<th></th>'
213-
: ''
214-
) .
201+
$this->renderLineNumberColumns($oldLineNum, null) .
215202
'<th class="sign del">-</th>' .
216203
'<td class="old">' . $oldLine . '</td>' .
217204
'</tr>';
@@ -222,16 +209,37 @@ protected function renderTableReplace(array $change): string
222209

223210
$html .=
224211
'<tr data-type="+">' .
225-
($this->options['lineNumbers'] ?
226-
'<th></th>' .
227-
'<th class="n-new">' . $newLineNum . '</th>'
228-
: ''
229-
) .
212+
$this->renderLineNumberColumns(null, $newLineNum) .
230213
'<th class="sign ins">+</th>' .
231214
'<td class="new">' . $newLine . '</td>' .
232215
'</tr>';
233216
}
234217

235218
return $html;
236219
}
220+
221+
/**
222+
* Renderer the line number columns.
223+
*
224+
* @param null|int $oldLineNum The old line number
225+
* @param null|int $newLineNum The new line number
226+
*/
227+
protected function renderLineNumberColumns(?int $oldLineNum, ?int $newLineNum): string
228+
{
229+
if (!$this->options['lineNumbers']) {
230+
return '';
231+
}
232+
233+
return
234+
(
235+
isset($oldLineNum)
236+
? '<th class="n-old">' . $oldLineNum . '</th>'
237+
: '<th></th>'
238+
) .
239+
(
240+
isset($newLineNum)
241+
? '<th class="n-new">' . $newLineNum . '</th>'
242+
: '<th></th>'
243+
);
244+
}
237245
}

src/Renderer/Html/SideBySide.php

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected function redererChanges(array $changes): string
5555
*/
5656
protected function renderTableHeader(): string
5757
{
58-
$colspan = ($this->options['lineNumbers'] ? ' colspan="2"' : '');
58+
$colspan = $this->options['lineNumbers'] ? ' colspan="2"' : '';
5959

6060
return
6161
'<thead>' .
@@ -71,7 +71,7 @@ protected function renderTableHeader(): string
7171
*/
7272
protected function renderTableSeparateBlock(): string
7373
{
74-
$colspan = (!$this->options['lineNumbers'] ? '2' : '4');
74+
$colspan = $this->options['lineNumbers'] ? '4' : '2';
7575

7676
return
7777
'<tbody class="skipped">' .
@@ -121,15 +121,9 @@ protected function renderTableEqual(array $change): string
121121

122122
$html .=
123123
'<tr>' .
124-
($this->options['lineNumbers'] ?
125-
'<th class="n-old">' . $oldLineNum . '</th>'
126-
: ''
127-
) .
124+
$this->renderLineNumberColumn('old', $oldLineNum) .
128125
'<td class="old">' . $oldLine . '</td>' .
129-
($this->options['lineNumbers'] ?
130-
'<th class="n-new">' . $newLineNum . '</th>'
131-
: ''
132-
) .
126+
$this->renderLineNumberColumn('new', $newLineNum) .
133127
'<td class="new">' . $newLine . '</td>' .
134128
'</tr>';
135129
}
@@ -151,15 +145,9 @@ protected function renderTableInsert(array $change): string
151145

152146
$html .=
153147
'<tr>' .
154-
($this->options['lineNumbers'] ?
155-
'<th></th>'
156-
: ''
157-
) .
148+
$this->renderLineNumberColumn('', null) .
158149
'<td class="old"></td>' .
159-
($this->options['lineNumbers'] ?
160-
'<th class="n-new">' . $newLineNum . '</th>'
161-
: ''
162-
) .
150+
$this->renderLineNumberColumn('new', $newLineNum) .
163151
'<td class="new">' . $newLine . '</td>' .
164152
'</tr>';
165153
}
@@ -181,15 +169,9 @@ protected function renderTableDelete(array $change): string
181169

182170
$html .=
183171
'<tr>' .
184-
($this->options['lineNumbers'] ?
185-
'<th class="n-old">' . $oldLineNum . '</th>'
186-
: ''
187-
) .
172+
$this->renderLineNumberColumn('old', $oldLineNum) .
188173
'<td class="old">' . $oldLine . '</td>' .
189-
($this->options['lineNumbers'] ?
190-
'<th></th>'
191-
: ''
192-
) .
174+
$this->renderLineNumberColumn('', null) .
193175
'<td class="new"></td>' .
194176
'</tr>';
195177
}
@@ -220,15 +202,9 @@ protected function renderTableReplace(array $change): string
220202

221203
$html .=
222204
'<tr>' .
223-
($this->options['lineNumbers'] ?
224-
'<th class="n-old">' . $oldLineNum . '</th>'
225-
: ''
226-
) .
205+
$this->renderLineNumberColumn('old', $oldLineNum) .
227206
'<td class="old"><span>' . $oldLine . '</span></td>' .
228-
($this->options['lineNumbers'] ?
229-
'<th class="n-new">' . $newLineNum . '</th>'
230-
: ''
231-
) .
207+
$this->renderLineNumberColumn('new', $oldLineNum) .
232208
'<td class="new">' . $newLine . '</td>' .
233209
'</tr>';
234210
}
@@ -246,20 +222,31 @@ protected function renderTableReplace(array $change): string
246222

247223
$html .=
248224
'<tr>' .
249-
($this->options['lineNumbers'] ?
250-
'<th class="n-old">' . $oldLineNum . '</th>'
251-
: ''
252-
) .
225+
$this->renderLineNumberColumn('old', $oldLineNum) .
253226
'<td class="old"><span>' . $oldLine . '</span></td>' .
254-
($this->options['lineNumbers'] ?
255-
'<th class="n-new">' . $newLineNum . '</th>'
256-
: ''
257-
) .
227+
$this->renderLineNumberColumn('new', $newLineNum) .
258228
'<td class="new">' . $newLine . '</td>' .
259229
'</tr>';
260230
}
261231
}
262232

263233
return $html;
264234
}
235+
236+
/**
237+
* Renderer the line number column.
238+
*
239+
* @param string $type The diff type
240+
* @param null|int $lineNum The line number
241+
*/
242+
protected function renderLineNumberColumn(string $type, ?int $lineNum): string
243+
{
244+
if (!$this->options['lineNumbers']) {
245+
return '';
246+
}
247+
248+
return isset($lineNum)
249+
? '<th class="n-' . $type . '">' . $lineNum . '</th>'
250+
: '<th></th>';
251+
}
265252
}

0 commit comments

Comments
 (0)