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 | 23 | '+', // plus sign |
|
81 | '-', // minus sign (hyphen) |
||
82 | '.', // dot |
||
83 | 23 | '!', // exclamation mark |
|
84 | 23 | '<', '>', |
|
85 | ]; |
||
86 | |||
87 | |||
88 | /** |
||
89 | * @inheritDoc |
||
90 | */ |
||
91 | 8 | protected function prepare() |
|
96 | 8 | ||
97 | 8 | /** |
|
98 | 8 | * Consume lines for a paragraph |
|
99 | 8 | * |
|
100 | 8 | * Allow headlines and code to break paragraphs |
|
101 | 8 | */ |
|
102 | 8 | protected function consumeParagraph($lines, $current) |
|
123 | |||
124 | |||
125 | // rendering adjusted for LaTeX output |
||
126 | |||
127 | |||
128 | 2 | /** |
|
129 | * @inheritdoc |
||
130 | 2 | */ |
|
131 | protected function renderParagraph($block) |
||
135 | |||
136 | 3 | /** |
|
137 | * @inheritdoc |
||
138 | 3 | */ |
|
139 | protected function renderQuote($block) |
||
143 | |||
144 | 3 | /** |
|
145 | * @inheritdoc |
||
146 | */ |
||
147 | protected function renderCode($block) |
||
157 | 2 | ||
158 | /** |
||
159 | 2 | * @inheritdoc |
|
160 | */ |
||
161 | protected function renderList($block) |
||
172 | 2 | ||
173 | 2 | /** |
|
174 | * @inheritdoc |
||
175 | */ |
||
176 | protected function renderHeadline($block) |
||
186 | |||
187 | 2 | /** |
|
188 | * @inheritdoc |
||
189 | 2 | */ |
|
190 | protected function renderHr($block) |
||
194 | |||
195 | /** |
||
196 | * @inheritdoc |
||
197 | 2 | */ |
|
198 | 2 | protected function renderLink($block) |
|
224 | |||
225 | /** |
||
226 | 2 | * @inheritdoc |
|
227 | 2 | */ |
|
228 | 2 | protected function renderImage($block) |
|
250 | |||
251 | /** |
||
252 | * Parses <a name="..."></a> tags as reference labels |
||
253 | */ |
||
254 | private function parseInlineHtml($text) |
||
267 | 2 | ||
268 | 2 | /** |
|
269 | * renders a reference label |
||
270 | */ |
||
271 | protected function renderLabel($block) |
||
275 | |||
276 | 2 | /** |
|
277 | * @inheritdoc |
||
278 | */ |
||
279 | protected function renderEmail($block) |
||
284 | |||
285 | 1 | /** |
|
286 | * @inheritdoc |
||
287 | 1 | */ |
|
288 | 1 | protected function renderUrl($block) |
|
292 | |||
293 | /** |
||
294 | * @inheritdoc |
||
295 | */ |
||
296 | protected function renderInlineCode($block) |
||
307 | 1 | ||
308 | /** |
||
309 | * @inheritdoc |
||
310 | */ |
||
311 | protected function renderStrong($block) |
||
315 | 1 | ||
316 | /** |
||
317 | 1 | * @inheritdoc |
|
318 | 1 | */ |
|
319 | 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 | protected function parseEscape($text) |
||
339 | |||
340 | protected function renderBackslash() |
||
344 | 21 | ||
345 | private $_escaper; |
||
346 | 21 | ||
347 | 21 | /** |
|
348 | 21 | * Escape special characters in URLs |
|
349 | 21 | */ |
|
350 | protected function escapeUrl($string) |
||
354 | |||
355 | /** |
||
356 | * Escape special LaTeX characters |
||
357 | 21 | */ |
|
358 | protected function escapeLatex($string) |
||
365 | 21 | ||
366 | 21 | /** |
|
367 | * @inheritdocs |
||
368 | * |
||
369 | 1 | * Parses a newline indicated by two spaces on the end of a markdown line. |
|
370 | */ |
||
371 | 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.