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 | protected function addCall($handler, $args, $pos) { |
||
51 | |||
52 | /** |
||
53 | * Similar to addCall, but adds a plugin call |
||
54 | * |
||
55 | * @param string $plugin name of the plugin |
||
56 | * @param mixed $args arguments for this call |
||
57 | * @param int $state a LEXER_STATE_* constant |
||
58 | * @param int $pos byte position in the original source file |
||
59 | * @param string $match matched syntax |
||
60 | */ |
||
61 | protected function addPluginCall($plugin, $args, $state, $pos, $match) { |
||
65 | |||
66 | /** |
||
67 | * Finishes handling |
||
68 | * |
||
69 | * Called from the parser. Calls finalise() on the call writer, closes open |
||
70 | * sections, rewrites blocks and adds document_start and document_end calls. |
||
71 | * |
||
72 | * @triggers PARSER_HANDLER_DONE |
||
73 | */ |
||
74 | public function finalize(){ |
||
93 | |||
94 | /** |
||
95 | * fetch the current call and advance the pointer to the next one |
||
96 | * |
||
97 | * @fixme seems to be unused? |
||
98 | * @return bool|mixed |
||
99 | */ |
||
100 | public function fetch() { |
||
108 | |||
109 | |||
110 | /** |
||
111 | * Internal function for parsing highlight options. |
||
112 | * $options is parsed for key value pairs separated by commas. |
||
113 | * A value might also be missing in which case the value will simple |
||
114 | * be set to true. Commas in strings are ignored, e.g. option="4,56" |
||
115 | * will work as expected and will only create one entry. |
||
116 | * |
||
117 | * @param string $options space separated list of key-value pairs, |
||
118 | * e.g. option1=123, option2="456" |
||
119 | * @return array|null Array of key-value pairs $array['key'] = 'value'; |
||
120 | * or null if no entries found |
||
121 | */ |
||
122 | protected function parse_highlight_options($options) { |
||
180 | |||
181 | /** |
||
182 | * Simplifies handling for the formatting tags which all behave the same |
||
183 | * |
||
184 | * @param string $match matched syntax |
||
185 | * @param int $state a LEXER_STATE_* constant |
||
186 | * @param int $pos byte position in the original source file |
||
187 | * @param string $name actual mode name |
||
188 | */ |
||
189 | protected function nestingTag($match, $state, $pos, $name) { |
||
202 | |||
203 | |||
204 | /** |
||
205 | * The following methods define the handlers for the different Syntax modes |
||
206 | * |
||
207 | * The handlers are called from dokuwiki\Parsing\Lexer\Lexer\invokeParser() |
||
208 | * |
||
209 | * @todo it might make sense to move these into their own class or merge them with the |
||
210 | * ParserMode classes some time. |
||
211 | */ |
||
212 | // region mode handlers |
||
213 | |||
214 | /** |
||
215 | * Special plugin handler |
||
216 | * |
||
217 | * This handler is called for all modes starting with 'plugin_'. |
||
218 | * An additional parameter with the plugin name is passed. The plugin's handle() |
||
219 | * method is called here |
||
220 | * |
||
221 | * @author Andreas Gohr <[email protected]> |
||
222 | * |
||
223 | * @param string $match matched syntax |
||
224 | * @param int $state a LEXER_STATE_* constant |
||
225 | * @param int $pos byte position in the original source file |
||
226 | * @param string $pluginname name of the plugin |
||
227 | * @return bool mode handled? |
||
228 | */ |
||
229 | public function plugin($match, $state, $pos, $pluginname){ |
||
241 | |||
242 | /** |
||
243 | * @param string $match matched syntax |
||
244 | * @param int $state a LEXER_STATE_* constant |
||
245 | * @param int $pos byte position in the original source file |
||
246 | * @return bool mode handled? |
||
247 | */ |
||
248 | public function base($match, $state, $pos) { |
||
257 | |||
258 | /** |
||
259 | * @param string $match matched syntax |
||
260 | * @param int $state a LEXER_STATE_* constant |
||
261 | * @param int $pos byte position in the original source file |
||
262 | * @return bool mode handled? |
||
263 | */ |
||
264 | public function header($match, $state, $pos) { |
||
280 | |||
281 | /** |
||
282 | * @param string $match matched syntax |
||
283 | * @param int $state a LEXER_STATE_* constant |
||
284 | * @param int $pos byte position in the original source file |
||
285 | * @return bool mode handled? |
||
286 | */ |
||
287 | public function notoc($match, $state, $pos) { |
||
291 | |||
292 | /** |
||
293 | * @param string $match matched syntax |
||
294 | * @param int $state a LEXER_STATE_* constant |
||
295 | * @param int $pos byte position in the original source file |
||
296 | * @return bool mode handled? |
||
297 | */ |
||
298 | public function nocache($match, $state, $pos) { |
||
302 | |||
303 | /** |
||
304 | * @param string $match matched syntax |
||
305 | * @param int $state a LEXER_STATE_* constant |
||
306 | * @param int $pos byte position in the original source file |
||
307 | * @return bool mode handled? |
||
308 | */ |
||
309 | public function linebreak($match, $state, $pos) { |
||
313 | |||
314 | /** |
||
315 | * @param string $match matched syntax |
||
316 | * @param int $state a LEXER_STATE_* constant |
||
317 | * @param int $pos byte position in the original source file |
||
318 | * @return bool mode handled? |
||
319 | */ |
||
320 | public function eol($match, $state, $pos) { |
||
324 | |||
325 | /** |
||
326 | * @param string $match matched syntax |
||
327 | * @param int $state a LEXER_STATE_* constant |
||
328 | * @param int $pos byte position in the original source file |
||
329 | * @return bool mode handled? |
||
330 | */ |
||
331 | public function hr($match, $state, $pos) { |
||
335 | |||
336 | /** |
||
337 | * @param string $match matched syntax |
||
338 | * @param int $state a LEXER_STATE_* constant |
||
339 | * @param int $pos byte position in the original source file |
||
340 | * @return bool mode handled? |
||
341 | */ |
||
342 | public function strong($match, $state, $pos) { |
||
346 | |||
347 | /** |
||
348 | * @param string $match matched syntax |
||
349 | * @param int $state a LEXER_STATE_* constant |
||
350 | * @param int $pos byte position in the original source file |
||
351 | * @return bool mode handled? |
||
352 | */ |
||
353 | public function emphasis($match, $state, $pos) { |
||
357 | |||
358 | /** |
||
359 | * @param string $match matched syntax |
||
360 | * @param int $state a LEXER_STATE_* constant |
||
361 | * @param int $pos byte position in the original source file |
||
362 | * @return bool mode handled? |
||
363 | */ |
||
364 | public function underline($match, $state, $pos) { |
||
368 | |||
369 | /** |
||
370 | * @param string $match matched syntax |
||
371 | * @param int $state a LEXER_STATE_* constant |
||
372 | * @param int $pos byte position in the original source file |
||
373 | * @return bool mode handled? |
||
374 | */ |
||
375 | public function monospace($match, $state, $pos) { |
||
379 | |||
380 | /** |
||
381 | * @param string $match matched syntax |
||
382 | * @param int $state a LEXER_STATE_* constant |
||
383 | * @param int $pos byte position in the original source file |
||
384 | * @return bool mode handled? |
||
385 | */ |
||
386 | public function subscript($match, $state, $pos) { |
||
390 | |||
391 | /** |
||
392 | * @param string $match matched syntax |
||
393 | * @param int $state a LEXER_STATE_* constant |
||
394 | * @param int $pos byte position in the original source file |
||
395 | * @return bool mode handled? |
||
396 | */ |
||
397 | public function superscript($match, $state, $pos) { |
||
401 | |||
402 | /** |
||
403 | * @param string $match matched syntax |
||
404 | * @param int $state a LEXER_STATE_* constant |
||
405 | * @param int $pos byte position in the original source file |
||
406 | * @return bool mode handled? |
||
407 | */ |
||
408 | public function deleted($match, $state, $pos) { |
||
412 | |||
413 | /** |
||
414 | * @param string $match matched syntax |
||
415 | * @param int $state a LEXER_STATE_* constant |
||
416 | * @param int $pos byte position in the original source file |
||
417 | * @return bool mode handled? |
||
418 | */ |
||
419 | public function footnote($match, $state, $pos) { |
||
455 | |||
456 | /** |
||
457 | * @param string $match matched syntax |
||
458 | * @param int $state a LEXER_STATE_* constant |
||
459 | * @param int $pos byte position in the original source file |
||
460 | * @return bool mode handled? |
||
461 | */ |
||
462 | public function listblock($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 unformatted($match, $state, $pos) { |
||
496 | |||
497 | /** |
||
498 | * @param string $match matched syntax |
||
499 | * @param int $state a LEXER_STATE_* constant |
||
500 | * @param int $pos byte position in the original source file |
||
501 | * @return bool mode handled? |
||
502 | */ |
||
503 | public function php($match, $state, $pos) { |
||
509 | |||
510 | /** |
||
511 | * @param string $match matched syntax |
||
512 | * @param int $state a LEXER_STATE_* constant |
||
513 | * @param int $pos byte position in the original source file |
||
514 | * @return bool mode handled? |
||
515 | */ |
||
516 | public function phpblock($match, $state, $pos) { |
||
522 | |||
523 | /** |
||
524 | * @param string $match matched syntax |
||
525 | * @param int $state a LEXER_STATE_* constant |
||
526 | * @param int $pos byte position in the original source file |
||
527 | * @return bool mode handled? |
||
528 | */ |
||
529 | public function html($match, $state, $pos) { |
||
535 | |||
536 | /** |
||
537 | * @param string $match matched syntax |
||
538 | * @param int $state a LEXER_STATE_* constant |
||
539 | * @param int $pos byte position in the original source file |
||
540 | * @return bool mode handled? |
||
541 | */ |
||
542 | public function htmlblock($match, $state, $pos) { |
||
548 | |||
549 | /** |
||
550 | * @param string $match matched syntax |
||
551 | * @param int $state a LEXER_STATE_* constant |
||
552 | * @param int $pos byte position in the original source file |
||
553 | * @return bool mode handled? |
||
554 | */ |
||
555 | public function preformatted($match, $state, $pos) { |
||
577 | |||
578 | /** |
||
579 | * @param string $match matched syntax |
||
580 | * @param int $state a LEXER_STATE_* constant |
||
581 | * @param int $pos byte position in the original source file |
||
582 | * @return bool mode handled? |
||
583 | */ |
||
584 | public function quote($match, $state, $pos) { |
||
612 | |||
613 | /** |
||
614 | * @param string $match matched syntax |
||
615 | * @param int $state a LEXER_STATE_* constant |
||
616 | * @param int $pos byte position in the original source file |
||
617 | * @return bool mode handled? |
||
618 | */ |
||
619 | public function file($match, $state, $pos) { |
||
622 | |||
623 | /** |
||
624 | * @param string $match matched syntax |
||
625 | * @param int $state a LEXER_STATE_* constant |
||
626 | * @param int $pos byte position in the original source file |
||
627 | * @param string $type either 'code' or 'file' |
||
628 | * @return bool mode handled? |
||
629 | */ |
||
630 | public function code($match, $state, $pos, $type='code') { |
||
651 | |||
652 | /** |
||
653 | * @param string $match matched syntax |
||
654 | * @param int $state a LEXER_STATE_* constant |
||
655 | * @param int $pos byte position in the original source file |
||
656 | * @return bool mode handled? |
||
657 | */ |
||
658 | public function acronym($match, $state, $pos) { |
||
662 | |||
663 | /** |
||
664 | * @param string $match matched syntax |
||
665 | * @param int $state a LEXER_STATE_* constant |
||
666 | * @param int $pos byte position in the original source file |
||
667 | * @return bool mode handled? |
||
668 | */ |
||
669 | public function smiley($match, $state, $pos) { |
||
673 | |||
674 | /** |
||
675 | * @param string $match matched syntax |
||
676 | * @param int $state a LEXER_STATE_* constant |
||
677 | * @param int $pos byte position in the original source file |
||
678 | * @return bool mode handled? |
||
679 | */ |
||
680 | public function wordblock($match, $state, $pos) { |
||
684 | |||
685 | /** |
||
686 | * @param string $match matched syntax |
||
687 | * @param int $state a LEXER_STATE_* constant |
||
688 | * @param int $pos byte position in the original source file |
||
689 | * @return bool mode handled? |
||
690 | */ |
||
691 | public function entity($match, $state, $pos) { |
||
695 | |||
696 | /** |
||
697 | * @param string $match matched syntax |
||
698 | * @param int $state a LEXER_STATE_* constant |
||
699 | * @param int $pos byte position in the original source file |
||
700 | * @return bool mode handled? |
||
701 | */ |
||
702 | public function multiplyentity($match, $state, $pos) { |
||
707 | |||
708 | /** |
||
709 | * @param string $match matched syntax |
||
710 | * @param int $state a LEXER_STATE_* constant |
||
711 | * @param int $pos byte position in the original source file |
||
712 | * @return bool mode handled? |
||
713 | */ |
||
714 | public function singlequoteopening($match, $state, $pos) { |
||
718 | |||
719 | /** |
||
720 | * @param string $match matched syntax |
||
721 | * @param int $state a LEXER_STATE_* constant |
||
722 | * @param int $pos byte position in the original source file |
||
723 | * @return bool mode handled? |
||
724 | */ |
||
725 | public function singlequoteclosing($match, $state, $pos) { |
||
729 | |||
730 | /** |
||
731 | * @param string $match matched syntax |
||
732 | * @param int $state a LEXER_STATE_* constant |
||
733 | * @param int $pos byte position in the original source file |
||
734 | * @return bool mode handled? |
||
735 | */ |
||
736 | public function apostrophe($match, $state, $pos) { |
||
740 | |||
741 | /** |
||
742 | * @param string $match matched syntax |
||
743 | * @param int $state a LEXER_STATE_* constant |
||
744 | * @param int $pos byte position in the original source file |
||
745 | * @return bool mode handled? |
||
746 | */ |
||
747 | public function doublequoteopening($match, $state, $pos) { |
||
752 | |||
753 | /** |
||
754 | * @param string $match matched syntax |
||
755 | * @param int $state a LEXER_STATE_* constant |
||
756 | * @param int $pos byte position in the original source file |
||
757 | * @return bool mode handled? |
||
758 | */ |
||
759 | public function doublequoteclosing($match, $state, $pos) { |
||
768 | |||
769 | /** |
||
770 | * @param string $match matched syntax |
||
771 | * @param int $state a LEXER_STATE_* constant |
||
772 | * @param int $pos byte position in the original source file |
||
773 | * @return bool mode handled? |
||
774 | */ |
||
775 | public function camelcaselink($match, $state, $pos) { |
||
779 | |||
780 | /** |
||
781 | * @param string $match matched syntax |
||
782 | * @param int $state a LEXER_STATE_* constant |
||
783 | * @param int $pos byte position in the original source file |
||
784 | * @return bool mode handled? |
||
785 | */ |
||
786 | public function internallink($match, $state, $pos) { |
||
849 | |||
850 | /** |
||
851 | * @param string $match matched syntax |
||
852 | * @param int $state a LEXER_STATE_* constant |
||
853 | * @param int $pos byte position in the original source file |
||
854 | * @return bool mode handled? |
||
855 | */ |
||
856 | public function filelink($match, $state, $pos) { |
||
860 | |||
861 | /** |
||
862 | * @param string $match matched syntax |
||
863 | * @param int $state a LEXER_STATE_* constant |
||
864 | * @param int $pos byte position in the original source file |
||
865 | * @return bool mode handled? |
||
866 | */ |
||
867 | public function windowssharelink($match, $state, $pos) { |
||
871 | |||
872 | /** |
||
873 | * @param string $match matched syntax |
||
874 | * @param int $state a LEXER_STATE_* constant |
||
875 | * @param int $pos byte position in the original source file |
||
876 | * @return bool mode handled? |
||
877 | */ |
||
878 | public function media($match, $state, $pos) { |
||
889 | |||
890 | /** |
||
891 | * @param string $match matched syntax |
||
892 | * @param int $state a LEXER_STATE_* constant |
||
893 | * @param int $pos byte position in the original source file |
||
894 | * @return bool mode handled? |
||
895 | */ |
||
896 | public function rss($match, $state, $pos) { |
||
924 | |||
925 | /** |
||
926 | * @param string $match matched syntax |
||
927 | * @param int $state a LEXER_STATE_* constant |
||
928 | * @param int $pos byte position in the original source file |
||
929 | * @return bool mode handled? |
||
930 | */ |
||
931 | public function externallink($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 emaillink($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 table($match, $state, $pos) { |
||
1019 | |||
1020 | // endregion modes |
||
1021 | } |
||
1022 | |||
1113 |
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.