Skip to content

Commit efc8c8a

Browse files
committed
Merge branch 'master' of github.com:/JBlond/php-diff
2 parents 145f0e0 + 93663b5 commit efc8c8a

File tree

12 files changed

+241
-343
lines changed

12 files changed

+241
-343
lines changed

README.md

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
PHP Diff Class
2-
--------------
1+
# PHP Diff Class
32

43
[![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)
54

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

8-
Introduction
9-
------------
7+
## Introduction
8+
109
A comprehensive library for generating differences between
1110
two hashable objects (strings or arrays). Generated differences can be
1211
rendered in all of the standard formats including:
@@ -19,69 +18,46 @@ The logic behind the core of the diff engine (ie, the sequence matcher)
1918
is primarily based on the Python difflib package. The reason for doing
2019
so is primarily because of its high degree of accuracy.
2120

22-
Example Use
23-
-----------
21+
22+
## Install
23+
24+
```
25+
composer require jblond/php-diff
26+
```
27+
28+
## Example Use
29+
2430
A quick usage example can be found in the example/ directory and under
2531
example.php.
2632

27-
![Example Image](https://github.com/jblond/php-diff/raw/master/readme.png "Example")
33+
![Example Image](readme.png "Example")
2834

29-
![Example 2 Image](https://github.com/jblond/php-diff/raw/master/readme2.png "Example2")
35+
![Example 2 Image](readme2.png "Example2")
36+
37+
## Requirements
3038

31-
Requirements
32-
-----------
3339
- PHP 7.1 or greater
3440
- PHP Multibyte String
3541

36-
Merge files using jQuery
37-
------------------------
42+
## Merge files using jQuery
43+
3844
Xiphe has build a jQuery plugin with that you can merge the compared
3945
files. Have a look at [jQuery-Merge-for-php-diff](https://github.com/Xiphe/jQuery-Merge-for-php-diff).
4046

41-
Todo
42-
----
47+
## Todo
48+
4349
* Ability to ignore blank line changes
4450
* 3 way diff support
4551
* Performance optimizations
4652

47-
Contributors
48-
---------------------
53+
## Contributors
54+
4955
Contributors since I forked the repo.
5056

5157
- maxxer
5258
- Creris
5359
- jfcherng
5460

55-
License (BSD License)
56-
---------------------
57-
Copyright (c) 2009 Chris Boulton <[email protected]>
58-
59-
Copyright (c) 2015 Mario Brandt <[email protected]>
60-
61-
All rights reserved.
61+
### License (BSD License)
6262

63-
Redistribution and use in source and binary forms, with or without
64-
modification, are permitted provided that the following conditions are met:
65-
66-
- Redistributions of source code must retain the above copyright notice,
67-
this list of conditions and the following disclaimer.
68-
- Redistributions in binary form must reproduce the above copyright notice,
69-
this list of conditions and the following disclaimer in the documentation
70-
and/or other materials provided with the distribution.
71-
- Neither the name of the Chris Boulton nor the names of its contributors
72-
may be used to endorse or promote products derived from this software
73-
without specific prior written permission.
74-
75-
```
76-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
77-
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
78-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
79-
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
80-
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
81-
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
82-
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
83-
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
84-
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
85-
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
86-
POSSIBILITY OF SUCH DAMAGE.
87-
```
63+
see [License](LICENSE)

example/a.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
<pre>
1515
另外我覺得那個評價的白色櫃子有點沒有必要欸。外觀我就不說了 ,怎麼連空間都那麼狹隘。不過倒是從這個地方看出所謂的“改革” 😅😅
1616
</pre>
17+
<p>Do you know in Chinese, "金槍魚罐頭" means tuna can.</p>
1718
</body>
1819
</html>

example/b.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<pre>
1515
另外我覺得那個評鑑的白色櫃子有點沒有必要欸。外觀我就不說了 ,怎麼連空間都那麼狹隘。不過倒是從這個地方看出所謂的“改革” 😅😅
1616
</pre>
17+
<p>Do you know in Japanese, "魚の缶詰" means fish can.</p>
1718
<p>Just a small amount of new text...</p>
1819
</body>
19-
</html>
20+
</html>

lib/jblond/Diff.php

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* A comprehensive library for generating differences between two strings
1111
* in multiple formats (unified, side by side HTML etc)
1212
*
13-
* PHP version 5
13+
* PHP version 7.1 or greater
1414
*
1515
* Copyright (c) 2009 Chris Boulton <[email protected]>
1616
*
@@ -40,24 +40,24 @@
4040
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
4141
* POSSIBILITY OF SUCH DAMAGE.
4242
*
43-
* @package Diff
43+
* @package jblond
4444
* @author Chris Boulton <[email protected]>
4545
* @copyright (c) 2009 Chris Boulton
4646
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
47-
* @version 1.6
47+
* @version 1.10
4848
* @link https://github.com/JBlond/php-diff
4949
*/
5050
class Diff
5151
{
5252
/**
5353
* @var array The "old" sequence to use as the basis for the comparison.
5454
*/
55-
private $a = null;
55+
private $old = null;
5656

5757
/**
5858
* @var array The "new" sequence to generate the changes for.
5959
*/
60-
private $b = null;
60+
private $new = null;
6161

6262
/**
6363
* @var array Array containing the generated op codes for the differences between the two items.
@@ -83,14 +83,14 @@ class Diff
8383
/**
8484
* The constructor.
8585
*
86-
* @param array $a Array containing the lines of the first string to compare.
87-
* @param array $b Array containing the lines for the second string to compare.
86+
* @param array $oldArray Array containing the lines of the first string to compare.
87+
* @param array $newArray Array containing the lines for the second string to compare.
8888
* @param array $options Array for the options
8989
*/
90-
public function __construct($a, $b, $options = array())
90+
public function __construct(array $oldArray, array $newArray, array $options = array())
9191
{
92-
$this->a = $a;
93-
$this->b = $b;
92+
$this->old = $oldArray;
93+
$this->new = $newArray;
9494

9595
if (is_array($options)) {
9696
$this->options = array_merge($this->defaultOptions, $options);
@@ -119,22 +119,21 @@ public function render($renderer)
119119
* that line.
120120
*
121121
* @param int $start The starting number.
122-
* @param int $end The ending number. If not supplied, only the item in $start will be returned.
122+
* @param int|null $end The ending number. If not supplied, only the item in $start will be returned.
123123
* @return array Array of all of the lines between the specified range.
124124
*/
125-
public function getA($start = 0, $end = null) : array
125+
public function getOld(int $start = 0, $end = null) : array
126126
{
127127
if ($start == 0 && $end === null) {
128-
return $this->a;
128+
return $this->old;
129129
}
130130

131131
if ($end === null) {
132-
$length = 1;
133-
} else {
134-
$length = $end - $start;
132+
return array_slice($this->old, $start, 1);
135133
}
136134

137-
return array_slice($this->a, $start, $length);
135+
$length = $end - $start;
136+
return array_slice($this->old, $start, $length);
138137
}
139138

140139
/**
@@ -144,22 +143,21 @@ public function getA($start = 0, $end = null) : array
144143
* that line.
145144
*
146145
* @param int $start The starting number.
147-
* @param int $end The ending number. If not supplied, only the item in $start will be returned.
146+
* @param int|null $end The ending number. If not supplied, only the item in $start will be returned.
148147
* @return array Array of all of the lines between the specified range.
149148
*/
150-
public function getB($start = 0, $end = null) : array
149+
public function getNew(int $start = 0, $end = null) : array
151150
{
152151
if ($start == 0 && $end === null) {
153-
return $this->b;
152+
return $this->new;
154153
}
155154

156155
if ($end === null) {
157-
$length = 1;
158-
} else {
159-
$length = $end - $start;
156+
return array_slice($this->new, $start, 1);
160157
}
161158

162-
return array_slice($this->b, $start, $length);
159+
$length = $end - $start;
160+
return array_slice($this->new, $start, $length);
163161
}
164162

165163
/**
@@ -176,7 +174,7 @@ public function getGroupedOpcodes() : array
176174
return $this->groupedCodes;
177175
}
178176

179-
$sequenceMatcher = new SequenceMatcher($this->a, $this->b, $this->options, null);
177+
$sequenceMatcher = new SequenceMatcher($this->old, $this->new, $this->options, null);
180178
$this->groupedCodes = $sequenceMatcher->getGroupedOpcodes($this->options['context']);
181179
return $this->groupedCodes;
182180
}

0 commit comments

Comments
 (0)