Skip to content

Commit ef68e3a

Browse files
authored
Merge pull request #44 from JBlond/followup-42
Followup 42
2 parents 1c9d9d7 + 78bfe41 commit ef68e3a

File tree

2 files changed

+5
-25
lines changed

2 files changed

+5
-25
lines changed

lib/jblond/Diff/Renderer/SequenceMatcherHelper.php renamed to lib/jblond/Diff/DiffUtils.php

+2-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace jblond\Diff\Renderer;
3+
namespace jblond\Diff;
44

55
/**
66
* Sequence matcher helper functions for Diff
@@ -13,26 +13,8 @@
1313
* @version 2.0.0
1414
* @link https://github.com/JBlond/php-diff
1515
*/
16-
class SequenceMatcherHelper
16+
class DiffUtils
1717
{
18-
/**
19-
* Helper function that provides the ability to return the value for a key
20-
* in an array of it exists, or if it doesn't then return a default value.
21-
* Essentially cleaner than doing a series of if (isset()) {} else {} calls.
22-
*
23-
* @param array $array The array to search.
24-
* @param string|int $key The key to check that exists.
25-
* @param mixed $default The value to return as the default value if the key doesn't exist.
26-
* @return mixed The value from the array if the key exists or otherwise the default.
27-
*/
28-
public static function arrayGetDefault(array $array, $key, $default)
29-
{
30-
if (isset($array[$key])) {
31-
return $array[$key];
32-
}
33-
return $default;
34-
}
35-
3618
/**
3719
* Sort an array by the nested arrays it contains. Helper function for getMatchingBlocks
3820
*

lib/jblond/Diff/SequenceMatcher.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
namespace jblond\Diff;
66

77
use InvalidArgumentException;
8-
use jblond\Diff\Renderer\SequenceMatcherHelper;
9-
108

119
/**
1210
* Sequence matcher for Diff
@@ -259,15 +257,15 @@ public function findLongestMatch(int $aLower, int $aUpper, int $bLower, int $bUp
259257

260258
for ($i = $aLower; $i < $aUpper; ++$i) {
261259
$newJ2Len = [];
262-
$jDict = SequenceMatcherHelper::arrayGetDefault($this->b2j, $old[$i], $nothing);
260+
$jDict = $this->b2j[$old[$i]] ?? $nothing;
263261
foreach ($jDict as $j) {
264262
if ($j < $bLower) {
265263
continue;
266264
} elseif ($j >= $bUpper) {
267265
break;
268266
}
269267

270-
$k = SequenceMatcherHelper::arrayGetDefault($j2Len, $j - 1, 0) + 1;
268+
$k = ($j2Len[$j - 1] ?? 0) + 1;
271269
$newJ2Len[$j] = $k;
272270
if ($k > $bestSize) {
273271
$bestI = $i - $k + 1;
@@ -414,7 +412,7 @@ public function getMatchingBlocks(): array
414412
usort(
415413
$matchingBlocks,
416414
function ($aArray, $bArray) {
417-
return SequenceMatcherHelper::tupleSort($aArray, $bArray);
415+
return DiffUtils::tupleSort($aArray, $bArray);
418416
}
419417
);
420418

0 commit comments

Comments
 (0)