Skip to content

Commit c29a8aa

Browse files
committed
Code Optimization, cleanup, refactoring and commenting.
1 parent 60cb97f commit c29a8aa

File tree

12 files changed

+95
-93
lines changed

12 files changed

+95
-93
lines changed

example/example.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818

1919
// Options for generating the diff.
2020
$options = [
21-
//'ignoreWhitespace' => true,
22-
//'ignoreCase' => true,
23-
//'context' => 2, /* Remove // to see text collapse in action */
21+
'ignoreWhitespace' => true,
22+
'ignoreCase' => true,
23+
'context' => 2,
2424
];
2525

2626
// Initialize the diff class.
27-
// \jblond\diff
28-
$diff = new Diff($a, $b, $options);
27+
$diff = new Diff($a, $b /*, $options */);
2928
?><!DOCTYPE html>
3029
<html lang="en">
3130
<head>
@@ -43,8 +42,8 @@
4342
// Generate a side by side diff.
4443
// \jblond\Diff\Renderer\Html
4544
$renderer = new SideBySide([
46-
'title_a' => 'Custom title for OLD version',
47-
'title_b' => 'Custom title for NEW version',
45+
'title1' => 'Custom title for version1',
46+
'title2' => 'Custom title for version2',
4847
]);
4948
echo $diff->Render($renderer);
5049
?>

lib/jblond/Diff.php

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,29 @@
1616
*
1717
* PHP version 7.2 or greater
1818
*
19-
* @package jblond
20-
* @author Chris Boulton <chris.boulton@interspire.com>
19+
* @package jblond
20+
* @author Chris Boulton <chris.boulton@interspire.com>
2121
* @copyright (c) 2009 Chris Boulton
22-
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
23-
* @version 1.15
24-
* @link https://github.com/JBlond/php-diff
22+
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
23+
* @version 1.15
24+
* @link https://github.com/JBlond/php-diff
2525
*/
2626
class Diff
2727
{
2828
/**
29-
* @var array The "old" string to compare to.
29+
* @var array The first version to compare.
3030
* Each element contains a line of this string.
3131
*/
32-
private $old;
32+
private $version1;
3333

3434
/**
35-
* @var array The "new" string to compare.
35+
* @var array The second version to compare.
3636
* Each element contains a line of this string.
3737
*/
38-
private $new;
38+
private $version2;
3939

4040
/**
41-
* @var array Contains generated op-codes which represent the differences between "old" and "new".
41+
* @var array Contains generated op-codes which represent the differences between version1 and version2.
4242
*/
4343
private $groupedCodes;
4444

@@ -47,18 +47,20 @@ class Diff
4747
* value.
4848
* - context The amount of lines to include around blocks that differ.
4949
* - ignoreWhitespace When true, tabs and spaces are ignored while comparing.
50+
* The spacing of version1 is leading.
5051
* - ignoreCase When true, character casing is ignored while comparing.
52+
* The casing of version1 is leading.
5153
*/
5254
private $defaultOptions = [
53-
'context' => 3,
54-
'ignoreWhitespace' => false,
55-
'ignoreCase' => false,
55+
'context' => 3,
56+
'ignoreWhitespace' => false,
57+
'ignoreCase' => false,
5658
];
5759

5860
/**
5961
* @var array Associative array containing the options that will be applied for generating the diff.
6062
* The key-value pairs are set at the constructor of this class.
61-
* @see Diff::setOptions()
63+
* @see Diff::setOptions()
6264
*/
6365
private $options = [];
6466

@@ -73,28 +75,55 @@ class Diff
7375
* associative array where each key-value pair represents an option and its value (E.g. ['context' => 3], ...).
7476
* When a keyName matches the name of a default option, that option's value will be overridden by the key's value.
7577
* Any other keyName (and it's value) can be added as an option, but will not be used if not implemented.
78+
*
79+
* @param string|array $version1 Data to compare to.
80+
* @param string|array $version2 Data to compare.
81+
* @param array $options User defined option values.
82+
*
7683
* @see Diff::$defaultOptions
7784
*
78-
* @param string|array $old Data to compare to.
79-
* @param string|array $new Data to compare.
80-
* @param array $options User defined option values.
8185
*/
82-
public function __construct($old, $new, array $options = [])
86+
public function __construct($version1, $version2, array $options = [])
8387
{
8488
//Convert "old" and "new" into an array of lines when they are strings.
85-
$this->old = $this->getArgumentType($old) ? preg_split("/\r\n|\n|\r/", $old) : $old;
86-
$this->new = $this->getArgumentType($new) ? preg_split("/\r\n|\n|\r/", $new) : $new;
89+
$this->version1 = $this->getArgumentType($version1) ? preg_split("/\r\n|\n|\r/", $version1) : $version1;
90+
$this->version2 = $this->getArgumentType($version2) ? preg_split("/\r\n|\n|\r/", $version2) : $version2;
8791

8892
//Override the default options, define others.
8993
$this->setOptions($options);
9094
}
9195

96+
/**
97+
* Get the type of a variable.
98+
*
99+
* The return value depend on the type of variable:
100+
* 0 If the type is 'array'
101+
* 1 if the type is 'string'
102+
*
103+
* @param mixed $var Variable to get type from.
104+
*
105+
* @return int Number indicating the type of the variable. 0 for array type and 1 for string type.
106+
* @throws InvalidArgumentException When the type isn't 'array' or 'string'.
107+
*
108+
*/
109+
public function getArgumentType($var): int
110+
{
111+
switch (true) {
112+
case (is_array($var)):
113+
return 0;
114+
case (is_string($var)):
115+
return 1;
116+
default:
117+
throw new InvalidArgumentException('Invalid argument type! Argument must be of type array or string.');
118+
}
119+
}
120+
92121
/**
93122
* Set the options to be used by the sequence matcher, called by this class.
94123
*
95124
* @param array $options User defined option names and values.
96125
*
97-
*@see Diff::$defaultOptions
126+
* @see Diff::$defaultOptions
98127
*
99128
* @see Diff::getGroupedOpCodes()
100129
*
@@ -111,22 +140,21 @@ public function setOptions(array $options)
111140
*
112141
* @return array Contains the lines of the "old" string to compare to.
113142
*/
114-
public function getOld(): array
143+
public function getVersion1(): array
115144
{
116-
return $this->old;
145+
return $this->version1;
117146
}
118147

119148
/**
120149
* Get the lines of "new".
121150
*
122151
* @return array Contains the lines of the "new" string to compare.
123152
*/
124-
public function getNew(): array
153+
public function getVersion2(): array
125154
{
126-
return $this->new;
155+
return $this->version2;
127156
}
128157

129-
130158
/**
131159
* Render a diff-view using a rendering class and get its results.
132160
*
@@ -151,14 +179,14 @@ public function render(object $renderer)
151179
* If the arguments for both parameters are omitted, the entire array will be returned.
152180
* If the argument for the second parameter is ommitted, the element defined as start will be returned.
153181
*
154-
* @param array $array The source array.
155-
* @param int $start The first element of the range to get.
156-
* @param int|null $end The last element of the range to get.
182+
* @param array $array The source array.
183+
* @param int $start The first element of the range to get.
184+
* @param int|null $end The last element of the range to get.
157185
* If not supplied, only the element at start will be returned.
158186
*
187+
* @return array Array containing all of the elements of the specified range.
159188
* @throws OutOfRangeException When the value of start or end are invalid to define a range.
160189
*
161-
* @return array Array containing all of the elements of the specified range.
162190
*/
163191
public function getArrayRange(array $array, int $start = 0, $end = null): array
164192
{
@@ -182,31 +210,6 @@ public function getArrayRange(array $array, int $start = 0, $end = null): array
182210
return array_slice($array, $start, $length);
183211
}
184212

185-
/**
186-
* Get the type of a variable.
187-
*
188-
* The return value depend on the type of variable:
189-
* 0 If the type is 'array'
190-
* 1 if the type is 'string'
191-
*
192-
* @param mixed $var Variable to get type from.
193-
*
194-
* @throws InvalidArgumentException When the type isn't 'array' or 'string'.
195-
*
196-
* @return int Number indicating the type of the variable. 0 for array type and 1 for string type.
197-
*/
198-
public function getArgumentType($var): int
199-
{
200-
switch (true) {
201-
case (is_array($var)):
202-
return 0;
203-
case (is_string($var)):
204-
return 1;
205-
default:
206-
throw new InvalidArgumentException('Invalid argument type! Argument must be of type array or string.');
207-
}
208-
}
209-
210213
/**
211214
* Generate a list of the compiled and grouped op-codes for the differences between two strings.
212215
*
@@ -224,7 +227,7 @@ public function getGroupedOpCodes(): array
224227
}
225228

226229
//Get and cache the grouped op-codes.
227-
$sequenceMatcher = new SequenceMatcher($this->old, $this->new, $this->options);
230+
$sequenceMatcher = new SequenceMatcher($this->version1, $this->version2, $this->options);
228231
$this->groupedCodes = $sequenceMatcher->getGroupedOpCodes($this->options['context']);
229232

230233
return $this->groupedCodes;

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class HtmlArray extends RendererAbstract
2929
*/
3030
protected $defaultOptions = [
3131
'tabSize' => 4,
32-
'title_a' => 'Old Version',
33-
'title_b' => 'New Version',
32+
'title1' => 'Version1',
33+
'title2' => 'Version2',
3434
];
3535

3636
/**
@@ -106,8 +106,8 @@ public function renderHtml(array $changes, object $htmlRenderer): string
106106
public function render()
107107
{
108108
// The old and New texts are copied so change markers can be added without modifying the original sequences.
109-
$oldText = $this->diff->getOld();
110-
$newText = $this->diff->getNew();
109+
$oldText = $this->diff->getVersion1();
110+
$newText = $this->diff->getVersion2();
111111

112112
$changes = [];
113113

@@ -116,7 +116,7 @@ public function render()
116116
$this->lastTag = null;
117117

118118
foreach ($group as $code) {
119-
list($tag, $startOld, $endOld, $startNew, $endNew) = $code;
119+
[$tag, $startOld, $endOld, $startNew, $endNew] = $code;
120120
/**
121121
* $code is an array describing a op-code which includes:
122122
* 0 - The type of tag (as described below) for the op code.
@@ -203,7 +203,7 @@ private function markInlineChange(array &$oldText, array &$newText, $startOld, $
203203
$newString = $newText[$startNew + $i];
204204

205205
// Determine the start and end position of the line difference.
206-
list($start, $end) = $this->getInlineChange($oldString, $newString);
206+
[$start, $end] = $this->getInlineChange($oldString, $newString);
207207
if ($start != 0 || $end != 0) {
208208
// Changes between the lines exist.
209209
// Add markers around the changed character sequence in the old string.
@@ -265,7 +265,7 @@ private function getInlineChange(string $oldString, string $newString): array
265265

266266
return [
267267
$start,
268-
$end + 1
268+
$end + 1,
269269
];
270270
}
271271

@@ -292,12 +292,12 @@ private function appendChangesArray(array &$blocks, string $tag, int $lineInOld,
292292
'tag' => $tag,
293293
'base' => [
294294
'offset' => $lineInOld,
295-
'lines' => []
295+
'lines' => [],
296296
],
297297
'changed' => [
298298
'offset' => $lineInNew,
299-
'lines' => []
300-
]
299+
'lines' => [],
300+
],
301301
];
302302

303303
$this->lastTag = $tag;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public function generateTableHeader(): string
4242
<table class="Differences DifferencesInline">
4343
<thead>
4444
<tr>
45-
<th>{$this->options['title_a']}</th>
46-
<th>{$this->options['title_b']}</th>
45+
<th>{$this->options['title1']}</th>
46+
<th>{$this->options['title2']}</th>
4747
<th>Differences</th>
4848
</tr>
4949
</thead>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public function generateTableHeader(): string
4141
<table class="Differences DifferencesSideBySide">
4242
<thead>
4343
<tr>
44-
<th colspan="2">{$this->options['title_a']}</th>
45-
<th colspan="2">{$this->options['title_b']}</th>
44+
<th colspan="2">{$this->options['title1']}</th>
45+
<th colspan="2">{$this->options['title2']}</th>
4646
</tr>
4747
</thead>
4848
HTML;

lib/jblond/Diff/Renderer/RendererAbstract.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ abstract class RendererAbstract
3030
* @var array Array of the default options that apply to this renderer.
3131
*/
3232
protected $defaultOptions = array(
33-
'title_a' => 'Old Version',
34-
'title_b' => 'New Version',
33+
'title1' => 'Version1',
34+
'title2' => 'Version2',
3535
);
3636

3737
/**

lib/jblond/Diff/Renderer/Text/Context.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function render(): string
7777
$diff .= $this->tagMap[$tag] . ' ' .
7878
implode(
7979
"\n" . $this->tagMap[$tag] . ' ',
80-
$this->diff->getArrayRange($this->diff->getOld(), $i1, $i2)
80+
$this->diff->getArrayRange($this->diff->getVersion1(), $i1, $i2)
8181
) . "\n";
8282
}
8383
}
@@ -101,7 +101,7 @@ public function render(): string
101101
$diff .= $this->tagMap[$tag] . ' ' .
102102
implode(
103103
"\n" . $this->tagMap[$tag] . ' ',
104-
$this->diff->getArrayRange($this->diff->getNew(), $j1, $j2)
104+
$this->diff->getArrayRange($this->diff->getVersion2(), $j1, $j2)
105105
) . "\n";
106106
}
107107
}

lib/jblond/Diff/Renderer/Text/Unified.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,22 @@ public function render(): string
5151
$diff .= ' ' .
5252
implode(
5353
"\n ",
54-
$this->diff->getArrayRange($this->diff->getOld(), $i1, $i2)
54+
$this->diff->getArrayRange($this->diff->getVersion1(), $i1, $i2)
5555
) . "\n";
5656
} else {
5757
if ($tag == 'replace' || $tag == 'delete') {
5858
$diff .= '-' .
5959
implode(
6060
"\n-",
61-
$this->diff->getArrayRange($this->diff->getOld(), $i1, $i2)
61+
$this->diff->getArrayRange($this->diff->getVersion1(), $i1, $i2)
6262
) . "\n";
6363
}
6464

6565
if ($tag == 'replace' || $tag == 'insert') {
6666
$diff .= '+' .
6767
implode(
6868
"\n+",
69-
$this->diff->getArrayRange($this->diff->getNew(), $j1, $j2)
69+
$this->diff->getArrayRange($this->diff->getVersion2(), $j1, $j2)
7070
) . "\n";
7171
}
7272
}

0 commit comments

Comments
 (0)