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 | 23 | protected function prepare() |
|
85 | |||
86 | /** |
||
87 | * Consume lines for a paragraph |
||
88 | * |
||
89 | * Allow headlines and code to break paragraphs |
||
90 | */ |
||
91 | 8 | protected function consumeParagraph($lines, $current) |
|
112 | |||
113 | |||
114 | // rendering adjusted for LaTeX output |
||
115 | |||
116 | |||
117 | /** |
||
118 | * @inheritdoc |
||
119 | */ |
||
120 | 21 | 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 | 2 | protected function renderImage($block) |
|
236 | |||
237 | /** |
||
238 | * Parses <a name="..."></a> tags as reference labels |
||
239 | */ |
||
240 | 2 | private function parseInlineHtml($text) |
|
253 | |||
254 | /** |
||
255 | * renders a reference label |
||
256 | */ |
||
257 | 2 | protected function renderLabel($block) |
|
261 | |||
262 | /** |
||
263 | * @inheritdoc |
||
264 | */ |
||
265 | 2 | protected function renderEmail($block) |
|
270 | |||
271 | /** |
||
272 | * @inheritdoc |
||
273 | */ |
||
274 | 2 | protected function renderUrl($block) |
|
278 | |||
279 | /** |
||
280 | * @inheritdoc |
||
281 | */ |
||
282 | 1 | protected function renderInlineCode($block) |
|
293 | |||
294 | /** |
||
295 | * @inheritdoc |
||
296 | */ |
||
297 | 1 | protected function renderStrong($block) |
|
301 | |||
302 | /** |
||
303 | * @inheritdoc |
||
304 | */ |
||
305 | 1 | protected function renderEmph($block) |
|
309 | |||
310 | /** |
||
311 | * Parses escaped special characters. |
||
312 | * This allow a backslash to be interpreted as LaTeX |
||
313 | * @marker \ |
||
314 | */ |
||
315 | 1 | protected function parseEscape($text) |
|
325 | |||
326 | protected function renderBackslash() |
||
330 | |||
331 | private $_escaper; |
||
332 | |||
333 | /** |
||
334 | * Escape special characters in URLs |
||
335 | */ |
||
336 | 4 | protected function escapeUrl($string) |
|
340 | |||
341 | /** |
||
342 | * Escape special LaTeX characters |
||
343 | */ |
||
344 | 21 | protected function escapeLatex($string) |
|
351 | |||
352 | /** |
||
353 | * @inheritdocs |
||
354 | * |
||
355 | * Parses a newline indicated by two spaces on the end of a markdown line. |
||
356 | */ |
||
357 | 21 | protected function renderText($text) |
|
368 | } |
||
369 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.