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 |
||
28 | class Doku_Renderer_metadata extends Doku_Renderer { |
||
29 | /** the approximate byte lenght to capture for the abstract */ |
||
30 | const ABSTRACT_LEN = 250; |
||
31 | |||
32 | /** the maximum UTF8 character length for the abstract */ |
||
33 | const ABSTRACT_MAX = 500; |
||
34 | |||
35 | /** @var array transient meta data, will be reset on each rendering */ |
||
36 | public $meta = array(); |
||
37 | |||
38 | /** @var array persistent meta data, will be kept until explicitly deleted */ |
||
39 | public $persistent = array(); |
||
40 | |||
41 | /** @var array the list of headers used to create unique link ids */ |
||
42 | protected $headers = array(); |
||
43 | |||
44 | /** @var string temporary $doc store */ |
||
45 | protected $store = ''; |
||
46 | |||
47 | /** @var string keeps the first image reference */ |
||
48 | protected $firstimage = ''; |
||
49 | |||
50 | /** @var bool whether or not data is being captured for the abstract, public to be accessible by plugins */ |
||
51 | public $capturing = true; |
||
52 | |||
53 | /** @var bool determines if enough data for the abstract was collected, yet */ |
||
54 | public $capture = true; |
||
55 | |||
56 | /** @var int number of bytes captured for abstract */ |
||
57 | protected $captured = 0; |
||
58 | |||
59 | /** |
||
60 | * Returns the format produced by this renderer. |
||
61 | * |
||
62 | * @return string always 'metadata' |
||
63 | */ |
||
64 | function getFormat() { |
||
67 | |||
68 | /** |
||
69 | * Initialize the document |
||
70 | * |
||
71 | * Sets up some of the persistent info about the page if it doesn't exist, yet. |
||
72 | */ |
||
73 | function document_start() { |
||
91 | |||
92 | /** |
||
93 | * Finalize the document |
||
94 | * |
||
95 | * Stores collected data in the metadata |
||
96 | */ |
||
97 | function document_end() { |
||
119 | |||
120 | /** |
||
121 | * Render plain text data |
||
122 | * |
||
123 | * This function takes care of the amount captured data and will stop capturing when |
||
124 | * enough abstract data is available |
||
125 | * |
||
126 | * @param $text |
||
127 | */ |
||
128 | function cdata($text) { |
||
136 | |||
137 | /** |
||
138 | * Add an item to the TOC |
||
139 | * |
||
140 | * @param string $id the hash link |
||
141 | * @param string $text the text to display |
||
142 | * @param int $level the nesting level |
||
143 | */ |
||
144 | function toc_additem($id, $text, $level) { |
||
159 | |||
160 | /** |
||
161 | * Render a heading |
||
162 | * |
||
163 | * @param string $text the text to display |
||
164 | * @param int $level header level |
||
165 | * @param int $pos byte position in the original source |
||
166 | */ |
||
167 | function header($text, $level, $pos) { |
||
177 | |||
178 | /** |
||
179 | * Open a paragraph |
||
180 | */ |
||
181 | function p_open() { |
||
184 | |||
185 | /** |
||
186 | * Close a paragraph |
||
187 | */ |
||
188 | function p_close() { |
||
191 | |||
192 | /** |
||
193 | * Create a line break |
||
194 | */ |
||
195 | function linebreak() { |
||
198 | |||
199 | /** |
||
200 | * Create a horizontal line |
||
201 | */ |
||
202 | function hr() { |
||
205 | |||
206 | /** |
||
207 | * Callback for footnote start syntax |
||
208 | * |
||
209 | * All following content will go to the footnote instead of |
||
210 | * the document. To achieve this the previous rendered content |
||
211 | * is moved to $store and $doc is cleared |
||
212 | * |
||
213 | * @author Andreas Gohr <[email protected]> |
||
214 | */ |
||
215 | function footnote_open() { |
||
226 | |||
227 | /** |
||
228 | * Callback for footnote end syntax |
||
229 | * |
||
230 | * All content rendered whilst within footnote syntax mode is discarded, |
||
231 | * the previously rendered content is restored and capturing is re-enabled. |
||
232 | * |
||
233 | * @author Andreas Gohr |
||
234 | */ |
||
235 | function footnote_close() { |
||
244 | |||
245 | /** |
||
246 | * Open an unordered list |
||
247 | */ |
||
248 | function listu_open() { |
||
251 | |||
252 | /** |
||
253 | * Open an ordered list |
||
254 | */ |
||
255 | function listo_open() { |
||
258 | |||
259 | /** |
||
260 | * Open a list item |
||
261 | * |
||
262 | * @param int $level the nesting level |
||
263 | * @param bool $node true when a node; false when a leaf |
||
264 | */ |
||
265 | function listitem_open($level,$node=false) { |
||
268 | |||
269 | /** |
||
270 | * Close a list item |
||
271 | */ |
||
272 | function listitem_close() { |
||
275 | |||
276 | /** |
||
277 | * Output preformatted text |
||
278 | * |
||
279 | * @param string $text |
||
280 | */ |
||
281 | function preformatted($text) { |
||
284 | |||
285 | /** |
||
286 | * Start a block quote |
||
287 | */ |
||
288 | function quote_open() { |
||
291 | |||
292 | /** |
||
293 | * Stop a block quote |
||
294 | */ |
||
295 | function quote_close() { |
||
298 | |||
299 | /** |
||
300 | * Display text as file content, optionally syntax highlighted |
||
301 | * |
||
302 | * @param string $text text to show |
||
303 | * @param string $lang programming language to use for syntax highlighting |
||
304 | * @param string $file file path label |
||
305 | */ |
||
306 | function file($text, $lang = null, $file = null) { |
||
309 | |||
310 | /** |
||
311 | * Display text as code content, optionally syntax highlighted |
||
312 | * |
||
313 | * @param string $text text to show |
||
314 | * @param string $language programming language to use for syntax highlighting |
||
315 | * @param string $file file path label |
||
316 | */ |
||
317 | function code($text, $language = null, $file = null) { |
||
320 | |||
321 | /** |
||
322 | * Format an acronym |
||
323 | * |
||
324 | * Uses $this->acronyms |
||
325 | * |
||
326 | * @param string $acronym |
||
327 | */ |
||
328 | function acronym($acronym) { |
||
331 | |||
332 | /** |
||
333 | * Format a smiley |
||
334 | * |
||
335 | * Uses $this->smiley |
||
336 | * |
||
337 | * @param string $smiley |
||
338 | */ |
||
339 | function smiley($smiley) { |
||
342 | |||
343 | /** |
||
344 | * Format an entity |
||
345 | * |
||
346 | * Entities are basically small text replacements |
||
347 | * |
||
348 | * Uses $this->entities |
||
349 | * |
||
350 | * @param string $entity |
||
351 | */ |
||
352 | function entity($entity) { |
||
355 | |||
356 | /** |
||
357 | * Typographically format a multiply sign |
||
358 | * |
||
359 | * Example: ($x=640, $y=480) should result in "640×480" |
||
360 | * |
||
361 | * @param string|int $x first value |
||
362 | * @param string|int $y second value |
||
363 | */ |
||
364 | function multiplyentity($x, $y) { |
||
367 | |||
368 | /** |
||
369 | * Render an opening single quote char (language specific) |
||
370 | */ |
||
371 | function singlequoteopening() { |
||
375 | |||
376 | /** |
||
377 | * Render a closing single quote char (language specific) |
||
378 | */ |
||
379 | function singlequoteclosing() { |
||
383 | |||
384 | /** |
||
385 | * Render an apostrophe char (language specific) |
||
386 | */ |
||
387 | function apostrophe() { |
||
391 | |||
392 | /** |
||
393 | * Render an opening double quote char (language specific) |
||
394 | */ |
||
395 | function doublequoteopening() { |
||
399 | |||
400 | /** |
||
401 | * Render an closinging double quote char (language specific) |
||
402 | */ |
||
403 | function doublequoteclosing() { |
||
407 | |||
408 | /** |
||
409 | * Render a CamelCase link |
||
410 | * |
||
411 | * @param string $link The link name |
||
412 | * @see http://en.wikipedia.org/wiki/CamelCase |
||
413 | */ |
||
414 | function camelcaselink($link) { |
||
417 | |||
418 | /** |
||
419 | * Render a page local link |
||
420 | * |
||
421 | * @param string $hash hash link identifier |
||
422 | * @param string $name name for the link |
||
423 | */ |
||
424 | function locallink($hash, $name = null) { |
||
430 | |||
431 | /** |
||
432 | * keep track of internal links in $this->meta['relation']['references'] |
||
433 | * |
||
434 | * @param string $id page ID to link to. eg. 'wiki:syntax' |
||
435 | * @param string|array|null $name name for the link, array for media file |
||
436 | */ |
||
437 | function internallink($id, $name = null) { |
||
467 | |||
468 | /** |
||
469 | * Render an external link |
||
470 | * |
||
471 | * @param string $url full URL with scheme |
||
472 | * @param string|array|null $name name for the link, array for media file |
||
473 | */ |
||
474 | function externallink($url, $name = null) { |
||
484 | |||
485 | /** |
||
486 | * Render an interwiki link |
||
487 | * |
||
488 | * You may want to use $this->_resolveInterWiki() here |
||
489 | * |
||
490 | * @param string $match original link - probably not much use |
||
491 | * @param string|array $name name for the link, array for media file |
||
492 | * @param string $wikiName indentifier (shortcut) for the remote wiki |
||
493 | * @param string $wikiUri the fragment parsed from the original link |
||
494 | */ |
||
495 | function interwikilink($match, $name = null, $wikiName, $wikiUri) { |
||
507 | |||
508 | /** |
||
509 | * Link to windows share |
||
510 | * |
||
511 | * @param string $url the link |
||
512 | * @param string|array $name name for the link, array for media file |
||
513 | */ |
||
514 | function windowssharelink($url, $name = null) { |
||
525 | |||
526 | /** |
||
527 | * Render a linked E-Mail Address |
||
528 | * |
||
529 | * Should honor $conf['mailguard'] setting |
||
530 | * |
||
531 | * @param string $address Email-Address |
||
532 | * @param string|array $name name for the link, array for media file |
||
533 | */ |
||
534 | function emaillink($address, $name = null) { |
||
545 | |||
546 | /** |
||
547 | * Render an internal media file |
||
548 | * |
||
549 | * @param string $src media ID |
||
550 | * @param string $title descriptive text |
||
551 | * @param string $align left|center|right |
||
552 | * @param int $width width of media in pixel |
||
553 | * @param int $height height of media in pixel |
||
554 | * @param string $cache cache|recache|nocache |
||
555 | * @param string $linking linkonly|detail|nolink |
||
556 | */ |
||
557 | function internalmedia($src, $title = null, $align = null, $width = null, |
||
563 | |||
564 | /** |
||
565 | * Render an external media file |
||
566 | * |
||
567 | * @param string $src full media URL |
||
568 | * @param string $title descriptive text |
||
569 | * @param string $align left|center|right |
||
570 | * @param int $width width of media in pixel |
||
571 | * @param int $height height of media in pixel |
||
572 | * @param string $cache cache|recache|nocache |
||
573 | * @param string $linking linkonly|detail|nolink |
||
574 | */ |
||
575 | function externalmedia($src, $title = null, $align = null, $width = null, |
||
580 | |||
581 | /** |
||
582 | * Render the output of an RSS feed |
||
583 | * |
||
584 | * @param string $url URL of the feed |
||
585 | * @param array $params Finetuning of the output |
||
586 | */ |
||
587 | function rss($url, $params) { |
||
595 | |||
596 | #region Utils |
||
597 | |||
598 | /** |
||
599 | * Removes any Namespace from the given name but keeps |
||
600 | * casing and special chars |
||
601 | * |
||
602 | * @author Andreas Gohr <[email protected]> |
||
603 | * |
||
604 | * @param string $name |
||
605 | * |
||
606 | * @return mixed|string |
||
607 | */ |
||
608 | function _simpleTitle($name) { |
||
623 | |||
624 | /** |
||
625 | * Creates a linkid from a headline |
||
626 | * |
||
627 | * @author Andreas Gohr <[email protected]> |
||
628 | * @param string $title The headline title |
||
629 | * @param boolean $create Create a new unique ID? |
||
630 | * @return string |
||
631 | */ |
||
632 | function _headerToLink($title, $create = false) { |
||
640 | |||
641 | /** |
||
642 | * Construct a title and handle images in titles |
||
643 | * |
||
644 | * @author Harry Fuecks <[email protected]> |
||
645 | * @param string|array|null $title either string title or media array |
||
646 | * @param string $default default title if nothing else is found |
||
647 | * @param null|string $id linked page id (used to extract title from first heading) |
||
648 | * @return string title text |
||
649 | */ |
||
650 | function _getLinkTitle($title, $default, $id = null) { |
||
667 | |||
668 | /** |
||
669 | * Remember first image |
||
670 | * |
||
671 | * @param string $src image URL or ID |
||
672 | */ |
||
673 | function _firstimage($src) { |
||
685 | |||
686 | /** |
||
687 | * Store list of used media files in metadata |
||
688 | * |
||
689 | * @param string $src media ID |
||
690 | */ |
||
691 | function _recordMediaUsage($src) { |
||
699 | |||
700 | #endregion |
||
701 | } |
||
702 | |||
704 |
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.