@@ -45,11 +45,8 @@ public function diff($from, $to, LongestCommonSubsequenceCalculator $lcs = null)
45
45
$ from = $ this ->validateDiffInput ($ from );
46
46
$ to = $ this ->validateDiffInput ($ to );
47
47
$ diff = $ this ->diffToArray ($ from , $ to , $ lcs );
48
- $ old = $ this ->checkIfDiffInOld ($ diff );
49
- $ start = isset ($ old [0 ]) ? $ old [0 ] : 0 ;
50
- $ end = \count ($ diff );
51
48
52
- return $ this ->getBuffer ($ diff, $ old , $ start , $ end );
49
+ return $ this ->getDiff ($ diff );
53
50
}
54
51
55
52
/**
@@ -69,14 +66,15 @@ private function validateDiffInput($input): string
69
66
}
70
67
71
68
/**
72
- * Takes input of the diff array and returns the old array .
73
- * Iterates through diff line by line,
69
+ * Takes input of the diff array and returns the common parts .
70
+ * Iterates through diff line by line.
74
71
*
75
72
* @param array $diff
73
+ * @param int $lineThreshold
76
74
*
77
75
* @return array
78
76
*/
79
- private function checkIfDiffInOld (array $ diff ): array
77
+ private function getCommonChunks (array $ diff, int $ lineThreshold = 5 ): array
80
78
{
81
79
$ inOld = false ;
82
80
$ i = 0 ;
@@ -88,7 +86,7 @@ private function checkIfDiffInOld(array $diff): array
88
86
$ inOld = $ i ;
89
87
}
90
88
} elseif ($ inOld !== false ) {
91
- if (($ i - $ inOld ) > 5 ) {
89
+ if (($ i - $ inOld ) > $ lineThreshold ) {
92
90
$ old [$ inOld ] = $ i - 1 ;
93
91
}
94
92
@@ -105,14 +103,15 @@ private function checkIfDiffInOld(array $diff): array
105
103
* Generates buffer in string format, returning the patch.
106
104
*
107
105
* @param array $diff
108
- * @param array $old
109
- * @param int $start
110
- * @param int $end
111
106
*
112
107
* @return string
113
108
*/
114
- private function getBuffer (array $ diff, array $ old , int $ start , int $ end ): string
109
+ private function getDiff (array $ diff ): string
115
110
{
111
+ $ old = $ this ->getCommonChunks ($ diff , 5 );
112
+ $ start = isset ($ old [0 ]) ? $ old [0 ] : 0 ;
113
+ $ end = \count ($ diff );
114
+
116
115
$ buffer = $ this ->header ;
117
116
118
117
if (!isset ($ old [$ start ])) {
0 commit comments