1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Caxy\HtmlDiff; |
4
|
|
|
|
5
|
|
|
use Caxy\HtmlDiff\Strategy\EqualMatchStrategy; |
6
|
|
|
use Caxy\HtmlDiff\Strategy\MatchStrategyInterface; |
7
|
|
|
|
8
|
|
|
class LcsService |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var MatchStrategyInterface |
12
|
|
|
*/ |
13
|
|
|
protected $matchStrategy; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* LcsService constructor. |
17
|
|
|
* |
18
|
|
|
* @param MatchStrategyInterface $matchStrategy |
|
|
|
|
19
|
|
|
*/ |
20
|
7 |
|
public function __construct(MatchStrategyInterface $matchStrategy = null) |
21
|
|
|
{ |
22
|
7 |
|
if (null === $matchStrategy) { |
23
|
|
|
$matchStrategy = new EqualMatchStrategy(); |
24
|
|
|
} |
25
|
|
|
|
26
|
7 |
|
$this->matchStrategy = $matchStrategy; |
27
|
7 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array $a |
31
|
|
|
* @param array $b |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
7 |
|
public function longestCommonSubsequence(array $a, array $b) |
36
|
|
|
{ |
37
|
7 |
|
$c = array(); |
38
|
|
|
|
39
|
7 |
|
$m = count($a); |
40
|
7 |
|
$n = count($b); |
41
|
|
|
|
42
|
7 |
|
for ($i = 0; $i <= $m; $i++) { |
43
|
7 |
|
$c[$i][0] = 0; |
44
|
7 |
|
} |
45
|
|
|
|
46
|
7 |
|
for ($j = 0; $j <= $n; $j++) { |
47
|
7 |
|
$c[0][$j] = 0; |
48
|
7 |
|
} |
49
|
|
|
|
50
|
7 |
|
for ($i = 1; $i <= $m; $i++) { |
51
|
7 |
|
for ($j = 1; $j <= $n; $j++) { |
52
|
7 |
|
if ($this->matchStrategy->isMatch($a[$i - 1], $b[$j - 1])) { |
53
|
7 |
|
$c[$i][$j] = 1 + (isset($c[$i - 1][$j - 1]) ? $c[$i - 1][$j - 1] : 0); |
54
|
7 |
|
} else { |
55
|
6 |
|
$c[$i][$j] = max( |
56
|
6 |
|
isset($c[$i][$j - 1]) ? $c[$i][$j - 1] : 0, |
57
|
6 |
|
isset($c[$i - 1][$j]) ? $c[$i - 1][$j] : 0 |
58
|
6 |
|
); |
59
|
|
|
} |
60
|
7 |
|
} |
61
|
7 |
|
} |
62
|
|
|
|
63
|
7 |
|
$lcs = array_pad([], $m + 1, 0); |
64
|
7 |
|
$this->compileMatches($c, $a, $b, $m, $n, $lcs); |
65
|
|
|
|
66
|
7 |
|
return $lcs; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $c |
71
|
|
|
* @param $a |
72
|
|
|
* @param $b |
73
|
|
|
* @param $i |
74
|
|
|
* @param $j |
75
|
|
|
* @param $matches |
76
|
|
|
*/ |
77
|
7 |
|
protected function compileMatches($c, $a, $b, $i, $j, &$matches) |
78
|
|
|
{ |
79
|
7 |
|
if ($i > 0 && $j > 0 && $this->matchStrategy->isMatch($a[$i - 1], $b[$j - 1])) { |
80
|
7 |
|
$this->compileMatches($c, $a, $b, $i - 1, $j - 1, $matches); |
81
|
7 |
|
$matches[$i] = $j; |
82
|
7 |
|
} elseif ($j > 0 && ($i === 0 || $c[$i][$j - 1] >= $c[$i - 1][$j])) { |
83
|
4 |
|
$this->compileMatches($c, $a, $b, $i, $j - 1, $matches); |
84
|
7 |
|
} elseif ($i > 0 && ($j === 0 || $c[$i][$j - 1] < $c[$i - 1][$j])) { |
85
|
3 |
|
$this->compileMatches($c, $a, $b, $i - 1, $j, $matches); |
86
|
3 |
|
} |
87
|
7 |
|
} |
88
|
|
|
} |
89
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.