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 |
||
| 15 | class Doku_Handler { |
||
| 16 | /** @var CallWriterInterface */ |
||
| 17 | protected $callWriter = null; |
||
| 18 | |||
| 19 | /** @var array The current CallWriter will write directly to this list of calls, Parser reads it */ |
||
| 20 | public $calls = array(); |
||
| 21 | |||
| 22 | /** @var array internal status holders for some modes */ |
||
| 23 | protected $status = array( |
||
| 24 | 'section' => false, |
||
| 25 | 'doublequote' => 0, |
||
| 26 | ); |
||
| 27 | |||
| 28 | /** @var bool should blocks be rewritten? FIXME seems to always be true */ |
||
| 29 | protected $rewriteBlocks = true; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Doku_Handler constructor. |
||
| 33 | */ |
||
| 34 | public function __construct() { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Add a new call by passing it to the current CallWriter |
||
| 40 | * |
||
| 41 | * @param string $handler handler method name (see mode handlers below) |
||
| 42 | * @param mixed $args arguments for this call |
||
| 43 | * @param int $pos byte position in the original source file |
||
| 44 | */ |
||
| 45 | protected function addCall($handler, $args, $pos) { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Similar to addCall, but adds a plugin call |
||
| 52 | * |
||
| 53 | * @param string $plugin name of the plugin |
||
| 54 | * @param mixed $args arguments for this call |
||
| 55 | * @param int $state a LEXER_STATE_* constant |
||
| 56 | * @param int $pos byte position in the original source file |
||
| 57 | * @param string $match matched syntax |
||
| 58 | */ |
||
| 59 | protected function addPluginCall($plugin, $args, $state, $pos, $match) { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Finishes handling |
||
| 66 | * |
||
| 67 | * Called from the parser. Calls finalise() on the call writer, closes open |
||
| 68 | * sections, rewrites blocks and adds document_start and document_end calls. |
||
| 69 | * |
||
| 70 | * @triggers PARSER_HANDLER_DONE |
||
| 71 | */ |
||
| 72 | public function finalize(){ |
||
| 91 | |||
| 92 | /** |
||
| 93 | * fetch the current call and advance the pointer to the next one |
||
| 94 | * |
||
| 95 | * @fixme seems to be unused? |
||
| 96 | * @return bool|mixed |
||
| 97 | */ |
||
| 98 | public function fetch() { |
||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * Internal function for parsing highlight options. |
||
| 110 | * $options is parsed for key value pairs separated by commas. |
||
| 111 | * A value might also be missing in which case the value will simple |
||
| 112 | * be set to true. Commas in strings are ignored, e.g. option="4,56" |
||
| 113 | * will work as expected and will only create one entry. |
||
| 114 | * |
||
| 115 | * @param string $options space separated list of key-value pairs, |
||
| 116 | * e.g. option1=123, option2="456" |
||
| 117 | * @return array|null Array of key-value pairs $array['key'] = 'value'; |
||
| 118 | * or null if no entries found |
||
| 119 | */ |
||
| 120 | protected function parse_highlight_options($options) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Simplifies handling for the formatting tags which all behave the same |
||
| 181 | * |
||
| 182 | * @param string $match matched syntax |
||
| 183 | * @param int $state a LEXER_STATE_* constant |
||
| 184 | * @param int $pos byte position in the original source file |
||
| 185 | * @param string $name actual mode name |
||
| 186 | */ |
||
| 187 | protected function nestingTag($match, $state, $pos, $name) { |
||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * The following methods define the handlers for the different Syntax modes |
||
| 204 | * |
||
| 205 | * The handlers are called from dokuwiki\Parsing\Lexer\Lexer\invokeParser() |
||
| 206 | * |
||
| 207 | * @todo it might make sense to move these into their own class or merge them with the |
||
| 208 | * ParserMode classes some time. |
||
| 209 | */ |
||
| 210 | // region mode handlers |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Special plugin handler |
||
| 214 | * |
||
| 215 | * This handler is called for all modes starting with 'plugin_'. |
||
| 216 | * An additional parameter with the plugin name is passed. The plugin's handle() |
||
| 217 | * method is called here |
||
| 218 | * |
||
| 219 | * @author Andreas Gohr <[email protected]> |
||
| 220 | * |
||
| 221 | * @param string $match matched syntax |
||
| 222 | * @param int $state a LEXER_STATE_* constant |
||
| 223 | * @param int $pos byte position in the original source file |
||
| 224 | * @param string $pluginname name of the plugin |
||
| 225 | * @return bool mode handled? |
||
| 226 | */ |
||
| 227 | public function plugin($match, $state, $pos, $pluginname){ |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $match matched syntax |
||
| 242 | * @param int $state a LEXER_STATE_* constant |
||
| 243 | * @param int $pos byte position in the original source file |
||
| 244 | * @return bool mode handled? |
||
| 245 | */ |
||
| 246 | public function base($match, $state, $pos) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param string $match matched syntax |
||
| 258 | * @param int $state a LEXER_STATE_* constant |
||
| 259 | * @param int $pos byte position in the original source file |
||
| 260 | * @return bool mode handled? |
||
| 261 | */ |
||
| 262 | public function header($match, $state, $pos) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $match matched syntax |
||
| 281 | * @param int $state a LEXER_STATE_* constant |
||
| 282 | * @param int $pos byte position in the original source file |
||
| 283 | * @return bool mode handled? |
||
| 284 | */ |
||
| 285 | public function notoc($match, $state, $pos) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $match matched syntax |
||
| 292 | * @param int $state a LEXER_STATE_* constant |
||
| 293 | * @param int $pos byte position in the original source file |
||
| 294 | * @return bool mode handled? |
||
| 295 | */ |
||
| 296 | public function nocache($match, $state, $pos) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param string $match matched syntax |
||
| 303 | * @param int $state a LEXER_STATE_* constant |
||
| 304 | * @param int $pos byte position in the original source file |
||
| 305 | * @return bool mode handled? |
||
| 306 | */ |
||
| 307 | public function linebreak($match, $state, $pos) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param string $match matched syntax |
||
| 314 | * @param int $state a LEXER_STATE_* constant |
||
| 315 | * @param int $pos byte position in the original source file |
||
| 316 | * @return bool mode handled? |
||
| 317 | */ |
||
| 318 | public function eol($match, $state, $pos) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param string $match matched syntax |
||
| 325 | * @param int $state a LEXER_STATE_* constant |
||
| 326 | * @param int $pos byte position in the original source file |
||
| 327 | * @return bool mode handled? |
||
| 328 | */ |
||
| 329 | public function hr($match, $state, $pos) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param string $match matched syntax |
||
| 336 | * @param int $state a LEXER_STATE_* constant |
||
| 337 | * @param int $pos byte position in the original source file |
||
| 338 | * @return bool mode handled? |
||
| 339 | */ |
||
| 340 | public function strong($match, $state, $pos) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param string $match matched syntax |
||
| 347 | * @param int $state a LEXER_STATE_* constant |
||
| 348 | * @param int $pos byte position in the original source file |
||
| 349 | * @return bool mode handled? |
||
| 350 | */ |
||
| 351 | public function emphasis($match, $state, $pos) { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param string $match matched syntax |
||
| 358 | * @param int $state a LEXER_STATE_* constant |
||
| 359 | * @param int $pos byte position in the original source file |
||
| 360 | * @return bool mode handled? |
||
| 361 | */ |
||
| 362 | public function underline($match, $state, $pos) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param string $match matched syntax |
||
| 369 | * @param int $state a LEXER_STATE_* constant |
||
| 370 | * @param int $pos byte position in the original source file |
||
| 371 | * @return bool mode handled? |
||
| 372 | */ |
||
| 373 | public function monospace($match, $state, $pos) { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @param string $match matched syntax |
||
| 380 | * @param int $state a LEXER_STATE_* constant |
||
| 381 | * @param int $pos byte position in the original source file |
||
| 382 | * @return bool mode handled? |
||
| 383 | */ |
||
| 384 | public function subscript($match, $state, $pos) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param string $match matched syntax |
||
| 391 | * @param int $state a LEXER_STATE_* constant |
||
| 392 | * @param int $pos byte position in the original source file |
||
| 393 | * @return bool mode handled? |
||
| 394 | */ |
||
| 395 | public function superscript($match, $state, $pos) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param string $match matched syntax |
||
| 402 | * @param int $state a LEXER_STATE_* constant |
||
| 403 | * @param int $pos byte position in the original source file |
||
| 404 | * @return bool mode handled? |
||
| 405 | */ |
||
| 406 | public function deleted($match, $state, $pos) { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param string $match matched syntax |
||
| 413 | * @param int $state a LEXER_STATE_* constant |
||
| 414 | * @param int $pos byte position in the original source file |
||
| 415 | * @return bool mode handled? |
||
| 416 | */ |
||
| 417 | public function footnote($match, $state, $pos) { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @param string $match matched syntax |
||
| 456 | * @param int $state a LEXER_STATE_* constant |
||
| 457 | * @param int $pos byte position in the original source file |
||
| 458 | * @return bool mode handled? |
||
| 459 | */ |
||
| 460 | public function listblock($match, $state, $pos) { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @param string $match matched syntax |
||
| 484 | * @param int $state a LEXER_STATE_* constant |
||
| 485 | * @param int $pos byte position in the original source file |
||
| 486 | * @return bool mode handled? |
||
| 487 | */ |
||
| 488 | public function unformatted($match, $state, $pos) { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * @param string $match matched syntax |
||
| 497 | * @param int $state a LEXER_STATE_* constant |
||
| 498 | * @param int $pos byte position in the original source file |
||
| 499 | * @return bool mode handled? |
||
| 500 | */ |
||
| 501 | public function php($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 phpblock($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 html($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 htmlblock($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 preformatted($match, $state, $pos) { |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @param string $match matched syntax |
||
| 578 | * @param int $state a LEXER_STATE_* constant |
||
| 579 | * @param int $pos byte position in the original source file |
||
| 580 | * @return bool mode handled? |
||
| 581 | */ |
||
| 582 | public function quote($match, $state, $pos) { |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @param string $match matched syntax |
||
| 613 | * @param int $state a LEXER_STATE_* constant |
||
| 614 | * @param int $pos byte position in the original source file |
||
| 615 | * @return bool mode handled? |
||
| 616 | */ |
||
| 617 | public function file($match, $state, $pos) { |
||
| 620 | |||
| 621 | /** |
||
| 622 | * @param string $match matched syntax |
||
| 623 | * @param int $state a LEXER_STATE_* constant |
||
| 624 | * @param int $pos byte position in the original source file |
||
| 625 | * @param string $type either 'code' or 'file' |
||
| 626 | * @return bool mode handled? |
||
| 627 | */ |
||
| 628 | public function code($match, $state, $pos, $type='code') { |
||
| 649 | |||
| 650 | /** |
||
| 651 | * @param string $match matched syntax |
||
| 652 | * @param int $state a LEXER_STATE_* constant |
||
| 653 | * @param int $pos byte position in the original source file |
||
| 654 | * @return bool mode handled? |
||
| 655 | */ |
||
| 656 | public function acronym($match, $state, $pos) { |
||
| 660 | |||
| 661 | /** |
||
| 662 | * @param string $match matched syntax |
||
| 663 | * @param int $state a LEXER_STATE_* constant |
||
| 664 | * @param int $pos byte position in the original source file |
||
| 665 | * @return bool mode handled? |
||
| 666 | */ |
||
| 667 | public function smiley($match, $state, $pos) { |
||
| 671 | |||
| 672 | /** |
||
| 673 | * @param string $match matched syntax |
||
| 674 | * @param int $state a LEXER_STATE_* constant |
||
| 675 | * @param int $pos byte position in the original source file |
||
| 676 | * @return bool mode handled? |
||
| 677 | */ |
||
| 678 | public function wordblock($match, $state, $pos) { |
||
| 682 | |||
| 683 | /** |
||
| 684 | * @param string $match matched syntax |
||
| 685 | * @param int $state a LEXER_STATE_* constant |
||
| 686 | * @param int $pos byte position in the original source file |
||
| 687 | * @return bool mode handled? |
||
| 688 | */ |
||
| 689 | public function entity($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 | * @return bool mode handled? |
||
| 699 | */ |
||
| 700 | public function multiplyentity($match, $state, $pos) { |
||
| 705 | |||
| 706 | /** |
||
| 707 | * @param string $match matched syntax |
||
| 708 | * @param int $state a LEXER_STATE_* constant |
||
| 709 | * @param int $pos byte position in the original source file |
||
| 710 | * @return bool mode handled? |
||
| 711 | */ |
||
| 712 | public function singlequoteopening($match, $state, $pos) { |
||
| 716 | |||
| 717 | /** |
||
| 718 | * @param string $match matched syntax |
||
| 719 | * @param int $state a LEXER_STATE_* constant |
||
| 720 | * @param int $pos byte position in the original source file |
||
| 721 | * @return bool mode handled? |
||
| 722 | */ |
||
| 723 | public function singlequoteclosing($match, $state, $pos) { |
||
| 727 | |||
| 728 | /** |
||
| 729 | * @param string $match matched syntax |
||
| 730 | * @param int $state a LEXER_STATE_* constant |
||
| 731 | * @param int $pos byte position in the original source file |
||
| 732 | * @return bool mode handled? |
||
| 733 | */ |
||
| 734 | public function apostrophe($match, $state, $pos) { |
||
| 738 | |||
| 739 | /** |
||
| 740 | * @param string $match matched syntax |
||
| 741 | * @param int $state a LEXER_STATE_* constant |
||
| 742 | * @param int $pos byte position in the original source file |
||
| 743 | * @return bool mode handled? |
||
| 744 | */ |
||
| 745 | public function doublequoteopening($match, $state, $pos) { |
||
| 750 | |||
| 751 | /** |
||
| 752 | * @param string $match matched syntax |
||
| 753 | * @param int $state a LEXER_STATE_* constant |
||
| 754 | * @param int $pos byte position in the original source file |
||
| 755 | * @return bool mode handled? |
||
| 756 | */ |
||
| 757 | public function doublequoteclosing($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 camelcaselink($match, $state, $pos) { |
||
| 777 | |||
| 778 | /** |
||
| 779 | * @param string $match matched syntax |
||
| 780 | * @param int $state a LEXER_STATE_* constant |
||
| 781 | * @param int $pos byte position in the original source file |
||
| 782 | * @return bool mode handled? |
||
| 783 | */ |
||
| 784 | public function internallink($match, $state, $pos) { |
||
| 847 | |||
| 848 | /** |
||
| 849 | * @param string $match matched syntax |
||
| 850 | * @param int $state a LEXER_STATE_* constant |
||
| 851 | * @param int $pos byte position in the original source file |
||
| 852 | * @return bool mode handled? |
||
| 853 | */ |
||
| 854 | public function filelink($match, $state, $pos) { |
||
| 858 | |||
| 859 | /** |
||
| 860 | * @param string $match matched syntax |
||
| 861 | * @param int $state a LEXER_STATE_* constant |
||
| 862 | * @param int $pos byte position in the original source file |
||
| 863 | * @return bool mode handled? |
||
| 864 | */ |
||
| 865 | public function windowssharelink($match, $state, $pos) { |
||
| 869 | |||
| 870 | /** |
||
| 871 | * @param string $match matched syntax |
||
| 872 | * @param int $state a LEXER_STATE_* constant |
||
| 873 | * @param int $pos byte position in the original source file |
||
| 874 | * @return bool mode handled? |
||
| 875 | */ |
||
| 876 | public function media($match, $state, $pos) { |
||
| 887 | |||
| 888 | /** |
||
| 889 | * @param string $match matched syntax |
||
| 890 | * @param int $state a LEXER_STATE_* constant |
||
| 891 | * @param int $pos byte position in the original source file |
||
| 892 | * @return bool mode handled? |
||
| 893 | */ |
||
| 894 | public function rss($match, $state, $pos) { |
||
| 922 | |||
| 923 | /** |
||
| 924 | * @param string $match matched syntax |
||
| 925 | * @param int $state a LEXER_STATE_* constant |
||
| 926 | * @param int $pos byte position in the original source file |
||
| 927 | * @return bool mode handled? |
||
| 928 | */ |
||
| 929 | public function externallink($match, $state, $pos) { |
||
| 946 | |||
| 947 | /** |
||
| 948 | * @param string $match matched syntax |
||
| 949 | * @param int $state a LEXER_STATE_* constant |
||
| 950 | * @param int $pos byte position in the original source file |
||
| 951 | * @return bool mode handled? |
||
| 952 | */ |
||
| 953 | public function emaillink($match, $state, $pos) { |
||
| 958 | |||
| 959 | /** |
||
| 960 | * @param string $match matched syntax |
||
| 961 | * @param int $state a LEXER_STATE_* constant |
||
| 962 | * @param int $pos byte position in the original source file |
||
| 963 | * @return bool mode handled? |
||
| 964 | */ |
||
| 965 | public function table($match, $state, $pos) { |
||
| 1017 | |||
| 1018 | // endregion modes |
||
| 1019 | } |
||
| 1020 | |||
| 1111 |
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.