Skip to content

Commit f7679ff

Browse files
committed
Code tidy
Signed-off-by: Jack Cherng <[email protected]>
1 parent 159d244 commit f7679ff

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

src/Renderer/Html/AbstractHtml.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,29 +188,48 @@ protected function getDefaultBlock(string $tag, int $i1, int $j1): array
188188
}
189189

190190
/**
191-
* Format a series of lines suitable for output in a HTML rendered diff.
192-
* This involves replacing tab characters with spaces, making the HTML safe
193-
* for output, ensuring that double spaces are replaced with &nbsp; etc.
191+
* Make a series of lines suitable for outputting in a HTML rendered diff.
194192
*
195193
* @param string[] $lines array of lines to format
196194
*
197195
* @return string[] array of the formatted lines
198196
*/
199197
protected function formatLines(array $lines): array
200198
{
201-
// glue all lines into a single string to get rid of multiple function calls later
202-
// unnecessary, but should improve performance if there are many lines
203-
$string = \implode(RendererConstant::IMPLODE_DELIMITER, $lines);
199+
/**
200+
* To prevent from invoking the same function calls for several times,
201+
* we can glue lines into a string and call functions for one time.
202+
* After that, we split the string back into lines.
203+
*/
204+
return \explode(
205+
RendererConstant::IMPLODE_DELIMITER,
206+
$this->formatStringFromLines(\implode(
207+
RendererConstant::IMPLODE_DELIMITER,
208+
$lines
209+
))
210+
);
211+
}
204212

213+
/**
214+
* Make a string suitable for outputting in a HTML rendered diff.
215+
*
216+
* This my involve replacing tab characters with spaces, making the HTML safe
217+
* for output, ensuring that double spaces are replaced with &nbsp; etc.
218+
*
219+
* @param string $string the string of imploded lines
220+
*
221+
* @return string the formatted string
222+
*/
223+
protected function formatStringFromLines(string $string): string
224+
{
205225
$string = $this->expandTabs($string, $this->options['tabSize']);
206226
$string = $this->htmlSafe($string);
207227

208228
if ($this->options['spacesToNbsp']) {
209229
$string = $this->htmlFixSpaces($string);
210230
}
211231

212-
// split the string back to lines
213-
return \explode(RendererConstant::IMPLODE_DELIMITER, $string);
232+
return $string;
214233
}
215234

216235
/**

src/Renderer/Html/Json.php

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

55
namespace Jfcherng\Diff\Renderer\Html;
66

7-
use Jfcherng\Diff\Renderer\RendererConstant;
8-
97
/**
108
* Json diff generator.
119
*/
@@ -37,15 +35,8 @@ public function render(): string
3735
/**
3836
* {@inheritdoc}
3937
*/
40-
protected function formatLines(array $lines): array
38+
protected function formatStringFromLines(string $string): string
4139
{
42-
// glue all lines into a single string to get rid of multiple function calls later
43-
// unnecessary, but should improve performance if there are many lines
44-
$string = \implode(RendererConstant::IMPLODE_DELIMITER, $lines);
45-
46-
$string = $this->htmlSafe($string);
47-
48-
// split the string back to lines
49-
return \explode(RendererConstant::IMPLODE_DELIMITER, $string);
40+
return $this->htmlSafe($string);
5041
}
5142
}

0 commit comments

Comments
 (0)