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 | /** |
||
34 | * Doku_Handler constructor. |
||
35 | */ |
||
36 | public function __construct() { |
||
39 | |||
40 | /** |
||
41 | * Add a new call by passing it to the current CallWriter |
||
42 | * |
||
43 | * @param string $handler handler method name (see mode handlers below) |
||
44 | * @param mixed $args arguments for this call |
||
45 | * @param int $pos byte position in the original source file |
||
46 | */ |
||
47 | public function addCall($handler, $args, $pos) { |
||
51 | |||
52 | /** |
||
53 | * Accessor for the current CallWriter |
||
54 | * |
||
55 | * @return CallWriterInterface |
||
56 | */ |
||
57 | public function getCallWriter() { |
||
60 | |||
61 | /** |
||
62 | * Set a new CallWriter |
||
63 | * |
||
64 | * @param CallWriterInterface $callWriter |
||
65 | */ |
||
66 | public function setCallWriter($callWriter) { |
||
69 | |||
70 | /** @deprecated 2019-10-31 use addCall() instead */ |
||
71 | public function _addCall($handler, $args, $pos) { |
||
75 | |||
76 | /** |
||
77 | * Similar to addCall, but adds a plugin call |
||
78 | * |
||
79 | * @param string $plugin name of the plugin |
||
80 | * @param mixed $args arguments for this call |
||
81 | * @param int $state a LEXER_STATE_* constant |
||
82 | * @param int $pos byte position in the original source file |
||
83 | * @param string $match matched syntax |
||
84 | */ |
||
85 | public function addPluginCall($plugin, $args, $state, $pos, $match) { |
||
89 | |||
90 | /** |
||
91 | * Finishes handling |
||
92 | * |
||
93 | * Called from the parser. Calls finalise() on the call writer, closes open |
||
94 | * sections, rewrites blocks and adds document_start and document_end calls. |
||
95 | * |
||
96 | * @triggers PARSER_HANDLER_DONE |
||
97 | */ |
||
98 | public function finalize(){ |
||
117 | |||
118 | /** |
||
119 | * fetch the current call and advance the pointer to the next one |
||
120 | * |
||
121 | * @fixme seems to be unused? |
||
122 | * @return bool|mixed |
||
123 | */ |
||
124 | public function fetch() { |
||
132 | |||
133 | |||
134 | /** |
||
135 | * Internal function for parsing highlight options. |
||
136 | * $options is parsed for key value pairs separated by commas. |
||
137 | * A value might also be missing in which case the value will simple |
||
138 | * be set to true. Commas in strings are ignored, e.g. option="4,56" |
||
139 | * will work as expected and will only create one entry. |
||
140 | * |
||
141 | * @param string $options space separated list of key-value pairs, |
||
142 | * e.g. option1=123, option2="456" |
||
143 | * @return array|null Array of key-value pairs $array['key'] = 'value'; |
||
144 | * or null if no entries found |
||
145 | */ |
||
146 | protected function parse_highlight_options($options) { |
||
204 | |||
205 | /** |
||
206 | * Simplifies handling for the formatting tags which all behave the same |
||
207 | * |
||
208 | * @param string $match matched syntax |
||
209 | * @param int $state a LEXER_STATE_* constant |
||
210 | * @param int $pos byte position in the original source file |
||
211 | * @param string $name actual mode name |
||
212 | */ |
||
213 | protected function nestingTag($match, $state, $pos, $name) { |
||
226 | |||
227 | |||
228 | /** |
||
229 | * The following methods define the handlers for the different Syntax modes |
||
230 | * |
||
231 | * The handlers are called from dokuwiki\Parsing\Lexer\Lexer\invokeParser() |
||
232 | * |
||
233 | * @todo it might make sense to move these into their own class or merge them with the |
||
234 | * ParserMode classes some time. |
||
235 | */ |
||
236 | // region mode handlers |
||
237 | |||
238 | /** |
||
239 | * Special plugin handler |
||
240 | * |
||
241 | * This handler is called for all modes starting with 'plugin_'. |
||
242 | * An additional parameter with the plugin name is passed. The plugin's handle() |
||
243 | * method is called here |
||
244 | * |
||
245 | * @author Andreas Gohr <[email protected]> |
||
246 | * |
||
247 | * @param string $match matched syntax |
||
248 | * @param int $state a LEXER_STATE_* constant |
||
249 | * @param int $pos byte position in the original source file |
||
250 | * @param string $pluginname name of the plugin |
||
251 | * @return bool mode handled? |
||
252 | */ |
||
253 | public function plugin($match, $state, $pos, $pluginname){ |
||
265 | |||
266 | /** |
||
267 | * @param string $match matched syntax |
||
268 | * @param int $state a LEXER_STATE_* constant |
||
269 | * @param int $pos byte position in the original source file |
||
270 | * @return bool mode handled? |
||
271 | */ |
||
272 | public function base($match, $state, $pos) { |
||
281 | |||
282 | /** |
||
283 | * @param string $match matched syntax |
||
284 | * @param int $state a LEXER_STATE_* constant |
||
285 | * @param int $pos byte position in the original source file |
||
286 | * @return bool mode handled? |
||
287 | */ |
||
288 | public function header($match, $state, $pos) { |
||
304 | |||
305 | /** |
||
306 | * @param string $match matched syntax |
||
307 | * @param int $state a LEXER_STATE_* constant |
||
308 | * @param int $pos byte position in the original source file |
||
309 | * @return bool mode handled? |
||
310 | */ |
||
311 | public function notoc($match, $state, $pos) { |
||
315 | |||
316 | /** |
||
317 | * @param string $match matched syntax |
||
318 | * @param int $state a LEXER_STATE_* constant |
||
319 | * @param int $pos byte position in the original source file |
||
320 | * @return bool mode handled? |
||
321 | */ |
||
322 | public function nocache($match, $state, $pos) { |
||
326 | |||
327 | /** |
||
328 | * @param string $match matched syntax |
||
329 | * @param int $state a LEXER_STATE_* constant |
||
330 | * @param int $pos byte position in the original source file |
||
331 | * @return bool mode handled? |
||
332 | */ |
||
333 | public function linebreak($match, $state, $pos) { |
||
337 | |||
338 | /** |
||
339 | * @param string $match matched syntax |
||
340 | * @param int $state a LEXER_STATE_* constant |
||
341 | * @param int $pos byte position in the original source file |
||
342 | * @return bool mode handled? |
||
343 | */ |
||
344 | public function eol($match, $state, $pos) { |
||
348 | |||
349 | /** |
||
350 | * @param string $match matched syntax |
||
351 | * @param int $state a LEXER_STATE_* constant |
||
352 | * @param int $pos byte position in the original source file |
||
353 | * @return bool mode handled? |
||
354 | */ |
||
355 | public function hr($match, $state, $pos) { |
||
359 | |||
360 | /** |
||
361 | * @param string $match matched syntax |
||
362 | * @param int $state a LEXER_STATE_* constant |
||
363 | * @param int $pos byte position in the original source file |
||
364 | * @return bool mode handled? |
||
365 | */ |
||
366 | public function strong($match, $state, $pos) { |
||
370 | |||
371 | /** |
||
372 | * @param string $match matched syntax |
||
373 | * @param int $state a LEXER_STATE_* constant |
||
374 | * @param int $pos byte position in the original source file |
||
375 | * @return bool mode handled? |
||
376 | */ |
||
377 | public function emphasis($match, $state, $pos) { |
||
381 | |||
382 | /** |
||
383 | * @param string $match matched syntax |
||
384 | * @param int $state a LEXER_STATE_* constant |
||
385 | * @param int $pos byte position in the original source file |
||
386 | * @return bool mode handled? |
||
387 | */ |
||
388 | public function underline($match, $state, $pos) { |
||
392 | |||
393 | /** |
||
394 | * @param string $match matched syntax |
||
395 | * @param int $state a LEXER_STATE_* constant |
||
396 | * @param int $pos byte position in the original source file |
||
397 | * @return bool mode handled? |
||
398 | */ |
||
399 | public function monospace($match, $state, $pos) { |
||
403 | |||
404 | /** |
||
405 | * @param string $match matched syntax |
||
406 | * @param int $state a LEXER_STATE_* constant |
||
407 | * @param int $pos byte position in the original source file |
||
408 | * @return bool mode handled? |
||
409 | */ |
||
410 | public function subscript($match, $state, $pos) { |
||
414 | |||
415 | /** |
||
416 | * @param string $match matched syntax |
||
417 | * @param int $state a LEXER_STATE_* constant |
||
418 | * @param int $pos byte position in the original source file |
||
419 | * @return bool mode handled? |
||
420 | */ |
||
421 | public function superscript($match, $state, $pos) { |
||
425 | |||
426 | /** |
||
427 | * @param string $match matched syntax |
||
428 | * @param int $state a LEXER_STATE_* constant |
||
429 | * @param int $pos byte position in the original source file |
||
430 | * @return bool mode handled? |
||
431 | */ |
||
432 | public function deleted($match, $state, $pos) { |
||
436 | |||
437 | /** |
||
438 | * @param string $match matched syntax |
||
439 | * @param int $state a LEXER_STATE_* constant |
||
440 | * @param int $pos byte position in the original source file |
||
441 | * @return bool mode handled? |
||
442 | */ |
||
443 | public function footnote($match, $state, $pos) { |
||
479 | |||
480 | /** |
||
481 | * @param string $match matched syntax |
||
482 | * @param int $state a LEXER_STATE_* constant |
||
483 | * @param int $pos byte position in the original source file |
||
484 | * @return bool mode handled? |
||
485 | */ |
||
486 | public function listblock($match, $state, $pos) { |
||
507 | |||
508 | /** |
||
509 | * @param string $match matched syntax |
||
510 | * @param int $state a LEXER_STATE_* constant |
||
511 | * @param int $pos byte position in the original source file |
||
512 | * @return bool mode handled? |
||
513 | */ |
||
514 | public function unformatted($match, $state, $pos) { |
||
520 | |||
521 | /** |
||
522 | * @param string $match matched syntax |
||
523 | * @param int $state a LEXER_STATE_* constant |
||
524 | * @param int $pos byte position in the original source file |
||
525 | * @return bool mode handled? |
||
526 | */ |
||
527 | public function php($match, $state, $pos) { |
||
533 | |||
534 | /** |
||
535 | * @param string $match matched syntax |
||
536 | * @param int $state a LEXER_STATE_* constant |
||
537 | * @param int $pos byte position in the original source file |
||
538 | * @return bool mode handled? |
||
539 | */ |
||
540 | public function phpblock($match, $state, $pos) { |
||
546 | |||
547 | /** |
||
548 | * @param string $match matched syntax |
||
549 | * @param int $state a LEXER_STATE_* constant |
||
550 | * @param int $pos byte position in the original source file |
||
551 | * @return bool mode handled? |
||
552 | */ |
||
553 | public function html($match, $state, $pos) { |
||
559 | |||
560 | /** |
||
561 | * @param string $match matched syntax |
||
562 | * @param int $state a LEXER_STATE_* constant |
||
563 | * @param int $pos byte position in the original source file |
||
564 | * @return bool mode handled? |
||
565 | */ |
||
566 | public function htmlblock($match, $state, $pos) { |
||
572 | |||
573 | /** |
||
574 | * @param string $match matched syntax |
||
575 | * @param int $state a LEXER_STATE_* constant |
||
576 | * @param int $pos byte position in the original source file |
||
577 | * @return bool mode handled? |
||
578 | */ |
||
579 | public function preformatted($match, $state, $pos) { |
||
601 | |||
602 | /** |
||
603 | * @param string $match matched syntax |
||
604 | * @param int $state a LEXER_STATE_* constant |
||
605 | * @param int $pos byte position in the original source file |
||
606 | * @return bool mode handled? |
||
607 | */ |
||
608 | public function quote($match, $state, $pos) { |
||
636 | |||
637 | /** |
||
638 | * @param string $match matched syntax |
||
639 | * @param int $state a LEXER_STATE_* constant |
||
640 | * @param int $pos byte position in the original source file |
||
641 | * @return bool mode handled? |
||
642 | */ |
||
643 | public function file($match, $state, $pos) { |
||
646 | |||
647 | /** |
||
648 | * @param string $match matched syntax |
||
649 | * @param int $state a LEXER_STATE_* constant |
||
650 | * @param int $pos byte position in the original source file |
||
651 | * @param string $type either 'code' or 'file' |
||
652 | * @return bool mode handled? |
||
653 | */ |
||
654 | public function code($match, $state, $pos, $type='code') { |
||
675 | |||
676 | /** |
||
677 | * @param string $match matched syntax |
||
678 | * @param int $state a LEXER_STATE_* constant |
||
679 | * @param int $pos byte position in the original source file |
||
680 | * @return bool mode handled? |
||
681 | */ |
||
682 | public function acronym($match, $state, $pos) { |
||
686 | |||
687 | /** |
||
688 | * @param string $match matched syntax |
||
689 | * @param int $state a LEXER_STATE_* constant |
||
690 | * @param int $pos byte position in the original source file |
||
691 | * @return bool mode handled? |
||
692 | */ |
||
693 | public function smiley($match, $state, $pos) { |
||
697 | |||
698 | /** |
||
699 | * @param string $match matched syntax |
||
700 | * @param int $state a LEXER_STATE_* constant |
||
701 | * @param int $pos byte position in the original source file |
||
702 | * @return bool mode handled? |
||
703 | */ |
||
704 | public function wordblock($match, $state, $pos) { |
||
708 | |||
709 | /** |
||
710 | * @param string $match matched syntax |
||
711 | * @param int $state a LEXER_STATE_* constant |
||
712 | * @param int $pos byte position in the original source file |
||
713 | * @return bool mode handled? |
||
714 | */ |
||
715 | public function entity($match, $state, $pos) { |
||
719 | |||
720 | /** |
||
721 | * @param string $match matched syntax |
||
722 | * @param int $state a LEXER_STATE_* constant |
||
723 | * @param int $pos byte position in the original source file |
||
724 | * @return bool mode handled? |
||
725 | */ |
||
726 | public function multiplyentity($match, $state, $pos) { |
||
731 | |||
732 | /** |
||
733 | * @param string $match matched syntax |
||
734 | * @param int $state a LEXER_STATE_* constant |
||
735 | * @param int $pos byte position in the original source file |
||
736 | * @return bool mode handled? |
||
737 | */ |
||
738 | public function singlequoteopening($match, $state, $pos) { |
||
742 | |||
743 | /** |
||
744 | * @param string $match matched syntax |
||
745 | * @param int $state a LEXER_STATE_* constant |
||
746 | * @param int $pos byte position in the original source file |
||
747 | * @return bool mode handled? |
||
748 | */ |
||
749 | public function singlequoteclosing($match, $state, $pos) { |
||
753 | |||
754 | /** |
||
755 | * @param string $match matched syntax |
||
756 | * @param int $state a LEXER_STATE_* constant |
||
757 | * @param int $pos byte position in the original source file |
||
758 | * @return bool mode handled? |
||
759 | */ |
||
760 | public function apostrophe($match, $state, $pos) { |
||
764 | |||
765 | /** |
||
766 | * @param string $match matched syntax |
||
767 | * @param int $state a LEXER_STATE_* constant |
||
768 | * @param int $pos byte position in the original source file |
||
769 | * @return bool mode handled? |
||
770 | */ |
||
771 | public function doublequoteopening($match, $state, $pos) { |
||
776 | |||
777 | /** |
||
778 | * @param string $match matched syntax |
||
779 | * @param int $state a LEXER_STATE_* constant |
||
780 | * @param int $pos byte position in the original source file |
||
781 | * @return bool mode handled? |
||
782 | */ |
||
783 | public function doublequoteclosing($match, $state, $pos) { |
||
792 | |||
793 | /** |
||
794 | * @param string $match matched syntax |
||
795 | * @param int $state a LEXER_STATE_* constant |
||
796 | * @param int $pos byte position in the original source file |
||
797 | * @return bool mode handled? |
||
798 | */ |
||
799 | public function camelcaselink($match, $state, $pos) { |
||
803 | |||
804 | /** |
||
805 | * @param string $match matched syntax |
||
806 | * @param int $state a LEXER_STATE_* constant |
||
807 | * @param int $pos byte position in the original source file |
||
808 | * @return bool mode handled? |
||
809 | */ |
||
810 | public function internallink($match, $state, $pos) { |
||
873 | |||
874 | /** |
||
875 | * @param string $match matched syntax |
||
876 | * @param int $state a LEXER_STATE_* constant |
||
877 | * @param int $pos byte position in the original source file |
||
878 | * @return bool mode handled? |
||
879 | */ |
||
880 | public function filelink($match, $state, $pos) { |
||
884 | |||
885 | /** |
||
886 | * @param string $match matched syntax |
||
887 | * @param int $state a LEXER_STATE_* constant |
||
888 | * @param int $pos byte position in the original source file |
||
889 | * @return bool mode handled? |
||
890 | */ |
||
891 | public function windowssharelink($match, $state, $pos) { |
||
895 | |||
896 | /** |
||
897 | * @param string $match matched syntax |
||
898 | * @param int $state a LEXER_STATE_* constant |
||
899 | * @param int $pos byte position in the original source file |
||
900 | * @return bool mode handled? |
||
901 | */ |
||
902 | public function media($match, $state, $pos) { |
||
913 | |||
914 | /** |
||
915 | * @param string $match matched syntax |
||
916 | * @param int $state a LEXER_STATE_* constant |
||
917 | * @param int $pos byte position in the original source file |
||
918 | * @return bool mode handled? |
||
919 | */ |
||
920 | public function rss($match, $state, $pos) { |
||
948 | |||
949 | /** |
||
950 | * @param string $match matched syntax |
||
951 | * @param int $state a LEXER_STATE_* constant |
||
952 | * @param int $pos byte position in the original source file |
||
953 | * @return bool mode handled? |
||
954 | */ |
||
955 | public function externallink($match, $state, $pos) { |
||
972 | |||
973 | /** |
||
974 | * @param string $match matched syntax |
||
975 | * @param int $state a LEXER_STATE_* constant |
||
976 | * @param int $pos byte position in the original source file |
||
977 | * @return bool mode handled? |
||
978 | */ |
||
979 | public function emaillink($match, $state, $pos) { |
||
984 | |||
985 | /** |
||
986 | * @param string $match matched syntax |
||
987 | * @param int $state a LEXER_STATE_* constant |
||
988 | * @param int $pos byte position in the original source file |
||
989 | * @return bool mode handled? |
||
990 | */ |
||
991 | public function table($match, $state, $pos) { |
||
1043 | |||
1044 | // endregion modes |
||
1045 | } |
||
1046 | |||
1137 |
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.