Skip to content

Commit 3a52371

Browse files
authored
Merge pull request #42 from JBlond/remove-complexity
Remove complexity
2 parents f44ee81 + b48cbe6 commit 3a52371

File tree

2 files changed

+90
-72
lines changed

2 files changed

+90
-72
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: 28 additions & 72 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
@@ -236,32 +237,32 @@ private function isBJunk(string $bString): bool
236237
* that the junk element appears in the block. Extend it as far as possible
237238
* by matching only junk elements in both $a and $b.
238239
*
239-
* @param int $alo The lower constraint for the first sequence.
240-
* @param int $ahi The upper constraint for the first sequence.
241-
* @param int $blo The lower constraint for the second sequence.
242-
* @param int $bhi The upper constraint for the second sequence.
240+
* @param int $aLower The lower constraint for the first sequence.
241+
* @param int $aUpper The upper constraint for the first sequence.
242+
* @param int $bLower The lower constraint for the second sequence.
243+
* @param int $bUpper The upper constraint for the second sequence.
243244
* @return array Array containing the longest match that includes the starting position in $a,
244245
* start in $b and the length/size.
245246
*/
246-
public function findLongestMatch(int $alo, int $ahi, int $blo, int $bhi): array
247+
public function findLongestMatch(int $aLower, int $aUpper, int $bLower, int $bUpper): array
247248
{
248249
$old = $this->old;
249250
$new = $this->new;
250251

251-
$bestI = $alo;
252-
$bestJ = $blo;
252+
$bestI = $aLower;
253+
$bestJ = $bLower;
253254
$bestSize = 0;
254255

255256
$j2Len = [];
256257
$nothing = [];
257258

258-
for ($i = $alo; $i < $ahi; ++$i) {
259+
for ($i = $aLower; $i < $aUpper; ++$i) {
259260
$newJ2Len = [];
260261
$jDict = $this->arrayGetDefault($this->b2j, $old[$i], $nothing);
261262
foreach ($jDict as $j) {
262-
if ($j < $blo) {
263+
if ($j < $bLower) {
263264
continue;
264-
} elseif ($j >= $bhi) {
265+
} elseif ($j >= $bUpper) {
265266
break;
266267
}
267268

@@ -278,8 +279,8 @@ public function findLongestMatch(int $alo, int $ahi, int $blo, int $bhi): array
278279
}
279280

280281
while (
281-
$bestI > $alo &&
282-
$bestJ > $blo &&
282+
$bestI > $aLower &&
283+
$bestJ > $bLower &&
283284
!$this->isBJunk($new[$bestJ - 1]) &&
284285
!$this->linesAreDifferent($bestI - 1, $bestJ - 1)
285286
) {
@@ -289,17 +290,17 @@ public function findLongestMatch(int $alo, int $ahi, int $blo, int $bhi): array
289290
}
290291

291292
while (
292-
$bestI + $bestSize < $ahi &&
293-
($bestJ + $bestSize) < $bhi &&
293+
$bestI + $bestSize < $aUpper &&
294+
($bestJ + $bestSize) < $bUpper &&
294295
!$this->isBJunk($new[$bestJ + $bestSize]) &&
295296
!$this->linesAreDifferent($bestI + $bestSize, $bestJ + $bestSize)
296297
) {
297298
++$bestSize;
298299
}
299300

300301
while (
301-
$bestI > $alo &&
302-
$bestJ > $blo &&
302+
$bestI > $aLower &&
303+
$bestJ > $bLower &&
303304
$this->isBJunk($new[$bestJ - 1]) &&
304305
!$this->linesAreDifferent($bestI - 1, $bestJ - 1)
305306
) {
@@ -309,8 +310,8 @@ public function findLongestMatch(int $alo, int $ahi, int $blo, int $bhi): array
309310
}
310311

311312
while (
312-
$bestI + $bestSize < $ahi &&
313-
$bestJ + $bestSize < $bhi &&
313+
$bestI + $bestSize < $aUpper &&
314+
$bestJ + $bestSize < $bUpper &&
314315
$this->isBJunk($new[$bestJ + $bestSize]) &&
315316
!$this->linesAreDifferent($bestI + $bestSize, $bestJ + $bestSize)
316317
) {
@@ -384,26 +385,26 @@ public function getMatchingBlocks(): array
384385

385386
$matchingBlocks = [];
386387
while (!empty($queue)) {
387-
[$alo, $ahi, $blo, $bhi] = array_pop($queue);
388-
$longestMatch = $this->findLongestMatch($alo, $ahi, $blo, $bhi);
388+
[$aLower, $aUpper, $bLower, $bUpper] = array_pop($queue);
389+
$longestMatch = $this->findLongestMatch($aLower, $aUpper, $bLower, $bUpper);
389390
[$list1, $list2, $list3] = $longestMatch;
390391
if ($list3) {
391392
$matchingBlocks[] = $longestMatch;
392-
if ($alo < $list1 && $blo < $list2) {
393+
if ($aLower < $list1 && $bLower < $list2) {
393394
$queue[] = [
394-
$alo,
395+
$aLower,
395396
$list1,
396-
$blo,
397+
$bLower,
397398
$list2
398399
];
399400
}
400401

401-
if ($list1 + $list3 < $ahi && $list2 + $list3 < $bhi) {
402+
if ($list1 + $list3 < $aUpper && $list2 + $list3 < $bUpper) {
402403
$queue[] = [
403404
$list1 + $list3,
404-
$ahi,
405+
$aUpper,
405406
$list2 + $list3,
406-
$bhi
407+
$bUpper
407408
];
408409
}
409410
}
@@ -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)