Skip to content

Development #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/jblond/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* @author Chris Boulton <[email protected]>
* @copyright (c) 2009 Chris Boulton
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
* @version 1.8
* @version 1.9
* @link https://github.com/JBlond/php-diff
*/
class Diff
Expand Down Expand Up @@ -87,7 +87,7 @@ class Diff
* @param array $b Array containing the lines for the second string to compare.
* @param array $options Array for the options
*/
public function __construct($a, $b, $options = array())
public function __construct(array $a, array $b, array $options = array())
{
$this->a = $a;
$this->b = $b;
Expand Down Expand Up @@ -119,10 +119,10 @@ public function render($renderer)
* that line.
*
* @param int $start The starting number.
* @param int $end The ending number. If not supplied, only the item in $start will be returned.
* @param int|null $end The ending number. If not supplied, only the item in $start will be returned.
* @return array Array of all of the lines between the specified range.
*/
public function getA($start = 0, $end = null) : array
public function getA(int $start = 0, $end = null) : array
{
if ($start == 0 && $end === null) {
return $this->a;
Expand All @@ -144,10 +144,10 @@ public function getA($start = 0, $end = null) : array
* that line.
*
* @param int $start The starting number.
* @param int $end The ending number. If not supplied, only the item in $start will be returned.
* @param int|null $end The ending number. If not supplied, only the item in $start will be returned.
* @return array Array of all of the lines between the specified range.
*/
public function getB($start = 0, $end = null) : array
public function getB(int $start = 0, $end = null) : array
{
if ($start == 0 && $end === null) {
return $this->b;
Expand Down
72 changes: 60 additions & 12 deletions lib/jblond/Diff/Renderer/Html/HtmlArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* @author Chris Boulton <[email protected]>
* @copyright (c) 2009 Chris Boulton
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
* @version 1.8
* @version 1.9
* @link https://github.com/JBlond/php-diff
*/

Expand All @@ -61,18 +61,18 @@ class HtmlArray extends RendererAbstract

/**
* From https://gist.github.com/stemar/8287074
* @param mixed $string The input string.
* @param mixed $replacement The replacement string.
* @param mixed $start If start is positive, the replacing will begin at the start'th offset into string.
* @param string $string The input string.
* @param string $replacement The replacement string.
* @param int $start If start is positive, the replacing will begin at the start'th offset into string.
* If start is negative, the replacing will begin at the start'th character from the end of string.
* @param mixed $length If given and is positive, it represents the length of the portion of string which is to
* @param int|null $length If given and is positive, it represents the length of the portion of string which is to
* be replaced. If it is negative, it represents the number of characters from the end of string at which to
* stop replacing. If it is not given, then it will default to strlen( string ); i.e. end the replacing at the
* end of string. Of course, if length is zero then this function will have the effect of inserting replacement
* into string at the given start offset.
* @return string|array The result string is returned. If string is an array then array is returned.
*/
public function mbSubstrReplace($string, $replacement, $start, $length = null)
public function mbSubstrReplace(string $string, string $replacement, int $start, $length = null)
{
if (is_array($string)) {
$num = count($string);
Expand Down Expand Up @@ -115,6 +115,54 @@ public function mbSubstrReplace($string, $replacement, $start, $length = null)
return join($smatches['0']);
}

/**
* @param string|array $changes
* @param SideBySide|Inline $object
* @return string
*/
public function renderHtml($changes, $object)
{
$html = '';
if (empty($changes)) {
return $html;
}

$html .= $object->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 .= $object->generateSkippedTable();
}

foreach ($blocks as $change) {
$html .= '<tbody class="Change'.ucfirst($change['tag']).'">';
switch ($change['tag']) {
// Equal changes should be shown on both sides of the diff
case 'equal':
$html .= $object->generateTableRowsEqual($change);
break;
// Added lines only on the right side
case 'insert':
$html .= $object->generateTableRowsInsert($change);
break;
// Show deleted lines only on the left side
case 'delete':
$html .= $object->generateTableRowsDelete($change);
break;
// Show modified lines on both sides
case 'replace':
$html .= $object->generateTableRowsReplace($change);
break;
}
$html .= '</tbody>';
}
}
$html .= '</table>';
return $html;
}
/**
* Render and return an array structure suitable for generating HTML
* based differences. Generally called by subclasses that generate a
Expand Down Expand Up @@ -204,7 +252,7 @@ public function render()
* @param string $toLine The second string.
* @return array Array containing the starting position (0 by default) and the ending position (-1 by default)
*/
private function getChangeExtent($fromLine, $toLine)
private function getChangeExtent(string $fromLine, string $toLine)
{
$start = 0;
$limit = min(mb_strlen($fromLine), mb_strlen($toLine));
Expand All @@ -230,7 +278,7 @@ private function getChangeExtent($fromLine, $toLine)
* @param array $lines Array of lines to format.
* @return array Array of the formatted lines.
*/
protected function formatLines($lines)
protected function formatLines(array $lines) : array
{
if ($this->options['tabSize'] !== false) {
$lines = array_map(array($this, 'ExpandTabs'), $lines);
Expand All @@ -248,7 +296,7 @@ protected function formatLines($lines)
* @param array $matches The string of spaces.
* @return string The HTML representation of the string.
*/
protected function fixSpaces($matches)
protected function fixSpaces(array $matches) : string
{
$buffer = '';
$count = 0;
Expand All @@ -273,7 +321,7 @@ protected function fixSpaces($matches)
* @param string $line The containing tabs to convert.
* @return string The line with the tabs converted to spaces.
*/
private function expandTabs($line)
private function expandTabs(string $line) : string
{
$tabSize = $this->options['tabSize'];
while (($pos = strpos($line, "\t")) !== false) {
Expand All @@ -292,7 +340,7 @@ private function expandTabs($line)
* @param string $string The string.
* @return string The string with the HTML characters replaced by entities.
*/
private function htmlSafe($string)
private function htmlSafe(string $string) : string
{
return htmlspecialchars($string, ENT_NOQUOTES, 'UTF-8');
}
Expand All @@ -303,7 +351,7 @@ private function htmlSafe($string)
* @param integer $j1
* @return array
*/
private function getDefaultArray($tag, $i1, $j1)
private function getDefaultArray(string $tag, int $i1, int $j1) : array
{
return array
(
Expand Down
55 changes: 8 additions & 47 deletions lib/jblond/Diff/Renderer/Html/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* @author Chris Boulton <[email protected]>
* @copyright (c) 2009 Chris Boulton
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
* @version 1.8
* @version 1.9
* @link https://github.com/JBlond/php-diff
*/

Expand All @@ -57,46 +57,7 @@ class Inline extends HtmlArray
public function render() : string
{
$changes = parent::render();
$html = '';
if (empty($changes)) {
return $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 .= $this->generateSkippedTable();
}

foreach ($blocks as $change) {
$html .= '<tbody class="Change'.ucfirst($change['tag']).'">';
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 .= '</tbody>';
}
}
$html .= '</table>';
return $html;
return parent::renderHtml($changes, $this);
}


Expand All @@ -106,7 +67,7 @@ public function render() : string
*
* @return string Html code representation of the table's header.
*/
private function generateTableHeader() : string
public function generateTableHeader() : string
{
$html = '<table class="Differences DifferencesInline">';
$html .= '<thead>';
Expand All @@ -124,7 +85,7 @@ private function generateTableHeader() : string
*
* @return string Html code representing empty table body.
*/
private function generateSkippedTable() : string
public function generateSkippedTable() : string
{
$html = '<tbody class="Skipped">';
$html .= '<th>&hellip;</th>';
Expand All @@ -140,7 +101,7 @@ private function generateSkippedTable() : string
* @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) : string
public function generateTableRowsEqual(array &$change) : string
{
$html = "";
foreach ($change['base']['lines'] as $no => $line) {
Expand All @@ -161,7 +122,7 @@ private function generateTableRowsEqual(&$change) : string
* @param array &$change Array with data about changes.
* @return string Html code representing one or more rows of added text.
*/
private function generateTableRowsInsert(&$change) : string
public function generateTableRowsInsert(array &$change) : string
{
$html = "";
foreach ($change['changed']['lines'] as $no => $line) {
Expand All @@ -181,7 +142,7 @@ private function generateTableRowsInsert(&$change) : string
* @param array &$change Array with data about changes.
* @return string Html code representing one or more rows of removed text.
*/
private function generateTableRowsDelete(&$change) : string
public function generateTableRowsDelete(array &$change) : string
{
$html = "";
foreach ($change['base']['lines'] as $no => $line) {
Expand All @@ -201,7 +162,7 @@ private function generateTableRowsDelete(&$change) : string
* @param array &$change Array with data about changes.
* @return string Html code representing one or more rows of modified.
*/
private function generateTableRowsReplace(&$change) : string
public function generateTableRowsReplace(array &$change) : string
{
$html = "";

Expand Down
Loading