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 |
||
8 | trait CheckboxTrait { |
||
9 | |||
10 | public abstract function addToPropertyCtrl($name, $value, $typeCtrl); |
||
11 | |||
12 | public function setType($checkboxType) { |
||
15 | |||
16 | |||
17 | /** |
||
18 | * Attach $this to $selector and fire $action |
||
19 | * @param string $selector jquery selector of the associated element |
||
20 | * @param string $action action to execute : check, uncheck or NULL for toggle |
||
21 | * @return \Ajax\semantic\html\collections\form\AbstractHtmlFormRadioCheckbox |
||
22 | */ |
||
23 | public function attachEvent($selector, $action=NULL) { |
||
26 | |||
27 | /** |
||
28 | * Attach $this to an array of $action=>$selector |
||
29 | * @param array $events associative array of events to attach ex : ["#bt-toggle","check"=>"#bt-check","uncheck"=>"#bt-uncheck"] |
||
30 | * @return \Ajax\semantic\html\collections\form\AbstractHtmlFormRadioCheckbox |
||
31 | */ |
||
32 | public function attachEvents($events=array()) { |
||
35 | |||
36 | public function getField(){ |
||
39 | |||
40 | public function getHtmlCk(){ |
||
43 | |||
44 | public function getDataField(){ |
||
50 | |||
51 | /** |
||
52 | * Check the checkbox |
||
53 | * @param boolean $value |
||
54 | * @return \Ajax\semantic\html\collections\form\traits\CheckboxTrait |
||
55 | */ |
||
56 | View Code Duplication | public function setChecked($value=true){ |
|
64 | |||
65 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: