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 | trait CheckboxTrait { |
||
14 | |||
15 | abstract public function addToPropertyCtrl($name, $value, $typeCtrl); |
||
16 | |||
17 | public function setType($checkboxType) { |
||
20 | |||
21 | |||
22 | /** |
||
23 | * Attach $this to $selector and fire $action |
||
24 | * @param string $selector jquery selector of the associated element |
||
25 | * @param string $action action to execute : check, uncheck or NULL for toggle |
||
26 | * @return HtmlFormField |
||
27 | */ |
||
28 | public function attachEvent($selector, $action=NULL) { |
||
31 | |||
32 | /** |
||
33 | * Attach $this to an array of $action=>$selector |
||
34 | * @param array $events associative array of events to attach ex : ["#bt-toggle","check"=>"#bt-check","uncheck"=>"#bt-uncheck"] |
||
35 | * @return HtmlFormField |
||
36 | */ |
||
37 | public function attachEvents($events=array()) { |
||
40 | |||
41 | public function getField(){ |
||
44 | |||
45 | public function getHtmlCk(){ |
||
48 | |||
49 | public function setName($name){ |
||
53 | |||
54 | public function getDataField(){ |
||
60 | |||
61 | /** |
||
62 | * Check the checkbox |
||
63 | * @param boolean $value |
||
64 | * @return HtmlFormField |
||
65 | */ |
||
66 | View Code Duplication | public function setChecked($value=true){ |
|
74 | |||
75 | } |
||
76 |
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.