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) |
|
141 | |||
142 | /** |
||
143 | * @inheritdoc |
||
144 | */ |
||
145 | 2 | protected function renderList($block) |
|
156 | |||
157 | /** |
||
158 | * @inheritdoc |
||
159 | */ |
||
160 | 3 | protected function renderHeadline($block) |
|
170 | |||
171 | /** |
||
172 | * @inheritdoc |
||
173 | */ |
||
174 | 2 | protected function renderHr($block) |
|
178 | |||
179 | /** |
||
180 | * @inheritdoc |
||
181 | */ |
||
182 | 2 | protected function renderLink($block) |
|
205 | |||
206 | /** |
||
207 | * @inheritdoc |
||
208 | */ |
||
209 | protected function renderImage($block) |
||
223 | |||
224 | /** |
||
225 | * Parses <a name="..."></a> tags as reference labels |
||
226 | */ |
||
227 | 2 | private function parseInlineHtml($text) |
|
240 | |||
241 | /** |
||
242 | * renders a reference label |
||
243 | */ |
||
244 | 2 | protected function renderLabel($block) |
|
248 | |||
249 | /** |
||
250 | * @inheritdoc |
||
251 | */ |
||
252 | 2 | protected function renderEmail($block) |
|
257 | |||
258 | /** |
||
259 | * @inheritdoc |
||
260 | */ |
||
261 | 2 | protected function renderUrl($block) |
|
265 | |||
266 | /** |
||
267 | * @inheritdoc |
||
268 | */ |
||
269 | 1 | protected function renderInlineCode($block) |
|
277 | |||
278 | /** |
||
279 | * @inheritdoc |
||
280 | */ |
||
281 | 1 | protected function renderStrong($block) |
|
285 | |||
286 | /** |
||
287 | * @inheritdoc |
||
288 | */ |
||
289 | 1 | protected function renderEmph($block) |
|
293 | |||
294 | /** |
||
295 | * Parses escaped special characters. |
||
296 | * This allow a backslash to be interpreted as LaTeX |
||
297 | * @marker \ |
||
298 | */ |
||
299 | 1 | protected function parseEscape($text) |
|
300 | { |
||
301 | 1 | if (isset($text[1]) && in_array($text[1], $this->escapeCharacters)) { |
|
302 | 1 | if ($text[1] === '\\') { |
|
303 | return [['backslash'], 2]; |
||
304 | } |
||
305 | 1 | return [['text', $text[1]], 2]; |
|
306 | } |
||
307 | return [['text', $text[0]], 1]; |
||
308 | } |
||
309 | |||
310 | protected function renderBackslash() |
||
311 | { |
||
312 | return '\\'; |
||
313 | } |
||
314 | |||
315 | private $_escaper; |
||
316 | |||
317 | /** |
||
318 | * Escape special characters in URLs |
||
319 | */ |
||
320 | 4 | protected function escapeUrl($string) |
|
324 | |||
325 | /** |
||
326 | * Escape special LaTeX characters |
||
327 | */ |
||
328 | 19 | protected function escapeLatex($string) |
|
335 | |||
336 | /** |
||
337 | * @inheritdocs |
||
338 | * |
||
339 | * Parses a newline indicated by two spaces on the end of a markdown line. |
||
340 | */ |
||
341 | 19 | protected function renderText($text) |
|
352 | } |
||
353 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.