Complex classes like PageDiff 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 PageDiff, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class PageDiff extends Diff |
||
| 17 | { |
||
| 18 | /* @var PageChangeLog */ |
||
| 19 | protected $changelog; |
||
| 20 | |||
| 21 | /* @var string */ |
||
| 22 | protected $text; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * PageDiff Ui constructor |
||
| 26 | * |
||
| 27 | * @param string $id page id |
||
| 28 | */ |
||
| 29 | public function __construct($id = null) |
||
| 30 | { |
||
| 31 | global $INFO; |
||
| 32 | if (!isset($id)) $id = $INFO['id']; |
||
| 33 | $this->item = 'page'; |
||
| 34 | |||
| 35 | // init preference |
||
| 36 | $this->preference['showIntro'] = true; |
||
| 37 | $this->preference['difftype'] = 'sidebyside'; // diff view type: inline or sidebyside |
||
| 38 | |||
| 39 | parent::__construct($id); |
||
| 40 | } |
||
| 41 | |||
| 42 | /** @inheritdoc */ |
||
| 43 | protected function setChangeLog() |
||
| 44 | { |
||
| 45 | $this->changelog = new PageChangeLog($this->id); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** @inheritdoc */ |
||
| 49 | protected function itemFN($id, $rev = '') |
||
| 50 | { |
||
| 51 | return wikiFN($id, $rev); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Set text to be compared with most current version |
||
| 56 | * exclusively use of the compare($old, $new) method |
||
| 57 | * |
||
| 58 | * @param string $text |
||
| 59 | * @return $this |
||
| 60 | */ |
||
| 61 | public function compareWith($text = null) |
||
| 62 | { |
||
| 63 | if (isset($text)) { |
||
| 64 | $this->text = $text; |
||
| 65 | $this->oldRev = ''; |
||
| 66 | $this->newRev = null; |
||
| 67 | } |
||
| 68 | return $this; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** @inheritdoc */ |
||
| 72 | protected function preProcess() |
||
| 73 | { |
||
| 74 | parent::preProcess(); |
||
| 75 | if (!isset($this->oldRev, $this->newRev)) { |
||
| 76 | // no revision was given, compare previous to current |
||
| 77 | $this->oldRev = $this->changelog->getRevisions(0, 1)[0]; |
||
| 78 | $this->newRev = ''; |
||
| 79 | |||
| 80 | global $INFO, $REV; |
||
| 81 | if ($this->id == $INFO['id']) |
||
| 82 | $REV = $this->oldRev; // store revision back in $REV |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Show diff |
||
| 88 | * between current page version and provided $text |
||
| 89 | * or between the revisions provided via GET or POST |
||
| 90 | * |
||
| 91 | * @author Andreas Gohr <[email protected]> |
||
| 92 | * |
||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | public function show() |
||
| 96 | { |
||
| 97 | global $INFO, $lang; |
||
| 98 | |||
| 99 | // determine left and right revision |
||
| 100 | if (!isset($this->oldRev)) $this->preProcess(); |
||
| 101 | |||
| 102 | // create difference engine object |
||
| 103 | if (isset($this->text)) { // compare text to the most current revision |
||
| 104 | $oldText = rawWiki($this->id, ''); |
||
| 105 | $newText = cleanText($this->text); |
||
| 106 | } else { |
||
| 107 | // when both revisions are empty then the page was created just now |
||
| 108 | $oldText = (!$this->oldRev && !$this->newRev) ? '' : rawWiki($this->id, $this->oldRev); |
||
| 109 | $newText = rawWiki($this->id, $this->newRev); // empty when removed page |
||
| 110 | } |
||
| 111 | $Difference = new \Diff(explode("\n", $oldText), explode("\n", $newText)); |
||
| 112 | |||
| 113 | // revison info of older page (left side) |
||
| 114 | $oldRevInfo = $this->getExtendedRevisionInfo($this->oldRev); |
||
| 115 | |||
| 116 | // revison info of newer page (right side) |
||
| 117 | if (isset($this->text)) { |
||
| 118 | $newRevInfo = array('date' => null); |
||
| 119 | } else { |
||
| 120 | $newRevInfo = $this->getExtendedRevisionInfo($this->newRev); |
||
| 121 | } |
||
| 122 | |||
| 123 | // determin exact revision identifiers, even for current page |
||
| 124 | $oldRev = $oldRevInfo['date']; |
||
| 125 | $newRev = $newRevInfo['date']; |
||
| 126 | |||
| 127 | // build paired navigation |
||
| 128 | $navOlderRevisions = ''; |
||
| 129 | $navNewerRevisions = ''; |
||
| 130 | if (!isset($this->text)) { |
||
| 131 | list( |
||
| 132 | $navOlderRevisions, |
||
| 133 | $navNewerRevisions, |
||
| 134 | ) = $this->buildRevisionsNavigation($oldRev, $newRev); |
||
| 135 | } |
||
| 136 | |||
| 137 | // display intro |
||
| 138 | if ($this->preference['showIntro']) echo p_locale_xhtml('diff'); |
||
| 139 | |||
| 140 | // print form to choose diff view type, and exact url reference to the view |
||
| 141 | if (!isset($this->text)) { |
||
| 142 | $this->showDiffViewSelector($oldRev, $newRev); |
||
| 143 | } |
||
| 144 | |||
| 145 | // assign minor edit checker to the variable |
||
| 146 | $classEditType = function ($info) { |
||
| 147 | return ($info['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT) ? ' class="minor"' : ''; |
||
| 148 | }; |
||
| 149 | |||
| 150 | // display diff view table |
||
| 151 | echo '<div class="table">'; |
||
| 152 | echo '<table class="diff diff_'.$this->preference['difftype'] .'">'; |
||
| 153 | |||
| 154 | //navigation and header |
||
| 155 | switch ($this->preference['difftype']) { |
||
| 156 | case 'inline': |
||
| 157 | if (!isset($this->text)) { |
||
| 158 | echo '<tr>' |
||
| 159 | .'<td class="diff-lineheader">-</td>' |
||
| 160 | .'<td class="diffnav">'. $navOlderRevisions .'</td>' |
||
| 161 | .'</tr>'; |
||
| 162 | echo '<tr>' |
||
| 163 | .'<th class="diff-lineheader">-</th>' |
||
| 164 | .'<th'.$classEditType($oldRevInfo).'>'.$this->revisionTitle($oldRevInfo).'</th>' |
||
| 165 | .'</tr>'; |
||
| 166 | } |
||
| 167 | echo '<tr>' |
||
| 168 | .'<td class="diff-lineheader">+</td>' |
||
| 169 | .'<td class="diffnav">'. $navNewerRevisions .'</td>' |
||
| 170 | .'</tr>'; |
||
| 171 | echo '<tr>' |
||
| 172 | .'<th class="diff-lineheader">+</th>' |
||
| 173 | .'<th'.$classEditType($newRevInfo).'>'.$this->revisionTitle($newRevInfo).'</th>' |
||
| 174 | .'</tr>'; |
||
| 175 | // create formatter object |
||
| 176 | $DiffFormatter = new \InlineDiffFormatter(); |
||
| 177 | break; |
||
| 178 | |||
| 179 | case 'sidebyside': |
||
| 180 | default: |
||
| 181 | if (!isset($this->text)) { |
||
| 182 | echo '<tr>' |
||
| 183 | .'<td colspan="2" class="diffnav">'. $navOlderRevisions .'</td>' |
||
| 184 | .'<td colspan="2" class="diffnav">'. $navNewerRevisions .'</td>' |
||
| 185 | .'</tr>'; |
||
| 186 | } |
||
| 187 | echo '<tr>' |
||
| 188 | .'<th colspan="2"'.$classEditType($oldRevInfo).'>'.$this->revisionTitle($oldRevInfo).'</th>' |
||
| 189 | .'<th colspan="2"'.$classEditType($newRevInfo).'>'.$this->revisionTitle($newRevInfo).'</th>' |
||
| 190 | .'</tr>'; |
||
| 191 | // create formatter object |
||
| 192 | $DiffFormatter = new \TableDiffFormatter(); |
||
| 193 | break; |
||
| 194 | } |
||
| 195 | |||
| 196 | // output formatted difference |
||
| 197 | echo $this->insertSoftbreaks($DiffFormatter->format($Difference)); |
||
| 198 | |||
| 199 | echo '</table>'; |
||
| 200 | echo '</div>'; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Revision Title for PageDiff table headline |
||
| 205 | * |
||
| 206 | * @param array $info Revision info structure of a page |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | protected function revisionTitle(array $info) |
||
| 210 | { |
||
| 211 | global $lang, $INFO; |
||
| 212 | |||
| 213 | // use designated title when compare current page source with given text |
||
| 214 | if (array_key_exists('date', $info) && is_null($info['date'])) { |
||
| 215 | return $lang['yours']; |
||
| 216 | } |
||
| 217 | |||
| 218 | if (isset($info['date'])) { |
||
| 219 | $rev = $info['date']; |
||
| 220 | $title = '<bdi><a class="wikilink1" href="'.wl($this->id, ['rev' => $rev]).'">' |
||
| 221 | . $this->id.' ['.dformat($rev).']'.'</a></bdi>'; |
||
| 222 | } else { |
||
| 223 | $rev = false; |
||
| 224 | $title = '—'; |
||
| 225 | } |
||
| 226 | if (isset($info['current']) || ($rev && $rev == $INFO['currentrev'])) { |
||
| 227 | $title .= ' ('.$lang['current'].')'; |
||
| 228 | } |
||
| 229 | |||
| 230 | // append separator |
||
| 231 | $title .= ($this->preference['difftype'] === 'inline') ? ' ' : '<br />'; |
||
| 232 | |||
| 233 | // supplement |
||
| 234 | if (isset($info['date'])) { |
||
| 235 | $objRevInfo = (new PageRevisions($this->id))->getObjRevInfo($info); |
||
| 236 | $title .= $objRevInfo->editSummary().' '.$objRevInfo->editor(); |
||
| 237 | } |
||
| 238 | return $title; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Print form to choose diff view type, and exact url reference to the view |
||
| 243 | * |
||
| 244 | * @param int $oldRev timestamp of older revision, left side |
||
| 245 | * @param int $newRev timestamp of newer revision, right side |
||
| 246 | */ |
||
| 247 | protected function showDiffViewSelector($oldRev, $newRev) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Create html for revision navigation |
||
| 284 | * |
||
| 285 | * The navigation consists of older and newer revisions selectors, each |
||
| 286 | * state mutually depends on the selected revision of opposite side. |
||
| 287 | * |
||
| 288 | * @param int $oldRev timestamp of older revision, older side |
||
| 289 | * @param int $newRev timestamp of newer revision, newer side |
||
| 290 | * @return string[] html of navigation for both older and newer sides |
||
| 291 | */ |
||
| 292 | protected function buildRevisionsNavigation($oldRev, $newRev) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * prepare options for dropdwon selector |
||
| 374 | * |
||
| 375 | * @params string $side "older" or "newer" |
||
| 376 | * @params array $revs list of revsion |
||
| 377 | * @param int $oldRev timestamp of older revision, left side |
||
| 378 | * @param int $newRev timestamp of newer revision, right side |
||
| 379 | * @return array |
||
| 380 | */ |
||
| 381 | protected function buildRevisionOptions($side, $revs, $oldRev, $newRev) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * build Dropdown form for revisions navigation |
||
| 417 | * |
||
| 418 | * @params string $side "older" or "newer" |
||
| 419 | * @params array $options dropdown options |
||
| 420 | * @param int $oldRev timestamp of older revision, left side |
||
| 421 | * @param int $newRev timestamp of newer revision, right side |
||
| 422 | * @return sting |
||
| 423 | */ |
||
| 424 | protected function buildDropdownSelector($side, $options, $oldRev, $newRev) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Create html link to a diff view defined by two revisions |
||
| 451 | * |
||
| 452 | * @param string $linktype |
||
| 453 | * @param int $oldRev older revision |
||
| 454 | * @param int $newRev newer revision or null for diff with current revision |
||
| 455 | * @return string html of link to a diff view |
||
| 456 | */ |
||
| 457 | protected function diffViewlink($linktype, $oldRev, $newRev = null) |
||
| 481 | |||
| 482 | |||
| 483 | /** |
||
| 484 | * Insert soft breaks in diff html |
||
| 485 | * |
||
| 486 | * @param string $diffhtml |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | public function insertSoftbreaks($diffhtml) |
||
| 511 | |||
| 512 | } |
||
| 513 |