Skip to content

Commit d89a7b3

Browse files
committed
Replace "switch" statements with callback function tables
Signed-off-by: Jack Cherng <[email protected]>
1 parent 0a26477 commit d89a7b3

File tree

2 files changed

+20
-52
lines changed

2 files changed

+20
-52
lines changed

src/Renderer/Html/Inline.php

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -95,33 +95,17 @@ protected function renderTableSeparateBlock(): string
9595
*/
9696
protected function renderTableBlock(array $change): string
9797
{
98-
$html = '<tbody class="change change-' . self::TAG_CLASS_MAP[$change['tag']] . '">';
99-
100-
switch ($change['tag']) {
101-
default:
102-
// equal changes should be shown on both sides of the diff
103-
case SequenceMatcher::OP_EQ:
104-
$html .= $this->renderTableEqual($change);
105-
106-
break;
107-
// added lines only on the r side
108-
case SequenceMatcher::OP_INS:
109-
$html .= $this->renderTableInsert($change);
110-
111-
break;
112-
// show deleted lines only on the l side
113-
case SequenceMatcher::OP_DEL:
114-
$html .= $this->renderTableDelete($change);
115-
116-
break;
117-
// show modified lines on both sides
118-
case SequenceMatcher::OP_REP:
119-
$html .= $this->renderTableReplace($change);
120-
121-
break;
122-
}
98+
static $callbacks = [
99+
SequenceMatcher::OP_EQ => 'renderTableEqual',
100+
SequenceMatcher::OP_INS => 'renderTableInsert',
101+
SequenceMatcher::OP_DEL => 'renderTableDelete',
102+
SequenceMatcher::OP_REP => 'renderTableReplace',
103+
];
123104

124-
return $html . '</tbody>';
105+
return
106+
'<tbody class="change change-' . self::TAG_CLASS_MAP[$change['tag']] . '">' .
107+
$this->{$callbacks[$change['tag']]}($change) .
108+
'</tbody>';
125109
}
126110

127111
/**

src/Renderer/Html/SideBySide.php

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -93,33 +93,17 @@ protected function renderTableSeparateBlock(): string
9393
*/
9494
protected function renderTableBlock(array $change): string
9595
{
96-
$html = '<tbody class="change change-' . self::TAG_CLASS_MAP[$change['tag']] . '">';
97-
98-
switch ($change['tag']) {
99-
default:
100-
// equal changes should be shown on both sides of the diff
101-
case SequenceMatcher::OP_EQ:
102-
$html .= $this->renderTableEqual($change);
103-
104-
break;
105-
// added lines only on the r side
106-
case SequenceMatcher::OP_INS:
107-
$html .= $this->renderTableInsert($change);
108-
109-
break;
110-
// show deleted lines only on the l side
111-
case SequenceMatcher::OP_DEL:
112-
$html .= $this->renderTableDelete($change);
113-
114-
break;
115-
// show modified lines on both sides
116-
case SequenceMatcher::OP_REP:
117-
$html .= $this->renderTableReplace($change);
118-
119-
break;
120-
}
96+
static $callbacks = [
97+
SequenceMatcher::OP_EQ => 'renderTableEqual',
98+
SequenceMatcher::OP_INS => 'renderTableInsert',
99+
SequenceMatcher::OP_DEL => 'renderTableDelete',
100+
SequenceMatcher::OP_REP => 'renderTableReplace',
101+
];
121102

122-
return $html . '</tbody>';
103+
return
104+
'<tbody class="change change-' . self::TAG_CLASS_MAP[$change['tag']] . '">' .
105+
$this->{$callbacks[$change['tag']]}($change) .
106+
'</tbody>';
123107
}
124108

125109
/**

0 commit comments

Comments
 (0)