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 |
||
| 14 | class Parser extends ParserBase |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var array Attributes of the BBCode being parsed |
||
| 18 | */ |
||
| 19 | protected $attributes; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var array Configuration for the BBCode being parsed |
||
| 23 | */ |
||
| 24 | protected $bbcodeConfig; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string Name of the BBCode being parsed |
||
| 28 | */ |
||
| 29 | protected $bbcodeName; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string Suffix of the BBCode being parsed, including its colon |
||
| 33 | */ |
||
| 34 | protected $bbcodeSuffix; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var integer Position of the cursor in the original text |
||
| 38 | */ |
||
| 39 | protected $pos; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var integer Position of the start of the BBCode being parsed |
||
| 43 | */ |
||
| 44 | protected $startPos; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string Text being parsed |
||
| 48 | */ |
||
| 49 | protected $text; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var integer Length of the text being parsed |
||
| 53 | */ |
||
| 54 | protected $textLen; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string Text being parsed, normalized to uppercase |
||
| 58 | */ |
||
| 59 | protected $uppercaseText; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritdoc} |
||
| 63 | */ |
||
| 64 | 50 | public function parse($text, array $matches) |
|
| 65 | { |
||
| 66 | 50 | $this->text = $text; |
|
| 67 | 50 | $this->textLen = strlen($text); |
|
| 68 | 50 | $this->uppercaseText = ''; |
|
| 69 | 50 | foreach ($matches as $m) |
|
| 70 | { |
||
| 71 | 50 | $this->bbcodeName = strtoupper($m[1][0]); |
|
| 72 | 50 | if (!isset($this->config['bbcodes'][$this->bbcodeName])) |
|
| 73 | { |
||
| 74 | 3 | continue; |
|
| 75 | } |
||
| 76 | 50 | $this->bbcodeConfig = $this->config['bbcodes'][$this->bbcodeName]; |
|
| 77 | 50 | $this->startPos = $m[0][1]; |
|
| 78 | 50 | $this->pos = $this->startPos + strlen($m[0][0]); |
|
| 79 | |||
| 80 | try |
||
| 81 | { |
||
| 82 | 50 | $this->parseBBCode(); |
|
| 83 | } |
||
| 84 | 7 | catch (RuntimeException $e) |
|
| 85 | 50 | { |
|
| 86 | // Do nothing |
||
| 87 | } |
||
| 88 | } |
||
| 89 | 50 | } |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Add the end tag that matches current BBCode |
||
| 93 | * |
||
| 94 | * @return Tag |
||
| 95 | */ |
||
| 96 | 22 | protected function addBBCodeEndTag() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Add the self-closing tag that matches current BBCode |
||
| 103 | * |
||
| 104 | * @return Tag |
||
| 105 | */ |
||
| 106 | 13 | protected function addBBCodeSelfClosingTag() |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Add the start tag that matches current BBCode |
||
| 116 | * |
||
| 117 | * @return Tag |
||
| 118 | */ |
||
| 119 | 23 | protected function addBBCodeStartTag() |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Parse the end tag that matches given BBCode name and suffix starting at current position |
||
| 129 | * |
||
| 130 | * @return Tag|null |
||
| 131 | */ |
||
| 132 | 11 | protected function captureEndTag() |
|
| 133 | { |
||
| 134 | 11 | if (empty($this->uppercaseText)) |
|
| 135 | { |
||
| 136 | 11 | $this->uppercaseText = strtoupper($this->text); |
|
| 137 | } |
||
| 138 | 11 | $match = '[/' . $this->bbcodeName . $this->bbcodeSuffix . ']'; |
|
| 139 | 11 | $endTagPos = strpos($this->uppercaseText, $match, $this->pos); |
|
| 140 | 11 | if ($endTagPos === false) |
|
| 141 | { |
||
| 142 | 5 | return; |
|
| 143 | } |
||
| 144 | |||
| 145 | 7 | return $this->parser->addEndTag($this->getTagName(), $endTagPos, strlen($match)); |
|
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get the tag name for current BBCode |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | 37 | protected function getTagName() |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Parse attributes starting at current position |
||
| 163 | * |
||
| 164 | * @return array Associative array of [name => value] |
||
| 165 | */ |
||
| 166 | 49 | protected function parseAttributes() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Parse the attribute value starting at current position |
||
| 224 | * |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | 24 | protected function parseAttributeValue() |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Parse current BBCode |
||
| 257 | * |
||
| 258 | * @return void |
||
| 259 | */ |
||
| 260 | 50 | protected function parseBBCode() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Parse the BBCode suffix starting at current position |
||
| 339 | * |
||
| 340 | * Used to explicitly pair specific tags together, e.g. |
||
| 341 | * [code:123][code]type your code here[/code][/code:123] |
||
| 342 | * |
||
| 343 | * @return void |
||
| 344 | */ |
||
| 345 | 50 | protected function parseBBCodeSuffix() |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Parse a quoted attribute value that starts at current offset |
||
| 361 | * |
||
| 362 | * @return string |
||
| 363 | */ |
||
| 364 | 14 | protected function parseQuotedAttributeValue() |
|
| 406 | } |