Complex classes like ParserWrapper 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 ParserWrapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class ParserWrapper |
||
| 20 | { |
||
| 21 | /** @var array Disabled tags */ |
||
| 22 | protected $disabled = array(); |
||
| 23 | /** @var \BBC\Codes */ |
||
| 24 | protected $codes; |
||
| 25 | /** @var \BBC\BBCParser */ |
||
| 26 | protected $bbc_parser; |
||
| 27 | /** @var \BBC\SmileyParser */ |
||
| 28 | protected $smiley_parser; |
||
| 29 | /** @var \BBC\HtmlParser */ |
||
| 30 | protected $html_parser; |
||
| 31 | /** @var \BBC\Autolink */ |
||
| 32 | protected $autolink_parser; |
||
| 33 | /** @var bool If smileys are enabled */ |
||
| 34 | protected $smileys_enabled = true; |
||
| 35 | /** @var ParserWrapper */ |
||
| 36 | public static $instance; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Find and return ParserWrapper instance if it exists, |
||
| 40 | * or create a new instance |
||
| 41 | * |
||
| 42 | * @return ParserWrapper |
||
| 43 | */ |
||
| 44 | 1 | public static function getInstance() |
|
| 53 | |||
| 54 | /** |
||
| 55 | * ParserWrapper constructor. |
||
| 56 | */ |
||
| 57 | private function __construct() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Check if the server load is too high to execute BBC parsing |
||
| 64 | * |
||
| 65 | * @return bool If the parser can execute |
||
| 66 | */ |
||
| 67 | 1 | protected function checkLoad() |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Is BBC parsing enabled |
||
| 82 | * |
||
| 83 | * @return bool |
||
| 84 | */ |
||
| 85 | 1 | protected function isEnabled() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Enable or disable smileys |
||
| 94 | * |
||
| 95 | * @param bool $toggle |
||
| 96 | * @return $this |
||
| 97 | */ |
||
| 98 | 1 | public function enableSmileys($toggle) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Get parsers based on where it will be used |
||
| 106 | * |
||
| 107 | * @param string $area Where it is being called from |
||
| 108 | * @return array |
||
| 109 | */ |
||
| 110 | 1 | protected function getParsersByArea($area) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Return the current message parsers |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | public function getMessageParser() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Return the current signature parsers |
||
| 146 | * |
||
| 147 | * @return array |
||
| 148 | */ |
||
| 149 | public function getSignatureParser() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Return the news parsers |
||
| 156 | * |
||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | public function getNewsParser() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Parse a string based on where it's being called from |
||
| 166 | * |
||
| 167 | * @param string $area Where this is being called from |
||
| 168 | * @param string $message The message to be parsed |
||
| 169 | * @return string The Parsed message |
||
| 170 | */ |
||
| 171 | 1 | protected function parse($area, $message) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Parse the BBC and smileys in messages |
||
| 202 | * |
||
| 203 | * @param string $message |
||
| 204 | * @param bool $smileys_enabled |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | 1 | public function parseMessage($message, $smileys_enabled) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Parse the BBC and smileys in signatures |
||
| 214 | * |
||
| 215 | * @param string $signature |
||
| 216 | * @param string $smileys_enabled |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | public function parseSignature($signature, $smileys_enabled) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Parse the BBC and smileys in news items |
||
| 226 | * |
||
| 227 | * @param string $news |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | public function parseNews($news) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Parse the BBC and smileys in emails |
||
| 237 | * |
||
| 238 | * @param string $email |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | public function parseEmail($email) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Parse the BBC and smileys in custom profile fields |
||
| 248 | * |
||
| 249 | * @param string $field |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | public function parseCustomFields($field) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Parse the BBC and smileys in poll questions/answers |
||
| 261 | * |
||
| 262 | * @param string $poll |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public function parsePoll($poll) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Parse the BBC and smileys in the registration agreement |
||
| 272 | * |
||
| 273 | * @param string $agreement |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function parseAgreement($agreement) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Parse the BBC and smileys in personal messages |
||
| 283 | * |
||
| 284 | * @param string $pm |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function parsePM($pm) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Parse the BBC and smileys in user submitted reports |
||
| 294 | * |
||
| 295 | * @param string $report |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | public function parseReport($report) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Parse the BBC and smileys in package descriptions |
||
| 305 | * |
||
| 306 | * @param string $package |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | public function parsePackage($package) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Parse the BBC and smileys in user verification controls |
||
| 316 | * |
||
| 317 | * @param string $question |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | public function parseVerificationControls($question) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Parse the BBC and smileys in moderator notices to users |
||
| 327 | * |
||
| 328 | * @param string $notice |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | public function parseNotice($notice) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Parse the BBC and smileys in board descriptions |
||
| 338 | * |
||
| 339 | * @param string $board |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public function parseBoard($board) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Set the disabled tags |
||
| 349 | * |
||
| 350 | * @param string[] $disabled (usually from $modSettings['disabledBBC']) |
||
| 351 | * @return $this |
||
| 352 | */ |
||
| 353 | public function setDisabled(array $disabled) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Return the bbc code definitions for the parser |
||
| 365 | * |
||
| 366 | * @return Codes |
||
| 367 | */ |
||
| 368 | 1 | public function getCodes() |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Return an instance of the bbc parser |
||
| 382 | * |
||
| 383 | * @return BBCParser |
||
| 384 | */ |
||
| 385 | 1 | public function getBBCParser() |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Return an, that's right not and, just an, like a single instance of the autolink parser |
||
| 397 | * |
||
| 398 | * @return Autolink |
||
| 399 | */ |
||
| 400 | 1 | public function getAutolinkParser() |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Return an, that's right not and, just an, like a single instance of the Smiley parser |
||
| 412 | * |
||
| 413 | * @return SmileyParser |
||
| 414 | */ |
||
| 415 | 1 | public function getSmileyParser() |
|
| 416 | { |
||
| 417 | 1 | global $context; |
|
| 418 | |||
| 419 | 1 | if ($this->smiley_parser === null) |
|
| 420 | 1 | { |
|
| 421 | 1 | $this->smiley_parser = new \BBC\SmileyParser($context['user']['smiley_path']); |
|
| 422 | 1 | $this->smiley_parser->setEnabled($context['smiley_enabled']); |
|
| 423 | 1 | } |
|
| 424 | |||
| 425 | 1 | return $this->smiley_parser; |
|
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Return an, that's right not and, just an, like a single instance of the HTML parser |
||
| 430 | * |
||
| 431 | * @return HtmlParser |
||
| 432 | */ |
||
| 433 | 1 | public function getHtmlParser() |
|
| 442 | } |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: