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 | |||
| 34 | protected $smileys_enabled = true; |
||
| 35 | |||
| 36 | public static $instance; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @return ParserWrapper |
||
| 40 | */ |
||
| 41 | 1 | public static function getInstance() |
|
| 50 | |||
| 51 | private function __construct() |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Check if the server load is too high to execute BBC parsing |
||
| 58 | * |
||
| 59 | * @return bool If the parser can execute |
||
| 60 | */ |
||
| 61 | 1 | protected function checkLoad() |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Is BBC parsing enabled |
||
| 76 | * |
||
| 77 | * @return bool |
||
| 78 | */ |
||
| 79 | 1 | protected function isEnabled() |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @param bool $toggle |
||
| 88 | * @return $this |
||
| 89 | */ |
||
| 90 | 1 | public function enableSmileys($toggle) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Get parsers based on where it will be used |
||
| 98 | * |
||
| 99 | * @param string $area Where it is being called from |
||
| 100 | * @return array |
||
| 101 | */ |
||
| 102 | 1 | protected function getParsersByArea($area) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | public function getMessageParsers() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | public function getSignatureParser() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return array |
||
| 144 | */ |
||
| 145 | public function getNewsParser() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Parse a string based on where it's being called from |
||
| 152 | * |
||
| 153 | * @param string $area Where this is being called from |
||
| 154 | * @param string $message The message to be parsed |
||
| 155 | * @return string The Parsed message |
||
| 156 | */ |
||
| 157 | 1 | protected function parse($area, $message) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Parse the BBC and smileys in messages |
||
| 188 | * |
||
| 189 | * @param string $message |
||
| 190 | * @param bool $smileys_enabled |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | 1 | public function parseMessage($message, $smileys_enabled) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Parse the BBC and smileys in signatures |
||
| 200 | * |
||
| 201 | * @param string $signature |
||
| 202 | * @param string $smileys_enabled |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | public function parseSignature($signature, $smileys_enabled) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Parse the BBC and smileys in news items |
||
| 212 | * |
||
| 213 | * @param string $news |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | public function parseNews($news) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Parse the BBC and smileys in emails |
||
| 223 | * |
||
| 224 | * @param string $message |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | public function parseEmail($message) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Parse the BBC and smileys in custom profile fields |
||
| 234 | * @param string $field |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | public function parseCustomFields($field) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Parse the BBC and smileys in poll questions/answers |
||
| 246 | * |
||
| 247 | * @param string $field |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | public function parsePoll($field) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Parse the BBC and smileys in the registration agreement |
||
| 257 | * |
||
| 258 | * @param string $agreement |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | public function parseAgreement($agreement) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Parse the BBC and smileys in personal messages |
||
| 268 | * |
||
| 269 | * @param string $message |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | public function parsePM($message) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Parse the BBC and smileys in user submitted reports |
||
| 279 | * |
||
| 280 | * @param string $report |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | public function parseReport($report) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Parse the BBC and smileys in package descriptions |
||
| 290 | * |
||
| 291 | * @param string $report |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | public function parsePackage($string) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Parse the BBC and smileys in user verification controls |
||
| 301 | * |
||
| 302 | * @param string $question |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | public function parseVerificationControls($question) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Parse the BBC and smileys in moderator notices to users |
||
| 312 | * |
||
| 313 | * @param string $notice |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | public function parseNotice($notice) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Parse the BBC and smileys in board descriptions |
||
| 323 | * |
||
| 324 | * @param string $board |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public function parseBoard($board) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Set the disabled tags |
||
| 334 | * (usually from $modSettings['disabledBBC']) |
||
| 335 | * @param array $disabled |
||
| 336 | * @return $this |
||
| 337 | */ |
||
| 338 | public function setDisabled(array $disabled) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @return Codes |
||
| 350 | */ |
||
| 351 | 1 | public function getCodes() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * @return BBCParser |
||
| 365 | */ |
||
| 366 | 1 | public function getBBCParser() |
|
| 375 | |||
| 376 | /** |
||
| 377 | * @return Autolink |
||
| 378 | */ |
||
| 379 | 1 | public function getAutolinkParser() |
|
| 388 | |||
| 389 | /** |
||
| 390 | * @return SmileyParser |
||
| 391 | */ |
||
| 392 | 1 | public function getSmileyParser() |
|
| 405 | |||
| 406 | /** |
||
| 407 | * @return HtmlParser |
||
| 408 | */ |
||
| 409 | 1 | public function getHtmlParser() |
|
| 418 | } |
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: