Skip to content

Commit a1e61fb

Browse files
committed
remove some complexity
1 parent f44ee81 commit a1e61fb

File tree

2 files changed

+64
-46
lines changed

2 files changed

+64
-46
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace jblond\Diff\Renderer;
4+
5+
/**
6+
* Sequence matcher helper functions for Diff
7+
*
8+
* PHP version 7.2 or greater
9+
*
10+
* @package jblond\Diff
11+
* @author Mario Brandt <[email protected]>
12+
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
13+
* @version 2.0.0
14+
* @link https://github.com/JBlond/php-diff
15+
*/
16+
class SequenceMatcherHelper
17+
{
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+
36+
/**
37+
* Sort an array by the nested arrays it contains. Helper function for getMatchingBlocks
38+
*
39+
* @param array $aArray First array to compare.
40+
* @param array $bArray Second array to compare.
41+
* @return int -1, 0 or 1, as expected by the usort function.
42+
*/
43+
protected function tupleSort(array $aArray, array $bArray): int
44+
{
45+
$max = max(count($aArray), count($bArray));
46+
for ($counter = 0; $counter < $max; ++$counter) {
47+
if ($aArray[$counter] < $bArray[$counter]) {
48+
return -1;
49+
} elseif ($aArray[$counter] > $bArray[$counter]) {
50+
return 1;
51+
}
52+
}
53+
54+
if (count($aArray) == count($bArray)) {
55+
return 0;
56+
}
57+
if (count($aArray) < count($bArray)) {
58+
return -1;
59+
}
60+
return 1;
61+
}
62+
}

lib/jblond/Diff/SequenceMatcher.php

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

77
use InvalidArgumentException;
8+
use jblond\Diff\Renderer\SequenceMatcherHelper;
89

910
/**
1011
* Sequence matcher for Diff
@@ -20,7 +21,7 @@
2021
* @version 2.0.0
2122
* @link https://github.com/JBlond/php-diff
2223
*/
23-
class SequenceMatcher
24+
class SequenceMatcher extends SequenceMatcherHelper
2425
{
2526
/**
2627
* @var string|array Either a string or an array containing a callback function to determine
@@ -614,49 +615,4 @@ public function getGroupedOpCodes(): array
614615

615616
return $groups;
616617
}
617-
618-
/**
619-
* Helper function that provides the ability to return the value for a key
620-
* in an array of it exists, or if it doesn't then return a default value.
621-
* Essentially cleaner than doing a series of if (isset()) {} else {} calls.
622-
*
623-
* @param array $array The array to search.
624-
* @param string|int $key The key to check that exists.
625-
* @param mixed $default The value to return as the default value if the key doesn't exist.
626-
* @return mixed The value from the array if the key exists or otherwise the default.
627-
*/
628-
private function arrayGetDefault(array $array, $key, $default)
629-
{
630-
if (isset($array[$key])) {
631-
return $array[$key];
632-
}
633-
return $default;
634-
}
635-
636-
/**
637-
* Sort an array by the nested arrays it contains. Helper function for getMatchingBlocks
638-
*
639-
* @param array $aArray First array to compare.
640-
* @param array $bArray Second array to compare.
641-
* @return int -1, 0 or 1, as expected by the usort function.
642-
*/
643-
private function tupleSort(array $aArray, array $bArray): int
644-
{
645-
$max = max(count($aArray), count($bArray));
646-
for ($counter = 0; $counter < $max; ++$counter) {
647-
if ($aArray[$counter] < $bArray[$counter]) {
648-
return -1;
649-
} elseif ($aArray[$counter] > $bArray[$counter]) {
650-
return 1;
651-
}
652-
}
653-
654-
if (count($aArray) == count($bArray)) {
655-
return 0;
656-
}
657-
if (count($aArray) < count($bArray)) {
658-
return -1;
659-
}
660-
return 1;
661-
}
662618
}

0 commit comments

Comments
 (0)