Complex classes like Diff often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Diff, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Diff extends Ui |
||
16 | { |
||
17 | protected $text; |
||
18 | protected $showIntro; |
||
19 | protected $difftype; |
||
20 | |||
21 | /** |
||
22 | * Diff Ui constructor |
||
23 | * |
||
24 | * @param string $text when non-empty: compare with this text with most current version |
||
25 | * @param bool $showIntro display the intro text |
||
26 | * @param string $difftype diff view type (inline or sidebyside) |
||
27 | */ |
||
28 | public function __construct($text = '', $showIntro = true, $difftype = null) |
||
46 | |||
47 | /** |
||
48 | * Show diff |
||
49 | * between current page version and provided $text |
||
50 | * or between the revisions provided via GET or POST |
||
51 | * |
||
52 | * @author Andreas Gohr <[email protected]> |
||
53 | * |
||
54 | * @return void |
||
55 | */ |
||
56 | public function show() |
||
233 | |||
234 | |||
235 | /** |
||
236 | * Get header of diff HTML |
||
237 | * |
||
238 | * @param string $l_rev Left revisions |
||
239 | * @param string $r_rev Right revision |
||
240 | * @param string $id Page id, if null $ID is used |
||
241 | * @param bool $media If it is for media files |
||
242 | * @param bool $inline Return the header on a single line |
||
243 | * @return string[] HTML snippets for diff header |
||
244 | */ |
||
245 | public function diffHead($l_rev, $r_rev, $id = null, $media = false, $inline = false) |
||
317 | |||
318 | /** |
||
319 | * Create html for revision navigation |
||
320 | * |
||
321 | * @param PageChangeLog $pagelog changelog object of current page |
||
322 | * @param int $l_rev left revision timestamp |
||
323 | * @param int $r_rev right revision timestamp |
||
324 | * @return string[] html of left and right navigation elements |
||
325 | */ |
||
326 | protected function diffNavigation($pagelog, $l_rev, $r_rev) |
||
327 | { |
||
328 | global $INFO, $ID; |
||
329 | |||
330 | // last timestamp is not in changelog, retrieve timestamp from metadata |
||
331 | // note: when page is removed, the metadata timestamp is zero |
||
332 | if (!$r_rev) { |
||
333 | if (isset($INFO['meta']['last_change']['date'])) { |
||
334 | $r_rev = $INFO['meta']['last_change']['date']; |
||
335 | } else { |
||
336 | $r_rev = 0; |
||
337 | } |
||
338 | } |
||
339 | |||
340 | //retrieve revisions with additional info |
||
341 | list($l_revs, $r_revs) = $pagelog->getRevisionsAround($l_rev, $r_rev); |
||
342 | $l_revisions = array(); |
||
343 | if (!$l_rev) { |
||
344 | //no left revision given, add dummy |
||
345 | $l_revisions[0]= array('label' => '', 'attrs' => []); |
||
346 | } |
||
347 | foreach ($l_revs as $rev) { |
||
348 | $info = $pagelog->getRevisionInfo($rev); |
||
349 | $l_revisions[$rev] = array( |
||
350 | 'label' => dformat($info['date']) .' '. editorinfo($info['user'], true) .' '. $info['sum'], |
||
351 | 'attrs' => ['title' => $rev], |
||
352 | ); |
||
353 | if ($r_rev ? $rev >= $r_rev : false) $l_revisions[$rev]['attrs']['disabled'] = 'disabled'; |
||
354 | } |
||
355 | $r_revisions = array(); |
||
356 | if (!$r_rev) { |
||
357 | //no right revision given, add dummy |
||
358 | $r_revisions[0] = array('label' => '', 'attrs' => []); |
||
359 | } |
||
360 | foreach ($r_revs as $rev) { |
||
361 | $info = $pagelog->getRevisionInfo($rev); |
||
362 | $r_revisions[$rev] = array( |
||
363 | 'label' => dformat($info['date']) .' '. editorinfo($info['user'], true) .' '. $info['sum'], |
||
364 | 'attrs' => ['title' => $rev], |
||
365 | ); |
||
366 | if ($rev <= $l_rev) $r_revisions[$rev]['attrs']['disabled'] = 'disabled'; |
||
367 | } |
||
368 | |||
369 | //determine previous/next revisions |
||
370 | $l_index = array_search($l_rev, $l_revs); |
||
371 | $l_prev = $l_index < count($l_revs) - 1 ? $l_revs[$l_index + 1] : null; |
||
372 | $l_next = $l_index > 1 ? $l_revs[$l_index - 1] : null; |
||
373 | if ($r_rev) { |
||
374 | $r_index = array_search($r_rev, $r_revs); |
||
375 | $r_prev = $r_index < count($r_revs) - 1 ? $r_revs[$r_index + 1] : null; |
||
376 | $r_next = $r_index > 1 ? $r_revs[$r_index - 1] : null; |
||
377 | } else { |
||
378 | //removed page |
||
379 | if ($l_next) { |
||
380 | $r_prev = $r_revs[0]; |
||
381 | } else { |
||
382 | $r_prev = null; |
||
383 | } |
||
384 | $r_next = null; |
||
385 | } |
||
386 | |||
387 | /* |
||
388 | * Left side: |
||
389 | */ |
||
390 | $l_nav = ''; |
||
391 | //move back |
||
392 | if ($l_prev) { |
||
393 | $l_nav .= $this->diffViewlink('diffbothprevrev', $l_prev, $r_prev); |
||
394 | $l_nav .= $this->diffViewlink('diffprevrev', $l_prev, $r_rev); |
||
395 | } |
||
396 | //dropdown |
||
397 | $form = new Form(['action' => wl()]); |
||
398 | $form->setHiddenField('id', $ID); |
||
399 | $form->setHiddenField('difftype', $this->difftype); |
||
400 | $form->setHiddenField('rev2[1]', $r_rev); |
||
401 | $form->setHiddenField('do', 'diff'); |
||
402 | $input = $form->addDropdown('rev2[0]', $l_revisions)->val($l_rev)->addClass('quickselect'); |
||
403 | $input->useInput(false); // inhibit prefillInput() during toHTML() process |
||
404 | $form->addButton('do[diff]', 'Go')->attr('type','submit'); |
||
405 | $l_nav .= $form->toHTML(); |
||
406 | //move forward |
||
407 | if ($l_next && ($l_next < $r_rev || !$r_rev)) { |
||
408 | $l_nav .= $this->diffViewlink('diffnextrev', $l_next, $r_rev); |
||
409 | } |
||
410 | |||
411 | /* |
||
412 | * Right side: |
||
413 | */ |
||
414 | $r_nav = ''; |
||
415 | //move back |
||
416 | if ($l_rev < $r_prev) { |
||
417 | $r_nav .= $this->diffViewlink('diffprevrev', $l_rev, $r_prev); |
||
418 | } |
||
419 | //dropdown |
||
420 | $form = new Form(['action' => wl()]); |
||
421 | $form->setHiddenField('id', $ID); |
||
422 | $form->setHiddenField('rev2[0]', $l_rev); |
||
423 | $form->setHiddenField('difftype', $this->difftype); |
||
424 | $form->setHiddenField('do', 'diff'); |
||
425 | $input = $form->addDropdown('rev2[1]', $r_revisions)->val($r_rev)->addClass('quickselect'); |
||
426 | $input->useInput(false); // inhibit prefillInput() during toHTML() process |
||
427 | $form->addButton('do[diff]', 'Go')->attr('type','submit'); |
||
428 | $r_nav .= $form->toHTML(); |
||
429 | //move forward |
||
430 | if ($r_next) { |
||
431 | if ($pagelog->isCurrentRevision($r_next)) { |
||
432 | //last revision is diff with current page |
||
433 | $r_nav .= $this->diffViewlink('difflastrev', $l_rev); |
||
434 | } else { |
||
435 | $r_nav .= $this->diffViewlink('diffnextrev', $l_rev, $r_next); |
||
436 | } |
||
437 | } else { |
||
438 | $r_nav .= $this->diffViewlink('diffbothnextrev', $l_next, $r_next); |
||
439 | } |
||
440 | return array($l_nav, $r_nav); |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * Create html link to a diff view defined by two revisions |
||
445 | * |
||
446 | * @param string $linktype |
||
447 | * @param int $lrev oldest revision |
||
448 | * @param int $rrev newest revision or null for diff with current revision |
||
449 | * @return string html of link to a diff view |
||
450 | */ |
||
451 | protected function diffViewlink($linktype, $lrev, $rrev = null) |
||
472 | |||
473 | |||
474 | /** |
||
475 | * Insert soft breaks in diff html |
||
476 | * |
||
477 | * @param string $diffhtml |
||
478 | * @return string |
||
479 | */ |
||
480 | public function insertSoftbreaks($diffhtml) |
||
502 | |||
503 | } |
||
504 |