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(){ |
||
60 | $this->headers = array(); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Allow the plugin to prevent DokuWiki from reusing an instance |
||
65 | * |
||
66 | * Since most renderer plugins fail to implement Doku_Renderer::reset() we default |
||
67 | * to reinstantiating the renderer here |
||
68 | * |
||
69 | * @return bool false if the plugin has to be instantiated |
||
70 | */ |
||
71 | public function isSingleton() { |
||
72 | return false; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Returns the format produced by this renderer. |
||
77 | * |
||
78 | * Has to be overidden by sub classes |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | abstract public function getFormat(); |
||
83 | |||
84 | /** |
||
85 | * Disable caching of this renderer's output |
||
86 | */ |
||
87 | public function nocache() { |
||
90 | |||
91 | /** |
||
92 | * Disable TOC generation for this renderer's output |
||
93 | * |
||
94 | * This might not be used for certain sub renderer |
||
95 | */ |
||
96 | public function notoc() { |
||
99 | |||
100 | /** |
||
101 | * Handle plugin rendering |
||
102 | * |
||
103 | * Most likely this needs NOT to be overwritten by sub classes |
||
104 | * |
||
105 | * @param string $name Plugin name |
||
106 | * @param mixed $data custom data set by handler |
||
107 | * @param string $state matched state if any |
||
108 | * @param string $match raw matched syntax |
||
109 | */ |
||
110 | public function plugin($name, $data, $state = '', $match = '') { |
||
117 | |||
118 | /** |
||
119 | * handle nested render instructions |
||
120 | * this method (and nest_close method) should not be overloaded in actual renderer output classes |
||
121 | * |
||
122 | * @param array $instructions |
||
123 | */ |
||
124 | public function nest($instructions) { |
||
132 | |||
133 | /** |
||
134 | * dummy closing instruction issued by Doku_Handler_Nest |
||
135 | * |
||
136 | * normally the syntax mode should override this instruction when instantiating Doku_Handler_Nest - |
||
137 | * however plugins will not be able to - as their instructions require data. |
||
138 | */ |
||
139 | public function nest_close() { |
||
141 | |||
142 | #region Syntax modes - sub classes will need to implement them to fill $doc |
||
143 | |||
144 | /** |
||
145 | * Initialize the document |
||
146 | */ |
||
147 | public function document_start() { |
||
149 | |||
150 | /** |
||
151 | * Finalize the document |
||
152 | */ |
||
153 | public function document_end() { |
||
155 | |||
156 | /** |
||
157 | * Render the Table of Contents |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | public function render_TOC() { |
||
164 | |||
165 | /** |
||
166 | * Add an item to the TOC |
||
167 | * |
||
168 | * @param string $id the hash link |
||
169 | * @param string $text the text to display |
||
170 | * @param int $level the nesting level |
||
171 | */ |
||
172 | public function toc_additem($id, $text, $level) { |
||
174 | |||
175 | /** |
||
176 | * Render a heading |
||
177 | * |
||
178 | * @param string $text the text to display |
||
179 | * @param int $level header level |
||
180 | * @param int $pos byte position in the original source |
||
181 | */ |
||
182 | public function header($text, $level, $pos) { |
||
184 | |||
185 | /** |
||
186 | * Open a new section |
||
187 | * |
||
188 | * @param int $level section level (as determined by the previous header) |
||
189 | */ |
||
190 | public function section_open($level) { |
||
192 | |||
193 | /** |
||
194 | * Close the current section |
||
195 | */ |
||
196 | public function section_close() { |
||
198 | |||
199 | /** |
||
200 | * Render plain text data |
||
201 | * |
||
202 | * @param string $text |
||
203 | */ |
||
204 | public function cdata($text) { |
||
206 | |||
207 | /** |
||
208 | * Open a paragraph |
||
209 | */ |
||
210 | public function p_open() { |
||
212 | |||
213 | /** |
||
214 | * Close a paragraph |
||
215 | */ |
||
216 | public function p_close() { |
||
218 | |||
219 | /** |
||
220 | * Create a line break |
||
221 | */ |
||
222 | public function linebreak() { |
||
224 | |||
225 | /** |
||
226 | * Create a horizontal line |
||
227 | */ |
||
228 | public function hr() { |
||
230 | |||
231 | /** |
||
232 | * Start strong (bold) formatting |
||
233 | */ |
||
234 | public function strong_open() { |
||
236 | |||
237 | /** |
||
238 | * Stop strong (bold) formatting |
||
239 | */ |
||
240 | public function strong_close() { |
||
242 | |||
243 | /** |
||
244 | * Start emphasis (italics) formatting |
||
245 | */ |
||
246 | public function emphasis_open() { |
||
248 | |||
249 | /** |
||
250 | * Stop emphasis (italics) formatting |
||
251 | */ |
||
252 | public function emphasis_close() { |
||
254 | |||
255 | /** |
||
256 | * Start underline formatting |
||
257 | */ |
||
258 | public function underline_open() { |
||
260 | |||
261 | /** |
||
262 | * Stop underline formatting |
||
263 | */ |
||
264 | public function underline_close() { |
||
266 | |||
267 | /** |
||
268 | * Start monospace formatting |
||
269 | */ |
||
270 | public function monospace_open() { |
||
272 | |||
273 | /** |
||
274 | * Stop monospace formatting |
||
275 | */ |
||
276 | public function monospace_close() { |
||
278 | |||
279 | /** |
||
280 | * Start a subscript |
||
281 | */ |
||
282 | public function subscript_open() { |
||
284 | |||
285 | /** |
||
286 | * Stop a subscript |
||
287 | */ |
||
288 | public function subscript_close() { |
||
290 | |||
291 | /** |
||
292 | * Start a superscript |
||
293 | */ |
||
294 | public function superscript_open() { |
||
296 | |||
297 | /** |
||
298 | * Stop a superscript |
||
299 | */ |
||
300 | public function superscript_close() { |
||
302 | |||
303 | /** |
||
304 | * Start deleted (strike-through) formatting |
||
305 | */ |
||
306 | public function deleted_open() { |
||
308 | |||
309 | /** |
||
310 | * Stop deleted (strike-through) formatting |
||
311 | */ |
||
312 | public function deleted_close() { |
||
314 | |||
315 | /** |
||
316 | * Start a footnote |
||
317 | */ |
||
318 | public function footnote_open() { |
||
320 | |||
321 | /** |
||
322 | * Stop a footnote |
||
323 | */ |
||
324 | public function footnote_close() { |
||
326 | |||
327 | /** |
||
328 | * Open an unordered list |
||
329 | */ |
||
330 | public function listu_open() { |
||
332 | |||
333 | /** |
||
334 | * Close an unordered list |
||
335 | */ |
||
336 | public function listu_close() { |
||
338 | |||
339 | /** |
||
340 | * Open an ordered list |
||
341 | */ |
||
342 | public function listo_open() { |
||
344 | |||
345 | /** |
||
346 | * Close an ordered list |
||
347 | */ |
||
348 | public function listo_close() { |
||
350 | |||
351 | /** |
||
352 | * Open a list item |
||
353 | * |
||
354 | * @param int $level the nesting level |
||
355 | * @param bool $node true when a node; false when a leaf |
||
356 | */ |
||
357 | public function listitem_open($level,$node=false) { |
||
359 | |||
360 | /** |
||
361 | * Close a list item |
||
362 | */ |
||
363 | public function listitem_close() { |
||
365 | |||
366 | /** |
||
367 | * Start the content of a list item |
||
368 | */ |
||
369 | public function listcontent_open() { |
||
371 | |||
372 | /** |
||
373 | * Stop the content of a list item |
||
374 | */ |
||
375 | public function listcontent_close() { |
||
377 | |||
378 | /** |
||
379 | * Output unformatted $text |
||
380 | * |
||
381 | * Defaults to $this->cdata() |
||
382 | * |
||
383 | * @param string $text |
||
384 | */ |
||
385 | public function unformatted($text) { |
||
388 | |||
389 | /** |
||
390 | * Output inline PHP code |
||
391 | * |
||
392 | * If $conf['phpok'] is true this should evaluate the given code and append the result |
||
393 | * to $doc |
||
394 | * |
||
395 | * @param string $text The PHP code |
||
396 | */ |
||
397 | public function php($text) { |
||
399 | |||
400 | /** |
||
401 | * Output block level PHP code |
||
402 | * |
||
403 | * If $conf['phpok'] is true this should evaluate the given code and append the result |
||
404 | * to $doc |
||
405 | * |
||
406 | * @param string $text The PHP code |
||
407 | */ |
||
408 | public function phpblock($text) { |
||
410 | |||
411 | /** |
||
412 | * Output raw inline HTML |
||
413 | * |
||
414 | * If $conf['htmlok'] is true this should add the code as is to $doc |
||
415 | * |
||
416 | * @param string $text The HTML |
||
417 | */ |
||
418 | public function html($text) { |
||
420 | |||
421 | /** |
||
422 | * Output raw block-level HTML |
||
423 | * |
||
424 | * If $conf['htmlok'] is true this should add the code as is to $doc |
||
425 | * |
||
426 | * @param string $text The HTML |
||
427 | */ |
||
428 | public function htmlblock($text) { |
||
430 | |||
431 | /** |
||
432 | * Output preformatted text |
||
433 | * |
||
434 | * @param string $text |
||
435 | */ |
||
436 | public function preformatted($text) { |
||
438 | |||
439 | /** |
||
440 | * Start a block quote |
||
441 | */ |
||
442 | public function quote_open() { |
||
444 | |||
445 | /** |
||
446 | * Stop a block quote |
||
447 | */ |
||
448 | public function quote_close() { |
||
450 | |||
451 | /** |
||
452 | * Display text as file content, optionally syntax highlighted |
||
453 | * |
||
454 | * @param string $text text to show |
||
455 | * @param string $lang programming language to use for syntax highlighting |
||
456 | * @param string $file file path label |
||
457 | */ |
||
458 | public function file($text, $lang = null, $file = null) { |
||
460 | |||
461 | /** |
||
462 | * Display text as code content, optionally syntax highlighted |
||
463 | * |
||
464 | * @param string $text text to show |
||
465 | * @param string $lang programming language to use for syntax highlighting |
||
466 | * @param string $file file path label |
||
467 | */ |
||
468 | public function code($text, $lang = null, $file = null) { |
||
470 | |||
471 | /** |
||
472 | * Format an acronym |
||
473 | * |
||
474 | * Uses $this->acronyms |
||
475 | * |
||
476 | * @param string $acronym |
||
477 | */ |
||
478 | public function acronym($acronym) { |
||
480 | |||
481 | /** |
||
482 | * Format a smiley |
||
483 | * |
||
484 | * Uses $this->smiley |
||
485 | * |
||
486 | * @param string $smiley |
||
487 | */ |
||
488 | public function smiley($smiley) { |
||
490 | |||
491 | /** |
||
492 | * Format an entity |
||
493 | * |
||
494 | * Entities are basically small text replacements |
||
495 | * |
||
496 | * Uses $this->entities |
||
497 | * |
||
498 | * @param string $entity |
||
499 | */ |
||
500 | public function entity($entity) { |
||
502 | |||
503 | /** |
||
504 | * Typographically format a multiply sign |
||
505 | * |
||
506 | * Example: ($x=640, $y=480) should result in "640×480" |
||
507 | * |
||
508 | * @param string|int $x first value |
||
509 | * @param string|int $y second value |
||
510 | */ |
||
511 | public function multiplyentity($x, $y) { |
||
513 | |||
514 | /** |
||
515 | * Render an opening single quote char (language specific) |
||
516 | */ |
||
517 | public function singlequoteopening() { |
||
519 | |||
520 | /** |
||
521 | * Render a closing single quote char (language specific) |
||
522 | */ |
||
523 | public function singlequoteclosing() { |
||
525 | |||
526 | /** |
||
527 | * Render an apostrophe char (language specific) |
||
528 | */ |
||
529 | public function apostrophe() { |
||
531 | |||
532 | /** |
||
533 | * Render an opening double quote char (language specific) |
||
534 | */ |
||
535 | public function doublequoteopening() { |
||
537 | |||
538 | /** |
||
539 | * Render an closinging double quote char (language specific) |
||
540 | */ |
||
541 | public function doublequoteclosing() { |
||
543 | |||
544 | /** |
||
545 | * Render a CamelCase link |
||
546 | * |
||
547 | * @param string $link The link name |
||
548 | * @see http://en.wikipedia.org/wiki/CamelCase |
||
549 | */ |
||
550 | public function camelcaselink($link) { |
||
552 | |||
553 | /** |
||
554 | * Render a page local link |
||
555 | * |
||
556 | * @param string $hash hash link identifier |
||
557 | * @param string $name name for the link |
||
558 | */ |
||
559 | public function locallink($hash, $name = null) { |
||
561 | |||
562 | /** |
||
563 | * Render a wiki internal link |
||
564 | * |
||
565 | * @param string $link page ID to link to. eg. 'wiki:syntax' |
||
566 | * @param string|array $title name for the link, array for media file |
||
567 | */ |
||
568 | public function internallink($link, $title = null) { |
||
570 | |||
571 | /** |
||
572 | * Render an external link |
||
573 | * |
||
574 | * @param string $link full URL with scheme |
||
575 | * @param string|array $title name for the link, array for media file |
||
576 | */ |
||
577 | public function externallink($link, $title = null) { |
||
579 | |||
580 | /** |
||
581 | * Render the output of an RSS feed |
||
582 | * |
||
583 | * @param string $url URL of the feed |
||
584 | * @param array $params Finetuning of the output |
||
585 | */ |
||
586 | public function rss($url, $params) { |
||
588 | |||
589 | /** |
||
590 | * Render an interwiki link |
||
591 | * |
||
592 | * You may want to use $this->_resolveInterWiki() here |
||
593 | * |
||
594 | * @param string $link original link - probably not much use |
||
595 | * @param string|array $title name for the link, array for media file |
||
596 | * @param string $wikiName indentifier (shortcut) for the remote wiki |
||
597 | * @param string $wikiUri the fragment parsed from the original link |
||
598 | */ |
||
599 | public function interwikilink($link, $title, $wikiName, $wikiUri) { |
||
601 | |||
602 | /** |
||
603 | * Link to file on users OS |
||
604 | * |
||
605 | * @param string $link the link |
||
606 | * @param string|array $title name for the link, array for media file |
||
607 | */ |
||
608 | public function filelink($link, $title = null) { |
||
610 | |||
611 | /** |
||
612 | * Link to windows share |
||
613 | * |
||
614 | * @param string $link the link |
||
615 | * @param string|array $title name for the link, array for media file |
||
616 | */ |
||
617 | public function windowssharelink($link, $title = null) { |
||
619 | |||
620 | /** |
||
621 | * Render a linked E-Mail Address |
||
622 | * |
||
623 | * Should honor $conf['mailguard'] setting |
||
624 | * |
||
625 | * @param string $address Email-Address |
||
626 | * @param string|array $name name for the link, array for media file |
||
627 | */ |
||
628 | public function emaillink($address, $name = null) { |
||
630 | |||
631 | /** |
||
632 | * Render an internal media file |
||
633 | * |
||
634 | * @param string $src media ID |
||
635 | * @param string $title descriptive text |
||
636 | * @param string $align left|center|right |
||
637 | * @param int $width width of media in pixel |
||
638 | * @param int $height height of media in pixel |
||
639 | * @param string $cache cache|recache|nocache |
||
640 | * @param string $linking linkonly|detail|nolink |
||
641 | */ |
||
642 | public function internalmedia($src, $title = null, $align = null, $width = null, |
||
645 | |||
646 | /** |
||
647 | * Render an external media file |
||
648 | * |
||
649 | * @param string $src full media URL |
||
650 | * @param string $title descriptive text |
||
651 | * @param string $align left|center|right |
||
652 | * @param int $width width of media in pixel |
||
653 | * @param int $height height of media in pixel |
||
654 | * @param string $cache cache|recache|nocache |
||
655 | * @param string $linking linkonly|detail|nolink |
||
656 | */ |
||
657 | public function externalmedia($src, $title = null, $align = null, $width = null, |
||
660 | |||
661 | /** |
||
662 | * Render a link to an internal media file |
||
663 | * |
||
664 | * @param string $src media ID |
||
665 | * @param string $title descriptive text |
||
666 | * @param string $align left|center|right |
||
667 | * @param int $width width of media in pixel |
||
668 | * @param int $height height of media in pixel |
||
669 | * @param string $cache cache|recache|nocache |
||
670 | */ |
||
671 | public function internalmedialink($src, $title = null, $align = null, |
||
674 | |||
675 | /** |
||
676 | * Render a link to an external media file |
||
677 | * |
||
678 | * @param string $src media ID |
||
679 | * @param string $title descriptive text |
||
680 | * @param string $align left|center|right |
||
681 | * @param int $width width of media in pixel |
||
682 | * @param int $height height of media in pixel |
||
683 | * @param string $cache cache|recache|nocache |
||
684 | */ |
||
685 | public function externalmedialink($src, $title = null, $align = null, |
||
688 | |||
689 | /** |
||
690 | * Start a table |
||
691 | * |
||
692 | * @param int $maxcols maximum number of columns |
||
693 | * @param int $numrows NOT IMPLEMENTED |
||
694 | * @param int $pos byte position in the original source |
||
695 | */ |
||
696 | public function table_open($maxcols = null, $numrows = null, $pos = null) { |
||
698 | |||
699 | /** |
||
700 | * Close a table |
||
701 | * |
||
702 | * @param int $pos byte position in the original source |
||
703 | */ |
||
704 | public function table_close($pos = null) { |
||
706 | |||
707 | /** |
||
708 | * Open a table header |
||
709 | */ |
||
710 | public function tablethead_open() { |
||
712 | |||
713 | /** |
||
714 | * Close a table header |
||
715 | */ |
||
716 | public function tablethead_close() { |
||
718 | |||
719 | /** |
||
720 | * Open a table body |
||
721 | */ |
||
722 | public function tabletbody_open() { |
||
724 | |||
725 | /** |
||
726 | * Close a table body |
||
727 | */ |
||
728 | public function tabletbody_close() { |
||
730 | |||
731 | /** |
||
732 | * Open a table footer |
||
733 | */ |
||
734 | public function tabletfoot_open() { |
||
736 | |||
737 | /** |
||
738 | * Close a table footer |
||
739 | */ |
||
740 | public function tabletfoot_close() { |
||
742 | |||
743 | /** |
||
744 | * Open a table row |
||
745 | */ |
||
746 | public function tablerow_open() { |
||
748 | |||
749 | /** |
||
750 | * Close a table row |
||
751 | */ |
||
752 | public function tablerow_close() { |
||
754 | |||
755 | /** |
||
756 | * Open a table header cell |
||
757 | * |
||
758 | * @param int $colspan |
||
759 | * @param string $align left|center|right |
||
760 | * @param int $rowspan |
||
761 | */ |
||
762 | public function tableheader_open($colspan = 1, $align = null, $rowspan = 1) { |
||
764 | |||
765 | /** |
||
766 | * Close a table header cell |
||
767 | */ |
||
768 | public function tableheader_close() { |
||
770 | |||
771 | /** |
||
772 | * Open a table cell |
||
773 | * |
||
774 | * @param int $colspan |
||
775 | * @param string $align left|center|right |
||
776 | * @param int $rowspan |
||
777 | */ |
||
778 | public function tablecell_open($colspan = 1, $align = null, $rowspan = 1) { |
||
780 | |||
781 | /** |
||
782 | * Close a table cell |
||
783 | */ |
||
784 | public function tablecell_close() { |
||
786 | |||
787 | #endregion |
||
788 | |||
789 | #region util functions, you probably won't need to reimplement them |
||
790 | |||
791 | /** |
||
792 | * Creates a linkid from a headline |
||
793 | * |
||
794 | * @author Andreas Gohr <[email protected]> |
||
795 | * @param string $title The headline title |
||
796 | * @param boolean $create Create a new unique ID? |
||
797 | * @return string |
||
798 | */ |
||
799 | public function _headerToLink($title, $create = false) { |
||
807 | |||
808 | /** |
||
809 | * Removes any Namespace from the given name but keeps |
||
810 | * casing and special chars |
||
811 | * |
||
812 | * @author Andreas Gohr <[email protected]> |
||
813 | * |
||
814 | * @param string $name |
||
815 | * @return string |
||
816 | */ |
||
817 | protected function _simpleTitle($name) { |
||
832 | |||
833 | /** |
||
834 | * Resolve an interwikilink |
||
835 | * |
||
836 | * @param string $shortcut identifier for the interwiki link |
||
837 | * @param string $reference fragment that refers the content |
||
838 | * @param null|bool $exists reference which returns if an internal page exists |
||
839 | * @return string interwikilink |
||
840 | */ |
||
841 | public function _resolveInterWiki(&$shortcut, $reference, &$exists = null) { |
||
898 | |||
899 | #endregion |
||
900 | } |
||
901 | |||
904 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.