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 LinkTrait 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 LinkTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | trait LinkTrait |
||
39 | { |
||
40 | /** |
||
41 | * @var array a list of defined references in this document. |
||
42 | */ |
||
43 | protected $references = []; |
||
44 | |||
45 | /** |
||
46 | * Remove backslash from escaped characters |
||
47 | * @param $text |
||
48 | * @return string |
||
49 | */ |
||
50 | 58 | protected function replaceEscape($text) |
|
58 | |||
59 | /** |
||
60 | * Parses a link indicated by `[`. |
||
61 | * @marker [ |
||
62 | */ |
||
63 | 53 | protected function parseLink($markdown) |
|
89 | |||
90 | /** |
||
91 | * Parses an image indicated by `![`. |
||
92 | * @marker ![ |
||
93 | */ |
||
94 | 10 | protected function parseImage($markdown) |
|
121 | |||
122 | 53 | protected function parseLinkOrImage($markdown) |
|
163 | |||
164 | /** |
||
165 | * Parses inline HTML. |
||
166 | * @marker < |
||
167 | */ |
||
168 | 31 | protected function parseLt($text) |
|
193 | |||
194 | 6 | protected function renderEmail($block) |
|
199 | |||
200 | 13 | View Code Duplication | protected function renderUrl($block) |
208 | |||
209 | 43 | protected function lookupReference($key) |
|
217 | |||
218 | 35 | protected function renderLink($block) |
|
219 | { |
||
220 | 35 | View Code Duplication | if (isset($block['refkey'])) { |
221 | 28 | if (($ref = $this->lookupReference($block['refkey'])) !== false) { |
|
222 | 19 | $block = array_merge($block, $ref); |
|
223 | 19 | } else { |
|
224 | 15 | if (strncmp($block['orig'], '[', 1) === 0) { |
|
225 | 15 | return '[' . $this->renderAbsy($this->parseInline(substr($block['orig'], 1))); |
|
226 | } |
||
227 | return $block['orig']; |
||
228 | } |
||
229 | 19 | } |
|
230 | 34 | return '<a href="' . htmlspecialchars($block['url'], ENT_COMPAT | ENT_HTML401, 'UTF-8') . '"' |
|
231 | 34 | . (empty($block['title']) ? '' : ' title="' . htmlspecialchars($block['title'], ENT_COMPAT | ENT_HTML401 | ENT_SUBSTITUTE, 'UTF-8') . '"') |
|
232 | 34 | . '>' . $this->renderAbsy($block['text']) . '</a>'; |
|
233 | } |
||
234 | |||
235 | 6 | protected function renderImage($block) |
|
236 | { |
||
237 | 6 | View Code Duplication | if (isset($block['refkey'])) { |
238 | 4 | if (($ref = $this->lookupReference($block['refkey'])) !== false) { |
|
239 | $block = array_merge($block, $ref); |
||
240 | } else { |
||
241 | 4 | if (strncmp($block['orig'], '![', 2) === 0) { |
|
242 | 4 | return '![' . $this->renderAbsy($this->parseInline(substr($block['orig'], 2))); |
|
243 | } |
||
244 | return $block['orig']; |
||
245 | } |
||
246 | } |
||
247 | 4 | return '<img src="' . htmlspecialchars($block['url'], ENT_COMPAT | ENT_HTML401, 'UTF-8') . '"' |
|
248 | 4 | . ' alt="' . htmlspecialchars($block['text'], ENT_COMPAT | ENT_HTML401 | ENT_SUBSTITUTE, 'UTF-8') . '"' |
|
249 | 4 | . (empty($block['title']) ? '' : ' title="' . htmlspecialchars($block['title'], ENT_COMPAT | ENT_HTML401 | ENT_SUBSTITUTE, 'UTF-8') . '"') |
|
250 | 4 | . ($this->html5 ? '>' : ' />'); |
|
251 | } |
||
252 | |||
253 | // references |
||
254 | |||
255 | 143 | protected function identifyReference($line) |
|
256 | { |
||
257 | 143 | return isset($line[0]) && ($line[0] === ' ' || $line[0] === '[') && preg_match('/^ {0,3}\[(.+?)\]:\s*([^\s]+?)(?:\s+[\'"](.+?)[\'"])?\s*$/', $line); |
|
258 | } |
||
259 | |||
260 | /** |
||
261 | * Consume link references |
||
262 | */ |
||
263 | 19 | protected function consumeReference($lines, $current) |
|
264 | { |
||
265 | 19 | while (isset($lines[$current]) && preg_match('/^ {0,3}\[(.+?)\]:\s*(.+?)(?:\s+[\(\'"](.+?)[\)\'"])?\s*$/', $lines[$current], $matches)) { |
|
266 | 19 | $label = strtolower($matches[1]); |
|
267 | |||
268 | 19 | $this->references[$label] = [ |
|
269 | 19 | 'url' => $this->replaceEscape($matches[2]), |
|
270 | ]; |
||
271 | 19 | View Code Duplication | if (isset($matches[3])) { |
272 | 9 | $this->references[$label]['title'] = $matches[3]; |
|
273 | 9 | } else { |
|
274 | // title may be on the next line |
||
275 | 16 | if (isset($lines[$current + 1]) && preg_match('/^\s+[\(\'"](.+?)[\)\'"]\s*$/', $lines[$current + 1], $matches)) { |
|
276 | 2 | $this->references[$label]['title'] = $matches[1]; |
|
277 | 2 | $current++; |
|
278 | 2 | } |
|
279 | } |
||
280 | 19 | $current++; |
|
281 | 19 | } |
|
282 | 19 | return [false, --$current]; |
|
283 | } |
||
284 | |||
285 | abstract protected function parseInline($text); |
||
287 | } |
||
288 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.