@@ -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+ 'groupedCodes ' => [],
25+ 'oldNewComparison ' => 0 ,
2526 ];
2627
2728 /**
@@ -50,9 +51,14 @@ final class Diff
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
5455 */
55- private $ groupedCodes ;
56+ private $ oldNewComparison = 0 ;
57+
58+ /**
59+ * @var array array containing the generated opcodes for the differences between the two items
60+ */
61+ private $ groupedCodes = [];
5662
5763 /**
5864 * @var array associative array of the default options available for the diff class and their default value
@@ -186,6 +192,16 @@ public function getOptions(): array
186192 return $ this ->options ;
187193 }
188194
195+ /**
196+ * Compare the old and the new with the spaceship operator.
197+ *
198+ * @return int
199+ */
200+ public function getOldNewComparison (): int
201+ {
202+ return $ this ->oldNewComparison ;
203+ }
204+
189205 /**
190206 * Get the singleton.
191207 *
@@ -208,45 +224,32 @@ public static function getInstance(): self
208224 */
209225 public function getGroupedOpcodes (): array
210226 {
211- $ this ->finalize ();
227+ if (!empty ($ this ->groupedCodes )) {
228+ return $ this ->groupedCodes ;
229+ }
212230
213- return $ this ->groupedCodes = $ this ->groupedCodes ??
214- $ this -> sequenceMatcher ->getGroupedOpcodes ($ this ->options ['context ' ]);
231+ return $ this ->groupedCodes = $ this ->sequenceMatcher
232+ ->getGroupedOpcodes ($ this ->options ['context ' ]);
215233 }
216234
217235 /**
218- * Render a diff using the supplied rendering class and return it.
236+ * Claim this class has settled down which means properties will not
237+ * be changed before doing diff calculations.
219238 *
220- * @param AbstractRenderer $renderer an instance of the rendering object to use for generating the diff
239+ * Properties will be re-propagated to other classes. This method must be
240+ * re-called after any property changed before doing calculations.
221241 *
222- * @return string the generated diff
223- */
224- public function render (AbstractRenderer $ renderer ): string
225- {
226- $ this ->finalize ();
227-
228- $ renderer ->setDiff ($ this );
229-
230- // the "no difference" situation may happen frequently
231- // let's save some calculation if possible
232- return $ this ->old === $ this ->new
233- ? $ renderer ::getIdenticalResult ()
234- : $ renderer ->render ();
235- }
236-
237- /**
238- * Claim this class is all set.
239- *
240- * Properties will be propagated to other classes. You must re-call
241- * this method after any property changed before doing calculation.
242+ * This method is called in AbstractRenderer::render() automatically.
242243 *
243244 * @return self
244245 */
245- private function finalize (): self
246+ public function finalize (): self
246247 {
247248 if ($ this ->isCacheDirty ) {
248249 $ this ->resetCachedResults ();
249250
251+ $ this ->oldNewComparison = $ this ->old <=> $ this ->new ;
252+
250253 $ this ->sequenceMatcher
251254 ->setOptions ($ this ->options )
252255 ->setSequences ($ this ->old , $ this ->new );
0 commit comments