Complex classes like Doku_Renderer 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, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | abstract class Doku_Renderer extends Plugin { |
||
30 | /** @var array Settings, control the behavior of the renderer */ |
||
31 | public $info = array( |
||
32 | 'cache' => true, // may the rendered result cached? |
||
33 | 'toc' => true, // render the TOC? |
||
34 | ); |
||
35 | |||
36 | /** @var array contains the smiley configuration, set in p_render() */ |
||
37 | public $smileys = array(); |
||
38 | /** @var array contains the entity configuration, set in p_render() */ |
||
39 | public $entities = array(); |
||
40 | /** @var array contains the acronym configuration, set in p_render() */ |
||
41 | public $acronyms = array(); |
||
42 | /** @var array contains the interwiki configuration, set in p_render() */ |
||
43 | public $interwiki = array(); |
||
44 | |||
45 | /** @var array the list of headers used to create unique link ids */ |
||
46 | protected $headers = array(); |
||
47 | |||
48 | /** |
||
49 | * @var string the rendered document, this will be cached after the renderer ran through |
||
50 | */ |
||
51 | public $doc = ''; |
||
52 | |||
53 | /** |
||
54 | * clean out any per-use values |
||
55 | * |
||
56 | * This is called before each use of the renderer object and should be used to |
||
57 | * completely reset the state of the renderer to be reused for a new document |
||
58 | */ |
||
59 | public function reset(){ |
||
65 | |||
66 | /** |
||
67 | * Allow the plugin to prevent DokuWiki from reusing an instance |
||
68 | * |
||
69 | * Since most renderer plugins fail to implement Doku_Renderer::reset() we default |
||
70 | * to reinstantiating the renderer here |
||
71 | * |
||
72 | * @return bool false if the plugin has to be instantiated |
||
73 | */ |
||
74 | public function isSingleton() { |
||
77 | |||
78 | /** |
||
79 | * Returns the format produced by this renderer. |
||
80 | * |
||
81 | * Has to be overidden by sub classes |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | abstract public function getFormat(); |
||
86 | |||
87 | /** |
||
88 | * Disable caching of this renderer's output |
||
89 | */ |
||
90 | public function nocache() { |
||
93 | |||
94 | /** |
||
95 | * Disable TOC generation for this renderer's output |
||
96 | * |
||
97 | * This might not be used for certain sub renderer |
||
98 | */ |
||
99 | public function notoc() { |
||
102 | |||
103 | /** |
||
104 | * Handle plugin rendering |
||
105 | * |
||
106 | * Most likely this needs NOT to be overwritten by sub classes |
||
107 | * |
||
108 | * @param string $name Plugin name |
||
109 | * @param mixed $data custom data set by handler |
||
110 | * @param string $state matched state if any |
||
111 | * @param string $match raw matched syntax |
||
112 | */ |
||
113 | public function plugin($name, $data, $state = '', $match = '') { |
||
120 | |||
121 | /** |
||
122 | * handle nested render instructions |
||
123 | * this method (and nest_close method) should not be overloaded in actual renderer output classes |
||
124 | * |
||
125 | * @param array $instructions |
||
126 | */ |
||
127 | public function nest($instructions) { |
||
135 | |||
136 | /** |
||
137 | * dummy closing instruction issued by Doku_Handler_Nest |
||
138 | * |
||
139 | * normally the syntax mode should override this instruction when instantiating Doku_Handler_Nest - |
||
140 | * however plugins will not be able to - as their instructions require data. |
||
141 | */ |
||
142 | public function nest_close() { |
||
144 | |||
145 | #region Syntax modes - sub classes will need to implement them to fill $doc |
||
146 | |||
147 | /** |
||
148 | * Initialize the document |
||
149 | */ |
||
150 | public function document_start() { |
||
152 | |||
153 | /** |
||
154 | * Finalize the document |
||
155 | */ |
||
156 | public function document_end() { |
||
158 | |||
159 | /** |
||
160 | * Render the Table of Contents |
||
161 | * |
||
162 | * @return string |
||
163 | */ |
||
164 | public function render_TOC() { |
||
167 | |||
168 | /** |
||
169 | * Add an item to the TOC |
||
170 | * |
||
171 | * @param string $id the hash link |
||
172 | * @param string $text the text to display |
||
173 | * @param int $level the nesting level |
||
174 | */ |
||
175 | public function toc_additem($id, $text, $level) { |
||
177 | |||
178 | /** |
||
179 | * Render a heading |
||
180 | * |
||
181 | * @param string $text the text to display |
||
182 | * @param int $level header level |
||
183 | * @param int $pos byte position in the original source |
||
184 | */ |
||
185 | public function header($text, $level, $pos) { |
||
187 | |||
188 | /** |
||
189 | * Open a new section |
||
190 | * |
||
191 | * @param int $level section level (as determined by the previous header) |
||
192 | */ |
||
193 | public function section_open($level) { |
||
195 | |||
196 | /** |
||
197 | * Close the current section |
||
198 | */ |
||
199 | public function section_close() { |
||
201 | |||
202 | /** |
||
203 | * Render plain text data |
||
204 | * |
||
205 | * @param string $text |
||
206 | */ |
||
207 | public function cdata($text) { |
||
209 | |||
210 | /** |
||
211 | * Open a paragraph |
||
212 | */ |
||
213 | public function p_open() { |
||
215 | |||
216 | /** |
||
217 | * Close a paragraph |
||
218 | */ |
||
219 | public function p_close() { |
||
221 | |||
222 | /** |
||
223 | * Create a line break |
||
224 | */ |
||
225 | public function linebreak() { |
||
227 | |||
228 | /** |
||
229 | * Create a horizontal line |
||
230 | */ |
||
231 | public function hr() { |
||
233 | |||
234 | /** |
||
235 | * Start strong (bold) formatting |
||
236 | */ |
||
237 | public function strong_open() { |
||
239 | |||
240 | /** |
||
241 | * Stop strong (bold) formatting |
||
242 | */ |
||
243 | public function strong_close() { |
||
245 | |||
246 | /** |
||
247 | * Start emphasis (italics) formatting |
||
248 | */ |
||
249 | public function emphasis_open() { |
||
251 | |||
252 | /** |
||
253 | * Stop emphasis (italics) formatting |
||
254 | */ |
||
255 | public function emphasis_close() { |
||
257 | |||
258 | /** |
||
259 | * Start underline formatting |
||
260 | */ |
||
261 | public function underline_open() { |
||
263 | |||
264 | /** |
||
265 | * Stop underline formatting |
||
266 | */ |
||
267 | public function underline_close() { |
||
269 | |||
270 | /** |
||
271 | * Start monospace formatting |
||
272 | */ |
||
273 | public function monospace_open() { |
||
275 | |||
276 | /** |
||
277 | * Stop monospace formatting |
||
278 | */ |
||
279 | public function monospace_close() { |
||
281 | |||
282 | /** |
||
283 | * Start a subscript |
||
284 | */ |
||
285 | public function subscript_open() { |
||
287 | |||
288 | /** |
||
289 | * Stop a subscript |
||
290 | */ |
||
291 | public function subscript_close() { |
||
293 | |||
294 | /** |
||
295 | * Start a superscript |
||
296 | */ |
||
297 | public function superscript_open() { |
||
299 | |||
300 | /** |
||
301 | * Stop a superscript |
||
302 | */ |
||
303 | public function superscript_close() { |
||
305 | |||
306 | /** |
||
307 | * Start deleted (strike-through) formatting |
||
308 | */ |
||
309 | public function deleted_open() { |
||
311 | |||
312 | /** |
||
313 | * Stop deleted (strike-through) formatting |
||
314 | */ |
||
315 | public function deleted_close() { |
||
317 | |||
318 | /** |
||
319 | * Start a footnote |
||
320 | */ |
||
321 | public function footnote_open() { |
||
323 | |||
324 | /** |
||
325 | * Stop a footnote |
||
326 | */ |
||
327 | public function footnote_close() { |
||
329 | |||
330 | /** |
||
331 | * Open an unordered list |
||
332 | */ |
||
333 | public function listu_open() { |
||
335 | |||
336 | /** |
||
337 | * Close an unordered list |
||
338 | */ |
||
339 | public function listu_close() { |
||
341 | |||
342 | /** |
||
343 | * Open an ordered list |
||
344 | */ |
||
345 | public function listo_open() { |
||
347 | |||
348 | /** |
||
349 | * Close an ordered list |
||
350 | */ |
||
351 | public function listo_close() { |
||
353 | |||
354 | /** |
||
355 | * Open a list item |
||
356 | * |
||
357 | * @param int $level the nesting level |
||
358 | * @param bool $node true when a node; false when a leaf |
||
359 | */ |
||
360 | public function listitem_open($level,$node=false) { |
||
362 | |||
363 | /** |
||
364 | * Close a list item |
||
365 | */ |
||
366 | public function listitem_close() { |
||
368 | |||
369 | /** |
||
370 | * Start the content of a list item |
||
371 | */ |
||
372 | public function listcontent_open() { |
||
374 | |||
375 | /** |
||
376 | * Stop the content of a list item |
||
377 | */ |
||
378 | public function listcontent_close() { |
||
380 | |||
381 | /** |
||
382 | * Output unformatted $text |
||
383 | * |
||
384 | * Defaults to $this->cdata() |
||
385 | * |
||
386 | * @param string $text |
||
387 | */ |
||
388 | public function unformatted($text) { |
||
391 | |||
392 | /** |
||
393 | * Output inline PHP code |
||
394 | * |
||
395 | * If $conf['phpok'] is true this should evaluate the given code and append the result |
||
396 | * to $doc |
||
397 | * |
||
398 | * @param string $text The PHP code |
||
399 | */ |
||
400 | public function php($text) { |
||
402 | |||
403 | /** |
||
404 | * Output block level PHP code |
||
405 | * |
||
406 | * If $conf['phpok'] is true this should evaluate the given code and append the result |
||
407 | * to $doc |
||
408 | * |
||
409 | * @param string $text The PHP code |
||
410 | */ |
||
411 | public function phpblock($text) { |
||
413 | |||
414 | /** |
||
415 | * Output raw inline HTML |
||
416 | * |
||
417 | * If $conf['htmlok'] is true this should add the code as is to $doc |
||
418 | * |
||
419 | * @param string $text The HTML |
||
420 | */ |
||
421 | public function html($text) { |
||
423 | |||
424 | /** |
||
425 | * Output raw block-level HTML |
||
426 | * |
||
427 | * If $conf['htmlok'] is true this should add the code as is to $doc |
||
428 | * |
||
429 | * @param string $text The HTML |
||
430 | */ |
||
431 | public function htmlblock($text) { |
||
433 | |||
434 | /** |
||
435 | * Output preformatted text |
||
436 | * |
||
437 | * @param string $text |
||
438 | */ |
||
439 | public function preformatted($text) { |
||
441 | |||
442 | /** |
||
443 | * Start a block quote |
||
444 | */ |
||
445 | public function quote_open() { |
||
447 | |||
448 | /** |
||
449 | * Stop a block quote |
||
450 | */ |
||
451 | public function quote_close() { |
||
453 | |||
454 | /** |
||
455 | * Display text as file content, optionally syntax highlighted |
||
456 | * |
||
457 | * @param string $text text to show |
||
458 | * @param string $lang programming language to use for syntax highlighting |
||
459 | * @param string $file file path label |
||
460 | */ |
||
461 | public function file($text, $lang = null, $file = null) { |
||
463 | |||
464 | /** |
||
465 | * Display text as code content, optionally syntax highlighted |
||
466 | * |
||
467 | * @param string $text text to show |
||
468 | * @param string $lang programming language to use for syntax highlighting |
||
469 | * @param string $file file path label |
||
470 | */ |
||
471 | public function code($text, $lang = null, $file = null) { |
||
473 | |||
474 | /** |
||
475 | * Format an acronym |
||
476 | * |
||
477 | * Uses $this->acronyms |
||
478 | * |
||
479 | * @param string $acronym |
||
480 | */ |
||
481 | public function acronym($acronym) { |
||
483 | |||
484 | /** |
||
485 | * Format a smiley |
||
486 | * |
||
487 | * Uses $this->smiley |
||
488 | * |
||
489 | * @param string $smiley |
||
490 | */ |
||
491 | public function smiley($smiley) { |
||
493 | |||
494 | /** |
||
495 | * Format an entity |
||
496 | * |
||
497 | * Entities are basically small text replacements |
||
498 | * |
||
499 | * Uses $this->entities |
||
500 | * |
||
501 | * @param string $entity |
||
502 | */ |
||
503 | public function entity($entity) { |
||
505 | |||
506 | /** |
||
507 | * Typographically format a multiply sign |
||
508 | * |
||
509 | * Example: ($x=640, $y=480) should result in "640×480" |
||
510 | * |
||
511 | * @param string|int $x first value |
||
512 | * @param string|int $y second value |
||
513 | */ |
||
514 | public function multiplyentity($x, $y) { |
||
516 | |||
517 | /** |
||
518 | * Render an opening single quote char (language specific) |
||
519 | */ |
||
520 | public function singlequoteopening() { |
||
522 | |||
523 | /** |
||
524 | * Render a closing single quote char (language specific) |
||
525 | */ |
||
526 | public function singlequoteclosing() { |
||
528 | |||
529 | /** |
||
530 | * Render an apostrophe char (language specific) |
||
531 | */ |
||
532 | public function apostrophe() { |
||
534 | |||
535 | /** |
||
536 | * Render an opening double quote char (language specific) |
||
537 | */ |
||
538 | public function doublequoteopening() { |
||
540 | |||
541 | /** |
||
542 | * Render an closinging double quote char (language specific) |
||
543 | */ |
||
544 | public function doublequoteclosing() { |
||
546 | |||
547 | /** |
||
548 | * Render a CamelCase link |
||
549 | * |
||
550 | * @param string $link The link name |
||
551 | * @see http://en.wikipedia.org/wiki/CamelCase |
||
552 | */ |
||
553 | public function camelcaselink($link) { |
||
555 | |||
556 | /** |
||
557 | * Render a page local link |
||
558 | * |
||
559 | * @param string $hash hash link identifier |
||
560 | * @param string $name name for the link |
||
561 | */ |
||
562 | public function locallink($hash, $name = null) { |
||
564 | |||
565 | /** |
||
566 | * Render a wiki internal link |
||
567 | * |
||
568 | * @param string $link page ID to link to. eg. 'wiki:syntax' |
||
569 | * @param string|array $title name for the link, array for media file |
||
570 | */ |
||
571 | public function internallink($link, $title = null) { |
||
573 | |||
574 | /** |
||
575 | * Render an external link |
||
576 | * |
||
577 | * @param string $link full URL with scheme |
||
578 | * @param string|array $title name for the link, array for media file |
||
579 | */ |
||
580 | public function externallink($link, $title = null) { |
||
582 | |||
583 | /** |
||
584 | * Render the output of an RSS feed |
||
585 | * |
||
586 | * @param string $url URL of the feed |
||
587 | * @param array $params Finetuning of the output |
||
588 | */ |
||
589 | public function rss($url, $params) { |
||
591 | |||
592 | /** |
||
593 | * Render an interwiki link |
||
594 | * |
||
595 | * You may want to use $this->_resolveInterWiki() here |
||
596 | * |
||
597 | * @param string $link original link - probably not much use |
||
598 | * @param string|array $title name for the link, array for media file |
||
599 | * @param string $wikiName indentifier (shortcut) for the remote wiki |
||
600 | * @param string $wikiUri the fragment parsed from the original link |
||
601 | */ |
||
602 | public function interwikilink($link, $title, $wikiName, $wikiUri) { |
||
604 | |||
605 | /** |
||
606 | * Link to file on users OS |
||
607 | * |
||
608 | * @param string $link the link |
||
609 | * @param string|array $title name for the link, array for media file |
||
610 | */ |
||
611 | public function filelink($link, $title = null) { |
||
613 | |||
614 | /** |
||
615 | * Link to windows share |
||
616 | * |
||
617 | * @param string $link the link |
||
618 | * @param string|array $title name for the link, array for media file |
||
619 | */ |
||
620 | public function windowssharelink($link, $title = null) { |
||
622 | |||
623 | /** |
||
624 | * Render a linked E-Mail Address |
||
625 | * |
||
626 | * Should honor $conf['mailguard'] setting |
||
627 | * |
||
628 | * @param string $address Email-Address |
||
629 | * @param string|array $name name for the link, array for media file |
||
630 | */ |
||
631 | public function emaillink($address, $name = null) { |
||
633 | |||
634 | /** |
||
635 | * Render an internal media file |
||
636 | * |
||
637 | * @param string $src media ID |
||
638 | * @param string $title descriptive text |
||
639 | * @param string $align left|center|right |
||
640 | * @param int $width width of media in pixel |
||
641 | * @param int $height height of media in pixel |
||
642 | * @param string $cache cache|recache|nocache |
||
643 | * @param string $linking linkonly|detail|nolink |
||
644 | */ |
||
645 | public function internalmedia($src, $title = null, $align = null, $width = null, |
||
648 | |||
649 | /** |
||
650 | * Render an external media file |
||
651 | * |
||
652 | * @param string $src full media URL |
||
653 | * @param string $title descriptive text |
||
654 | * @param string $align left|center|right |
||
655 | * @param int $width width of media in pixel |
||
656 | * @param int $height height of media in pixel |
||
657 | * @param string $cache cache|recache|nocache |
||
658 | * @param string $linking linkonly|detail|nolink |
||
659 | */ |
||
660 | public function externalmedia($src, $title = null, $align = null, $width = null, |
||
663 | |||
664 | /** |
||
665 | * Render a link to an internal media file |
||
666 | * |
||
667 | * @param string $src media ID |
||
668 | * @param string $title descriptive text |
||
669 | * @param string $align left|center|right |
||
670 | * @param int $width width of media in pixel |
||
671 | * @param int $height height of media in pixel |
||
672 | * @param string $cache cache|recache|nocache |
||
673 | */ |
||
674 | public function internalmedialink($src, $title = null, $align = null, |
||
677 | |||
678 | /** |
||
679 | * Render a link to an external media file |
||
680 | * |
||
681 | * @param string $src media ID |
||
682 | * @param string $title descriptive text |
||
683 | * @param string $align left|center|right |
||
684 | * @param int $width width of media in pixel |
||
685 | * @param int $height height of media in pixel |
||
686 | * @param string $cache cache|recache|nocache |
||
687 | */ |
||
688 | public function externalmedialink($src, $title = null, $align = null, |
||
691 | |||
692 | /** |
||
693 | * Start a table |
||
694 | * |
||
695 | * @param int $maxcols maximum number of columns |
||
696 | * @param int $numrows NOT IMPLEMENTED |
||
697 | * @param int $pos byte position in the original source |
||
698 | */ |
||
699 | public function table_open($maxcols = null, $numrows = null, $pos = null) { |
||
701 | |||
702 | /** |
||
703 | * Close a table |
||
704 | * |
||
705 | * @param int $pos byte position in the original source |
||
706 | */ |
||
707 | public function table_close($pos = null) { |
||
709 | |||
710 | /** |
||
711 | * Open a table header |
||
712 | */ |
||
713 | public function tablethead_open() { |
||
715 | |||
716 | /** |
||
717 | * Close a table header |
||
718 | */ |
||
719 | public function tablethead_close() { |
||
721 | |||
722 | /** |
||
723 | * Open a table body |
||
724 | */ |
||
725 | public function tabletbody_open() { |
||
727 | |||
728 | /** |
||
729 | * Close a table body |
||
730 | */ |
||
731 | public function tabletbody_close() { |
||
733 | |||
734 | /** |
||
735 | * Open a table footer |
||
736 | */ |
||
737 | public function tabletfoot_open() { |
||
739 | |||
740 | /** |
||
741 | * Close a table footer |
||
742 | */ |
||
743 | public function tabletfoot_close() { |
||
745 | |||
746 | /** |
||
747 | * Open a table row |
||
748 | */ |
||
749 | public function tablerow_open() { |
||
751 | |||
752 | /** |
||
753 | * Close a table row |
||
754 | */ |
||
755 | public function tablerow_close() { |
||
757 | |||
758 | /** |
||
759 | * Open a table header cell |
||
760 | * |
||
761 | * @param int $colspan |
||
762 | * @param string $align left|center|right |
||
763 | * @param int $rowspan |
||
764 | */ |
||
765 | public function tableheader_open($colspan = 1, $align = null, $rowspan = 1) { |
||
767 | |||
768 | /** |
||
769 | * Close a table header cell |
||
770 | */ |
||
771 | public function tableheader_close() { |
||
773 | |||
774 | /** |
||
775 | * Open a table cell |
||
776 | * |
||
777 | * @param int $colspan |
||
778 | * @param string $align left|center|right |
||
779 | * @param int $rowspan |
||
780 | */ |
||
781 | public function tablecell_open($colspan = 1, $align = null, $rowspan = 1) { |
||
783 | |||
784 | /** |
||
785 | * Close a table cell |
||
786 | */ |
||
787 | public function tablecell_close() { |
||
789 | |||
790 | #endregion |
||
791 | |||
792 | #region util functions, you probably won't need to reimplement them |
||
793 | |||
794 | /** |
||
795 | * Creates a linkid from a headline |
||
796 | * |
||
797 | * @author Andreas Gohr <[email protected]> |
||
798 | * @param string $title The headline title |
||
799 | * @param boolean $create Create a new unique ID? |
||
800 | * @return string |
||
801 | */ |
||
802 | public function _headerToLink($title, $create = false) { |
||
810 | |||
811 | /** |
||
812 | * Removes any Namespace from the given name but keeps |
||
813 | * casing and special chars |
||
814 | * |
||
815 | * @author Andreas Gohr <[email protected]> |
||
816 | * |
||
817 | * @param string $name |
||
818 | * @return string |
||
819 | */ |
||
820 | public function _simpleTitle($name) { |
||
835 | |||
836 | /** |
||
837 | * Resolve an interwikilink |
||
838 | * |
||
839 | * @param string $shortcut identifier for the interwiki link |
||
840 | * @param string $reference fragment that refers the content |
||
841 | * @param null|bool $exists reference which returns if an internal page exists |
||
842 | * @return string interwikilink |
||
843 | */ |
||
844 | public function _resolveInterWiki(&$shortcut, $reference, &$exists = null) { |
||
905 | |||
906 | #endregion |
||
907 | } |
||
908 | |||
911 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.