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 |
||
13 | View Code Duplication | class sfWidgetFormI18nSelect2ChoiceCountry extends sfWidgetFormI18nChoiceCountry |
|
|
|||
14 | { |
||
15 | /** |
||
16 | * Configures the current widget. |
||
17 | * |
||
18 | * Available options: |
||
19 | * |
||
20 | * * url: The URL to call to get the choices to use (required) |
||
21 | * * config: A JavaScript array that configures the JQuery autocompleter widget |
||
22 | * * value_callback: A callback that converts the value before it is displayed |
||
23 | * |
||
24 | * @param array $options An array of options |
||
25 | * @param array $attributes An array of default HTML attributes |
||
26 | * |
||
27 | * @see sfWidgetForm |
||
28 | */ |
||
29 | protected function configure($options = array(), $attributes = array()) |
||
39 | |||
40 | public function getChoices() |
||
50 | |||
51 | /** |
||
52 | * @param string $name The element name |
||
53 | * @param string $value The date displayed in this widget |
||
54 | * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes |
||
55 | * @param array $errors An array of errors for the field |
||
56 | * |
||
57 | * @return string An HTML tag string |
||
58 | * |
||
59 | * @see sfWidgetForm |
||
60 | */ |
||
61 | public function render($name, $value = null, $attributes = array(), $errors = array()) |
||
89 | |||
90 | /** |
||
91 | * Gets the stylesheet paths associated with the widget. |
||
92 | * |
||
93 | * @return array An array of stylesheet paths |
||
94 | */ |
||
95 | public function getStylesheets() |
||
99 | |||
100 | /** |
||
101 | * Gets the JavaScript paths associated with the widget. |
||
102 | * |
||
103 | * @return array An array of JavaScript paths |
||
104 | */ |
||
105 | public function getJavascripts() |
||
109 | } |
||
110 |
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.