@@ -127,7 +127,8 @@ protected function renderTableBlockEqual(array $block): string
127127 // the old and the new may not be exactly the same
128128 // because of ignoreCase, ignoreWhitespace, etc
129129 foreach ($ block ['new ' ]['lines ' ] as $ newLine ) {
130- // but in this renderer, we can only pick either the old or the new to show
130+ // we could only pick either the old or the new to show
131+ // here we pick the new one to let the user know what it is now
131132 $ html .=
132133 '<tr data-type="="> ' .
133134 '<td class="new"> ' . $ newLine . '</td> ' .
@@ -235,16 +236,13 @@ protected function renderTableBlockReplace(array $block): string
235236 protected function mergeReplaceLines (string $ oldLine , string $ newLine ): ?string
236237 {
237238 $ delParts = $ this ->analyzeClosureParts (
238- RendererConstant::HTML_CLOSURES_DEL [0 ],
239- RendererConstant::HTML_CLOSURES_DEL [1 ],
240239 $ oldLine ,
240+ RendererConstant::HTML_CLOSURES_DEL ,
241241 SequenceMatcher::OP_DEL
242242 );
243-
244243 $ insParts = $ this ->analyzeClosureParts (
245- RendererConstant::HTML_CLOSURES_INS [0 ],
246- RendererConstant::HTML_CLOSURES_INS [1 ],
247244 $ newLine ,
245+ RendererConstant::HTML_CLOSURES_INS ,
248246 SequenceMatcher::OP_INS
249247 );
250248
@@ -298,15 +296,16 @@ protected function mergeReplaceLines(string $oldLine, string $newLine): ?string
298296 * extract informations for "<ins>part 1</ins>" and "<ins>part 2</ins>"
299297 * from "Hello <ins>part 1</ins>SOME OTHER TEXT<ins>part 2</ins> World"
300298 *
301- * @param string $ld the left delimiter
302- * @param string $rd the right delimiter
303- * @param string $line the line
304- * @param int $type the line type
299+ * @param string $line the line
300+ * @param string[] $closures the closures
301+ * @param int $type the type
305302 *
306303 * @return array[] the closure informations
307304 */
308- protected function analyzeClosureParts (string $ ld , string $ rd , string $ line , int $ type ): array
305+ protected function analyzeClosureParts (string $ line , array $ closures , int $ type ): array
309306 {
307+ [$ ld , $ rd ] = $ closures ;
308+
310309 $ ldLength = \strlen ($ ld );
311310 $ rdLength = \strlen ($ rd );
312311
@@ -355,19 +354,15 @@ protected function analyzeClosureParts(string $ld, string $rd, string $line, int
355354 */
356355 protected function markReplaceBlockDiff (array $ oldBlock , array $ newBlock ): array
357356 {
358- static $ isInitiated = false , $ mbOld , $ mbNew , $ lineRenderer ;
359-
360- if (!$ isInitiated ) {
361- $ isInitiated = true ;
362-
363- $ mbOld = new MbString ();
364- $ mbNew = new MbString ();
365- $ lineRenderer = LineRendererFactory::make (
366- $ this ->options ['detailLevel ' ],
367- [], /** @todo is it possible to get the differOptions here? */
368- $ this ->options
369- );
370- }
357+ static $ mbOld , $ mbNew , $ lineRenderer ;
358+
359+ $ mbOld = $ mbOld ?? new MbString ();
360+ $ mbNew = $ mbNew ?? new MbString ();
361+ $ lineRenderer = $ lineRenderer ?? LineRendererFactory::make (
362+ $ this ->options ['detailLevel ' ],
363+ [], /** @todo is it possible to get the differOptions here? */
364+ $ this ->options
365+ );
371366
372367 $ mbOld ->set (\implode ("\n" , $ oldBlock ));
373368 $ mbNew ->set (\implode ("\n" , $ newBlock ));
@@ -406,7 +401,7 @@ protected function isLinesMergeable(string $oldLine, string $newLine, string $cl
406401
407402 $ sumLength = \strlen ($ oldLine ) + \strlen ($ newLine );
408403
409- /** @var float the changed ratio, 0 <= range < 1 */
404+ /** @var float the changed ratio, 0 <= value < 1 */
410405 $ changedRatio = ($ sumLength - (\strlen ($ cleanLine ) << 1 )) / ($ sumLength + 1 );
411406
412407 return $ changedRatio <= $ this ->options ['mergeThreshold ' ];
@@ -422,19 +417,21 @@ protected function isLinesMergeable(string $oldLine, string $newLine, string $cl
422417 */
423418 protected function revisePartsForBoundaryNewlines (array &$ parts , array $ closures ): void
424419 {
425- $ closureRegexL = \preg_quote ($ closures [0 ], '/ ' );
426- $ closureRegexR = \preg_quote ($ closures [1 ], '/ ' );
420+ [$ ld , $ rd ] = $ closures ;
421+
422+ $ ldRegex = \preg_quote ($ ld , '/ ' );
423+ $ rdRegex = \preg_quote ($ rd , '/ ' );
427424
428425 for ($ i = \count ($ parts ) - 1 ; $ i >= 0 ; --$ i ) {
429426 $ part = &$ parts [$ i ];
430427
431428 // deal with leading newlines
432429 $ part ['content ' ] = \preg_replace_callback (
433- "/(?P<closure> {$ closureRegexL })(?P<nl>[ \r\n]++)/u " ,
434- function (array $ matches ) use (&$ parts , $ part , $ closures ): string {
430+ "/(?P<closure> {$ ldRegex })(?P<nl>[ \r\n]++)/u " ,
431+ function (array $ matches ) use (&$ parts , $ part , $ ld , $ rd ): string {
435432 // add a new part for the extracted newlines
436433 $ part ['order ' ] = -1 ;
437- $ part ['content ' ] = "{$ closures [ 0 ] }{$ matches ['nl ' ]}{$ closures [ 1 ] }" ;
434+ $ part ['content ' ] = "{$ ld }{$ matches ['nl ' ]}{$ rd }" ;
438435 $ parts [] = $ part ;
439436
440437 return $ matches ['closure ' ];
@@ -444,11 +441,11 @@ function (array $matches) use (&$parts, $part, $closures): string {
444441
445442 // deal with trailing newlines
446443 $ part ['content ' ] = \preg_replace_callback (
447- "/(?P<nl>[ \r\n]++)(?P<closure> {$ closureRegexR })/u " ,
448- function (array $ matches ) use (&$ parts , $ part , $ closures ): string {
444+ "/(?P<nl>[ \r\n]++)(?P<closure> {$ rdRegex })/u " ,
445+ function (array $ matches ) use (&$ parts , $ part , $ ld , $ rd ): string {
449446 // add a new part for the extracted newlines
450447 $ part ['order ' ] = 1 ;
451- $ part ['content ' ] = "{$ closures [ 0 ] }{$ matches ['nl ' ]}{$ closures [ 1 ] }" ;
448+ $ part ['content ' ] = "{$ ld }{$ matches ['nl ' ]}{$ rd }" ;
452449 $ parts [] = $ part ;
453450
454451 return $ matches ['closure ' ];
0 commit comments