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 |
||
| 4 | trait checkedTrait |
||
| 5 | { |
||
| 6 | protected $checked = false; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Sprawdza czy pole powinno być zaznaczone |
||
| 10 | * @param string|array $value |
||
| 11 | * @param string $oKey |
||
| 12 | * @param string $oName ???? |
||
| 13 | * @return $this |
||
| 14 | */ |
||
| 15 | public function checked($value = null, $oKey = null, $oName = null) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Pobiera atrybut checked dla html |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | public function getChecked() { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Dla pól checkbox value powinno ustawiać checked na true lub false a nie zastepowac warość |
||
| 105 | * @param null $value |
||
| 106 | * @param null $key |
||
| 107 | * @return null|string |
||
| 108 | */ |
||
| 109 | public function value($value = null, $key = null) { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Jeżeli chcemy zastąpić wartość |
||
| 118 | * @param null $value |
||
| 119 | * @param null $key |
||
| 120 | */ |
||
| 121 | public function setValue($value = null, $key = null) { |
||
| 124 | |||
| 125 | } |
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: