Complex classes like Parser 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 Parser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Parser extends ParserBase |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var bool Whether current text contains escape characters |
||
| 23 | */ |
||
| 24 | protected $hasEscapedChars; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var bool Whether current text contains references |
||
| 28 | */ |
||
| 29 | protected $hasRefs; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array Array of [label => link info] |
||
| 33 | */ |
||
| 34 | protected $refs; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string Text being parsed |
||
| 38 | */ |
||
| 39 | protected $text; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * {@inheritdoc} |
||
| 43 | */ |
||
| 44 | 263 | public function parse($text, array $matches) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Add an image tag for given text span |
||
| 71 | * |
||
| 72 | * @param integer $startTagPos Start tag position |
||
| 73 | * @param integer $endTagPos End tag position |
||
| 74 | * @param integer $endTagLen End tag length |
||
| 75 | * @param string $linkInfo URL optionally followed by space and a title |
||
| 76 | * @param string $alt Value for the alt attribute |
||
| 77 | * @return void |
||
| 78 | */ |
||
| 79 | 24 | protected function addImageTag($startTagPos, $endTagPos, $endTagLen, $linkInfo, $alt) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Add an image tag for given text span |
||
| 91 | * |
||
| 92 | * @param integer $startTagPos Start tag position |
||
| 93 | * @param integer $endTagPos End tag position |
||
| 94 | * @param integer $endTagLen End tag length |
||
| 95 | * @param string $linkInfo URL optionally followed by space and a title |
||
| 96 | * @return void |
||
| 97 | */ |
||
| 98 | 57 | protected function addLinkTag($startTagPos, $endTagPos, $endTagLen, $linkInfo) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Decode a chunk of encoded text to be used as an attribute value |
||
| 115 | * |
||
| 116 | * Decodes escaped literals and removes slashes and 0x1A characters |
||
| 117 | * |
||
| 118 | * @param string $str Encoded text |
||
| 119 | * @return string Decoded text |
||
| 120 | */ |
||
| 121 | 69 | protected function decode($str) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Encode escaped literals that have a special meaning |
||
| 147 | * |
||
| 148 | * @param string $str Original text |
||
| 149 | * @return string Encoded text |
||
| 150 | */ |
||
| 151 | 15 | protected function encode($str) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Capture and return labels used in current text |
||
| 166 | * |
||
| 167 | * @return array Labels' text position as keys, lowercased text content as values |
||
| 168 | */ |
||
| 169 | 26 | protected function getLabels() |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Initialize this parser with given text |
||
| 188 | * |
||
| 189 | * @param string $text Text to be parsed |
||
| 190 | * @return void |
||
| 191 | */ |
||
| 192 | 263 | protected function init($text) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Match images markup |
||
| 216 | * |
||
| 217 | * @return void |
||
| 218 | */ |
||
| 219 | 263 | protected function matchImages() |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Match inline images markup |
||
| 238 | * |
||
| 239 | * @return void |
||
| 240 | */ |
||
| 241 | 13 | protected function matchInlineImages() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Match reference images markup |
||
| 263 | * |
||
| 264 | * @return void |
||
| 265 | */ |
||
| 266 | 11 | protected function matchReferenceImages() |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Match inline links markup |
||
| 298 | * |
||
| 299 | * @return void |
||
| 300 | */ |
||
| 301 | 32 | protected function matchInlineLinks() |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Capture link reference definitions in current text |
||
| 322 | * |
||
| 323 | * @return void |
||
| 324 | */ |
||
| 325 | 263 | protected function matchLinkReferences() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Match inline and reference links |
||
| 354 | * |
||
| 355 | * @return void |
||
| 356 | */ |
||
| 357 | 263 | protected function matchLinks() |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Match reference links markup |
||
| 371 | * |
||
| 372 | * @return void |
||
| 373 | */ |
||
| 374 | 26 | protected function matchReferenceLinks() |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Overwrite part of the text with substitution characters ^Z (0x1A) |
||
| 401 | * |
||
| 402 | * @param integer $pos Start of the range |
||
| 403 | * @param integer $len Length of text to overwrite |
||
| 404 | * @return void |
||
| 405 | */ |
||
| 406 | 69 | protected function overwrite($pos, $len) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Set a URL or IMG tag's attributes |
||
| 416 | * |
||
| 417 | * @param Tag $tag URL or IMG tag |
||
| 418 | * @param string $linkInfo Link's info: an URL optionally followed by spaces and a title |
||
| 419 | * @param string $attrName Name of the URL attribute |
||
| 420 | * @return void |
||
| 421 | */ |
||
| 422 | 69 | protected function setLinkAttributes(Tag $tag, $linkInfo, $attrName) |
|
| 439 | } |