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 MarkdownExtra 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 MarkdownExtra, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class MarkdownExtra extends Markdown |
||
18 | { |
||
19 | // include block element parsing using traits |
||
20 | use block\TableTrait; |
||
21 | use block\FencedCodeTrait; |
||
22 | |||
23 | // include inline element parsing using traits |
||
24 | // TODO |
||
25 | |||
26 | /** |
||
27 | * @var bool whether special attributes on code blocks should be applied on the `<pre>` element. |
||
28 | * The default behavior is to put them on the `<code>` element. |
||
29 | */ |
||
30 | public $codeAttributesOnPre = false; |
||
31 | |||
32 | /** |
||
33 | * @inheritDoc |
||
34 | */ |
||
35 | protected $escapeCharacters = [ |
||
36 | // from Markdown |
||
37 | '\\', // backslash |
||
38 | '`', // backtick |
||
39 | '*', // asterisk |
||
40 | '_', // underscore |
||
41 | '{', '}', // curly braces |
||
42 | '[', ']', // square brackets |
||
43 | '(', ')', // parentheses |
||
44 | '#', // hash mark |
||
45 | '+', // plus sign |
||
46 | '-', // minus sign (hyphen) |
||
47 | '.', // dot |
||
48 | '!', // exclamation mark |
||
49 | '<', '>', |
||
50 | // added by MarkdownExtra |
||
51 | ':', // colon |
||
52 | '|', // pipe |
||
53 | ]; |
||
54 | |||
55 | private $_specialAttributesRegex = '\{(([#\.][A-z0-9-_]+\s*)+)\}'; |
||
56 | |||
57 | // TODO allow HTML intended 3 spaces |
||
58 | |||
59 | // TODO add markdown inside HTML blocks |
||
60 | |||
61 | // TODO implement definition lists |
||
62 | |||
63 | // TODO implement footnotes |
||
64 | |||
65 | // TODO implement Abbreviations |
||
66 | |||
67 | |||
68 | // block parsing |
||
69 | |||
70 | 61 | protected function identifyReference($line) |
|
74 | |||
75 | /** |
||
76 | * Consume link references |
||
77 | */ |
||
78 | 10 | protected function consumeReference($lines, $current) |
|
79 | { |
||
80 | 10 | while (isset($lines[$current]) && preg_match('/^ {0,3}\[(.+?)\]:\s*(.+?)(?:\s+[\(\'"](.+?)[\)\'"])?\s*('.$this->_specialAttributesRegex.')?\s*$/', $lines[$current], $matches)) { |
|
81 | 10 | $label = strtolower($matches[1]); |
|
82 | |||
83 | 10 | $this->references[$label] = [ |
|
84 | 10 | 'url' => $this->replaceEscape($matches[2]), |
|
85 | ]; |
||
86 | 10 | View Code Duplication | if (isset($matches[3])) { |
87 | 5 | $this->references[$label]['title'] = $matches[3]; |
|
88 | } else { |
||
89 | // title may be on the next line |
||
90 | 8 | if (isset($lines[$current + 1]) && preg_match('/^\s+[\(\'"](.+?)[\)\'"]\s*$/', $lines[$current + 1], $matches)) { |
|
91 | 1 | $this->references[$label]['title'] = $matches[1]; |
|
92 | 1 | $current++; |
|
93 | } |
||
94 | } |
||
95 | 10 | if (isset($matches[5])) { |
|
96 | 1 | $this->references[$label]['attributes'] = $matches[5]; |
|
97 | } |
||
98 | 10 | $current++; |
|
99 | } |
||
100 | 10 | return [false, --$current]; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Consume lines for a fenced code block |
||
105 | */ |
||
106 | 2 | protected function consumeFencedCode($lines, $current) |
|
107 | { |
||
108 | // consume until ``` |
||
109 | $block = [ |
||
110 | 2 | 'code', |
|
111 | ]; |
||
112 | 2 | $line = rtrim($lines[$current]); |
|
113 | 2 | if (($pos = strrpos($line, '`')) === false) { |
|
114 | 1 | $pos = strrpos($line, '~'); |
|
115 | } |
||
116 | 2 | $fence = substr($line, 0, $pos + 1); |
|
117 | 2 | $block['attributes'] = substr($line, $pos); |
|
118 | 2 | $content = []; |
|
119 | 2 | View Code Duplication | for($i = $current + 1, $count = count($lines); $i < $count; $i++) { |
120 | 2 | if (rtrim($line = $lines[$i]) !== $fence) { |
|
121 | 2 | $content[] = $line; |
|
122 | } else { |
||
123 | 2 | break; |
|
124 | } |
||
125 | } |
||
126 | 2 | $block['content'] = implode("\n", $content); |
|
127 | 2 | return [$block, $i]; |
|
128 | } |
||
129 | |||
130 | 18 | protected function renderCode($block) |
|
137 | |||
138 | /** |
||
139 | * Renders a headline |
||
140 | */ |
||
141 | 12 | protected function renderHeadline($block) |
|
142 | { |
||
143 | 12 | foreach($block['content'] as $i => $element) { |
|
144 | 12 | if ($element[0] === 'specialAttributes') { |
|
145 | 1 | unset($block['content'][$i]); |
|
146 | 12 | $block['attributes'] = $element[1]; |
|
147 | } |
||
148 | } |
||
149 | 12 | $tag = 'h' . $block['level']; |
|
150 | 12 | $attributes = $this->renderAttributes($block); |
|
151 | 12 | return "<$tag$attributes>" . rtrim($this->renderAbsy($block['content']), "# \t") . "</$tag>\n"; |
|
152 | } |
||
153 | |||
154 | 33 | protected function renderAttributes($block) |
|
155 | { |
||
156 | 33 | $html = []; |
|
157 | 33 | if (isset($block['attributes'])) { |
|
158 | 3 | $attributes = preg_split('/\s+/', $block['attributes'], -1, PREG_SPLIT_NO_EMPTY); |
|
159 | 3 | foreach($attributes as $attribute) { |
|
160 | 3 | if ($attribute[0] === '#') { |
|
161 | 2 | $html['id'] = substr($attribute, 1); |
|
162 | } else { |
||
163 | 3 | $html['class'][] = substr($attribute, 1); |
|
164 | } |
||
165 | } |
||
166 | } |
||
167 | 33 | $result = ''; |
|
168 | 33 | foreach($html as $attr => $value) { |
|
169 | 3 | if (is_array($value)) { |
|
170 | 3 | $value = trim(implode(' ', $value)); |
|
171 | } |
||
172 | 3 | if (!empty($value)) { |
|
173 | 3 | $result .= " $attr=\"$value\""; |
|
174 | } |
||
175 | } |
||
176 | 33 | return $result; |
|
177 | } |
||
178 | |||
179 | |||
180 | // inline parsing |
||
181 | |||
182 | |||
183 | /** |
||
184 | * @marker { |
||
185 | */ |
||
186 | 1 | View Code Duplication | protected function parseSpecialAttributes($text) |
193 | |||
194 | 1 | protected function renderSpecialAttributes($block) |
|
198 | |||
199 | 61 | protected function parseInline($text) |
|
200 | { |
||
201 | 61 | $elements = parent::parseInline($text); |
|
202 | // merge special attribute elements to links and images as they are not part of the final absy later |
||
219 | |||
220 | 18 | protected function renderLink($block) |
|
237 | |||
238 | 4 | protected function renderImage($block) |
|
256 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.