Complex classes like Doku_Handler 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_Handler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Doku_Handler { |
||
18 | /** @var CallWriterInterface */ |
||
19 | protected $callWriter = null; |
||
20 | |||
21 | /** @var array The current CallWriter will write directly to this list of calls, Parser reads it */ |
||
22 | public $calls = array(); |
||
23 | |||
24 | /** @var array internal status holders for some modes */ |
||
25 | protected $status = array( |
||
26 | 'section' => false, |
||
27 | 'doublequote' => 0, |
||
28 | ); |
||
29 | |||
30 | /** @var bool should blocks be rewritten? FIXME seems to always be true */ |
||
31 | protected $rewriteBlocks = true; |
||
32 | |||
33 | /** @var string|null the currently processed page, null for on the fly renders */ |
||
34 | protected $page = null; |
||
35 | |||
36 | /** |
||
37 | * Doku_Handler constructor. |
||
38 | */ |
||
39 | public function __construct() { |
||
42 | |||
43 | /** |
||
44 | * Set the currently processed page |
||
45 | * |
||
46 | * @param string $id |
||
47 | */ |
||
48 | public function setPage($id) |
||
52 | |||
53 | /** |
||
54 | * Get the currently processed page |
||
55 | * |
||
56 | * This may return null when the parsing/handling process is called "on the fly", eg. to |
||
57 | * render a wiki text snippet somewhere. |
||
58 | * |
||
59 | * @return string|null |
||
60 | */ |
||
61 | public function getPage() |
||
65 | |||
66 | /** |
||
67 | * Add a new call by passing it to the current CallWriter |
||
68 | * |
||
69 | * @param string $handler handler method name (see mode handlers below) |
||
70 | * @param mixed $args arguments for this call |
||
71 | * @param int $pos byte position in the original source file |
||
72 | */ |
||
73 | public function addCall($handler, $args, $pos) { |
||
77 | |||
78 | /** |
||
79 | * Accessor for the current CallWriter |
||
80 | * |
||
81 | * @return CallWriterInterface |
||
82 | */ |
||
83 | public function getCallWriter() { |
||
86 | |||
87 | /** |
||
88 | * Set a new CallWriter |
||
89 | * |
||
90 | * @param CallWriterInterface $callWriter |
||
91 | */ |
||
92 | public function setCallWriter($callWriter) { |
||
95 | |||
96 | /** |
||
97 | * Return the current internal status of the given name |
||
98 | * |
||
99 | * @param string $status |
||
100 | * @return mixed|null |
||
101 | */ |
||
102 | public function getStatus($status) { |
||
106 | |||
107 | /** |
||
108 | * Set a new internal status |
||
109 | * |
||
110 | * @param string $status |
||
111 | * @param mixed $value |
||
112 | */ |
||
113 | public function setStatus($status, $value) { |
||
116 | |||
117 | /** @deprecated 2019-10-31 use addCall() instead */ |
||
118 | public function _addCall($handler, $args, $pos) { |
||
122 | |||
123 | /** |
||
124 | * Similar to addCall, but adds a plugin call |
||
125 | * |
||
126 | * @param string $plugin name of the plugin |
||
127 | * @param mixed $args arguments for this call |
||
128 | * @param int $state a LEXER_STATE_* constant |
||
129 | * @param int $pos byte position in the original source file |
||
130 | * @param string $match matched syntax |
||
131 | */ |
||
132 | public function addPluginCall($plugin, $args, $state, $pos, $match) { |
||
136 | |||
137 | /** |
||
138 | * Finishes handling |
||
139 | * |
||
140 | * Called from the parser. Calls finalise() on the call writer, closes open |
||
141 | * sections, rewrites blocks and adds document_start and document_end calls. |
||
142 | * |
||
143 | * @triggers PARSER_HANDLER_DONE |
||
144 | */ |
||
145 | public function finalize(){ |
||
164 | |||
165 | /** |
||
166 | * fetch the current call and advance the pointer to the next one |
||
167 | * |
||
168 | * @fixme seems to be unused? |
||
169 | * @return bool|mixed |
||
170 | */ |
||
171 | public function fetch() { |
||
179 | |||
180 | |||
181 | /** |
||
182 | * Internal function for parsing highlight options. |
||
183 | * $options is parsed for key value pairs separated by commas. |
||
184 | * A value might also be missing in which case the value will simple |
||
185 | * be set to true. Commas in strings are ignored, e.g. option="4,56" |
||
186 | * will work as expected and will only create one entry. |
||
187 | * |
||
188 | * @param string $options space separated list of key-value pairs, |
||
189 | * e.g. option1=123, option2="456" |
||
190 | * @return array|null Array of key-value pairs $array['key'] = 'value'; |
||
191 | * or null if no entries found |
||
192 | */ |
||
193 | protected function parse_highlight_options($options) { |
||
251 | |||
252 | /** |
||
253 | * Simplifies handling for the formatting tags which all behave the same |
||
254 | * |
||
255 | * @param string $match matched syntax |
||
256 | * @param int $state a LEXER_STATE_* constant |
||
257 | * @param int $pos byte position in the original source file |
||
258 | * @param string $name actual mode name |
||
259 | */ |
||
260 | protected function nestingTag($match, $state, $pos, $name) { |
||
273 | |||
274 | |||
275 | /** |
||
276 | * The following methods define the handlers for the different Syntax modes |
||
277 | * |
||
278 | * The handlers are called from dokuwiki\Parsing\Lexer\Lexer\invokeParser() |
||
279 | * |
||
280 | * @todo it might make sense to move these into their own class or merge them with the |
||
281 | * ParserMode classes some time. |
||
282 | */ |
||
283 | // region mode handlers |
||
284 | |||
285 | /** |
||
286 | * Special plugin handler |
||
287 | * |
||
288 | * This handler is called for all modes starting with 'plugin_'. |
||
289 | * An additional parameter with the plugin name is passed. The plugin's handle() |
||
290 | * method is called here |
||
291 | * |
||
292 | * @author Andreas Gohr <[email protected]> |
||
293 | * |
||
294 | * @param string $match matched syntax |
||
295 | * @param int $state a LEXER_STATE_* constant |
||
296 | * @param int $pos byte position in the original source file |
||
297 | * @param string $pluginname name of the plugin |
||
298 | * @return bool mode handled? |
||
299 | */ |
||
300 | public function plugin($match, $state, $pos, $pluginname){ |
||
312 | |||
313 | /** |
||
314 | * @param string $match matched syntax |
||
315 | * @param int $state a LEXER_STATE_* constant |
||
316 | * @param int $pos byte position in the original source file |
||
317 | * @return bool mode handled? |
||
318 | */ |
||
319 | public function base($match, $state, $pos) { |
||
328 | |||
329 | /** |
||
330 | * @param string $match matched syntax |
||
331 | * @param int $state a LEXER_STATE_* constant |
||
332 | * @param int $pos byte position in the original source file |
||
333 | * @return bool mode handled? |
||
334 | */ |
||
335 | public function header($match, $state, $pos) { |
||
351 | |||
352 | /** |
||
353 | * @param string $match matched syntax |
||
354 | * @param int $state a LEXER_STATE_* constant |
||
355 | * @param int $pos byte position in the original source file |
||
356 | * @return bool mode handled? |
||
357 | */ |
||
358 | public function notoc($match, $state, $pos) { |
||
362 | |||
363 | /** |
||
364 | * @param string $match matched syntax |
||
365 | * @param int $state a LEXER_STATE_* constant |
||
366 | * @param int $pos byte position in the original source file |
||
367 | * @return bool mode handled? |
||
368 | */ |
||
369 | public function nocache($match, $state, $pos) { |
||
373 | |||
374 | /** |
||
375 | * @param string $match matched syntax |
||
376 | * @param int $state a LEXER_STATE_* constant |
||
377 | * @param int $pos byte position in the original source file |
||
378 | * @return bool mode handled? |
||
379 | */ |
||
380 | public function linebreak($match, $state, $pos) { |
||
384 | |||
385 | /** |
||
386 | * @param string $match matched syntax |
||
387 | * @param int $state a LEXER_STATE_* constant |
||
388 | * @param int $pos byte position in the original source file |
||
389 | * @return bool mode handled? |
||
390 | */ |
||
391 | public function eol($match, $state, $pos) { |
||
395 | |||
396 | /** |
||
397 | * @param string $match matched syntax |
||
398 | * @param int $state a LEXER_STATE_* constant |
||
399 | * @param int $pos byte position in the original source file |
||
400 | * @return bool mode handled? |
||
401 | */ |
||
402 | public function hr($match, $state, $pos) { |
||
406 | |||
407 | /** |
||
408 | * @param string $match matched syntax |
||
409 | * @param int $state a LEXER_STATE_* constant |
||
410 | * @param int $pos byte position in the original source file |
||
411 | * @return bool mode handled? |
||
412 | */ |
||
413 | public function strong($match, $state, $pos) { |
||
417 | |||
418 | /** |
||
419 | * @param string $match matched syntax |
||
420 | * @param int $state a LEXER_STATE_* constant |
||
421 | * @param int $pos byte position in the original source file |
||
422 | * @return bool mode handled? |
||
423 | */ |
||
424 | public function emphasis($match, $state, $pos) { |
||
428 | |||
429 | /** |
||
430 | * @param string $match matched syntax |
||
431 | * @param int $state a LEXER_STATE_* constant |
||
432 | * @param int $pos byte position in the original source file |
||
433 | * @return bool mode handled? |
||
434 | */ |
||
435 | public function underline($match, $state, $pos) { |
||
439 | |||
440 | /** |
||
441 | * @param string $match matched syntax |
||
442 | * @param int $state a LEXER_STATE_* constant |
||
443 | * @param int $pos byte position in the original source file |
||
444 | * @return bool mode handled? |
||
445 | */ |
||
446 | public function monospace($match, $state, $pos) { |
||
450 | |||
451 | /** |
||
452 | * @param string $match matched syntax |
||
453 | * @param int $state a LEXER_STATE_* constant |
||
454 | * @param int $pos byte position in the original source file |
||
455 | * @return bool mode handled? |
||
456 | */ |
||
457 | public function subscript($match, $state, $pos) { |
||
461 | |||
462 | /** |
||
463 | * @param string $match matched syntax |
||
464 | * @param int $state a LEXER_STATE_* constant |
||
465 | * @param int $pos byte position in the original source file |
||
466 | * @return bool mode handled? |
||
467 | */ |
||
468 | public function superscript($match, $state, $pos) { |
||
472 | |||
473 | /** |
||
474 | * @param string $match matched syntax |
||
475 | * @param int $state a LEXER_STATE_* constant |
||
476 | * @param int $pos byte position in the original source file |
||
477 | * @return bool mode handled? |
||
478 | */ |
||
479 | public function deleted($match, $state, $pos) { |
||
483 | |||
484 | /** |
||
485 | * @param string $match matched syntax |
||
486 | * @param int $state a LEXER_STATE_* constant |
||
487 | * @param int $pos byte position in the original source file |
||
488 | * @return bool mode handled? |
||
489 | */ |
||
490 | public function footnote($match, $state, $pos) { |
||
526 | |||
527 | /** |
||
528 | * @param string $match matched syntax |
||
529 | * @param int $state a LEXER_STATE_* constant |
||
530 | * @param int $pos byte position in the original source file |
||
531 | * @return bool mode handled? |
||
532 | */ |
||
533 | public function listblock($match, $state, $pos) { |
||
554 | |||
555 | /** |
||
556 | * @param string $match matched syntax |
||
557 | * @param int $state a LEXER_STATE_* constant |
||
558 | * @param int $pos byte position in the original source file |
||
559 | * @return bool mode handled? |
||
560 | */ |
||
561 | public function unformatted($match, $state, $pos) { |
||
567 | |||
568 | /** |
||
569 | * @param string $match matched syntax |
||
570 | * @param int $state a LEXER_STATE_* constant |
||
571 | * @param int $pos byte position in the original source file |
||
572 | * @return bool mode handled? |
||
573 | */ |
||
574 | public function php($match, $state, $pos) { |
||
580 | |||
581 | /** |
||
582 | * @param string $match matched syntax |
||
583 | * @param int $state a LEXER_STATE_* constant |
||
584 | * @param int $pos byte position in the original source file |
||
585 | * @return bool mode handled? |
||
586 | */ |
||
587 | public function phpblock($match, $state, $pos) { |
||
593 | |||
594 | /** |
||
595 | * @param string $match matched syntax |
||
596 | * @param int $state a LEXER_STATE_* constant |
||
597 | * @param int $pos byte position in the original source file |
||
598 | * @return bool mode handled? |
||
599 | */ |
||
600 | public function html($match, $state, $pos) { |
||
606 | |||
607 | /** |
||
608 | * @param string $match matched syntax |
||
609 | * @param int $state a LEXER_STATE_* constant |
||
610 | * @param int $pos byte position in the original source file |
||
611 | * @return bool mode handled? |
||
612 | */ |
||
613 | public function htmlblock($match, $state, $pos) { |
||
619 | |||
620 | /** |
||
621 | * @param string $match matched syntax |
||
622 | * @param int $state a LEXER_STATE_* constant |
||
623 | * @param int $pos byte position in the original source file |
||
624 | * @return bool mode handled? |
||
625 | */ |
||
626 | public function preformatted($match, $state, $pos) { |
||
648 | |||
649 | /** |
||
650 | * @param string $match matched syntax |
||
651 | * @param int $state a LEXER_STATE_* constant |
||
652 | * @param int $pos byte position in the original source file |
||
653 | * @return bool mode handled? |
||
654 | */ |
||
655 | public function quote($match, $state, $pos) { |
||
683 | |||
684 | /** |
||
685 | * @param string $match matched syntax |
||
686 | * @param int $state a LEXER_STATE_* constant |
||
687 | * @param int $pos byte position in the original source file |
||
688 | * @return bool mode handled? |
||
689 | */ |
||
690 | public function file($match, $state, $pos) { |
||
693 | |||
694 | /** |
||
695 | * @param string $match matched syntax |
||
696 | * @param int $state a LEXER_STATE_* constant |
||
697 | * @param int $pos byte position in the original source file |
||
698 | * @param string $type either 'code' or 'file' |
||
699 | * @return bool mode handled? |
||
700 | */ |
||
701 | public function code($match, $state, $pos, $type='code') { |
||
722 | |||
723 | /** |
||
724 | * @param string $match matched syntax |
||
725 | * @param int $state a LEXER_STATE_* constant |
||
726 | * @param int $pos byte position in the original source file |
||
727 | * @return bool mode handled? |
||
728 | */ |
||
729 | public function acronym($match, $state, $pos) { |
||
733 | |||
734 | /** |
||
735 | * @param string $match matched syntax |
||
736 | * @param int $state a LEXER_STATE_* constant |
||
737 | * @param int $pos byte position in the original source file |
||
738 | * @return bool mode handled? |
||
739 | */ |
||
740 | public function smiley($match, $state, $pos) { |
||
744 | |||
745 | /** |
||
746 | * @param string $match matched syntax |
||
747 | * @param int $state a LEXER_STATE_* constant |
||
748 | * @param int $pos byte position in the original source file |
||
749 | * @return bool mode handled? |
||
750 | */ |
||
751 | public function wordblock($match, $state, $pos) { |
||
755 | |||
756 | /** |
||
757 | * @param string $match matched syntax |
||
758 | * @param int $state a LEXER_STATE_* constant |
||
759 | * @param int $pos byte position in the original source file |
||
760 | * @return bool mode handled? |
||
761 | */ |
||
762 | public function entity($match, $state, $pos) { |
||
766 | |||
767 | /** |
||
768 | * @param string $match matched syntax |
||
769 | * @param int $state a LEXER_STATE_* constant |
||
770 | * @param int $pos byte position in the original source file |
||
771 | * @return bool mode handled? |
||
772 | */ |
||
773 | public function multiplyentity($match, $state, $pos) { |
||
778 | |||
779 | /** |
||
780 | * @param string $match matched syntax |
||
781 | * @param int $state a LEXER_STATE_* constant |
||
782 | * @param int $pos byte position in the original source file |
||
783 | * @return bool mode handled? |
||
784 | */ |
||
785 | public function singlequoteopening($match, $state, $pos) { |
||
789 | |||
790 | /** |
||
791 | * @param string $match matched syntax |
||
792 | * @param int $state a LEXER_STATE_* constant |
||
793 | * @param int $pos byte position in the original source file |
||
794 | * @return bool mode handled? |
||
795 | */ |
||
796 | public function singlequoteclosing($match, $state, $pos) { |
||
800 | |||
801 | /** |
||
802 | * @param string $match matched syntax |
||
803 | * @param int $state a LEXER_STATE_* constant |
||
804 | * @param int $pos byte position in the original source file |
||
805 | * @return bool mode handled? |
||
806 | */ |
||
807 | public function apostrophe($match, $state, $pos) { |
||
811 | |||
812 | /** |
||
813 | * @param string $match matched syntax |
||
814 | * @param int $state a LEXER_STATE_* constant |
||
815 | * @param int $pos byte position in the original source file |
||
816 | * @return bool mode handled? |
||
817 | */ |
||
818 | public function doublequoteopening($match, $state, $pos) { |
||
823 | |||
824 | /** |
||
825 | * @param string $match matched syntax |
||
826 | * @param int $state a LEXER_STATE_* constant |
||
827 | * @param int $pos byte position in the original source file |
||
828 | * @return bool mode handled? |
||
829 | */ |
||
830 | public function doublequoteclosing($match, $state, $pos) { |
||
839 | |||
840 | /** |
||
841 | * @param string $match matched syntax |
||
842 | * @param int $state a LEXER_STATE_* constant |
||
843 | * @param int $pos byte position in the original source file |
||
844 | * @return bool mode handled? |
||
845 | */ |
||
846 | public function camelcaselink($match, $state, $pos) { |
||
850 | |||
851 | /** |
||
852 | * @param string $match matched syntax |
||
853 | * @param int $state a LEXER_STATE_* constant |
||
854 | * @param int $pos byte position in the original source file |
||
855 | * @return bool mode handled? |
||
856 | */ |
||
857 | public function internallink($match, $state, $pos) { |
||
920 | |||
921 | /** |
||
922 | * @param string $match matched syntax |
||
923 | * @param int $state a LEXER_STATE_* constant |
||
924 | * @param int $pos byte position in the original source file |
||
925 | * @return bool mode handled? |
||
926 | */ |
||
927 | public function filelink($match, $state, $pos) { |
||
931 | |||
932 | /** |
||
933 | * @param string $match matched syntax |
||
934 | * @param int $state a LEXER_STATE_* constant |
||
935 | * @param int $pos byte position in the original source file |
||
936 | * @return bool mode handled? |
||
937 | */ |
||
938 | public function windowssharelink($match, $state, $pos) { |
||
942 | |||
943 | /** |
||
944 | * @param string $match matched syntax |
||
945 | * @param int $state a LEXER_STATE_* constant |
||
946 | * @param int $pos byte position in the original source file |
||
947 | * @return bool mode handled? |
||
948 | */ |
||
949 | public function media($match, $state, $pos) { |
||
960 | |||
961 | /** |
||
962 | * @param string $match matched syntax |
||
963 | * @param int $state a LEXER_STATE_* constant |
||
964 | * @param int $pos byte position in the original source file |
||
965 | * @return bool mode handled? |
||
966 | */ |
||
967 | public function rss($match, $state, $pos) { |
||
995 | |||
996 | /** |
||
997 | * @param string $match matched syntax |
||
998 | * @param int $state a LEXER_STATE_* constant |
||
999 | * @param int $pos byte position in the original source file |
||
1000 | * @return bool mode handled? |
||
1001 | */ |
||
1002 | public function externallink($match, $state, $pos) { |
||
1019 | |||
1020 | /** |
||
1021 | * @param string $match matched syntax |
||
1022 | * @param int $state a LEXER_STATE_* constant |
||
1023 | * @param int $pos byte position in the original source file |
||
1024 | * @return bool mode handled? |
||
1025 | */ |
||
1026 | public function emaillink($match, $state, $pos) { |
||
1031 | |||
1032 | /** |
||
1033 | * @param string $match matched syntax |
||
1034 | * @param int $state a LEXER_STATE_* constant |
||
1035 | * @param int $pos byte position in the original source file |
||
1036 | * @return bool mode handled? |
||
1037 | */ |
||
1038 | public function table($match, $state, $pos) { |
||
1090 | |||
1091 | // endregion modes |
||
1092 | } |
||
1093 | |||
1184 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.