Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 16 | abstract class Parser |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var integer the maximum nesting level for language elements. |
||
| 20 | */ |
||
| 21 | public $maximumNestingLevel = 32; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string the current context the parser is in. |
||
| 25 | * TODO remove in favor of absy |
||
| 26 | */ |
||
| 27 | protected $context = []; |
||
| 28 | /** |
||
| 29 | * @var array these are "escapeable" characters. When using one of these prefixed with a |
||
| 30 | * backslash, the character will be outputted without the backslash and is not interpreted |
||
| 31 | * as markdown. |
||
| 32 | */ |
||
| 33 | protected $escapeCharacters = [ |
||
| 34 | '\\', // backslash |
||
| 35 | ]; |
||
| 36 | |||
| 37 | private $_depth = 0; |
||
| 38 | |||
| 39 | |||
| 40 | /** |
||
| 41 | * Parses the given text considering the full language. |
||
| 42 | * |
||
| 43 | * This includes parsing block elements as well as inline elements. |
||
| 44 | * |
||
| 45 | * @param string $text the text to parse |
||
| 46 | * @return string parsed markup |
||
| 47 | */ |
||
| 48 | 195 | public function parse($text) |
|
| 49 | { |
||
| 50 | 195 | $this->prepare(); |
|
| 51 | |||
| 52 | 195 | if (ltrim($text) === '') { |
|
| 53 | return ''; |
||
| 54 | } |
||
| 55 | |||
| 56 | 195 | $text = str_replace(["\r\n", "\n\r", "\r"], "\n", $text); |
|
| 57 | |||
| 58 | 195 | $this->prepareMarkers($text); |
|
| 59 | |||
| 60 | 195 | $absy = $this->parseBlocks(explode("\n", $text)); |
|
| 61 | 195 | $markup = $this->renderAbsy($absy); |
|
| 62 | |||
| 63 | 195 | $this->cleanup(); |
|
| 64 | 195 | return $markup; |
|
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Parses a paragraph without block elements (block elements are ignored). |
||
| 69 | * |
||
| 70 | * @param string $text the text to parse |
||
| 71 | * @return string parsed markup |
||
| 72 | */ |
||
| 73 | 46 | public function parseParagraph($text) |
|
| 74 | { |
||
| 75 | 46 | $this->prepare(); |
|
| 76 | |||
| 77 | 46 | if (ltrim($text) === '') { |
|
| 78 | return ''; |
||
| 79 | } |
||
| 80 | |||
| 81 | 46 | $text = str_replace(["\r\n", "\n\r", "\r"], "\n", $text); |
|
| 82 | |||
| 83 | 46 | $this->prepareMarkers($text); |
|
| 84 | |||
| 85 | 46 | $absy = $this->parseInline($text); |
|
| 86 | 46 | $markup = $this->renderAbsy($absy); |
|
| 87 | |||
| 88 | 46 | $this->cleanup(); |
|
| 89 | 46 | return $markup; |
|
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * This method will be called before `parse()` and `parseParagraph()`. |
||
| 94 | * You can override it to do some initialization work. |
||
| 95 | */ |
||
| 96 | 3 | protected function prepare() |
|
| 99 | |||
| 100 | /** |
||
| 101 | * This method will be called after `parse()` and `parseParagraph()`. |
||
| 102 | * You can override it to do cleanup. |
||
| 103 | */ |
||
| 104 | 196 | protected function cleanup() |
|
| 107 | |||
| 108 | |||
| 109 | // block parsing |
||
| 110 | |||
| 111 | private $_blockTypes; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return array a list of block element types available. |
||
| 115 | */ |
||
| 116 | 195 | protected function blockTypes() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Given a set of lines and an index of a current line it uses the registed block types to |
||
| 133 | * detect the type of this line. |
||
| 134 | * @param array $lines |
||
| 135 | * @param integer $current |
||
| 136 | * @return string name of the block type in lower case |
||
| 137 | */ |
||
| 138 | 195 | protected function detectLineType($lines, $current) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Parse block elements by calling `detectLineType()` to identify them |
||
| 153 | * and call consume function afterwards. |
||
| 154 | */ |
||
| 155 | 195 | protected function parseBlocks($lines) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Parses the block at current line by identifying the block type and parsing the content |
||
| 184 | * @param $lines |
||
| 185 | * @param $current |
||
| 186 | * @return array Array of two elements, the first element contains the block, |
||
| 187 | * the second contains the next line index to be parsed. |
||
| 188 | */ |
||
| 189 | 195 | protected function parseBlock($lines, $current) |
|
| 197 | |||
| 198 | 196 | protected function renderAbsy($blocks) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Consume lines for a paragraph |
||
| 211 | * |
||
| 212 | * @param $lines |
||
| 213 | * @param $current |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | 2 | protected function consumeParagraph($lines, $current) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Render a paragraph block |
||
| 236 | * |
||
| 237 | * @param $block |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | 190 | protected function renderParagraph($block) |
|
| 244 | |||
| 245 | |||
| 246 | // inline parsing |
||
| 247 | |||
| 248 | |||
| 249 | /** |
||
| 250 | * @var array the set of inline markers to use in different contexts. |
||
| 251 | */ |
||
| 252 | private $_inlineMarkers = []; |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Returns a map of inline markers to the corresponding parser methods. |
||
| 256 | * |
||
| 257 | * This array defines handler methods for inline markdown markers. |
||
| 258 | * When a marker is found in the text, the handler method is called with the text |
||
| 259 | * starting at the position of the marker. |
||
| 260 | * |
||
| 261 | * Note that markers starting with whitespace may slow down the parser, |
||
| 262 | * you may want to use [[renderText]] to deal with them. |
||
| 263 | * |
||
| 264 | * You may override this method to define a set of markers and parsing methods. |
||
| 265 | * The default implementation looks for protected methods starting with `parse` that |
||
| 266 | * also have an `@marker` annotation in PHPDoc. |
||
| 267 | * |
||
| 268 | * @return array a map of markers to parser methods |
||
| 269 | */ |
||
| 270 | 193 | protected function inlineMarkers() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Prepare markers that are used in the text to parse |
||
| 289 | * |
||
| 290 | * Add all markers that are present in markdown. |
||
| 291 | * Check is done to avoid iterations in parseInline(), good for huge markdown files |
||
| 292 | * @param string $text |
||
| 293 | */ |
||
| 294 | 196 | protected function prepareMarkers($text) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Parses inline elements of the language. |
||
| 315 | * |
||
| 316 | * @param string $text the inline text to parse. |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | 195 | protected function parseInline($text) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Parses escaped special characters. |
||
| 370 | * @marker \ |
||
| 371 | */ |
||
| 372 | 17 | protected function parseEscape($text) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * This function renders plain text sections in the markdown text. |
||
| 382 | * It can be used to work on normal text sections for example to highlight keywords or |
||
| 383 | * do special escaping. |
||
| 384 | */ |
||
| 385 | 3 | protected function renderText($block) |
|
| 389 | } |
||
| 390 |