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 |
||
| 19 | class DateRangePicker extends Filter |
||
| 20 | { |
||
| 21 | protected $js_options; |
||
| 22 | |||
| 23 | protected $use_clear_button; |
||
| 24 | |||
| 25 | protected $template = '*.components.filters.date_range_picker'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Returns javascript options |
||
| 29 | * |
||
| 30 | * Available options: |
||
| 31 | * @see https://github.com/dangrossman/bootstrap-daterangepicker#options |
||
| 32 | * |
||
| 33 | * @return array |
||
| 34 | */ |
||
| 35 | public function getJsOptions() |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Sets javascript options |
||
| 45 | * |
||
| 46 | * Available options: |
||
| 47 | * @see https://github.com/dangrossman/bootstrap-daterangepicker#options |
||
| 48 | * |
||
| 49 | * @param array $options |
||
| 50 | */ |
||
| 51 | public function setJsOptions($options) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Returns true if form must be submitted immediately |
||
| 59 | * when filter value selected. |
||
| 60 | * |
||
| 61 | * @return bool |
||
| 62 | */ |
||
| 63 | public function isSubmittedOnChange() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Allows to submit form immediately when filter value selected. |
||
| 70 | * |
||
| 71 | * @param bool $isSubmittedOnChange |
||
| 72 | * @return $this |
||
| 73 | */ |
||
| 74 | public function setSubmittedOnChange($isSubmittedOnChange) |
||
| 79 | |||
| 80 | View Code Duplication | public function getStartValue() |
|
| 92 | |||
| 93 | |||
| 94 | View Code Duplication | public function getEndValue() |
|
| 106 | |||
| 107 | public function getValue() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Returns true if non-empty value specified for the filter. |
||
| 114 | * |
||
| 115 | * @return bool |
||
| 116 | */ |
||
| 117 | protected function hasValue() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns default javascript options |
||
| 125 | * |
||
| 126 | * Available options: |
||
| 127 | * @see https://github.com/dangrossman/bootstrap-daterangepicker#options |
||
| 128 | * |
||
| 129 | * @return array |
||
| 130 | */ |
||
| 131 | protected function getDefaultJsOptions() |
||
| 179 | |||
| 180 | public function getDefaultStartValue() |
||
| 184 | |||
| 185 | public function getDefaultEndValue() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Returns default filter value as [$startDate, $endDate] |
||
| 192 | * |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | public function getDefaultValue() |
||
| 202 | |||
| 203 | public function getStartInputName() |
||
| 208 | |||
| 209 | public function getEndInputName() |
||
| 214 | |||
| 215 | public function getFilteringFunc() |
||
| 222 | |||
| 223 | protected function getDefaultFilteringFunc() |
||
| 230 | } |
||
| 231 |
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: