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 | const LINK_STYLE_FOOTNOTE = 'footnote'; |
||
| 56 | const LINK_STYLE_HREF = 'href'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string link style defines how links are rendered in LaTeX, there are two different options: |
||
| 60 | * |
||
| 61 | * - `footnote` (default) - render all links with a footnote, which contains the full URL of the link. This is good for printing the PDF. |
||
| 62 | * - `href` - render all links with a hyperref, similar to HTML, the link target is not visible in this case. |
||
| 63 | */ |
||
| 64 | public $linkStyle = self::LINK_STYLE_FOOTNOTE; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array these are "escapeable" characters. When using one of these prefixed with a |
||
| 68 | * backslash, the character will be outputted without the backslash and is not interpreted |
||
| 69 | * as markdown. |
||
| 70 | */ |
||
| 71 | protected $escapeCharacters = [ |
||
| 72 | '\\', // backslash |
||
| 73 | '`', // backtick |
||
| 74 | '*', // asterisk |
||
| 75 | '_', // underscore |
||
| 76 | '{', '}', // curly braces |
||
| 77 | '[', ']', // square brackets |
||
| 78 | '(', ')', // parentheses |
||
| 79 | '#', // hash mark |
||
| 80 | '+', // plus sign |
||
| 81 | '-', // minus sign (hyphen) |
||
| 82 | '.', // dot |
||
| 83 | '!', // exclamation mark |
||
| 84 | '<', '>', |
||
| 85 | ]; |
||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * @inheritDoc |
||
| 90 | */ |
||
| 91 | 23 | protected function prepare() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Consume lines for a paragraph |
||
| 99 | * |
||
| 100 | * Allow headlines and code to break paragraphs |
||
| 101 | */ |
||
| 102 | 8 | protected function consumeParagraph($lines, $current) |
|
| 103 | { |
||
| 104 | // consume until newline |
||
| 105 | 8 | $content = []; |
|
| 106 | 8 | for ($i = $current, $count = count($lines); $i < $count; $i++) { |
|
| 107 | 8 | $line = $lines[$i]; |
|
| 108 | 8 | if (!empty($line) && ltrim($line) !== '' && |
|
| 109 | 8 | !($line[0] === "\t" || $line[0] === " " && strncmp($line, ' ', 4) === 0) && |
|
| 110 | 8 | !$this->identifyHeadline($line, $lines, $i)) |
|
| 111 | 8 | { |
|
| 112 | 8 | $content[] = $line; |
|
| 113 | 8 | } else { |
|
| 114 | 7 | break; |
|
| 115 | } |
||
| 116 | 8 | } |
|
| 117 | $block = [ |
||
| 118 | 8 | 'paragraph', |
|
| 119 | 8 | 'content' => $this->parseInline(implode("\n", $content)), |
|
| 120 | 8 | ]; |
|
| 121 | 8 | return [$block, --$i]; |
|
| 122 | } |
||
| 123 | |||
| 124 | |||
| 125 | // rendering adjusted for LaTeX output |
||
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * @inheritdoc |
||
| 130 | */ |
||
| 131 | 21 | protected function renderParagraph($block) |
|
| 132 | { |
||
| 133 | 21 | return $this->renderAbsy($block['content']) . "\n\n"; |
|
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @inheritdoc |
||
| 138 | */ |
||
| 139 | 2 | protected function renderQuote($block) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * @inheritdoc |
||
| 146 | */ |
||
| 147 | 3 | protected function renderCode($block) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @inheritdoc |
||
| 160 | */ |
||
| 161 | 2 | protected function renderList($block) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @inheritdoc |
||
| 175 | */ |
||
| 176 | 3 | protected function renderHeadline($block) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @inheritdoc |
||
| 189 | */ |
||
| 190 | 2 | protected function renderHr($block) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @inheritdoc |
||
| 197 | */ |
||
| 198 | 2 | protected function renderLink($block) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @inheritdoc |
||
| 227 | */ |
||
| 228 | 2 | protected function renderImage($block) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Parses <a name="..."></a> tags as reference labels |
||
| 253 | */ |
||
| 254 | 2 | private function parseInlineHtml($text) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * renders a reference label |
||
| 270 | */ |
||
| 271 | 2 | protected function renderLabel($block) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @inheritdoc |
||
| 278 | */ |
||
| 279 | 2 | protected function renderEmail($block) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @inheritdoc |
||
| 287 | */ |
||
| 288 | 2 | protected function renderUrl($block) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * @inheritdoc |
||
| 295 | */ |
||
| 296 | 1 | protected function renderInlineCode($block) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * @inheritdoc |
||
| 310 | */ |
||
| 311 | 1 | protected function renderStrong($block) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * @inheritdoc |
||
| 318 | */ |
||
| 319 | 1 | protected function renderEmph($block) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Parses escaped special characters. |
||
| 326 | * This allow a backslash to be interpreted as LaTeX |
||
| 327 | * @marker \ |
||
| 328 | */ |
||
| 329 | 1 | protected function parseEscape($text) |
|
| 339 | |||
| 340 | protected function renderBackslash() |
||
| 344 | |||
| 345 | private $_escaper; |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Escape special characters in URLs |
||
| 349 | */ |
||
| 350 | 4 | protected function escapeUrl($string) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Escape special LaTeX characters |
||
| 357 | */ |
||
| 358 | 21 | protected function escapeLatex($string) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * @inheritdocs |
||
| 368 | * |
||
| 369 | * Parses a newline indicated by two spaces on the end of a markdown line. |
||
| 370 | */ |
||
| 371 | 21 | protected function renderText($text) |
|
| 382 | } |
||
| 383 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.