@@ -21,7 +21,8 @@ final class Diff
21
21
* @var array cached properties and their default values
22
22
*/
23
23
private const CACHED_PROPERTIES = [
24
- 'groupedCodes ' => null ,
24
+ 'groupedOpcodes ' => [],
25
+ 'oldNewComparison ' => 0 ,
25
26
];
26
27
27
28
/**
@@ -45,14 +46,20 @@ final class Diff
45
46
private $ isCacheDirty = true ;
46
47
47
48
/**
48
- * @var null| SequenceMatcher the sequence matcher
49
+ * @var SequenceMatcher the sequence matcher
49
50
*/
50
51
private $ sequenceMatcher ;
51
52
52
53
/**
53
- * @var null|array array containing the generated opcodes for the differences between the two items
54
+ * @var int the result of comparing the old and the new with the spaceship operator
55
+ * -1 means old < new, 0 means old == new, 1 means old > new
54
56
*/
55
- private $ groupedCodes ;
57
+ private $ oldNewComparison = 0 ;
58
+
59
+ /**
60
+ * @var array array containing the generated opcodes for the differences between the two items
61
+ */
62
+ private $ groupedOpcodes = [];
56
63
57
64
/**
58
65
* @var array associative array of the default options available for the diff class and their default value
@@ -186,6 +193,16 @@ public function getOptions(): array
186
193
return $ this ->options ;
187
194
}
188
195
196
+ /**
197
+ * Compare the old and the new with the spaceship operator.
198
+ *
199
+ * @return int
200
+ */
201
+ public function getOldNewComparison (): int
202
+ {
203
+ return $ this ->oldNewComparison ;
204
+ }
205
+
189
206
/**
190
207
* Get the singleton.
191
208
*
@@ -210,8 +227,12 @@ public function getGroupedOpcodes(): array
210
227
{
211
228
$ this ->finalize ();
212
229
213
- return $ this ->groupedCodes = $ this ->groupedCodes ??
214
- $ this ->sequenceMatcher ->getGroupedOpcodes ($ this ->options ['context ' ]);
230
+ if (!empty ($ this ->groupedOpcodes )) {
231
+ return $ this ->groupedOpcodes ;
232
+ }
233
+
234
+ return $ this ->groupedOpcodes = $ this ->sequenceMatcher
235
+ ->getGroupedOpcodes ($ this ->options ['context ' ]);
215
236
}
216
237
217
238
/**
@@ -228,8 +249,7 @@ public function render(AbstractRenderer $renderer): string
228
249
$ renderer ->setDiff ($ this );
229
250
230
251
// the "no difference" situation may happen frequently
231
- // let's save some calculation if possible
232
- return $ this ->old === $ this ->new
252
+ return $ this ->oldNewComparison === 0
233
253
? $ renderer ::getIdenticalResult ()
234
254
: $ renderer ->render ();
235
255
}
@@ -326,6 +346,8 @@ private function finalize(): self
326
346
if ($ this ->isCacheDirty ) {
327
347
$ this ->resetCachedResults ();
328
348
349
+ $ this ->oldNewComparison = $ this ->old <=> $ this ->new ;
350
+
329
351
$ this ->sequenceMatcher
330
352
->setOptions ($ this ->options )
331
353
->setSequences ($ this ->old , $ this ->new );
0 commit comments