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 |
||
9 | abstract class AbstractCheckbox extends HtmlSemDoubleElement { |
||
10 | protected $_params=[]; |
||
11 | |||
12 | public function __construct($identifier, $name=NULL, $label=NULL, $value=NULL, $inputType="checkbox", $type="checkbox") { |
||
21 | |||
22 | View Code Duplication | public function setChecked($value=true){ |
|
30 | |||
31 | public function setType($checkboxType) { |
||
34 | |||
35 | public function setLabel($label) { |
||
44 | |||
45 | public function setField($field) { |
||
48 | |||
49 | /** |
||
50 | * Returns the label or null |
||
51 | * @return mixed |
||
52 | */ |
||
53 | public function getLabel() { |
||
57 | |||
58 | /** |
||
59 | * Return the field |
||
60 | * @return mixed |
||
61 | */ |
||
62 | public function getField() { |
||
65 | |||
66 | /** |
||
67 | * puts the label before or behind |
||
68 | */ |
||
69 | public function swapLabel() { |
||
74 | |||
75 | public function setReadonly() { |
||
79 | |||
80 | /** |
||
81 | * Attach $this to $selector and fire $action |
||
82 | * @param string $selector jquery selector of the associated element |
||
83 | * @param string $action action to execute : check, uncheck or NULL for toggle |
||
84 | * @return \Ajax\semantic\html\collections\form\AbstractHtmlFormRadioCheckbox |
||
85 | */ |
||
86 | public function attachEvent($selector, $action=NULL) { |
||
95 | |||
96 | /** |
||
97 | * Attach $this to an array of $action=>$selector |
||
98 | * @param array $events associative array of events to attach ex : ["#bt-toggle","check"=>"#bt-check","uncheck"=>"#bt-uncheck"] |
||
99 | * @return \Ajax\semantic\html\collections\form\AbstractHtmlFormRadioCheckbox |
||
100 | */ |
||
101 | public function attachEvents($events=array()) { |
||
109 | |||
110 | public function setFitted() { |
||
113 | |||
114 | public function setOnChecked($jsCode){ |
||
118 | |||
119 | public function setOnUnchecked($jsCode){ |
||
123 | |||
124 | public function setOnChange($jsCode){ |
||
128 | |||
129 | public function run(JsUtils $js) { |
||
133 | } |
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.