Skip to content

code clean up #13

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 18 commits into from
Feb 20, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/.idea
/vendor
37 changes: 21 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
PHP Diff Class
--------------
# PHP Diff Class

[![SensioLabsInsight](https://insight.sensiolabs.com/projects/aa609edb-cdb1-45cf-ad51-afbdab48f6a1/mini.png)](https://insight.sensiolabs.com/projects/aa609edb-cdb1-45cf-ad51-afbdab48f6a1) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/db5f8d57b1234502aeb852afc87e0dfe)](https://www.codacy.com/app/leet31337/php-diff)

[![Latest Version](https://img.shields.io/github/release/JBlond/php-diff.svg?style=flat-square&label=Release)](https://github.com/JBlond/php-diff/releases)

Introduction
------------
## Introduction

A comprehensive library for generating differences between
two hashable objects (strings or arrays). Generated differences can be
rendered in all of the standard formats including:
Expand All @@ -19,40 +18,46 @@ The logic behind the core of the diff engine (ie, the sequence matcher)
is primarily based on the Python difflib package. The reason for doing
so is primarily because of its high degree of accuracy.

Example Use
-----------

## Install

```
composer require jblond/php-diff
```

## Example Use

A quick usage example can be found in the example/ directory and under
example.php.

![Example Image](readme.png "Example")

![Example 2 Image](readme2.png "Example2")

Requirements
-----------
## Requirements

- PHP 7.1 or greater
- PHP Multibyte String

Merge files using jQuery
------------------------
## Merge files using jQuery

Xiphe has build a jQuery plugin with that you can merge the compared
files. Have a look at [jQuery-Merge-for-php-diff](https://github.com/Xiphe/jQuery-Merge-for-php-diff).

Todo
----
## Todo

* Ability to ignore blank line changes
* 3 way diff support
* Performance optimizations

Contributors
---------------------
## Contributors

Contributors since I forked the repo.

- maxxer
- Creris
- jfcherng

License (BSD License)
---------------------
### License (BSD License)

see [License](LICENSE)
1 change: 1 addition & 0 deletions example/a.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
<pre>
另外我覺得那個評價的白色櫃子有點沒有必要欸。外觀我就不說了 ,怎麼連空間都那麼狹隘。不過倒是從這個地方看出所謂的“改革” 😅😅
</pre>
<p>Do you know in Chinese, "金槍魚罐頭" means tuna can.</p>
</body>
</html>
3 changes: 2 additions & 1 deletion example/b.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<pre>
另外我覺得那個評鑑的白色櫃子有點沒有必要欸。外觀我就不說了 ,怎麼連空間都那麼狹隘。不過倒是從這個地方看出所謂的“改革” 😅😅
</pre>
<p>Do you know in Japanese, "魚の缶詰" means fish can.</p>
<p>Just a small amount of new text...</p>
</body>
</html>
</html>
42 changes: 20 additions & 22 deletions lib/jblond/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,24 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package Diff
* @package jblond
* @author Chris Boulton <[email protected]>
* @copyright (c) 2009 Chris Boulton
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
* @version 1.9
* @version 1.10
* @link https://github.com/JBlond/php-diff
*/
class Diff
{
/**
* @var array The "old" sequence to use as the basis for the comparison.
*/
private $a = null;
private $old = null;

/**
* @var array The "new" sequence to generate the changes for.
*/
private $b = null;
private $new = null;

/**
* @var array Array containing the generated op codes for the differences between the two items.
Expand All @@ -83,14 +83,14 @@ class Diff
/**
* The constructor.
*
* @param array $a Array containing the lines of the first string to compare.
* @param array $b Array containing the lines for the second string to compare.
* @param array $oldArray Array containing the lines of the first string to compare.
* @param array $newArray Array containing the lines for the second string to compare.
* @param array $options Array for the options
*/
public function __construct(array $a, array $b, array $options = array())
public function __construct(array $oldArray, array $newArray, array $options = array())
{
$this->a = $a;
$this->b = $b;
$this->old = $oldArray;
$this->new = $newArray;

if (is_array($options)) {
$this->options = array_merge($this->defaultOptions, $options);
Expand Down Expand Up @@ -122,19 +122,18 @@ public function render($renderer)
* @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(int $start = 0, $end = null) : array
public function getOld(int $start = 0, $end = null) : array
{
if ($start == 0 && $end === null) {
return $this->a;
return $this->old;
}

if ($end === null) {
$length = 1;
} else {
$length = $end - $start;
return array_slice($this->old, $start, 1);
}

return array_slice($this->a, $start, $length);
$length = $end - $start;
return array_slice($this->old, $start, $length);
}

/**
Expand All @@ -147,19 +146,18 @@ public function getA(int $start = 0, $end = null) : array
* @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(int $start = 0, $end = null) : array
public function getNew(int $start = 0, $end = null) : array
{
if ($start == 0 && $end === null) {
return $this->b;
return $this->new;
}

if ($end === null) {
$length = 1;
} else {
$length = $end - $start;
return array_slice($this->new, $start, 1);
}

return array_slice($this->b, $start, $length);
$length = $end - $start;
return array_slice($this->new, $start, $length);
}

/**
Expand All @@ -176,7 +174,7 @@ public function getGroupedOpcodes() : array
return $this->groupedCodes;
}

$sequenceMatcher = new SequenceMatcher($this->a, $this->b, $this->options, null);
$sequenceMatcher = new SequenceMatcher($this->old, $this->new, $this->options, null);
$this->groupedCodes = $sequenceMatcher->getGroupedOpcodes($this->options['context']);
return $this->groupedCodes;
}
Expand Down
82 changes: 16 additions & 66 deletions lib/jblond/Diff/Renderer/Html/HtmlArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,13 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package DiffLib
* @package jblond\Diff\Renderer\Html
* @author Chris Boulton <[email protected]>
* @copyright (c) 2009 Chris Boulton
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
* @version 1.9
* @version 1.10
* @link https://github.com/JBlond/php-diff
*/

/**
* Class Diff_Renderer_Html_Array
*/
class HtmlArray extends RendererAbstract
{
/**
Expand All @@ -59,62 +55,6 @@ class HtmlArray extends RendererAbstract
'title_b' => 'New Version',
);

/**
* From https://gist.github.com/stemar/8287074
* @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 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 $string, string $replacement, int $start, $length = null)
{
if (is_array($string)) {
$num = count($string);
// $replacement
if (is_array($replacement)) {
$replacement = array_slice($replacement, 0, $num);
} else {
$replacement = array_pad(array($replacement), $num, $replacement);
}

// $start
if (is_array($start)) {
$start = array_slice($start, 0, $num);
foreach ($start as $key => $value) {
$start[$key] = is_int($value) ? $value : 0;
}
} else {
$start = array_pad(array($start), $num, $start);
}
// $length
if (!isset($length)) {
$length = array_fill(0, $num, 0);
} elseif (is_array($length)) {
$length = array_slice($length, 0, $num);
foreach ($length as $key => $value) {
$length[$key] = isset($value) ? (is_int($value) ? $value : $num) : 0;
}
} else {
$length = array_pad(array($length), $num, $length);
}
// Recursive call
return array_map(array($this, 'mbSubstrReplace'), $string, $replacement, $start, $length);
}
preg_match_all('/./us', (string)$string, $smatches);
preg_match_all('/./us', (string)$replacement, $rmatches);
if ($length === null) {
$length = mb_strlen($string);
}
array_splice($smatches['0'], $start, $length, $rmatches[0]);
return join($smatches['0']);
}

/**
* @param string|array $changes
* @param SideBySide|Inline $object
Expand Down Expand Up @@ -175,8 +115,8 @@ public function render()
// As we'll be modifying a & b to include our change markers,
// we need to get the contents and store them here. That way
// we're not going to destroy the original data
$a = $this->diff->getA();
$b = $this->diff->getB();
$a = $this->diff->getOld();
$b = $this->diff->getNew();

$changes = array();
$opCodes = $this->diff->getGroupedOpcodes();
Expand Down Expand Up @@ -281,9 +221,19 @@ private function getChangeExtent(string $fromLine, string $toLine)
protected function formatLines(array $lines) : array
{
if ($this->options['tabSize'] !== false) {
$lines = array_map(array($this, 'ExpandTabs'), $lines);
$lines = array_map(
function ($item) {
return $this->expandTabs($item);
},
$lines
);
}
$lines = array_map(array($this, 'HtmlSafe'), $lines);
$lines = array_map(
function ($item) {
return $this->htmlSafe($item);
},
$lines
);
foreach ($lines as &$line) {
$line = preg_replace_callback('# ( +)|^ #', array($this, 'fixSpaces'), $line);
}
Expand Down
8 changes: 2 additions & 6 deletions lib/jblond/Diff/Renderer/Html/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,13 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package DiffLib
* @package jblond\Diff\Renderer\Html
* @author Chris Boulton <[email protected]>
* @copyright (c) 2009 Chris Boulton
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
* @version 1.9
* @version 1.10
* @link https://github.com/JBlond/php-diff
*/

/**
* Class Diff_Renderer_Html_Inline
*/
class Inline extends HtmlArray
{
/**
Expand Down
8 changes: 2 additions & 6 deletions lib/jblond/Diff/Renderer/Html/SideBySide.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,13 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package DiffLib
* @package jblond\Diff\Renderer\Html
* @author Chris Boulton <[email protected]>
* @copyright (c) 2009 Chris Boulton
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
* @version 1.9
* @version 1.10
* @link https://github.com/JBlond/php-diff
*/

/**
* Class Diff_Renderer_Html_SideBySide
*/
class SideBySide extends HtmlArray
{
/**
Expand Down
4 changes: 2 additions & 2 deletions lib/jblond/Diff/Renderer/RendererAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package DiffLib
* @package jblond\Diff\Renderer
* @author Chris Boulton <[email protected]>
* @copyright (c) 2009 Chris Boulton
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
* @version 1.9
* @version 1.10
* @link https://github.com/JBlond/php-diff
*/
abstract class RendererAbstract
Expand Down
Loading