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 |
||
19 | class HtmlForm extends HtmlSemCollection { |
||
20 | |||
21 | use FieldsTrait; |
||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $_fields; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $_validationParams; |
||
31 | |||
32 | public function __construct($identifier, $elements=array()) { |
||
40 | |||
41 | /** |
||
42 | * @param string $title |
||
43 | * @param number $niveau |
||
44 | * @param string $dividing |
||
45 | * @return HtmlHeader |
||
46 | */ |
||
47 | public function addHeader($title, $niveau=1, $dividing=true) { |
||
53 | |||
54 | /** |
||
55 | * @param string $caption |
||
56 | * @return \Ajax\semantic\html\collections\form\HtmlForm |
||
57 | */ |
||
58 | public function addDivider($caption=NULL){ |
||
61 | |||
62 | public function addFields($fields=NULL, $label=NULL) { |
||
85 | |||
86 | public function addItem($item) { |
||
93 | |||
94 | public function getField($index) { |
||
102 | |||
103 | /** |
||
104 | * automatically divide fields to be equal width |
||
105 | * @return \Ajax\semantic\html\collections\form\HtmlForm |
||
106 | */ |
||
107 | public function setEqualWidth() { |
||
110 | |||
111 | /** |
||
112 | * Adds a field (alias for addItem) |
||
113 | * @param HtmlFormField $field |
||
114 | * @return \Ajax\common\html\HtmlDoubleElement |
||
115 | */ |
||
116 | public function addField($field) { |
||
119 | |||
120 | public function addFieldRule($index,$type,$prompt=NULL,$value=NULL){ |
||
127 | |||
128 | /** |
||
129 | * |
||
130 | * @param string $identifier |
||
131 | * @param string $content |
||
132 | * @param string $header |
||
133 | * @param string $icon |
||
134 | * @param string $type |
||
135 | * @return \Ajax\semantic\html\collections\HtmlMessage |
||
136 | */ |
||
137 | public function addMessage($identifier, $content, $header=NULL, $icon=NULL, $type=NULL) { |
||
147 | |||
148 | public function run(JsUtils $js) { |
||
168 | |||
169 | public function setLoading() { |
||
172 | |||
173 | public function addErrorMessage(){ |
||
176 | |||
177 | public function jsState($state) { |
||
180 | |||
181 | public function setValidationParams(array $_validationParams) { |
||
185 | |||
186 | } |
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.