Complex classes like ParserHook 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 ParserHook, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | abstract class ParserHook { |
||
| 19 | |||
| 20 | const TYPE_TAG = 0; |
||
| 21 | const TYPE_FUNCTION = 1; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @since 0.4.3 |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected static $registeredHooks = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Returns an array of registered parser hooks (keys) and their handling |
||
| 32 | * ParserHook deriving class names (values). |
||
| 33 | * |
||
| 34 | * @since 0.4.3 |
||
| 35 | * |
||
| 36 | * @return array |
||
| 37 | */ |
||
| 38 | public static function getRegisteredParserHooks() { |
||
| 39 | return self::$registeredHooks; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Returns the name of the ParserHook deriving class that defines a certain parser hook, |
||
| 44 | * or false if there is none. |
||
| 45 | * |
||
| 46 | * @since 0.4.3 |
||
| 47 | * |
||
| 48 | * @param string $hookName |
||
| 49 | * |
||
| 50 | * @return mixed string or false |
||
| 51 | */ |
||
| 52 | public static function getHookClassName( $hookName ) { |
||
| 53 | return array_key_exists( $hookName, self::$registeredHooks ) ? self::$registeredHooks[$hookName] : false; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @since 0.4 |
||
| 58 | * |
||
| 59 | * @var Processor |
||
| 60 | */ |
||
| 61 | protected $validator; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @since 0.4 |
||
| 65 | * |
||
| 66 | * @var Parser |
||
| 67 | */ |
||
| 68 | protected $parser; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @since 0.4.4 |
||
| 72 | * |
||
| 73 | * @var PPFrame |
||
| 74 | */ |
||
| 75 | protected $frame; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @since 0.4.4 |
||
| 79 | * |
||
| 80 | * @var ParserHook::TYPE_ enum item |
||
| 81 | */ |
||
| 82 | protected $currentType; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @since 0.4 |
||
| 86 | * |
||
| 87 | * @var boolean |
||
| 88 | */ |
||
| 89 | public $forTagExtensions; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @since 0.4 |
||
| 93 | * |
||
| 94 | * @var boolean |
||
| 95 | */ |
||
| 96 | public $forParserFunctions; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Bitfifeld of Options influencing the characteristics of the registered |
||
| 100 | * tag/parser function. |
||
| 101 | * |
||
| 102 | * @since 0.4.13 |
||
| 103 | * |
||
| 104 | * @var int |
||
| 105 | */ |
||
| 106 | protected $parserHookOptions; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Gets the name of the parser hook. |
||
| 110 | * |
||
| 111 | * @since 0.4 |
||
| 112 | * |
||
| 113 | * @return string or array of string |
||
| 114 | */ |
||
| 115 | protected abstract function getName(); |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Renders and returns the output. |
||
| 119 | * |
||
| 120 | * @since 0.4 |
||
| 121 | * |
||
| 122 | * @param array $parameters |
||
| 123 | * |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | protected abstract function render( array $parameters ); |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Flag for constructor, whether the function hook should be one callable without |
||
| 130 | * leading hash, i.e. {{plural:...}} instead of {{#if:...}} |
||
| 131 | * |
||
| 132 | * @since 0.4.13 |
||
| 133 | */ |
||
| 134 | const FH_NO_HASH = 1; |
||
| 135 | |||
| 136 | /* * |
||
| 137 | * @ToDo: implementation of this functionality |
||
| 138 | * |
||
| 139 | * Flag for constructor, whether the tag hook should be handled as function tag hook |
||
| 140 | * and not as a normal tag hook. See Parser::setFunctionTagHook() for details. |
||
| 141 | */ |
||
| 142 | #const TH_AS_FUNCTION_TAG = 2; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Constructor. |
||
| 146 | * |
||
| 147 | * @since 0.4 |
||
| 148 | * |
||
| 149 | * @param boolean $forTagExtensions |
||
| 150 | * @param boolean $forParserFunctions |
||
| 151 | * @param integer $flag combination of option flags to manipulare the parser hooks |
||
|
|
|||
| 152 | * characteristics. The following are available: |
||
| 153 | * - ParserHook::FH_NO_HASH makes the function callable without leading hash. |
||
| 154 | */ |
||
| 155 | public function __construct( $forTagExtensions = true, $forParserFunctions = true, $flags = 0 ) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Function to hook up the coordinate rendering functions to the parser. |
||
| 164 | * |
||
| 165 | * @since 0.4 |
||
| 166 | * |
||
| 167 | * @param Parser $parser |
||
| 168 | * |
||
| 169 | * @return true |
||
| 170 | */ |
||
| 171 | public function init( Parser &$parser ) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Returns an array with the names for the parser hook. |
||
| 219 | * |
||
| 220 | * @since 0.4 |
||
| 221 | * |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | protected function getNames() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Function to add the magic word in pre MW 1.16. |
||
| 236 | * |
||
| 237 | * @since 0.4 |
||
| 238 | * |
||
| 239 | * @param array $magicWords |
||
| 240 | * @param string $langCode |
||
| 241 | * |
||
| 242 | * @return boolean |
||
| 243 | */ |
||
| 244 | public function magic( array &$magicWords, $langCode ) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Handler for rendering the tag hook registered by Parser::setHook() |
||
| 254 | * |
||
| 255 | * @since 0.4 |
||
| 256 | * |
||
| 257 | * @param mixed $input string or null |
||
| 258 | * @param array $args |
||
| 259 | * @param Parser $parser |
||
| 260 | * @param PPFrame $frame Available from 1.16 |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | public function renderTag( $input, array $args, Parser $parser, PPFrame $frame = null ) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Handler for rendering the function hook registered by Parser::setFunctionHook() |
||
| 281 | * |
||
| 282 | * @since 0.4 |
||
| 283 | * |
||
| 284 | * @param Parser &$parser |
||
| 285 | * ... further arguments ... |
||
| 286 | * |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | public function renderFunction( Parser &$parser /*, n args */ ) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Handler for rendering the function hook registered by Parser::setFunctionHook() together |
||
| 313 | * with object style arguments (SFH_OBJECT_ARGS flag). |
||
| 314 | * |
||
| 315 | * @since 0.4.13 |
||
| 316 | * |
||
| 317 | * @param Parser &$parser |
||
| 318 | * @param PPFrame $frame |
||
| 319 | * @param type $args |
||
| 320 | * @return array |
||
| 321 | */ |
||
| 322 | public function renderFunctionObj( Parser &$parser, PPFrame $frame, $args ) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Returns the parser function otpions. |
||
| 341 | * |
||
| 342 | * @since 0.4 |
||
| 343 | * |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | protected function getFunctionOptions() { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Takes care of validation and rendering, and returns the output. |
||
| 352 | * |
||
| 353 | * @since 0.4 |
||
| 354 | * |
||
| 355 | * @param array $arguments |
||
| 356 | * @param integer $type Item of the ParserHook::TYPE_ enum |
||
| 357 | * |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | public function validateAndRender( array $arguments, $type ) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Returns the ProcessingError objects for the errors and warnings that should be displayed. |
||
| 388 | * |
||
| 389 | * @since 0.4 |
||
| 390 | * |
||
| 391 | * @return array of array of ProcessingError |
||
| 392 | */ |
||
| 393 | protected function getErrorsToDisplay() { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Creates and returns the output when a fatal error prevent regular rendering. |
||
| 412 | * |
||
| 413 | * @since 0.4 |
||
| 414 | * |
||
| 415 | * @param ProcessingError $error |
||
| 416 | * |
||
| 417 | * @return string |
||
| 418 | */ |
||
| 419 | protected function renderFatalError( ProcessingError $error ) { |
||
| 424 | |||
| 425 | // TODO: replace render errors functionality |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Returns an array containing the parameter info. |
||
| 429 | * Override in deriving classes to add parameter info. |
||
| 430 | * |
||
| 431 | * @since 0.4 |
||
| 432 | * |
||
| 433 | * @param integer $type Item of the ParserHook::TYPE_ enum |
||
| 434 | * |
||
| 435 | * @return array |
||
| 436 | */ |
||
| 437 | protected function getParameterInfo( $type ) { |
||
| 440 | |||
| 441 | public function getParamDefinitions( $type = self::TYPE_FUNCTION ) { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Returns the list of default parameters. These parameters can be used as |
||
| 447 | * unnamed parameters where it is not necessary to use the name and the '=' as |
||
| 448 | * long as there is no '=' within the value. |
||
| 449 | * It is possible to define that a parameter should not have a named fallback. |
||
| 450 | * Therefore the information has to be returnd as sub-array with the parameter |
||
| 451 | * name as first and Validator::PARAM_UNNAMED as second value. Parameter using |
||
| 452 | * this option must be set first, before any unnamed parameter in the same order |
||
| 453 | * as set here. All parameters defined before the last parameter making use of |
||
| 454 | * Validator::PARAM_UNNAMED will automatically be populated with this option. |
||
| 455 | * |
||
| 456 | * Override in deriving classes to add default parameters. |
||
| 457 | * |
||
| 458 | * @since 0.4 |
||
| 459 | * |
||
| 460 | * @param integer $type Item of the ParserHook::TYPE_ enum |
||
| 461 | * |
||
| 462 | * @return array |
||
| 463 | */ |
||
| 464 | protected function getDefaultParameters( $type ) { |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Returns the data needed to describe the parser hook. |
||
| 470 | * This is mainly needed because some of the individual get methods |
||
| 471 | * that return the needed data are protected, and cannot be made |
||
| 472 | * public without breaking b/c in a rather bad way. |
||
| 473 | * |
||
| 474 | * @since 0.4.3 |
||
| 475 | * |
||
| 476 | * @param integer $type Item of the ParserHook::TYPE_ enum |
||
| 477 | * |
||
| 478 | * @return array |
||
| 479 | */ |
||
| 480 | public function getDescriptionData( $type ) { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Returns a description for the parser hook, or false when there is none. |
||
| 492 | * Override in deriving classes to add a message. |
||
| 493 | * |
||
| 494 | * @since 0.4.3 |
||
| 495 | * @deprecated since 1.0 |
||
| 496 | * |
||
| 497 | * @return mixed string or false |
||
| 498 | */ |
||
| 499 | public function getDescription() { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Returns a description message for the parser hook, or false when there is none. |
||
| 506 | * Override in deriving classes to add a message. |
||
| 507 | * |
||
| 508 | * @since 0.4.10 |
||
| 509 | * |
||
| 510 | * @return mixed string or false |
||
| 511 | */ |
||
| 512 | public function getMessage() { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Returns if the current render request is coming from a tag extension. |
||
| 518 | * |
||
| 519 | * @since 0.4.4 |
||
| 520 | * |
||
| 521 | * @return boolean |
||
| 522 | */ |
||
| 523 | protected function isTag() { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Returns if the current render request is coming from a parser function. |
||
| 529 | * |
||
| 530 | * @since 0.4.4 |
||
| 531 | * |
||
| 532 | * @return boolean |
||
| 533 | */ |
||
| 534 | protected function isFunction() { |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Utility function to parse wikitext without having to care |
||
| 540 | * about handling a tag extension or parser function. |
||
| 541 | * |
||
| 542 | * @since 0.4.4 |
||
| 543 | * |
||
| 544 | * @param string $text The wikitext to be parsed |
||
| 545 | * |
||
| 546 | * @return string the parsed output |
||
| 547 | */ |
||
| 548 | protected function parseWikitext( $text ) { |
||
| 566 | |||
| 567 | } |
||
| 568 | |||
| 614 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.