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 | /** @deprecated 2019-10-31 use addCall() instead */ |
||
| 53 | public function _addCall($handler, $args, $pos) { |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Similar to addCall, but adds a plugin call |
||
| 60 | * |
||
| 61 | * @param string $plugin name of the plugin |
||
| 62 | * @param mixed $args arguments for this call |
||
| 63 | * @param int $state a LEXER_STATE_* constant |
||
| 64 | * @param int $pos byte position in the original source file |
||
| 65 | * @param string $match matched syntax |
||
| 66 | */ |
||
| 67 | public function addPluginCall($plugin, $args, $state, $pos, $match) { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Finishes handling |
||
| 74 | * |
||
| 75 | * Called from the parser. Calls finalise() on the call writer, closes open |
||
| 76 | * sections, rewrites blocks and adds document_start and document_end calls. |
||
| 77 | * |
||
| 78 | * @triggers PARSER_HANDLER_DONE |
||
| 79 | */ |
||
| 80 | public function finalize(){ |
||
| 99 | |||
| 100 | /** |
||
| 101 | * fetch the current call and advance the pointer to the next one |
||
| 102 | * |
||
| 103 | * @fixme seems to be unused? |
||
| 104 | * @return bool|mixed |
||
| 105 | */ |
||
| 106 | public function fetch() { |
||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * Internal function for parsing highlight options. |
||
| 118 | * $options is parsed for key value pairs separated by commas. |
||
| 119 | * A value might also be missing in which case the value will simple |
||
| 120 | * be set to true. Commas in strings are ignored, e.g. option="4,56" |
||
| 121 | * will work as expected and will only create one entry. |
||
| 122 | * |
||
| 123 | * @param string $options space separated list of key-value pairs, |
||
| 124 | * e.g. option1=123, option2="456" |
||
| 125 | * @return array|null Array of key-value pairs $array['key'] = 'value'; |
||
| 126 | * or null if no entries found |
||
| 127 | */ |
||
| 128 | protected function parse_highlight_options($options) { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Simplifies handling for the formatting tags which all behave the same |
||
| 189 | * |
||
| 190 | * @param string $match matched syntax |
||
| 191 | * @param int $state a LEXER_STATE_* constant |
||
| 192 | * @param int $pos byte position in the original source file |
||
| 193 | * @param string $name actual mode name |
||
| 194 | */ |
||
| 195 | protected function nestingTag($match, $state, $pos, $name) { |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * The following methods define the handlers for the different Syntax modes |
||
| 212 | * |
||
| 213 | * The handlers are called from dokuwiki\Parsing\Lexer\Lexer\invokeParser() |
||
| 214 | * |
||
| 215 | * @todo it might make sense to move these into their own class or merge them with the |
||
| 216 | * ParserMode classes some time. |
||
| 217 | */ |
||
| 218 | // region mode handlers |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Special plugin handler |
||
| 222 | * |
||
| 223 | * This handler is called for all modes starting with 'plugin_'. |
||
| 224 | * An additional parameter with the plugin name is passed. The plugin's handle() |
||
| 225 | * method is called here |
||
| 226 | * |
||
| 227 | * @author Andreas Gohr <[email protected]> |
||
| 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 $pluginname name of the plugin |
||
| 233 | * @return bool mode handled? |
||
| 234 | */ |
||
| 235 | public function plugin($match, $state, $pos, $pluginname){ |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param string $match matched syntax |
||
| 250 | * @param int $state a LEXER_STATE_* constant |
||
| 251 | * @param int $pos byte position in the original source file |
||
| 252 | * @return bool mode handled? |
||
| 253 | */ |
||
| 254 | public function base($match, $state, $pos) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $match matched syntax |
||
| 266 | * @param int $state a LEXER_STATE_* constant |
||
| 267 | * @param int $pos byte position in the original source file |
||
| 268 | * @return bool mode handled? |
||
| 269 | */ |
||
| 270 | public function header($match, $state, $pos) { |
||
| 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 notoc($match, $state, $pos) { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $match matched syntax |
||
| 300 | * @param int $state a LEXER_STATE_* constant |
||
| 301 | * @param int $pos byte position in the original source file |
||
| 302 | * @return bool mode handled? |
||
| 303 | */ |
||
| 304 | public function nocache($match, $state, $pos) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param string $match matched syntax |
||
| 311 | * @param int $state a LEXER_STATE_* constant |
||
| 312 | * @param int $pos byte position in the original source file |
||
| 313 | * @return bool mode handled? |
||
| 314 | */ |
||
| 315 | public function linebreak($match, $state, $pos) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param string $match matched syntax |
||
| 322 | * @param int $state a LEXER_STATE_* constant |
||
| 323 | * @param int $pos byte position in the original source file |
||
| 324 | * @return bool mode handled? |
||
| 325 | */ |
||
| 326 | public function eol($match, $state, $pos) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param string $match matched syntax |
||
| 333 | * @param int $state a LEXER_STATE_* constant |
||
| 334 | * @param int $pos byte position in the original source file |
||
| 335 | * @return bool mode handled? |
||
| 336 | */ |
||
| 337 | public function hr($match, $state, $pos) { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param string $match matched syntax |
||
| 344 | * @param int $state a LEXER_STATE_* constant |
||
| 345 | * @param int $pos byte position in the original source file |
||
| 346 | * @return bool mode handled? |
||
| 347 | */ |
||
| 348 | public function strong($match, $state, $pos) { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param string $match matched syntax |
||
| 355 | * @param int $state a LEXER_STATE_* constant |
||
| 356 | * @param int $pos byte position in the original source file |
||
| 357 | * @return bool mode handled? |
||
| 358 | */ |
||
| 359 | public function emphasis($match, $state, $pos) { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param string $match matched syntax |
||
| 366 | * @param int $state a LEXER_STATE_* constant |
||
| 367 | * @param int $pos byte position in the original source file |
||
| 368 | * @return bool mode handled? |
||
| 369 | */ |
||
| 370 | public function underline($match, $state, $pos) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param string $match matched syntax |
||
| 377 | * @param int $state a LEXER_STATE_* constant |
||
| 378 | * @param int $pos byte position in the original source file |
||
| 379 | * @return bool mode handled? |
||
| 380 | */ |
||
| 381 | public function monospace($match, $state, $pos) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param string $match matched syntax |
||
| 388 | * @param int $state a LEXER_STATE_* constant |
||
| 389 | * @param int $pos byte position in the original source file |
||
| 390 | * @return bool mode handled? |
||
| 391 | */ |
||
| 392 | public function subscript($match, $state, $pos) { |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @param string $match matched syntax |
||
| 399 | * @param int $state a LEXER_STATE_* constant |
||
| 400 | * @param int $pos byte position in the original source file |
||
| 401 | * @return bool mode handled? |
||
| 402 | */ |
||
| 403 | public function superscript($match, $state, $pos) { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param string $match matched syntax |
||
| 410 | * @param int $state a LEXER_STATE_* constant |
||
| 411 | * @param int $pos byte position in the original source file |
||
| 412 | * @return bool mode handled? |
||
| 413 | */ |
||
| 414 | public function deleted($match, $state, $pos) { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param string $match matched syntax |
||
| 421 | * @param int $state a LEXER_STATE_* constant |
||
| 422 | * @param int $pos byte position in the original source file |
||
| 423 | * @return bool mode handled? |
||
| 424 | */ |
||
| 425 | public function footnote($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 listblock($match, $state, $pos) { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @param string $match matched syntax |
||
| 492 | * @param int $state a LEXER_STATE_* constant |
||
| 493 | * @param int $pos byte position in the original source file |
||
| 494 | * @return bool mode handled? |
||
| 495 | */ |
||
| 496 | public function unformatted($match, $state, $pos) { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @param string $match matched syntax |
||
| 505 | * @param int $state a LEXER_STATE_* constant |
||
| 506 | * @param int $pos byte position in the original source file |
||
| 507 | * @return bool mode handled? |
||
| 508 | */ |
||
| 509 | public function php($match, $state, $pos) { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @param string $match matched syntax |
||
| 518 | * @param int $state a LEXER_STATE_* constant |
||
| 519 | * @param int $pos byte position in the original source file |
||
| 520 | * @return bool mode handled? |
||
| 521 | */ |
||
| 522 | public function phpblock($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 html($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 htmlblock($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 preformatted($match, $state, $pos) { |
||
| 583 | |||
| 584 | /** |
||
| 585 | * @param string $match matched syntax |
||
| 586 | * @param int $state a LEXER_STATE_* constant |
||
| 587 | * @param int $pos byte position in the original source file |
||
| 588 | * @return bool mode handled? |
||
| 589 | */ |
||
| 590 | public function quote($match, $state, $pos) { |
||
| 618 | |||
| 619 | /** |
||
| 620 | * @param string $match matched syntax |
||
| 621 | * @param int $state a LEXER_STATE_* constant |
||
| 622 | * @param int $pos byte position in the original source file |
||
| 623 | * @return bool mode handled? |
||
| 624 | */ |
||
| 625 | public function file($match, $state, $pos) { |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @param string $match matched syntax |
||
| 631 | * @param int $state a LEXER_STATE_* constant |
||
| 632 | * @param int $pos byte position in the original source file |
||
| 633 | * @param string $type either 'code' or 'file' |
||
| 634 | * @return bool mode handled? |
||
| 635 | */ |
||
| 636 | public function code($match, $state, $pos, $type='code') { |
||
| 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 acronym($match, $state, $pos) { |
||
| 668 | |||
| 669 | /** |
||
| 670 | * @param string $match matched syntax |
||
| 671 | * @param int $state a LEXER_STATE_* constant |
||
| 672 | * @param int $pos byte position in the original source file |
||
| 673 | * @return bool mode handled? |
||
| 674 | */ |
||
| 675 | public function smiley($match, $state, $pos) { |
||
| 679 | |||
| 680 | /** |
||
| 681 | * @param string $match matched syntax |
||
| 682 | * @param int $state a LEXER_STATE_* constant |
||
| 683 | * @param int $pos byte position in the original source file |
||
| 684 | * @return bool mode handled? |
||
| 685 | */ |
||
| 686 | public function wordblock($match, $state, $pos) { |
||
| 690 | |||
| 691 | /** |
||
| 692 | * @param string $match matched syntax |
||
| 693 | * @param int $state a LEXER_STATE_* constant |
||
| 694 | * @param int $pos byte position in the original source file |
||
| 695 | * @return bool mode handled? |
||
| 696 | */ |
||
| 697 | public function entity($match, $state, $pos) { |
||
| 701 | |||
| 702 | /** |
||
| 703 | * @param string $match matched syntax |
||
| 704 | * @param int $state a LEXER_STATE_* constant |
||
| 705 | * @param int $pos byte position in the original source file |
||
| 706 | * @return bool mode handled? |
||
| 707 | */ |
||
| 708 | public function multiplyentity($match, $state, $pos) { |
||
| 713 | |||
| 714 | /** |
||
| 715 | * @param string $match matched syntax |
||
| 716 | * @param int $state a LEXER_STATE_* constant |
||
| 717 | * @param int $pos byte position in the original source file |
||
| 718 | * @return bool mode handled? |
||
| 719 | */ |
||
| 720 | public function singlequoteopening($match, $state, $pos) { |
||
| 724 | |||
| 725 | /** |
||
| 726 | * @param string $match matched syntax |
||
| 727 | * @param int $state a LEXER_STATE_* constant |
||
| 728 | * @param int $pos byte position in the original source file |
||
| 729 | * @return bool mode handled? |
||
| 730 | */ |
||
| 731 | public function singlequoteclosing($match, $state, $pos) { |
||
| 735 | |||
| 736 | /** |
||
| 737 | * @param string $match matched syntax |
||
| 738 | * @param int $state a LEXER_STATE_* constant |
||
| 739 | * @param int $pos byte position in the original source file |
||
| 740 | * @return bool mode handled? |
||
| 741 | */ |
||
| 742 | public function apostrophe($match, $state, $pos) { |
||
| 746 | |||
| 747 | /** |
||
| 748 | * @param string $match matched syntax |
||
| 749 | * @param int $state a LEXER_STATE_* constant |
||
| 750 | * @param int $pos byte position in the original source file |
||
| 751 | * @return bool mode handled? |
||
| 752 | */ |
||
| 753 | public function doublequoteopening($match, $state, $pos) { |
||
| 758 | |||
| 759 | /** |
||
| 760 | * @param string $match matched syntax |
||
| 761 | * @param int $state a LEXER_STATE_* constant |
||
| 762 | * @param int $pos byte position in the original source file |
||
| 763 | * @return bool mode handled? |
||
| 764 | */ |
||
| 765 | public function doublequoteclosing($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 camelcaselink($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 internallink($match, $state, $pos) { |
||
| 855 | |||
| 856 | /** |
||
| 857 | * @param string $match matched syntax |
||
| 858 | * @param int $state a LEXER_STATE_* constant |
||
| 859 | * @param int $pos byte position in the original source file |
||
| 860 | * @return bool mode handled? |
||
| 861 | */ |
||
| 862 | public function filelink($match, $state, $pos) { |
||
| 866 | |||
| 867 | /** |
||
| 868 | * @param string $match matched syntax |
||
| 869 | * @param int $state a LEXER_STATE_* constant |
||
| 870 | * @param int $pos byte position in the original source file |
||
| 871 | * @return bool mode handled? |
||
| 872 | */ |
||
| 873 | public function windowssharelink($match, $state, $pos) { |
||
| 877 | |||
| 878 | /** |
||
| 879 | * @param string $match matched syntax |
||
| 880 | * @param int $state a LEXER_STATE_* constant |
||
| 881 | * @param int $pos byte position in the original source file |
||
| 882 | * @return bool mode handled? |
||
| 883 | */ |
||
| 884 | public function media($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 rss($match, $state, $pos) { |
||
| 930 | |||
| 931 | /** |
||
| 932 | * @param string $match matched syntax |
||
| 933 | * @param int $state a LEXER_STATE_* constant |
||
| 934 | * @param int $pos byte position in the original source file |
||
| 935 | * @return bool mode handled? |
||
| 936 | */ |
||
| 937 | public function externallink($match, $state, $pos) { |
||
| 954 | |||
| 955 | /** |
||
| 956 | * @param string $match matched syntax |
||
| 957 | * @param int $state a LEXER_STATE_* constant |
||
| 958 | * @param int $pos byte position in the original source file |
||
| 959 | * @return bool mode handled? |
||
| 960 | */ |
||
| 961 | public function emaillink($match, $state, $pos) { |
||
| 966 | |||
| 967 | /** |
||
| 968 | * @param string $match matched syntax |
||
| 969 | * @param int $state a LEXER_STATE_* constant |
||
| 970 | * @param int $pos byte position in the original source file |
||
| 971 | * @return bool mode handled? |
||
| 972 | */ |
||
| 973 | public function table($match, $state, $pos) { |
||
| 1025 | |||
| 1026 | // endregion modes |
||
| 1027 | } |
||
| 1028 | |||
| 1119 |
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.