@@ -21,7 +21,8 @@ final class Diff
2121 * @var array cached properties and their default values
2222 */
2323 private const CACHED_PROPERTIES = [
24- 'groupedCodes ' => null ,
24+ 'groupedOpcodes ' => [],
25+ 'oldNewComparison ' => 0 ,
2526 ];
2627
2728 /**
@@ -45,14 +46,20 @@ final class Diff
4546 private $ isCacheDirty = true ;
4647
4748 /**
48- * @var null| SequenceMatcher the sequence matcher
49+ * @var SequenceMatcher the sequence matcher
4950 */
5051 private $ sequenceMatcher ;
5152
5253 /**
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
5456 */
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 = [];
5663
5764 /**
5865 * @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
186193 return $ this ->options ;
187194 }
188195
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+
189206 /**
190207 * Get the singleton.
191208 *
@@ -210,8 +227,12 @@ public function getGroupedOpcodes(): array
210227 {
211228 $ this ->finalize ();
212229
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 ' ]);
215236 }
216237
217238 /**
@@ -228,8 +249,7 @@ public function render(AbstractRenderer $renderer): string
228249 $ renderer ->setDiff ($ this );
229250
230251 // 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
233253 ? $ renderer ::getIdenticalResult ()
234254 : $ renderer ->render ();
235255 }
@@ -326,6 +346,8 @@ private function finalize(): self
326346 if ($ this ->isCacheDirty ) {
327347 $ this ->resetCachedResults ();
328348
349+ $ this ->oldNewComparison = $ this ->old <=> $ this ->new ;
350+
329351 $ this ->sequenceMatcher
330352 ->setOptions ($ this ->options )
331353 ->setSequences ($ this ->old , $ this ->new );
0 commit comments