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 Picture |
||
12 | { |
||
13 | /** |
||
14 | * Constants for regex patterns of pictures contents. |
||
15 | */ |
||
16 | const REGEX_INTEGER = '/^9\((\d+?)\)$/'; |
||
17 | const REGEX_MONEY = '/^9\((\d+?)\)V9\((\d+?)\)$/'; |
||
18 | const REGEX_STRING = '/^X\((\d+?)\)$/'; |
||
19 | |||
20 | /** |
||
21 | * Format a value from a picture. |
||
22 | * |
||
23 | * @param string $picture |
||
24 | * @param mixed $value |
||
25 | * @param array $meta |
||
26 | * @return mixed |
||
27 | */ |
||
28 | public function from($picture, $value, array $meta = []) |
||
43 | |||
44 | /** |
||
45 | * Execute a content limit or pad if is shorten then size. |
||
46 | * |
||
47 | * @param mixed $value |
||
48 | * @param integer $size |
||
49 | * @param array $meta |
||
50 | * @return string |
||
51 | */ |
||
52 | public function limit($value, $size, array $meta = []) |
||
62 | |||
63 | /** |
||
64 | * Execute a numeric pad (left with 0). |
||
65 | * |
||
66 | * @param mixed $number |
||
67 | * @param integer $size |
||
68 | * @return string |
||
69 | */ |
||
70 | public function numericPad($number, $size) |
||
76 | |||
77 | /** |
||
78 | * Execute a numeric pad (left with no 0). |
||
79 | * |
||
80 | * @param mixed $number |
||
81 | * @return integer |
||
82 | */ |
||
83 | public function numericTrim($number) |
||
87 | |||
88 | /** |
||
89 | * Parses a received picture to tokens. |
||
90 | * |
||
91 | * @param string $picture |
||
92 | * @param array $meta |
||
93 | * @return array |
||
94 | */ |
||
95 | public function parse($picture, array $meta = []) |
||
122 | |||
123 | /** |
||
124 | * Execute a string pad (right with spaces). |
||
125 | * |
||
126 | * @param mixed $string |
||
127 | * @param integer $size |
||
128 | * @return string |
||
129 | */ |
||
130 | public function stringPad($string, $size) |
||
134 | |||
135 | /** |
||
136 | * Execute a string trim (remove right with spaces). |
||
137 | * |
||
138 | * @param mixed $string |
||
139 | * @return string |
||
140 | */ |
||
141 | public function stringTrim($string) |
||
145 | |||
146 | /** |
||
147 | * Format a value to picture. |
||
148 | * |
||
149 | * @param string $picture |
||
150 | * @param mixed $value |
||
151 | * @param array $meta |
||
152 | * @return string |
||
153 | */ |
||
154 | public function to($picture, $value, array $meta = []) |
||
170 | |||
171 | /** |
||
172 | * Format a value from a date. |
||
173 | * |
||
174 | * @param mixed $value |
||
175 | * @return \DateTime|false |
||
176 | */ |
||
177 | protected function fromTypeDate($value) |
||
181 | |||
182 | /** |
||
183 | * Format a value from a money. |
||
184 | * |
||
185 | * @param numeric $value |
||
186 | * @param array $meta |
||
187 | * @return float |
||
188 | */ |
||
189 | protected function fromTypeMoney($value, array $meta = []) |
||
197 | |||
198 | /** |
||
199 | * Format a value to an auto date. |
||
200 | * |
||
201 | * @return \DateTime |
||
202 | */ |
||
203 | protected function toAutoDate() |
||
207 | |||
208 | /** |
||
209 | * Format a value to a default value. |
||
210 | * |
||
211 | * @param mixed $value |
||
212 | * @param array $meta |
||
213 | * @return mixed |
||
214 | */ |
||
215 | protected function toDefault($value, array $meta = []) |
||
229 | |||
230 | /** |
||
231 | * Format a value to an automatic default value. |
||
232 | * |
||
233 | * @param mixed $value |
||
234 | * @param array $meta |
||
235 | * @return mixed |
||
236 | */ |
||
237 | protected function toDefaultAuto($value, array $meta = []) |
||
247 | |||
248 | /** |
||
249 | * Format a value to a date. |
||
250 | * |
||
251 | * @param mixed $value |
||
252 | * @param array $meta |
||
253 | * @return string |
||
254 | */ |
||
255 | protected function toTypeDate($value, array $meta = []) |
||
269 | |||
270 | /** |
||
271 | * Format a value to a money. |
||
272 | * |
||
273 | * @param numeric $value |
||
274 | * @param array $meta |
||
275 | * @return string |
||
276 | */ |
||
277 | protected function toTypeMoney($value, array $meta = []) |
||
283 | |||
284 | /** |
||
285 | * Transliterate from UTF-8 to ASCII |
||
286 | * |
||
287 | * @param string $value |
||
288 | * @return string |
||
289 | */ |
||
290 | protected function transliterate($value) |
||
295 | } |
||
296 |
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.