Skip to content

Commit 0e230e3

Browse files
committed
Code Optimization, cleanup, refactoring and commenting.
1 parent 3e527d1 commit 0e230e3

File tree

6 files changed

+26
-25
lines changed

6 files changed

+26
-25
lines changed

example/example.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
'title_b' => 'Custom title for NEW version',
4848
]);
4949
echo $diff->Render($renderer);
50-
5150
?>
5251

5352
<h2>HTML Inline Diff</h2>

lib/jblond/Diff.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace jblond;
66

7+
use InvalidArgumentException;
78
use jblond\Diff\SequenceMatcher;
9+
use OutOfRangeException;
810

911
/**
1012
* Diff
@@ -63,7 +65,7 @@ class Diff
6365
/**
6466
* The constructor.
6567
*
66-
* The first two parameters define the data to compare to eachother.
68+
* The first two parameters define the data to compare to each other.
6769
* The values can be of type string or array.
6870
* If the type is string, it's split into array elements by line-end characters.
6971
*
@@ -89,13 +91,15 @@ public function __construct($old, $new, array $options = [])
8991

9092
/**
9193
* Set the options to be used by the sequence matcher, called by this class.
92-
* @see Diff::getGroupedOpcodes()
94+
*
95+
* @param array $options User defined option names and values.
96+
*
97+
*@see Diff::$defaultOptions
98+
*
99+
* @see Diff::getGroupedOpCodes()
93100
*
94101
* When a keyName matches the name of a default option, that option's value will be overridden by the key's value.
95102
* Any other keyName (and it's value) will be added as an option, but will not be used if not implemented.
96-
* @see Diff::$defaultOptions
97-
*
98-
* @param array $options User defined option names and values.
99103
*/
100104
public function setOptions(array $options)
101105
{
@@ -152,14 +156,14 @@ public function render(object $renderer)
152156
* @param int|null $end The last element of the range to get.
153157
* If not supplied, only the element at start will be returned.
154158
*
155-
* @throws \OutOfRangeException When the value of start or end are invalid to define a range.
159+
* @throws OutOfRangeException When the value of start or end are invalid to define a range.
156160
*
157161
* @return array Array containing all of the elements of the specified range.
158162
*/
159163
public function getArrayRange(array $array, int $start = 0, $end = null): array
160164
{
161165
if ($start < 0 || $end < 0 || $end < $start) {
162-
throw new \OutOfRangeException('Start parameter must be lower than End parameter while both are positive!');
166+
throw new OutOfRangeException('Start parameter must be lower than End parameter while both are positive!');
163167
}
164168

165169
if ($start == 0 && $end === null) {
@@ -187,7 +191,7 @@ public function getArrayRange(array $array, int $start = 0, $end = null): array
187191
*
188192
* @param mixed $var Variable to get type from.
189193
*
190-
* @throws \InvalidArgumentException When the type isn't 'array' or 'string'.
194+
* @throws InvalidArgumentException When the type isn't 'array' or 'string'.
191195
*
192196
* @return int Number indicating the type of the variable. 0 for array type and 1 for string type.
193197
*/
@@ -199,7 +203,7 @@ public function getArgumentType($var): int
199203
case (is_string($var)):
200204
return 1;
201205
default:
202-
throw new \InvalidArgumentException('Invalid argument type! Argument must be of type array or string.');
206+
throw new InvalidArgumentException('Invalid argument type! Argument must be of type array or string.');
203207
}
204208
}
205209

@@ -212,15 +216,15 @@ public function getArgumentType($var): int
212216
*
213217
* @return array Array of the grouped op-codes for the generated diff.
214218
*/
215-
public function getGroupedOpcodes(): array
219+
public function getGroupedOpCodes(): array
216220
{
217221
if ($this->groupedCodes !== null) {
218222
//Return the cached results.
219223
return $this->groupedCodes;
220224
}
221225

222226
//Get and cache the grouped op-codes.
223-
$sequenceMatcher = new SequenceMatcher($this->old, $this->new, $this->options, null);
227+
$sequenceMatcher = new SequenceMatcher($this->old, $this->new, $this->options);
224228
$this->groupedCodes = $sequenceMatcher->getGroupedOpCodes($this->options['context']);
225229

226230
return $this->groupedCodes;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function render()
111111

112112
$changes = [];
113113

114-
foreach ($this->diff->getGroupedOpcodes() as $group) {
114+
foreach ($this->diff->getGroupedOpCodes() as $group) {
115115
$blocks = [];
116116
$this->lastTag = null;
117117

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,10 @@ public function generateTableRowsReplace(array $changes): string
169169
//TODO: Is below comparison result ever false?
170170
if (count($changes['base']['lines']) >= count($changes['changed']['lines'])) {
171171
foreach ($changes['base']['lines'] as $lineNo => $line) {
172-
$fromLine = $changes['base']['offset'] + $lineNo + 1;
173-
if (!isset($changes['changed']['lines'][$lineNo])) {
174-
$toLine = "&nbsp;";
175-
$changedLine = "&nbsp;";
176-
} else {
172+
$fromLine = $changes['base']['offset'] + $lineNo + 1;
173+
$toLine = "&nbsp;";
174+
$changedLine = "&nbsp;";
175+
if (isset($changes['changed']['lines'][$lineNo])) {
177176
$toLine = $changes['changed']['offset'] + $lineNo + 1;
178177
$changedLine = $changes['changed']['lines'][$lineNo];
179178
}
@@ -196,11 +195,10 @@ public function generateTableRowsReplace(array $changes): string
196195
}
197196

198197
foreach ($changes['changed']['lines'] as $lineNo => $changedLine) {
199-
$toLine = $changes['changed']['offset'] + $lineNo + 1;
200-
if (!isset($changes['base']['lines'][$lineNo])) {
201-
$fromLine = "&nbsp;";
202-
$line = "&nbsp;";
203-
} else {
198+
$toLine = $changes['changed']['offset'] + $lineNo + 1;
199+
$fromLine = "&nbsp;";
200+
$line = "&nbsp;";
201+
if (isset($changes['base']['lines'][$lineNo])) {
204202
$fromLine = $changes['base']['offset'] + $lineNo + 1;
205203
$line = $changes['base']['lines'][$lineNo];
206204
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Context extends RendererAbstract
3838
public function render(): string
3939
{
4040
$diff = '';
41-
$opCodes = $this->diff->getGroupedOpcodes();
41+
$opCodes = $this->diff->getGroupedOpCodes();
4242

4343
foreach ($opCodes as $group) {
4444
$diff .= "***************\n";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Unified extends RendererAbstract
3232
public function render(): string
3333
{
3434
$diff = '';
35-
$opCodes = $this->diff->getGroupedOpcodes();
35+
$opCodes = $this->diff->getGroupedOpCodes();
3636
foreach ($opCodes as $group) {
3737
$lastItem = count($group) - 1;
3838
$i1 = $group['0']['1'];

0 commit comments

Comments
 (0)