Skip to content

Commit d2f4198

Browse files
Refactor and sync with latest implementation of PHPUnit_Util_Diff
1 parent 1aa4f4d commit d2f4198

File tree

1 file changed

+32
-54
lines changed

1 file changed

+32
-54
lines changed

src/Diff.php

Lines changed: 32 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -57,67 +57,31 @@
5757
class Diff
5858
{
5959
/**
60-
* The original string
61-
*
6260
* @var string
6361
*/
64-
private $from;
65-
66-
/**
67-
* The new string
68-
*
69-
* @var string
70-
*/
71-
private $to;
72-
73-
/**
74-
* The diff header
75-
*
76-
* @var string
77-
*/
78-
private $header = "--- Original\n+++ New\n";
79-
80-
/**
81-
* Constructs a new diff for two given strings
82-
*
83-
* @param mixed $from The original string
84-
* @param mixed $to The new string
85-
* @param mixed $header The diff header
86-
*/
87-
public function __construct($from, $to, $header = '')
88-
{
89-
$this->from = $from;
90-
$this->to = $to;
91-
92-
if ($header) {
93-
$this->header = $header;
94-
}
95-
}
62+
private $header;
9663

9764
/**
98-
* Exports a value into a string.
65+
* Constructor
9966
*
100-
* @return string
101-
* @see PHP_Exporter\Diff::diff
67+
* @param string $header
10268
*/
103-
public function __toString()
69+
public function __construct($header = "--- Original\n+++ New\n")
10470
{
105-
return $this->diff();
71+
$this->header = $header;
10672
}
10773

10874
/**
109-
* Returns the diff between two strings as a string.
75+
* Returns the diff between two arrays or strings as string.
11076
*
77+
* @param array|string $from
78+
* @param array|string $to
11179
* @return string
11280
*/
113-
public function diff()
81+
public function diff($from, $to)
11482
{
115-
if ($this->from === $this->to) {
116-
return '';
117-
}
118-
11983
$buffer = $this->header;
120-
$diff = $this->toArray();
84+
$diff = $this->diffToArray($from,$to);
12185

12286
$inOld = FALSE;
12387
$i = 0;
@@ -179,7 +143,7 @@ public function diff()
179143
}
180144

181145
/**
182-
* Returns the diff between two strings as an array.
146+
* Returns the diff between two arrays or strings as array.
183147
*
184148
* every array-entry containts two elements:
185149
* - [0] => string $token
@@ -189,16 +153,22 @@ public function diff()
189153
* - 1: ADDED: $token was added to $from
190154
* - 0: OLD: $token is not changed in $to
191155
*
156+
* @param array|string $from
157+
* @param array|string $to
192158
* @return array
193159
*/
194-
public function toArray()
160+
public function diffToArray($from, $to)
195161
{
196-
if ($this->from === $this->to) {
197-
return array();
162+
preg_match_all('(\r\n|\r|\n)', $from, $fromMatches);
163+
preg_match_all('(\r\n|\r|\n)', $to, $toMatches);
164+
165+
if (is_string($from)) {
166+
$from = preg_split('(\r\n|\r|\n)', $from);
198167
}
199168

200-
$from = preg_split('(\r\n|\r|\n)', $this->from);
201-
$to = preg_split('(\r\n|\r|\n)', $this->to);
169+
if (is_string($to)) {
170+
$to = preg_split('(\r\n|\r|\n)', $to);
171+
}
202172

203173
$start = array();
204174
$end = array();
@@ -226,12 +196,20 @@ public function toArray()
226196
}
227197
}
228198

229-
$common = self::longestCommonSubsequence(
199+
$common = $this->longestCommonSubsequence(
230200
array_values($from), array_values($to)
231201
);
232202

233203
$diff = array();
234204

205+
if (isset($fromMatches[0]) && $toMatches[0] &&
206+
count($fromMatches[0]) === count($toMatches[0]) &&
207+
$fromMatches[0] !== $toMatches[0]) {
208+
$diff[] = array(
209+
'#Warning: Strings contain different line endings!', 0
210+
);
211+
}
212+
235213
foreach ($start as $token) {
236214
$diff[] = array($token, 0 /* OLD */);
237215
}
@@ -276,7 +254,7 @@ public function toArray()
276254
* @param array $to
277255
* @return array
278256
*/
279-
protected static function longestCommonSubsequence(array $from, array $to)
257+
protected function longestCommonSubsequence(array $from, array $to)
280258
{
281259
$common = array();
282260
$matrix = array();

0 commit comments

Comments
 (0)