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 |
||
11 | class TextExtension extends AbstractExtension |
||
12 | { |
||
13 | /** |
||
14 | * Return extension name |
||
15 | * |
||
16 | * @return string |
||
17 | */ |
||
18 | public function getName() |
||
19 | { |
||
20 | return 'jasny/text'; |
||
21 | } |
||
22 | |||
23 | /** |
||
24 | * {@inheritdoc} |
||
25 | */ |
||
26 | 25 | public function getFilters() |
|
36 | |||
37 | /** |
||
38 | * Add paragraph and line breaks to text. |
||
39 | * |
||
40 | * @param string $value |
||
41 | * @return string |
||
42 | */ |
||
43 | 2 | public function paragraph($value) |
|
52 | |||
53 | /** |
||
54 | * Get a single line |
||
55 | * |
||
56 | * @param string $value |
||
57 | * @param int $line Line number (starts at 1) |
||
58 | * @return string |
||
59 | */ |
||
60 | 4 | public function line($value, $line = 1) |
|
70 | |||
71 | /** |
||
72 | * Cut of text on a pagebreak. |
||
73 | * |
||
74 | * @param string $value |
||
75 | * @param string $replace |
||
76 | * @param string $break |
||
77 | * @return string |
||
78 | */ |
||
79 | 4 | View Code Duplication | public function less($value, $replace = '...', $break = '<!-- pagebreak -->') |
88 | |||
89 | /** |
||
90 | * Cut of text if it's to long. |
||
91 | * |
||
92 | * @param string $value |
||
93 | * @param int $length |
||
94 | * @param string $replace |
||
95 | * @return string |
||
96 | */ |
||
97 | 4 | View Code Duplication | public function truncate($value, $length, $replace = '...') |
105 | |||
106 | /** |
||
107 | * Linkify a HTTP(S) link. |
||
108 | * |
||
109 | * @param string $protocol 'http' or 'https' |
||
110 | * @param string $text |
||
111 | * @param array $links OUTPUT |
||
112 | * @param string $attr |
||
113 | * @param string $mode |
||
114 | * @return string |
||
115 | */ |
||
116 | 6 | protected function linkifyHttp($protocol, $text, array &$links, $attr, $mode) |
|
130 | |||
131 | /** |
||
132 | * Linkify a mail link. |
||
133 | * |
||
134 | * @param string $text |
||
135 | * @param array $links OUTPUT |
||
136 | * @param string $attr |
||
137 | * @return string |
||
138 | */ |
||
139 | 5 | protected function linkifyMail($text, array &$links, $attr) |
|
148 | |||
149 | |||
150 | /** |
||
151 | * Linkify a link. |
||
152 | * |
||
153 | * @param string $protocol |
||
154 | * @param string $text |
||
155 | * @param array $links OUTPUT |
||
156 | * @param string $attr |
||
157 | * @param string $mode |
||
158 | * @return string |
||
159 | */ |
||
160 | 4 | protected function linkifyOther($protocol, $text, array &$links, $attr, $mode) |
|
175 | |||
176 | /** |
||
177 | * Turn all URLs in clickable links. |
||
178 | * |
||
179 | * @param string $value |
||
180 | * @param array $protocols 'http'/'https', 'mail' and also 'ftp', 'scp', 'tel', etc |
||
181 | * @param array $attributes HTML attributes for the link |
||
182 | * @param string $mode normal or all |
||
183 | * @return string |
||
184 | */ |
||
185 | 11 | public function linkify($value, $protocols = ['http', 'mail'], array $attributes = [], $mode = 'normal') |
|
219 | } |
||
220 |
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.