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 Markdown 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 Markdown, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Markdown extends \cebe\markdown\Parser |
||
| 28 | { |
||
| 29 | // include block element parsing using traits |
||
| 30 | use CodeTrait; |
||
| 31 | use HeadlineTrait; |
||
| 32 | use ListTrait { |
||
| 33 | // Check Ul List before headline |
||
| 34 | identifyUl as protected identifyBUl; |
||
| 35 | consumeUl as protected consumeBUl; |
||
| 36 | } |
||
| 37 | use QuoteTrait; |
||
| 38 | use RuleTrait { |
||
| 39 | // Check Hr before checking lists |
||
| 40 | identifyHr as protected identifyAHr; |
||
| 41 | consumeHr as protected consumeAHr; |
||
| 42 | } |
||
| 43 | |||
| 44 | // include inline element parsing using traits |
||
| 45 | use InlineCodeTrait; |
||
| 46 | use EmphStrongTrait; |
||
| 47 | use LinkTrait; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string this string will be prefixed to all auto generated labels. |
||
| 51 | * This can be used to disambiguate labels when combining multiple markdown files into one document. |
||
| 52 | */ |
||
| 53 | public $labelPrefix = ''; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array these are "escapeable" characters. When using one of these prefixed with a |
||
| 57 | * backslash, the character will be outputted without the backslash and is not interpreted |
||
| 58 | * as markdown. |
||
| 59 | */ |
||
| 60 | protected $escapeCharacters = [ |
||
| 61 | '\\', // backslash |
||
| 62 | '`', // backtick |
||
| 63 | '*', // asterisk |
||
| 64 | '_', // underscore |
||
| 65 | '{', '}', // curly braces |
||
| 66 | '[', ']', // square brackets |
||
| 67 | '(', ')', // parentheses |
||
| 68 | '#', // hash mark |
||
| 69 | '+', // plus sign |
||
| 70 | '-', // minus sign (hyphen) |
||
| 71 | '.', // dot |
||
| 72 | '!', // exclamation mark |
||
| 73 | '<', '>', |
||
| 74 | ]; |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * @inheritDoc |
||
| 79 | */ |
||
| 80 | 21 | protected function prepare() |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Consume lines for a paragraph |
||
| 88 | * |
||
| 89 | * Allow headlines and code to break paragraphs |
||
| 90 | */ |
||
| 91 | 7 | protected function consumeParagraph($lines, $current) |
|
| 112 | |||
| 113 | |||
| 114 | // rendering adjusted for LaTeX output |
||
| 115 | |||
| 116 | |||
| 117 | /** |
||
| 118 | * @inheritdoc |
||
| 119 | */ |
||
| 120 | 19 | protected function renderParagraph($block) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @inheritdoc |
||
| 127 | */ |
||
| 128 | 2 | protected function renderQuote($block) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @inheritdoc |
||
| 135 | */ |
||
| 136 | 3 | protected function renderCode($block) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @inheritdoc |
||
| 149 | */ |
||
| 150 | 2 | protected function renderList($block) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @inheritdoc |
||
| 164 | */ |
||
| 165 | 3 | protected function renderHeadline($block) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @inheritdoc |
||
| 178 | */ |
||
| 179 | 2 | protected function renderHr($block) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * @inheritdoc |
||
| 186 | */ |
||
| 187 | 2 | protected function renderLink($block) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @inheritdoc |
||
| 213 | */ |
||
| 214 | protected function renderImage($block) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Parses <a name="..."></a> tags as reference labels |
||
| 231 | */ |
||
| 232 | 2 | private function parseInlineHtml($text) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * renders a reference label |
||
| 248 | */ |
||
| 249 | 2 | protected function renderLabel($block) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @inheritdoc |
||
| 256 | */ |
||
| 257 | 2 | protected function renderEmail($block) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @inheritdoc |
||
| 265 | */ |
||
| 266 | 2 | protected function renderUrl($block) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @inheritdoc |
||
| 273 | */ |
||
| 274 | 1 | protected function renderInlineCode($block) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @inheritdoc |
||
| 288 | */ |
||
| 289 | 1 | protected function renderStrong($block) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * @inheritdoc |
||
| 296 | */ |
||
| 297 | 1 | protected function renderEmph($block) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Parses escaped special characters. |
||
| 304 | * This allow a backslash to be interpreted as LaTeX |
||
| 305 | * @marker \ |
||
| 306 | */ |
||
| 307 | 1 | protected function parseEscape($text) |
|
| 317 | |||
| 318 | protected function renderBackslash() |
||
| 322 | |||
| 323 | private $_escaper; |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Escape special LaTeX characters |
||
| 327 | */ |
||
| 328 | 19 | protected function escapeLatex($string) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * @inheritdocs |
||
| 340 | * |
||
| 341 | * Parses a newline indicated by two spaces on the end of a markdown line. |
||
| 342 | */ |
||
| 343 | 19 | protected function renderText($text) |
|
| 354 | } |
||
| 355 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.