Skip to content

Commit c017af5

Browse files
committed
Code Optimization, cleanup, refactoring and commenting.
1 parent 795fe20 commit c017af5

File tree

2 files changed

+254
-75
lines changed

2 files changed

+254
-75
lines changed

lib/jblond/Diff/Renderer/Html/SideBySide.php

+120-74
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919
class SideBySide extends HtmlArray
2020
{
2121
/**
22-
* Render a and return diff with changes between the two sequences
23-
* displayed side by side.
22+
* Render a and return diff-view with changes between the two sequences displayed side by side.
2423
*
25-
* @return string The generated side by side diff.
24+
* @return string The generated side by side diff-view.
2625
*/
2726
public function render(): string
2827
{
@@ -31,130 +30,177 @@ public function render(): string
3130
}
3231

3332
/**
34-
* Generates a string representation of a predefined table and its head with
35-
* titles from options.
33+
* Generates a string representation of the opening of a predefined table and its header with titles from options.
3634
*
37-
* @return string Html code representation of the table's header.
35+
* @return string HTML code representation of a table's header.
3836
*/
3937
public function generateTableHeader(): string
4038
{
41-
$html = '<table class="Differences DifferencesSideBySide">';
42-
$html .= '<thead>';
43-
$html .= '<tr>';
44-
$html .= '<th colspan="2">' . $this->options['title_a'] . '</th>';
45-
$html .= '<th colspan="2">' . $this->options['title_b'] . '</th>';
46-
$html .= '</tr>';
47-
$html .= '</thead>';
48-
return $html;
39+
return <<<HTML
40+
<table class="Differences DifferencesSideBySide">
41+
<thead>
42+
<tr>
43+
<th colspan="2">{$this->options['title_a']}</th>
44+
<th colspan="2">{$this->options['title_b']}</th>
45+
</tr>
46+
</thead>
47+
HTML;
4948
}
5049

5150
/**
52-
* Generates a string representation of one or more rows of a table of lines of text with no difference.
51+
* Generates a string representation of table rows showing text with no difference.
52+
*
53+
* @param array $change Contains the op-codes about the changes between two blocks.
5354
*
54-
* @param array &$change Array with data about changes.
55-
* @return string Html code representing one or more rows of text with no difference.
55+
* @return string HTML code representing table rows showing text with no difference.
5656
*/
57-
public function generateTableRowsEqual(array &$change): string
57+
public function generateTableRowsEqual(array $change): string
5858
{
59-
$html = "";
59+
$html = '';
60+
6061
foreach ($change['base']['lines'] as $no => $line) {
61-
$fromLine = $change['base']['offset'] + $no + 1;
62-
$toLine = $change['changed']['offset'] + $no + 1;
63-
$html .= '<tr>';
64-
$html .= '<th>' . $fromLine . '</th>';
65-
$html .= '<td class="Left"><span>' . $line . '</span>&#xA0;</td>';
66-
$html .= '<th>' . $toLine . '</th>';
67-
$html .= '<td class="Right"><span>' . $line . '</span>&#xA0;</td>';
68-
$html .= '</tr>';
62+
$fromLine = $change['base']['offset'] + $no + 1;
63+
$toLine = $change['changed']['offset'] + $no + 1;
64+
65+
$html .= <<<HTML
66+
<tr>
67+
<th>$fromLine</th>
68+
<td class="Left"><span>$line</span>&nbsp;</td>
69+
<th>$toLine</th>
70+
<td class="Right"><span>$line</span>&nbsp;</td>
71+
</tr>
72+
HTML;
6973
}
74+
7075
return $html;
7176
}
7277

7378
/**
74-
* Generates a string representation of one or more rows of a table of lines, where new text was added.
79+
* Generates a string representation of table rows showing added text.
80+
*
81+
* @param array $change Contains the op-codes about the changes between two blocks of text.
7582
*
76-
* @param array &$change Array with data about changes.
77-
* @return string Html code representing one or more rows of added text.
83+
* @return string HTML code representing table rows showing with added text.
7884
*/
79-
public function generateTableRowsInsert(array &$change): string
85+
public function generateTableRowsInsert(array $change): string
8086
{
81-
$html = "";
87+
$html = '';
88+
8289
foreach ($change['changed']['lines'] as $no => $line) {
8390
$toLine = $change['changed']['offset'] + $no + 1;
84-
$html .= '<tr>';
85-
$html .= '<th>&#xA0;</th>';
86-
$html .= '<td class="Left">&#xA0;</td>';
87-
$html .= '<th>' . $toLine . '</th>';
88-
$html .= '<td class="Right"><ins>' . $line . '</ins>&#xA0;</td>';
89-
$html .= '</tr>';
91+
92+
$html .= <<<HTML
93+
<tr>
94+
<th>&nbsp;</th>
95+
<td class="Left">&nbsp;</td>
96+
<th>$toLine</th>
97+
<td class="Right">
98+
<ins>$line</ins>
99+
&nbsp;
100+
</td>
101+
</tr>
102+
HTML;
90103
}
104+
91105
return $html;
92106
}
93107

94108
/**
95-
* Generates a string representation of one or more rows of a table of lines, where text was removed.
109+
* Generates a string representation of table rows showing removed text.
96110
*
97-
* @param array &$change Array with data about changes.
98-
* @return string Html code representing one or more rows of removed text.
111+
* @param array $change Contains the op-codes about the changes between two blocks of text.
112+
*
113+
* @return string HTML code representing table rows showing removed text.
99114
*/
100115
public function generateTableRowsDelete(array &$change): string
101116
{
102-
$html = "";
117+
$html = '';
118+
103119
foreach ($change['base']['lines'] as $no => $line) {
104120
$fromLine = $change['base']['offset'] + $no + 1;
105-
$html .= '<tr>';
106-
$html .= '<th>' . $fromLine . '</th>';
107-
$html .= '<td class="Left"><del>' . $line . '</del>&#xA0;</td>';
108-
$html .= '<th>&#xA0;</th>';
109-
$html .= '<td class="Right">&#xA0;</td>';
110-
$html .= '</tr>';
121+
122+
$html = <<<HTML
123+
<tr>
124+
<th>$fromLine</th>
125+
<td class="Left">
126+
<del>$line</del>
127+
&nbsp;
128+
</td>
129+
<th>&nbsp;</th>
130+
<td class="Right">&nbsp;</td>
131+
</tr>
132+
HTML;
111133
}
134+
112135
return $html;
113136
}
114137

115138
/**
116-
* Generates a string representation of one or more rows of a table of lines, where text was partially modified.
139+
* Generates a string representation of table rows showing partialy modified text.
140+
*
141+
* @param array $change Contains the op-codes about the changes between two blocks of text.
117142
*
118-
* @param array &$change Array with data about changes.
119-
* @return string Html code representing one or more rows of modified.
143+
* @return string Html code representing table rows showing modified text.
120144
*/
121145
public function generateTableRowsReplace(array &$change): string
122146
{
123-
$html = "";
147+
$html = '';
124148

125149
if (count($change['base']['lines']) >= count($change['changed']['lines'])) {
126150
foreach ($change['base']['lines'] as $no => $line) {
127-
$fromLine = $change['base']['offset'] + $no + 1;
128-
$html .= '<tr>';
129-
$html .= '<th>' . $fromLine . '</th>';
130-
$html .= '<td class="Left"><span>' . $line . '</span>&#xA0;</td>';
151+
$fromLine = $change['base']['offset'] + $no + 1;
152+
153+
$html .= <<<HTML
154+
<tr>
155+
<th>$fromLine</th>
156+
<td class="Left">
157+
<span>$line</span>
158+
&nbsp;
159+
</td>
160+
HTML;
161+
131162
if (!isset($change['changed']['lines'][$no])) {
132-
$toLine = '&#xA0;';
133-
$changedLine = '&#xA0;';
163+
$toLine = "&nbsp;";
164+
$changedLine = "&nbsp;";
134165
} else {
135-
$toLine = $change['changed']['offset'] + $no + 1;
136-
$changedLine = '<span>' . $change['changed']['lines'][$no] . '</span>';
166+
$toLine = $change['changed']['offset'] + $no + 1;
167+
$changedLine = "<span>{$change['changed']['lines'][$no]}</span>";
137168
}
138-
$html .= '<th>' . $toLine . '</th>';
139-
$html .= '<td class="Right">' . $changedLine . '</td>';
140-
$html .= '</tr>';
169+
170+
$html .= <<<HTML
171+
<th>$toLine</th>
172+
<td class="Right">$changedLine</td>
173+
</tr>
174+
HTML;
141175
}
142176
} else {
143177
foreach ($change['changed']['lines'] as $no => $changedLine) {
144178
if (!isset($change['base']['lines'][$no])) {
145-
$fromLine = '&#xA0;';
146-
$line = '&#xA0;';
179+
$fromLine = "&nbsp;";
180+
$line = "&nbsp;";
147181
} else {
148-
$fromLine = $change['base']['offset'] + $no + 1;
149-
$line = '<span>' . $change['base']['lines'][$no] . '</span>';
182+
$fromLine = $change['base']['offset'] + $no + 1;
183+
$line = "<span>{$change['base']['lines'][$no]}</span>";
150184
}
151-
$html .= '<tr>';
152-
$html .= '<th>' . $fromLine . '</th>';
153-
$html .= '<td class="Left"><span>' . $line . '</span>&#xA0;</td>';
154-
$toLine = $change['changed']['offset'] + $no + 1;
155-
$html .= '<th>' . $toLine . '</th>';
156-
$html .= '<td class="Right">' . $changedLine . '</td>';
157-
$html .= '</tr>';
185+
186+
$html .= <<<HTML
187+
<tr>
188+
<th>$fromLine</th>
189+
<td class="Left">
190+
<span>$line</span>
191+
&nbsp;
192+
</td>
193+
HTML;
194+
195+
$toLine = $change['changed']['offset'] + $no + 1;
196+
197+
$html .= <<<HTML
198+
<th>$toLine</th>
199+
<td class="Right">
200+
<span>$changedLine</span>
201+
</td>
202+
</tr>
203+
HTML;
158204
}
159205
}
160206

0 commit comments

Comments
 (0)