Skip to content

Commit 8e35b71

Browse files
authored
Merge pull request #13 from JBlond/development
code clean up
2 parents 6344e5f + 3af88d2 commit 8e35b71

File tree

12 files changed

+141
-186
lines changed

12 files changed

+141
-186
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/.idea
12
/vendor

README.md

Lines changed: 21 additions & 16 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,40 +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

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

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

31-
Requirements
32-
-----------
37+
## Requirements
38+
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-
---------------------
61+
### License (BSD License)
5762

5863
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: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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.9
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(array $a, array $b, array $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);
@@ -122,19 +122,18 @@ public function render($renderer)
122122
* @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(int $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
/**
@@ -147,19 +146,18 @@ public function getA(int $start = 0, $end = null) : array
147146
* @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(int $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
}

lib/jblond/Diff/Renderer/Html/HtmlArray.php

Lines changed: 16 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,13 @@
3737
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3838
* POSSIBILITY OF SUCH DAMAGE.
3939
*
40-
* @package DiffLib
40+
* @package jblond\Diff\Renderer\Html
4141
* @author Chris Boulton <[email protected]>
4242
* @copyright (c) 2009 Chris Boulton
4343
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
44-
* @version 1.9
44+
* @version 1.10
4545
* @link https://github.com/JBlond/php-diff
4646
*/
47-
48-
/**
49-
* Class Diff_Renderer_Html_Array
50-
*/
5147
class HtmlArray extends RendererAbstract
5248
{
5349
/**
@@ -59,62 +55,6 @@ class HtmlArray extends RendererAbstract
5955
'title_b' => 'New Version',
6056
);
6157

62-
/**
63-
* From https://gist.github.com/stemar/8287074
64-
* @param string $string The input string.
65-
* @param string $replacement The replacement string.
66-
* @param int $start If start is positive, the replacing will begin at the start'th offset into string.
67-
* If start is negative, the replacing will begin at the start'th character from the end of string.
68-
* @param int|null $length If given and is positive, it represents the length of the portion of string which is to
69-
* be replaced. If it is negative, it represents the number of characters from the end of string at which to
70-
* stop replacing. If it is not given, then it will default to strlen( string ); i.e. end the replacing at the
71-
* end of string. Of course, if length is zero then this function will have the effect of inserting replacement
72-
* into string at the given start offset.
73-
* @return string|array The result string is returned. If string is an array then array is returned.
74-
*/
75-
public function mbSubstrReplace(string $string, string $replacement, int $start, $length = null)
76-
{
77-
if (is_array($string)) {
78-
$num = count($string);
79-
// $replacement
80-
if (is_array($replacement)) {
81-
$replacement = array_slice($replacement, 0, $num);
82-
} else {
83-
$replacement = array_pad(array($replacement), $num, $replacement);
84-
}
85-
86-
// $start
87-
if (is_array($start)) {
88-
$start = array_slice($start, 0, $num);
89-
foreach ($start as $key => $value) {
90-
$start[$key] = is_int($value) ? $value : 0;
91-
}
92-
} else {
93-
$start = array_pad(array($start), $num, $start);
94-
}
95-
// $length
96-
if (!isset($length)) {
97-
$length = array_fill(0, $num, 0);
98-
} elseif (is_array($length)) {
99-
$length = array_slice($length, 0, $num);
100-
foreach ($length as $key => $value) {
101-
$length[$key] = isset($value) ? (is_int($value) ? $value : $num) : 0;
102-
}
103-
} else {
104-
$length = array_pad(array($length), $num, $length);
105-
}
106-
// Recursive call
107-
return array_map(array($this, 'mbSubstrReplace'), $string, $replacement, $start, $length);
108-
}
109-
preg_match_all('/./us', (string)$string, $smatches);
110-
preg_match_all('/./us', (string)$replacement, $rmatches);
111-
if ($length === null) {
112-
$length = mb_strlen($string);
113-
}
114-
array_splice($smatches['0'], $start, $length, $rmatches[0]);
115-
return join($smatches['0']);
116-
}
117-
11858
/**
11959
* @param string|array $changes
12060
* @param SideBySide|Inline $object
@@ -175,8 +115,8 @@ public function render()
175115
// As we'll be modifying a & b to include our change markers,
176116
// we need to get the contents and store them here. That way
177117
// we're not going to destroy the original data
178-
$a = $this->diff->getA();
179-
$b = $this->diff->getB();
118+
$a = $this->diff->getOld();
119+
$b = $this->diff->getNew();
180120

181121
$changes = array();
182122
$opCodes = $this->diff->getGroupedOpcodes();
@@ -281,9 +221,19 @@ private function getChangeExtent(string $fromLine, string $toLine)
281221
protected function formatLines(array $lines) : array
282222
{
283223
if ($this->options['tabSize'] !== false) {
284-
$lines = array_map(array($this, 'ExpandTabs'), $lines);
224+
$lines = array_map(
225+
function ($item) {
226+
return $this->expandTabs($item);
227+
},
228+
$lines
229+
);
285230
}
286-
$lines = array_map(array($this, 'HtmlSafe'), $lines);
231+
$lines = array_map(
232+
function ($item) {
233+
return $this->htmlSafe($item);
234+
},
235+
$lines
236+
);
287237
foreach ($lines as &$line) {
288238
$line = preg_replace_callback('# ( +)|^ #', array($this, 'fixSpaces'), $line);
289239
}

lib/jblond/Diff/Renderer/Html/Inline.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,13 @@
3535
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3636
* POSSIBILITY OF SUCH DAMAGE.
3737
*
38-
* @package DiffLib
38+
* @package jblond\Diff\Renderer\Html
3939
* @author Chris Boulton <[email protected]>
4040
* @copyright (c) 2009 Chris Boulton
4141
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
42-
* @version 1.9
42+
* @version 1.10
4343
* @link https://github.com/JBlond/php-diff
4444
*/
45-
46-
/**
47-
* Class Diff_Renderer_Html_Inline
48-
*/
4945
class Inline extends HtmlArray
5046
{
5147
/**

lib/jblond/Diff/Renderer/Html/SideBySide.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,13 @@
3535
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3636
* POSSIBILITY OF SUCH DAMAGE.
3737
*
38-
* @package DiffLib
38+
* @package jblond\Diff\Renderer\Html
3939
* @author Chris Boulton <[email protected]>
4040
* @copyright (c) 2009 Chris Boulton
4141
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
42-
* @version 1.9
42+
* @version 1.10
4343
* @link https://github.com/JBlond/php-diff
4444
*/
45-
46-
/**
47-
* Class Diff_Renderer_Html_SideBySide
48-
*/
4945
class SideBySide extends HtmlArray
5046
{
5147
/**

lib/jblond/Diff/Renderer/RendererAbstract.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@
3535
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3636
* POSSIBILITY OF SUCH DAMAGE.
3737
*
38-
* @package DiffLib
38+
* @package jblond\Diff\Renderer
3939
* @author Chris Boulton <[email protected]>
4040
* @copyright (c) 2009 Chris Boulton
4141
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
42-
* @version 1.9
42+
* @version 1.10
4343
* @link https://github.com/JBlond/php-diff
4444
*/
4545
abstract class RendererAbstract

0 commit comments

Comments
 (0)