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 | /** | ||
| 71 | * Return the current internal status of the given name | ||
| 72 | * | ||
| 73 | * @param string $status | ||
| 74 | * @return mixed|null | ||
| 75 | */ | ||
| 76 |     public function getStatus($status) { | ||
| 80 | |||
| 81 | /** | ||
| 82 | * Set a new internal status | ||
| 83 | * | ||
| 84 | * @param string $status | ||
| 85 | * @param mixed $value | ||
| 86 | */ | ||
| 87 |     public function setStatus($status, $value) { | ||
| 90 | |||
| 91 | /** @deprecated 2019-10-31 use addCall() instead */ | ||
| 92 |     public function _addCall($handler, $args, $pos) { | ||
| 96 | |||
| 97 | /** | ||
| 98 | * Similar to addCall, but adds a plugin call | ||
| 99 | * | ||
| 100 | * @param string $plugin name of the plugin | ||
| 101 | * @param mixed $args arguments for this call | ||
| 102 | * @param int $state a LEXER_STATE_* constant | ||
| 103 | * @param int $pos byte position in the original source file | ||
| 104 | * @param string $match matched syntax | ||
| 105 | */ | ||
| 106 |     public function addPluginCall($plugin, $args, $state, $pos, $match) { | ||
| 110 | |||
| 111 | /** | ||
| 112 | * Finishes handling | ||
| 113 | * | ||
| 114 | * Called from the parser. Calls finalise() on the call writer, closes open | ||
| 115 | * sections, rewrites blocks and adds document_start and document_end calls. | ||
| 116 | * | ||
| 117 | * @triggers PARSER_HANDLER_DONE | ||
| 118 | */ | ||
| 119 |     public function finalize(){ | ||
| 138 | |||
| 139 | /** | ||
| 140 | * fetch the current call and advance the pointer to the next one | ||
| 141 | * | ||
| 142 | * @fixme seems to be unused? | ||
| 143 | * @return bool|mixed | ||
| 144 | */ | ||
| 145 |     public function fetch() { | ||
| 153 | |||
| 154 | |||
| 155 | /** | ||
| 156 | * Internal function for parsing highlight options. | ||
| 157 | * $options is parsed for key value pairs separated by commas. | ||
| 158 | * A value might also be missing in which case the value will simple | ||
| 159 | * be set to true. Commas in strings are ignored, e.g. option="4,56" | ||
| 160 | * will work as expected and will only create one entry. | ||
| 161 | * | ||
| 162 | * @param string $options space separated list of key-value pairs, | ||
| 163 | * e.g. option1=123, option2="456" | ||
| 164 | * @return array|null Array of key-value pairs $array['key'] = 'value'; | ||
| 165 | * or null if no entries found | ||
| 166 | */ | ||
| 167 |     protected function parse_highlight_options($options) { | ||
| 225 | |||
| 226 | /** | ||
| 227 | * Simplifies handling for the formatting tags which all behave the same | ||
| 228 | * | ||
| 229 | * @param string $match matched syntax | ||
| 230 | * @param int $state a LEXER_STATE_* constant | ||
| 231 | * @param int $pos byte position in the original source file | ||
| 232 | * @param string $name actual mode name | ||
| 233 | */ | ||
| 234 |     protected function nestingTag($match, $state, $pos, $name) { | ||
| 247 | |||
| 248 | |||
| 249 | /** | ||
| 250 | * The following methods define the handlers for the different Syntax modes | ||
| 251 | * | ||
| 252 | * The handlers are called from dokuwiki\Parsing\Lexer\Lexer\invokeParser() | ||
| 253 | * | ||
| 254 | * @todo it might make sense to move these into their own class or merge them with the | ||
| 255 | * ParserMode classes some time. | ||
| 256 | */ | ||
| 257 | // region mode handlers | ||
| 258 | |||
| 259 | /** | ||
| 260 | * Special plugin handler | ||
| 261 | * | ||
| 262 | * This handler is called for all modes starting with 'plugin_'. | ||
| 263 | * An additional parameter with the plugin name is passed. The plugin's handle() | ||
| 264 | * method is called here | ||
| 265 | * | ||
| 266 | * @author Andreas Gohr <[email protected]> | ||
| 267 | * | ||
| 268 | * @param string $match matched syntax | ||
| 269 | * @param int $state a LEXER_STATE_* constant | ||
| 270 | * @param int $pos byte position in the original source file | ||
| 271 | * @param string $pluginname name of the plugin | ||
| 272 | * @return bool mode handled? | ||
| 273 | */ | ||
| 274 |     public function plugin($match, $state, $pos, $pluginname){ | ||
| 286 | |||
| 287 | /** | ||
| 288 | * @param string $match matched syntax | ||
| 289 | * @param int $state a LEXER_STATE_* constant | ||
| 290 | * @param int $pos byte position in the original source file | ||
| 291 | * @return bool mode handled? | ||
| 292 | */ | ||
| 293 |     public function base($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 header($match, $state, $pos) { | ||
| 325 | |||
| 326 | /** | ||
| 327 | * @param string $match matched syntax | ||
| 328 | * @param int $state a LEXER_STATE_* constant | ||
| 329 | * @param int $pos byte position in the original source file | ||
| 330 | * @return bool mode handled? | ||
| 331 | */ | ||
| 332 |     public function notoc($match, $state, $pos) { | ||
| 336 | |||
| 337 | /** | ||
| 338 | * @param string $match matched syntax | ||
| 339 | * @param int $state a LEXER_STATE_* constant | ||
| 340 | * @param int $pos byte position in the original source file | ||
| 341 | * @return bool mode handled? | ||
| 342 | */ | ||
| 343 |     public function nocache($match, $state, $pos) { | ||
| 347 | |||
| 348 | /** | ||
| 349 | * @param string $match matched syntax | ||
| 350 | * @param int $state a LEXER_STATE_* constant | ||
| 351 | * @param int $pos byte position in the original source file | ||
| 352 | * @return bool mode handled? | ||
| 353 | */ | ||
| 354 |     public function linebreak($match, $state, $pos) { | ||
| 358 | |||
| 359 | /** | ||
| 360 | * @param string $match matched syntax | ||
| 361 | * @param int $state a LEXER_STATE_* constant | ||
| 362 | * @param int $pos byte position in the original source file | ||
| 363 | * @return bool mode handled? | ||
| 364 | */ | ||
| 365 |     public function eol($match, $state, $pos) { | ||
| 369 | |||
| 370 | /** | ||
| 371 | * @param string $match matched syntax | ||
| 372 | * @param int $state a LEXER_STATE_* constant | ||
| 373 | * @param int $pos byte position in the original source file | ||
| 374 | * @return bool mode handled? | ||
| 375 | */ | ||
| 376 |     public function hr($match, $state, $pos) { | ||
| 380 | |||
| 381 | /** | ||
| 382 | * @param string $match matched syntax | ||
| 383 | * @param int $state a LEXER_STATE_* constant | ||
| 384 | * @param int $pos byte position in the original source file | ||
| 385 | * @return bool mode handled? | ||
| 386 | */ | ||
| 387 |     public function strong($match, $state, $pos) { | ||
| 391 | |||
| 392 | /** | ||
| 393 | * @param string $match matched syntax | ||
| 394 | * @param int $state a LEXER_STATE_* constant | ||
| 395 | * @param int $pos byte position in the original source file | ||
| 396 | * @return bool mode handled? | ||
| 397 | */ | ||
| 398 |     public function emphasis($match, $state, $pos) { | ||
| 402 | |||
| 403 | /** | ||
| 404 | * @param string $match matched syntax | ||
| 405 | * @param int $state a LEXER_STATE_* constant | ||
| 406 | * @param int $pos byte position in the original source file | ||
| 407 | * @return bool mode handled? | ||
| 408 | */ | ||
| 409 |     public function underline($match, $state, $pos) { | ||
| 413 | |||
| 414 | /** | ||
| 415 | * @param string $match matched syntax | ||
| 416 | * @param int $state a LEXER_STATE_* constant | ||
| 417 | * @param int $pos byte position in the original source file | ||
| 418 | * @return bool mode handled? | ||
| 419 | */ | ||
| 420 |     public function monospace($match, $state, $pos) { | ||
| 424 | |||
| 425 | /** | ||
| 426 | * @param string $match matched syntax | ||
| 427 | * @param int $state a LEXER_STATE_* constant | ||
| 428 | * @param int $pos byte position in the original source file | ||
| 429 | * @return bool mode handled? | ||
| 430 | */ | ||
| 431 |     public function subscript($match, $state, $pos) { | ||
| 435 | |||
| 436 | /** | ||
| 437 | * @param string $match matched syntax | ||
| 438 | * @param int $state a LEXER_STATE_* constant | ||
| 439 | * @param int $pos byte position in the original source file | ||
| 440 | * @return bool mode handled? | ||
| 441 | */ | ||
| 442 |     public function superscript($match, $state, $pos) { | ||
| 446 | |||
| 447 | /** | ||
| 448 | * @param string $match matched syntax | ||
| 449 | * @param int $state a LEXER_STATE_* constant | ||
| 450 | * @param int $pos byte position in the original source file | ||
| 451 | * @return bool mode handled? | ||
| 452 | */ | ||
| 453 |     public function deleted($match, $state, $pos) { | ||
| 457 | |||
| 458 | /** | ||
| 459 | * @param string $match matched syntax | ||
| 460 | * @param int $state a LEXER_STATE_* constant | ||
| 461 | * @param int $pos byte position in the original source file | ||
| 462 | * @return bool mode handled? | ||
| 463 | */ | ||
| 464 |     public function footnote($match, $state, $pos) { | ||
| 500 | |||
| 501 | /** | ||
| 502 | * @param string $match matched syntax | ||
| 503 | * @param int $state a LEXER_STATE_* constant | ||
| 504 | * @param int $pos byte position in the original source file | ||
| 505 | * @return bool mode handled? | ||
| 506 | */ | ||
| 507 |     public function listblock($match, $state, $pos) { | ||
| 528 | |||
| 529 | /** | ||
| 530 | * @param string $match matched syntax | ||
| 531 | * @param int $state a LEXER_STATE_* constant | ||
| 532 | * @param int $pos byte position in the original source file | ||
| 533 | * @return bool mode handled? | ||
| 534 | */ | ||
| 535 |     public function unformatted($match, $state, $pos) { | ||
| 541 | |||
| 542 | /** | ||
| 543 | * @param string $match matched syntax | ||
| 544 | * @param int $state a LEXER_STATE_* constant | ||
| 545 | * @param int $pos byte position in the original source file | ||
| 546 | * @return bool mode handled? | ||
| 547 | */ | ||
| 548 |     public function php($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 phpblock($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 html($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 htmlblock($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 preformatted($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 | * @return bool mode handled? | ||
| 628 | */ | ||
| 629 |     public function quote($match, $state, $pos) { | ||
| 657 | |||
| 658 | /** | ||
| 659 | * @param string $match matched syntax | ||
| 660 | * @param int $state a LEXER_STATE_* constant | ||
| 661 | * @param int $pos byte position in the original source file | ||
| 662 | * @return bool mode handled? | ||
| 663 | */ | ||
| 664 |     public function file($match, $state, $pos) { | ||
| 667 | |||
| 668 | /** | ||
| 669 | * @param string $match matched syntax | ||
| 670 | * @param int $state a LEXER_STATE_* constant | ||
| 671 | * @param int $pos byte position in the original source file | ||
| 672 | * @param string $type either 'code' or 'file' | ||
| 673 | * @return bool mode handled? | ||
| 674 | */ | ||
| 675 |     public function code($match, $state, $pos, $type='code') { | ||
| 696 | |||
| 697 | /** | ||
| 698 | * @param string $match matched syntax | ||
| 699 | * @param int $state a LEXER_STATE_* constant | ||
| 700 | * @param int $pos byte position in the original source file | ||
| 701 | * @return bool mode handled? | ||
| 702 | */ | ||
| 703 |     public function acronym($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 smiley($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 wordblock($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 entity($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 multiplyentity($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 singlequoteopening($match, $state, $pos) { | ||
| 763 | |||
| 764 | /** | ||
| 765 | * @param string $match matched syntax | ||
| 766 | * @param int $state a LEXER_STATE_* constant | ||
| 767 | * @param int $pos byte position in the original source file | ||
| 768 | * @return bool mode handled? | ||
| 769 | */ | ||
| 770 |     public function singlequoteclosing($match, $state, $pos) { | ||
| 774 | |||
| 775 | /** | ||
| 776 | * @param string $match matched syntax | ||
| 777 | * @param int $state a LEXER_STATE_* constant | ||
| 778 | * @param int $pos byte position in the original source file | ||
| 779 | * @return bool mode handled? | ||
| 780 | */ | ||
| 781 |     public function apostrophe($match, $state, $pos) { | ||
| 785 | |||
| 786 | /** | ||
| 787 | * @param string $match matched syntax | ||
| 788 | * @param int $state a LEXER_STATE_* constant | ||
| 789 | * @param int $pos byte position in the original source file | ||
| 790 | * @return bool mode handled? | ||
| 791 | */ | ||
| 792 |     public function doublequoteopening($match, $state, $pos) { | ||
| 797 | |||
| 798 | /** | ||
| 799 | * @param string $match matched syntax | ||
| 800 | * @param int $state a LEXER_STATE_* constant | ||
| 801 | * @param int $pos byte position in the original source file | ||
| 802 | * @return bool mode handled? | ||
| 803 | */ | ||
| 804 |     public function doublequoteclosing($match, $state, $pos) { | ||
| 813 | |||
| 814 | /** | ||
| 815 | * @param string $match matched syntax | ||
| 816 | * @param int $state a LEXER_STATE_* constant | ||
| 817 | * @param int $pos byte position in the original source file | ||
| 818 | * @return bool mode handled? | ||
| 819 | */ | ||
| 820 |     public function camelcaselink($match, $state, $pos) { | ||
| 824 | |||
| 825 | /** | ||
| 826 | * @param string $match matched syntax | ||
| 827 | * @param int $state a LEXER_STATE_* constant | ||
| 828 | * @param int $pos byte position in the original source file | ||
| 829 | * @return bool mode handled? | ||
| 830 | */ | ||
| 831 |     public function internallink($match, $state, $pos) { | ||
| 894 | |||
| 895 | /** | ||
| 896 | * @param string $match matched syntax | ||
| 897 | * @param int $state a LEXER_STATE_* constant | ||
| 898 | * @param int $pos byte position in the original source file | ||
| 899 | * @return bool mode handled? | ||
| 900 | */ | ||
| 901 |     public function filelink($match, $state, $pos) { | ||
| 905 | |||
| 906 | /** | ||
| 907 | * @param string $match matched syntax | ||
| 908 | * @param int $state a LEXER_STATE_* constant | ||
| 909 | * @param int $pos byte position in the original source file | ||
| 910 | * @return bool mode handled? | ||
| 911 | */ | ||
| 912 |     public function windowssharelink($match, $state, $pos) { | ||
| 916 | |||
| 917 | /** | ||
| 918 | * @param string $match matched syntax | ||
| 919 | * @param int $state a LEXER_STATE_* constant | ||
| 920 | * @param int $pos byte position in the original source file | ||
| 921 | * @return bool mode handled? | ||
| 922 | */ | ||
| 923 |     public function media($match, $state, $pos) { | ||
| 934 | |||
| 935 | /** | ||
| 936 | * @param string $match matched syntax | ||
| 937 | * @param int $state a LEXER_STATE_* constant | ||
| 938 | * @param int $pos byte position in the original source file | ||
| 939 | * @return bool mode handled? | ||
| 940 | */ | ||
| 941 |     public function rss($match, $state, $pos) { | ||
| 969 | |||
| 970 | /** | ||
| 971 | * @param string $match matched syntax | ||
| 972 | * @param int $state a LEXER_STATE_* constant | ||
| 973 | * @param int $pos byte position in the original source file | ||
| 974 | * @return bool mode handled? | ||
| 975 | */ | ||
| 976 |     public function externallink($match, $state, $pos) { | ||
| 993 | |||
| 994 | /** | ||
| 995 | * @param string $match matched syntax | ||
| 996 | * @param int $state a LEXER_STATE_* constant | ||
| 997 | * @param int $pos byte position in the original source file | ||
| 998 | * @return bool mode handled? | ||
| 999 | */ | ||
| 1000 |     public function emaillink($match, $state, $pos) { | ||
| 1005 | |||
| 1006 | /** | ||
| 1007 | * @param string $match matched syntax | ||
| 1008 | * @param int $state a LEXER_STATE_* constant | ||
| 1009 | * @param int $pos byte position in the original source file | ||
| 1010 | * @return bool mode handled? | ||
| 1011 | */ | ||
| 1012 |     public function table($match, $state, $pos) { | ||
| 1064 | |||
| 1065 | // endregion modes | ||
| 1066 | } | ||
| 1067 | |||
| 1158 | 
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.