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 | * @return $this |
||
| 13 | */ |
||
| 14 | public function checked($value = null, $oKey = null) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Pobiera atrybut checked dla html |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | public function getChecked() { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Dla pól checkbox value powinno ustawiać checked na true lub false a nie zastepowac warość |
||
| 104 | * @param null $value |
||
| 105 | * @param null $key |
||
| 106 | * @return null|string |
||
| 107 | */ |
||
| 108 | public function value($value = null, $key = null) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Jeżeli chcemy zastąpić wartość |
||
| 117 | * @param null $value |
||
| 118 | * @param null $key |
||
| 119 | */ |
||
| 120 | public function setValue($value = null, $key = null) { |
||
| 123 | |||
| 124 | } |
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: