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 |
||
5 | trait SlackMessageTrait |
||
6 | { |
||
7 | /** |
||
8 | * @param array $lines |
||
9 | * @return string |
||
10 | */ |
||
11 | public function inlineMultilines(array $lines): string |
||
15 | |||
16 | /** |
||
17 | * @param string $string |
||
18 | * @return string |
||
19 | */ |
||
20 | public function formatBold(string $string): string |
||
24 | |||
25 | /** |
||
26 | * @param string $string |
||
27 | * @return string |
||
28 | */ |
||
29 | public function formatItalic(string $string): string |
||
33 | |||
34 | /** |
||
35 | * @param string $string |
||
36 | * @return string |
||
37 | */ |
||
38 | public function formatStrikeThought(string $string): string |
||
42 | |||
43 | /** |
||
44 | * @param array $list |
||
45 | * @return string |
||
46 | * @throws \InvalidArgumentException |
||
47 | */ |
||
48 | View Code Duplication | public function formatListMarker(array $list): string |
|
61 | |||
62 | /** |
||
63 | * @param array $list |
||
64 | * @return string |
||
65 | * @throws \InvalidArgumentException |
||
66 | */ |
||
67 | View Code Duplication | public function formatListNumeric(array $list): string |
|
81 | |||
82 | /** |
||
83 | * @param array $lines |
||
84 | * @return string |
||
85 | */ |
||
86 | public function formatCode(array $lines): string |
||
94 | |||
95 | /** |
||
96 | * @return string |
||
97 | */ |
||
98 | public function newLine(): string |
||
102 | |||
103 | /** |
||
104 | * @param string $title |
||
105 | * @param string $url |
||
106 | * @return string |
||
107 | */ |
||
108 | public function formatLink(string $title, string $url): string |
||
112 | |||
113 | /** |
||
114 | * @param string $string |
||
115 | * @return string |
||
116 | */ |
||
117 | public function escapeCharacters(string $string): string |
||
124 | } |
||
125 |
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.