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 |
||
| 16 | View Code Duplication | class JavaScript |
|
|
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @param $name |
||
| 20 | * @param null $function |
||
| 21 | * @return string |
||
| 22 | */ |
||
| 23 | public function button_to_function($name, $function = null) |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param $javascript |
||
| 30 | * @return mixed|string |
||
| 31 | */ |
||
| 32 | public function escape($javascript) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param $content |
||
| 42 | * @return string |
||
| 43 | */ |
||
| 44 | public function tag($content) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param $name |
||
| 51 | * @param $function |
||
| 52 | * @param null $html_options |
||
| 53 | * @return string |
||
| 54 | */ |
||
| 55 | public function link_to_function($name, $function, $html_options = null) |
||
| 59 | |||
| 60 | ///////////////////////////////////////////////////////////////////////////////////// |
||
| 61 | // Private functions |
||
| 62 | ///////////////////////////////////////////////////////////////////////////////////// |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param $option |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | public function _array_or_string_for_javascript($option) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param $options |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | public function _options_for_javascript($options) |
||
| 104 | } |
||
| 105 |
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.