Skip to content

Commit f3a8f0d

Browse files
committed
Following up #42.
- Removed redundant method SequenceMatcherHelper::arrayGetDefault(). - Renamed SequenceMatcherHelper to more generic name DiffUtils. - Changed calls to SequenceMatcherHelper::arrayGetDefault() into comparison with null coalesce operator. - Moved helper class from renderer directory to Diff directory. - Changed namespaces to reflect above changes.
1 parent 3a52371 commit f3a8f0d

File tree

2 files changed

+7
-26
lines changed

2 files changed

+7
-26
lines changed

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

Lines changed: 3 additions & 21 deletions
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,34 +13,16 @@
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-
protected 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
*
3921
* @param array $aArray First array to compare.
4022
* @param array $bArray Second array to compare.
4123
* @return int -1, 0 or 1, as expected by the usort function.
4224
*/
43-
protected function tupleSort(array $aArray, array $bArray): int
25+
public static function tupleSort(array $aArray, array $bArray): int
4426
{
4527
$max = max(count($aArray), count($bArray));
4628
for ($counter = 0; $counter < $max; ++$counter) {

lib/jblond/Diff/SequenceMatcher.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace jblond\Diff;
66

77
use InvalidArgumentException;
8-
use jblond\Diff\Renderer\SequenceMatcherHelper;
98

109
/**
1110
* Sequence matcher for Diff
@@ -21,7 +20,7 @@
2120
* @version 2.0.0
2221
* @link https://github.com/JBlond/php-diff
2322
*/
24-
class SequenceMatcher extends SequenceMatcherHelper
23+
class SequenceMatcher
2524
{
2625
/**
2726
* @var string|array Either a string or an array containing a callback function to determine
@@ -258,15 +257,15 @@ public function findLongestMatch(int $aLower, int $aUpper, int $bLower, int $bUp
258257

259258
for ($i = $aLower; $i < $aUpper; ++$i) {
260259
$newJ2Len = [];
261-
$jDict = $this->arrayGetDefault($this->b2j, $old[$i], $nothing);
260+
$jDict = $this->b2j[$old[$i]] ?? $nothing;
262261
foreach ($jDict as $j) {
263262
if ($j < $bLower) {
264263
continue;
265264
} elseif ($j >= $bUpper) {
266265
break;
267266
}
268267

269-
$k = $this->arrayGetDefault($j2Len, $j - 1, 0) + 1;
268+
$k = ($j2Len[$j - 1] ?? 0) + 1;
270269
$newJ2Len[$j] = $k;
271270
if ($k > $bestSize) {
272271
$bestI = $i - $k + 1;
@@ -413,7 +412,7 @@ public function getMatchingBlocks(): array
413412
usort(
414413
$matchingBlocks,
415414
function ($aArray, $bArray) {
416-
return $this->tupleSort($aArray, $bArray);
415+
return DiffUtils::tupleSort($aArray, $bArray);
417416
}
418417
);
419418

0 commit comments

Comments
 (0)