Skip to content

Commit 3a6259a

Browse files
committed
Code tidy
- Diff::render() has the same $old==$new check - AbstractRenderer::getIdenticalResult() would be more flexible than using a constant Signed-off-by: Jack Cherng <jfcherng@gmail.com>
1 parent 1fd7935 commit 3a6259a

File tree

6 files changed

+18
-26
lines changed

6 files changed

+18
-26
lines changed

src/Diff.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,9 @@ public function render(AbstractRenderer $renderer): string
216216

217217
// the "no difference" situation may happen frequently
218218
// let's save some calculation if possible
219-
return $this->a !== $this->b
220-
? $renderer->render()
221-
: $renderer::IDENTICAL_RESULT;
219+
return $this->a === $this->b
220+
? $renderer::getIdenticalResult()
221+
: $renderer->render();
222222
}
223223

224224
/**
@@ -232,10 +232,8 @@ public function render(AbstractRenderer $renderer): string
232232
*/
233233
private function getText(array $lines, int $start = 0, ?int $end = null): array
234234
{
235-
if ($start === 0 && (!isset($end) || $end === \count($lines))) {
236-
return $lines;
237-
}
238-
239-
return \array_slice($lines, $start, isset($end) ? $end - $start : 1);
235+
return $start === 0 && (!isset($end) || $end === \count($lines))
236+
? $lines
237+
: \array_slice($lines, $start, isset($end) ? $end - $start : 1);
240238
}
241239
}

src/DiffHelper.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,6 @@ public static function calculate($old, $new, string $template = 'Unified', array
9090
\is_string($old) && ($old = \explode("\n", $old));
9191
\is_string($new) && ($new = \explode("\n", $new));
9292

93-
// the "no difference" situation may happen frequently
94-
// let's save some calculation if possible
95-
if ($old === $new) {
96-
return RendererFactory::resolveTemplate($template)::IDENTICAL_RESULT;
97-
}
98-
9993
return Diff::getInstance()
10094
->setAB($old, $new)
10195
->setOptions($diffOptions)

src/Renderer/AbstractRenderer.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ abstract class AbstractRenderer implements RendererInterface
2424
*/
2525
const IS_HTML_TEMPLATE = true;
2626

27-
/**
28-
* @var string the output result when the old and the new are the same
29-
*/
30-
const IDENTICAL_RESULT = '';
31-
3227
/**
3328
* @var Diff the instance of the diff class that this renderer is generating the rendered diff for
3429
*/
@@ -127,6 +122,16 @@ public function getOptions(): array
127122
return $this->options;
128123
}
129124

125+
/**
126+
* Get the renderer result when the old and the new are the same.
127+
*
128+
* @return string
129+
*/
130+
public static function getIdenticalResult(): string
131+
{
132+
return '';
133+
}
134+
130135
/**
131136
* Update the Language object.
132137
*

src/Renderer/Html/Inline.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function render(): string
2626
$changes = $this->getChanges();
2727

2828
if (empty($changes)) {
29-
return self::IDENTICAL_RESULT;
29+
return self::getIdenticalResult();
3030
}
3131

3232
$html = '<table class="diff diff-html diff-inline">';

src/Renderer/Html/Json.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ final class Json extends AbstractHtml
2323
*/
2424
const IS_TEXT_TEMPLATE = true;
2525

26-
/**
27-
* {@inheritdoc}
28-
*/
29-
const IDENTICAL_RESULT = '[]';
30-
3126
/**
3227
* {@inheritdoc}
3328
*/

src/Renderer/Html/SideBySide.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function render(): string
2626
$changes = $this->getChanges();
2727

2828
if (empty($changes)) {
29-
return self::IDENTICAL_RESULT;
29+
return self::getIdenticalResult();
3030
}
3131

3232
$html = '<table class="diff diff-html diff-side-by-side">';

0 commit comments

Comments
 (0)