Complex classes like Doku_Renderer_metadata 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 Doku_Renderer_metadata, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Doku_Renderer_metadata extends Doku_Renderer { |
||
| 14 | /** the approximate byte lenght to capture for the abstract */ |
||
| 15 | const ABSTRACT_LEN = 250; |
||
| 16 | |||
| 17 | /** the maximum UTF8 character length for the abstract */ |
||
| 18 | const ABSTRACT_MAX = 500; |
||
| 19 | |||
| 20 | /** @var array transient meta data, will be reset on each rendering */ |
||
| 21 | public $meta = array(); |
||
| 22 | |||
| 23 | /** @var array persistent meta data, will be kept until explicitly deleted */ |
||
| 24 | public $persistent = array(); |
||
| 25 | |||
| 26 | /** @var array the list of headers used to create unique link ids */ |
||
| 27 | protected $headers = array(); |
||
| 28 | |||
| 29 | /** @var string temporary $doc store */ |
||
| 30 | protected $store = ''; |
||
| 31 | |||
| 32 | /** @var string keeps the first image reference */ |
||
| 33 | protected $firstimage = ''; |
||
| 34 | |||
| 35 | /** @var bool whether or not data is being captured for the abstract, public to be accessible by plugins */ |
||
| 36 | public $capturing = true; |
||
| 37 | |||
| 38 | /** @var bool determines if enough data for the abstract was collected, yet */ |
||
| 39 | public $capture = true; |
||
| 40 | |||
| 41 | /** @var int number of bytes captured for abstract */ |
||
| 42 | protected $captured = 0; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Returns the format produced by this renderer. |
||
| 46 | * |
||
| 47 | * @return string always 'metadata' |
||
| 48 | */ |
||
| 49 | public function getFormat() { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Initialize the document |
||
| 55 | * |
||
| 56 | * Sets up some of the persistent info about the page if it doesn't exist, yet. |
||
| 57 | */ |
||
| 58 | public function document_start() { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Finalize the document |
||
| 79 | * |
||
| 80 | * Stores collected data in the metadata |
||
| 81 | */ |
||
| 82 | public function document_end() { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Render plain text data |
||
| 107 | * |
||
| 108 | * This function takes care of the amount captured data and will stop capturing when |
||
| 109 | * enough abstract data is available |
||
| 110 | * |
||
| 111 | * @param $text |
||
| 112 | */ |
||
| 113 | function cdata($text) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Add an item to the TOC |
||
| 124 | * |
||
| 125 | * @param string $id the hash link |
||
| 126 | * @param string $text the text to display |
||
| 127 | * @param int $level the nesting level |
||
| 128 | */ |
||
| 129 | public function toc_additem($id, $text, $level) { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Render a heading |
||
| 147 | * |
||
| 148 | * @param string $text the text to display |
||
| 149 | * @param int $level header level |
||
| 150 | * @param int $pos byte position in the original source |
||
| 151 | */ |
||
| 152 | public function header($text, $level, $pos) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Open a paragraph |
||
| 165 | */ |
||
| 166 | public function p_open() { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Close a paragraph |
||
| 172 | */ |
||
| 173 | public function p_close() { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Create a line break |
||
| 179 | */ |
||
| 180 | public function linebreak() { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Create a horizontal line |
||
| 186 | */ |
||
| 187 | public function hr() { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Callback for footnote start syntax |
||
| 193 | * |
||
| 194 | * All following content will go to the footnote instead of |
||
| 195 | * the document. To achieve this the previous rendered content |
||
| 196 | * is moved to $store and $doc is cleared |
||
| 197 | * |
||
| 198 | * @author Andreas Gohr <[email protected]> |
||
| 199 | */ |
||
| 200 | public function footnote_open() { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Callback for footnote end syntax |
||
| 214 | * |
||
| 215 | * All content rendered whilst within footnote syntax mode is discarded, |
||
| 216 | * the previously rendered content is restored and capturing is re-enabled. |
||
| 217 | * |
||
| 218 | * @author Andreas Gohr |
||
| 219 | */ |
||
| 220 | public function footnote_close() { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Open an unordered list |
||
| 232 | */ |
||
| 233 | public function listu_open() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Open an ordered list |
||
| 239 | */ |
||
| 240 | public function listo_open() { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Open a list item |
||
| 246 | * |
||
| 247 | * @param int $level the nesting level |
||
| 248 | * @param bool $node true when a node; false when a leaf |
||
| 249 | */ |
||
| 250 | public function listitem_open($level,$node=false) { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Close a list item |
||
| 256 | */ |
||
| 257 | public function listitem_close() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Output preformatted text |
||
| 263 | * |
||
| 264 | * @param string $text |
||
| 265 | */ |
||
| 266 | public function preformatted($text) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Start a block quote |
||
| 272 | */ |
||
| 273 | public function quote_open() { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Stop a block quote |
||
| 279 | */ |
||
| 280 | public function quote_close() { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Display text as file content, optionally syntax highlighted |
||
| 286 | * |
||
| 287 | * @param string $text text to show |
||
| 288 | * @param string $lang programming language to use for syntax highlighting |
||
| 289 | * @param string $file file path label |
||
| 290 | */ |
||
| 291 | public function file($text, $lang = null, $file = null) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Display text as code content, optionally syntax highlighted |
||
| 297 | * |
||
| 298 | * @param string $text text to show |
||
| 299 | * @param string $language programming language to use for syntax highlighting |
||
| 300 | * @param string $file file path label |
||
| 301 | */ |
||
| 302 | public function code($text, $language = null, $file = null) { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Format an acronym |
||
| 308 | * |
||
| 309 | * Uses $this->acronyms |
||
| 310 | * |
||
| 311 | * @param string $acronym |
||
| 312 | */ |
||
| 313 | public function acronym($acronym) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Format a smiley |
||
| 319 | * |
||
| 320 | * Uses $this->smiley |
||
| 321 | * |
||
| 322 | * @param string $smiley |
||
| 323 | */ |
||
| 324 | public function smiley($smiley) { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Format an entity |
||
| 330 | * |
||
| 331 | * Entities are basically small text replacements |
||
| 332 | * |
||
| 333 | * Uses $this->entities |
||
| 334 | * |
||
| 335 | * @param string $entity |
||
| 336 | */ |
||
| 337 | public function entity($entity) { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Typographically format a multiply sign |
||
| 343 | * |
||
| 344 | * Example: ($x=640, $y=480) should result in "640×480" |
||
| 345 | * |
||
| 346 | * @param string|int $x first value |
||
| 347 | * @param string|int $y second value |
||
| 348 | */ |
||
| 349 | public function multiplyentity($x, $y) { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Render an opening single quote char (language specific) |
||
| 355 | */ |
||
| 356 | public function singlequoteopening() { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Render a closing single quote char (language specific) |
||
| 363 | */ |
||
| 364 | public function singlequoteclosing() { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Render an apostrophe char (language specific) |
||
| 371 | */ |
||
| 372 | public function apostrophe() { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Render an opening double quote char (language specific) |
||
| 379 | */ |
||
| 380 | public function doublequoteopening() { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Render an closinging double quote char (language specific) |
||
| 387 | */ |
||
| 388 | public function doublequoteclosing() { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Render a CamelCase link |
||
| 395 | * |
||
| 396 | * @param string $link The link name |
||
| 397 | * @see http://en.wikipedia.org/wiki/CamelCase |
||
| 398 | */ |
||
| 399 | public function camelcaselink($link) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Render a page local link |
||
| 405 | * |
||
| 406 | * @param string $hash hash link identifier |
||
| 407 | * @param string $name name for the link |
||
| 408 | */ |
||
| 409 | public function locallink($hash, $name = null) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * keep track of internal links in $this->meta['relation']['references'] |
||
| 418 | * |
||
| 419 | * @param string $id page ID to link to. eg. 'wiki:syntax' |
||
| 420 | * @param string|array|null $name name for the link, array for media file |
||
| 421 | */ |
||
| 422 | public function internallink($id, $name = null) { |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Render an external link |
||
| 455 | * |
||
| 456 | * @param string $url full URL with scheme |
||
| 457 | * @param string|array|null $name name for the link, array for media file |
||
| 458 | */ |
||
| 459 | public function externallink($url, $name = null) { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Render an interwiki link |
||
| 472 | * |
||
| 473 | * You may want to use $this->_resolveInterWiki() here |
||
| 474 | * |
||
| 475 | * @param string $match original link - probably not much use |
||
| 476 | * @param string|array $name name for the link, array for media file |
||
| 477 | * @param string $wikiName indentifier (shortcut) for the remote wiki |
||
| 478 | * @param string $wikiUri the fragment parsed from the original link |
||
| 479 | */ |
||
| 480 | public function interwikilink($match, $name, $wikiName, $wikiUri) { |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Link to windows share |
||
| 495 | * |
||
| 496 | * @param string $url the link |
||
| 497 | * @param string|array $name name for the link, array for media file |
||
| 498 | */ |
||
| 499 | public function windowssharelink($url, $name = null) { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Render a linked E-Mail Address |
||
| 513 | * |
||
| 514 | * Should honor $conf['mailguard'] setting |
||
| 515 | * |
||
| 516 | * @param string $address Email-Address |
||
| 517 | * @param string|array $name name for the link, array for media file |
||
| 518 | */ |
||
| 519 | public function emaillink($address, $name = null) { |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Render an internal media file |
||
| 533 | * |
||
| 534 | * @param string $src media ID |
||
| 535 | * @param string $title descriptive text |
||
| 536 | * @param string $align left|center|right |
||
| 537 | * @param int $width width of media in pixel |
||
| 538 | * @param int $height height of media in pixel |
||
| 539 | * @param string $cache cache|recache|nocache |
||
| 540 | * @param string $linking linkonly|detail|nolink |
||
| 541 | */ |
||
| 542 | public function internalmedia($src, $title = null, $align = null, $width = null, |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Render an external media file |
||
| 551 | * |
||
| 552 | * @param string $src full media URL |
||
| 553 | * @param string $title descriptive text |
||
| 554 | * @param string $align left|center|right |
||
| 555 | * @param int $width width of media in pixel |
||
| 556 | * @param int $height height of media in pixel |
||
| 557 | * @param string $cache cache|recache|nocache |
||
| 558 | * @param string $linking linkonly|detail|nolink |
||
| 559 | */ |
||
| 560 | public function externalmedia($src, $title = null, $align = null, $width = null, |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Render the output of an RSS feed |
||
| 568 | * |
||
| 569 | * @param string $url URL of the feed |
||
| 570 | * @param array $params Finetuning of the output |
||
| 571 | */ |
||
| 572 | public function rss($url, $params) { |
||
| 580 | |||
| 581 | #region Utils |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Removes any Namespace from the given name but keeps |
||
| 585 | * casing and special chars |
||
| 586 | * |
||
| 587 | * @author Andreas Gohr <[email protected]> |
||
| 588 | * |
||
| 589 | * @param string $name |
||
| 590 | * |
||
| 591 | * @return mixed|string |
||
| 592 | */ |
||
| 593 | public function _simpleTitle($name) { |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Construct a title and handle images in titles |
||
| 611 | * |
||
| 612 | * @author Harry Fuecks <[email protected]> |
||
| 613 | * @param string|array|null $title either string title or media array |
||
| 614 | * @param string $default default title if nothing else is found |
||
| 615 | * @param null|string $id linked page id (used to extract title from first heading) |
||
| 616 | * @return string title text |
||
| 617 | */ |
||
| 618 | public function _getLinkTitle($title, $default, $id = null) { |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Remember first image |
||
| 638 | * |
||
| 639 | * @param string $src image URL or ID |
||
| 640 | */ |
||
| 641 | protected function _firstimage($src) { |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Store list of used media files in metadata |
||
| 656 | * |
||
| 657 | * @param string $src media ID |
||
| 658 | */ |
||
| 659 | protected function _recordMediaUsage($src) { |
||
| 667 | |||
| 668 | #endregion |
||
| 669 | } |
||
| 670 | |||
| 672 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.