From 576830c45c0fd86365bffa45231feaa83bac0fd5 Mon Sep 17 00:00:00 2001 From: DigiLive Date: Thu, 10 Dec 2020 08:02:29 +0100 Subject: [PATCH] Optimize Sequence renderer * Reduction of cyclomatic complexity. --- lib/jblond/Diff/Renderer/MainRenderer.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/jblond/Diff/Renderer/MainRenderer.php b/lib/jblond/Diff/Renderer/MainRenderer.php index 64f1aca..f429154 100644 --- a/lib/jblond/Diff/Renderer/MainRenderer.php +++ b/lib/jblond/Diff/Renderer/MainRenderer.php @@ -146,23 +146,23 @@ protected function renderSequences(): array $oldBlock = $this->formatLines(array_slice($oldText, $startOld, $blockSizeOld)); $newBlock = $this->formatLines(array_slice($newText, $startNew, $blockSizeNew)); - if ($tag == 'equal') { - // Old block equals New block + if ($tag != 'delete' && $tag != 'insert') { + // Old block "equals" New block or is replaced. $blocks[$lastBlock]['base']['lines'] += $oldBlock; $blocks[$lastBlock]['changed']['lines'] += $newBlock; continue; } - if ($tag == 'replace' || $tag == 'delete') { - // Inline differences or old block doesn't exist in the new text. + if ($tag == 'delete') { + // Block of version1 doesn't exist in version2. $blocks[$lastBlock]['base']['lines'] += $oldBlock; + continue; } - if ($tag == 'replace' || $tag == 'insert') { - // Inline differences or the new block doesn't exist in the old text. - $blocks[$lastBlock]['changed']['lines'] += $newBlock; - } + // Block of version2 doesn't exist in version1. + $blocks[$lastBlock]['changed']['lines'] += $newBlock; } + $changes[] = $blocks; }