From 35917898b3adeec1b180d7277b4ba86dd3444b45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduard=20L=C3=A1hl?= Date: Fri, 11 Jan 2019 06:57:49 +0100 Subject: [PATCH] Fixed lengths of functions Changed length of 3 functions so that they do not take more than 50 lines by separating them to smaller blocks. Also found and fixed a bug in SideBySide renderer, where it would print wrong line count for modified lines on the right side. --- lib/Diff/Renderer/Html/Array.php | 28 +-- lib/Diff/Renderer/Html/Inline.php | 213 +++++++++++++++------- lib/Diff/Renderer/Html/SideBySide.php | 252 +++++++++++++++++--------- 3 files changed, 329 insertions(+), 164 deletions(-) diff --git a/lib/Diff/Renderer/Html/Array.php b/lib/Diff/Renderer/Html/Array.php index 97e9ea06..e0aa4994 100644 --- a/lib/Diff/Renderer/Html/Array.php +++ b/lib/Diff/Renderer/Html/Array.php @@ -110,7 +110,7 @@ public function mb_substr_replace($string, $replacement, $start, $length=NULL) { * based differences. Generally called by subclasses that generate a * HTML based diff and return an array of the changes to show in the diff. * - * @return array An array of the generated chances, suitable for presentation in HTML. + * @return array An array of the generated changes, suitable for presentation in HTML. */ public function render() { @@ -154,17 +154,7 @@ public function render() } if($tag != $lastTag) { - $blocks[] = array( - 'tag' => $tag, - 'base' => array( - 'offset' => $i1, - 'lines' => array() - ), - 'changed' => array( - 'offset' => $j1, - 'lines' => array() - ) - ); + $blocks[] = $this->getDefaultArray($tag, $i1, $j1); $lastBlock = count($blocks)-1; } @@ -297,4 +287,18 @@ private function htmlSafe($string) { return htmlspecialchars($string, ENT_NOQUOTES, 'UTF-8'); } + + private function getDefaultArray($tag, $i1, $j1){ + return array( + 'tag' => $tag, + 'base' => array( + 'offset' => $i1, + 'lines' => array() + ), + 'changed' => array( + 'offset' => $j1, + 'lines' => array() + ) + ); + } } diff --git a/lib/Diff/Renderer/Html/Inline.php b/lib/Diff/Renderer/Html/Inline.php index a4859dfa..73ac4dbf 100644 --- a/lib/Diff/Renderer/Html/Inline.php +++ b/lib/Diff/Renderer/Html/Inline.php @@ -61,81 +61,35 @@ public function render() return $html; } - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; + $html .= $this->generateTableHeader(); + foreach($changes as $i => $blocks) { // If this is a separate block, we're condensing code so output ..., // indicating a significant portion of the code has been collapsed as // it is the same if($i > 0) { - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; + $html .= $this->generateSkippedTable(); } foreach($blocks as $change) { $html .= ''; - // Equal changes should be shown on both sides of the diff - if($change['tag'] == 'equal') { - foreach($change['base']['lines'] as $no => $line) { - $fromLine = $change['base']['offset'] + $no + 1; - $toLine = $change['changed']['offset'] + $no + 1; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - } - } - // Added lines only on the right side - else if($change['tag'] == 'insert') { - foreach($change['changed']['lines'] as $no => $line) { - $toLine = $change['changed']['offset'] + $no + 1; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - } - } - // Show deleted lines only on the left side - else if($change['tag'] == 'delete') { - foreach($change['base']['lines'] as $no => $line) { - $fromLine = $change['base']['offset'] + $no + 1; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - } - } - // Show modified lines on both sides - else if($change['tag'] == 'replace') { - foreach($change['base']['lines'] as $no => $line) { - $fromLine = $change['base']['offset'] + $no + 1; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - } - - foreach($change['changed']['lines'] as $no => $line) { - $toLine = $change['changed']['offset'] + $no + 1; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - } + switch ($change['tag']){ + // Equal changes should be shown on both sides of the diff + case 'equal': + $html .= $this->generateTableRowsEqual($change); + break; + // Added lines only on the right side + case 'insert': + $html .= $this->generateTableRowsInsert($change); + break; + // Show deleted lines only on the left side + case 'delete': + $html .= $this->generateTableRowsDelete($change); + break; + // Show modified lines on both sides + case 'replace': + $html .= $this->generateTableRowsReplace($change); + break; } $html .= ''; } @@ -143,4 +97,131 @@ public function render() $html .= '
OldNewDifferences
 
'.$fromLine.''.$toLine.''.$line.'
 '.$toLine.''.$line.' 
'.$fromLine.' '.$line.' 
'.$fromLine.' '.$line.'
 '.$toLine.''.$line.'
'; return $html; } + + + /** + * Generates a string representation of a predefined table and its head with + * titles from options. + * + * @return string Html code representation of the table's header. + */ + private function generateTableHeader() + { + $html = ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + return $html; + } + + /** + * Generates a string representation of empty table body. + * + * @return string Html code representing empty table body. + */ + private function generateSkippedTable() + { + $html = ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + return $html; + } + + /** + * Generates a string representation of one or more rows of a table of lines of text with no difference. + * + * @param array &$change Array with data about changes. + * @return string Html code representing one or more rows of text with no difference. + */ + private function generateTableRowsEqual(&$change) + { + $html = ""; + foreach($change['base']['lines'] as $no => $line) { + $fromLine = $change['base']['offset'] + $no + 1; + $toLine = $change['changed']['offset'] + $no + 1; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + } + return $html; + } + + /** + * Generates a string representation of one or more rows of a table of lines, where new text was added. + * + * @param array &$change Array with data about changes. + * @return string Html code representing one or more rows of added text. + */ + private function generateTableRowsInsert(&$change) + { + $html = ""; + foreach($change['changed']['lines'] as $no => $line) { + $toLine = $change['changed']['offset'] + $no + 1; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + } + return $html; + } + + /** + * Generates a string representation of one or more rows of a table of lines, where text was removed. + * + * @param array &$change Array with data about changes. + * @return string Html code representing one or more rows of removed text. + */ + private function generateTableRowsDelete(&$change) + { + $html = ""; + foreach($change['base']['lines'] as $no => $line) { + $fromLine = $change['base']['offset'] + $no + 1; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + } + return $html; + } + + /** + * Generates a string representation of one or more rows of a table of lines, where text was partially modified. + * + * @param array &$change Array with data about changes. + * @return string Html code representing one or more rows of modified. + */ + private function generateTableRowsReplace(&$change) + { + $html = ""; + + foreach($change['base']['lines'] as $no => $line) { + $fromLine = $change['base']['offset'] + $no + 1; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + } + + foreach($change['changed']['lines'] as $no => $line) { + $toLine = $change['changed']['offset'] + $no + 1; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + } + + return $html; + } } diff --git a/lib/Diff/Renderer/Html/SideBySide.php b/lib/Diff/Renderer/Html/SideBySide.php index 75a3ed65..59473f5a 100644 --- a/lib/Diff/Renderer/Html/SideBySide.php +++ b/lib/Diff/Renderer/Html/SideBySide.php @@ -62,105 +62,185 @@ public function render() return $html; } - $html .= '
OldNewDifferences
 
'.$fromLine.''.$toLine.''.$line.'
 '.$toLine.''.$line.' 
'.$fromLine.' '.$line.' 
'.$fromLine.' '.$line.'
 '.$toLine.''.$line.'
'; + $html .= $this->generateTableHeader(); + + foreach($changes as $i => $blocks) { + if($i > 0) { + $html .= $this->generateSkippedTable(); + } + + foreach($blocks as $change) { + $html .= ''; + switch ($change['tag']){ + // Equal changes should be shown on both sides of the diff + case 'equal': + $html .= $this->generateTableRowsEqual($change); + break; + // Added lines only on the right side + case 'insert': + $html .= $this->generateTableRowsInsert($change); + break; + // Show deleted lines only on the left side + case 'delete': + $html .= $this->generateTableRowsDelete($change); + break; + // Show modified lines on both sides + case 'replace': + $html .= $this->generateTableRowsReplace($change); + break; + } + $html .= ''; + } + } + $html .= '
'; + return $html; + } + + /** + * Generates a string representation of a predefined table and its head with + * titles from options. + * + * @return string Html code representation of the table's header. + */ + private function generateTableHeader() + { + $html = ''; $html .= ''; $html .= ''; $html .= ''; $html .= ''; $html .= ''; $html .= ''; - foreach($changes as $i => $blocks) { - if($i > 0) { - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - } + return $html; + } - foreach($blocks as $change) { - $html .= ''; - // Equal changes should be shown on both sides of the diff - if($change['tag'] == 'equal') { - foreach($change['base']['lines'] as $no => $line) { - $fromLine = $change['base']['offset'] + $no + 1; - $toLine = $change['changed']['offset'] + $no + 1; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - } + /** + * Generates a string representation of empty table body. + * + * @return string Html code representing empty table body. + */ + private function generateSkippedTable() + { + $html = ''; + $html .= ''; + $html .= ''; + $html .= ''; + return $html; + } + + /** + * Generates a string representation of one or more rows of a table of lines of text with no difference. + * + * @param array &$change Array with data about changes. + * @return string Html code representing one or more rows of text with no difference. + */ + private function generateTableRowsEqual(&$change) + { + $html = ""; + foreach($change['base']['lines'] as $no => $line) { + $fromLine = $change['base']['offset'] + $no + 1; + $toLine = $change['changed']['offset'] + $no + 1; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + } + return $html; + } + + /** + * Generates a string representation of one or more rows of a table of lines, where new text was added. + * + * @param array &$change Array with data about changes. + * @return string Html code representing one or more rows of added text. + */ + private function generateTableRowsInsert(&$change) + { + $html = ""; + foreach($change['changed']['lines'] as $no => $line) { + $toLine = $change['changed']['offset'] + $no + 1; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + } + return $html; + } + + /** + * Generates a string representation of one or more rows of a table of lines, where text was removed. + * + * @param array &$change Array with data about changes. + * @return string Html code representing one or more rows of removed text. + */ + private function generateTableRowsDelete(&$change) + { + $html = ""; + foreach($change['base']['lines'] as $no => $line) { + $fromLine = $change['base']['offset'] + $no + 1; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + } + return $html; + } + + /** + * Generates a string representation of one or more rows of a table of lines, where text was partially modified. + * + * @param array &$change Array with data about changes. + * @return string Html code representing one or more rows of modified. + */ + private function generateTableRowsReplace(&$change) + { + $html = ""; + + if(count($change['base']['lines']) >= count($change['changed']['lines'])) { + foreach($change['base']['lines'] as $no => $line) { + $fromLine = $change['base']['offset'] + $no + 1; + $html .= ''; + $html .= ''; + $html .= ''; + if(!isset($change['changed']['lines'][$no])) { + $toLine = ' '; + $changedLine = ' '; } - // Added lines only on the right side - else if($change['tag'] == 'insert') { - foreach($change['changed']['lines'] as $no => $line) { - $toLine = $change['changed']['offset'] + $no + 1; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - } + else { + $toLine = $change['changed']['offset'] + $no + 1; + $changedLine = ''.$change['changed']['lines'][$no].''; } - // Show deleted lines only on the left side - else if($change['tag'] == 'delete') { - foreach($change['base']['lines'] as $no => $line) { - $fromLine = $change['base']['offset'] + $no + 1; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - $html .= ''; - } + $html .= ''; + $html .= ''; + $html .= ''; + } + } + else { + foreach($change['changed']['lines'] as $no => $changedLine) { + if(!isset($change['base']['lines'][$no])) { + $fromLine = ' '; + $line = ' '; } - // Show modified lines on both sides - else if($change['tag'] == 'replace') { - if(count($change['base']['lines']) >= count($change['changed']['lines'])) { - foreach($change['base']['lines'] as $no => $line) { - $fromLine = $change['base']['offset'] + $no + 1; - $html .= ''; - $html .= ''; - $html .= ''; - if(!isset($change['changed']['lines'][$no])) { - $toLine = ' '; - $changedLine = ' '; - } - else { - $toLine = $change['base']['offset'] + $no + 1; - $changedLine = ''.$change['changed']['lines'][$no].''; - } - $html .= ''; - $html .= ''; - $html .= ''; - } - } - else { - foreach($change['changed']['lines'] as $no => $changedLine) { - if(!isset($change['base']['lines'][$no])) { - $fromLine = ' '; - $line = ' '; - } - else { - $fromLine = $change['base']['offset'] + $no + 1; - $line = ''.$change['base']['lines'][$no].''; - } - $html .= ''; - $html .= ''; - $html .= ''; - $toLine = $change['changed']['offset'] + $no + 1; - $html .= ''; - $html .= ''; - $html .= ''; - } - } + else { + $fromLine = $change['base']['offset'] + $no + 1; + $line = ''.$change['base']['lines'][$no].''; } - $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $toLine = $change['changed']['offset'] + $no + 1; + $html .= ''; + $html .= ''; + $html .= ''; } } - $html .= '
'.$this->options['title_a'].''.$this->options['title_b'].'
  
'.$fromLine.''.$line.' '.$toLine.''.$line.' 
  
'.$fromLine.''.$line.' '.$toLine.''.$line.' 
  '.$toLine.''.$line.' 
'.$fromLine.''.$line.'   
'.$fromLine.''.$line.' 
  '.$toLine.''.$line.' 
'.$fromLine.''.$line.'   
'.$toLine.''.$changedLine.'
'.$fromLine.''.$line.' '.$toLine.''.$changedLine.'
'.$fromLine.''.$line.' '.$toLine.''.$changedLine.'
'.$fromLine.''.$line.' '.$toLine.''.$changedLine.'
'; + return $html; } }