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:
1 | <?php |
||
15 | class GithubMarkdown extends Markdown |
||
16 | { |
||
17 | // include block element parsing using traits |
||
18 | use block\TableTrait; |
||
19 | use block\FencedCodeTrait; |
||
20 | |||
21 | // include inline element parsing using traits |
||
22 | use inline\StrikeoutTrait; |
||
23 | use inline\UrlLinkTrait; |
||
24 | |||
25 | /** |
||
26 | * @var boolean whether to interpret newlines as `<br />`-tags. |
||
27 | * This feature is useful for comments where newlines are often meant to be real new lines. |
||
28 | */ |
||
29 | public $enableNewlines = false; |
||
30 | |||
31 | /** |
||
32 | * @inheritDoc |
||
33 | */ |
||
34 | protected $escapeCharacters = [ |
||
35 | // from Markdown |
||
36 | '\\', // backslash |
||
37 | '`', // backtick |
||
38 | '*', // asterisk |
||
39 | '_', // underscore |
||
40 | '{', '}', // curly braces |
||
41 | '[', ']', // square brackets |
||
42 | '(', ')', // parentheses |
||
43 | '#', // hash mark |
||
44 | '+', // plus sign |
||
45 | '-', // minus sign (hyphen) |
||
46 | '.', // dot |
||
47 | '!', // exclamation mark |
||
48 | '<', '>', |
||
49 | // added by GithubMarkdown |
||
50 | ':', // colon |
||
51 | '|', // pipe |
||
52 | ]; |
||
53 | |||
54 | |||
55 | |||
56 | /** |
||
57 | * Consume lines for a paragraph |
||
58 | * |
||
59 | * Allow headlines, lists and code to break paragraphs |
||
60 | */ |
||
61 | 66 | protected function consumeParagraph($lines, $current) |
|
100 | |||
101 | /** |
||
102 | * @inheritdocs |
||
103 | * |
||
104 | * Parses a newline indicated by two spaces on the end of a markdown line. |
||
105 | 68 | */ |
|
106 | protected function renderText($text) |
||
115 | } |
||
116 |
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.