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 |
||
| 20 | class TimelogController extends BaseController |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var TimelogRepository |
||
| 24 | */ |
||
| 25 | public $timelog; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var Employee |
||
| 29 | */ |
||
| 30 | public $employee; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var int |
||
| 34 | */ |
||
| 35 | public $rows_per_page = 10; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * TimelogController constructor. |
||
| 39 | * |
||
| 40 | * @param Employee $employee |
||
| 41 | */ |
||
| 42 | public function __construct(Employee $employee) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param Request $request |
||
| 50 | * |
||
| 51 | * @return \Dingo\Api\Http\Response |
||
| 52 | * |
||
| 53 | * @author Bertrand Kintanar <[email protected]> |
||
| 54 | */ |
||
| 55 | public function index(Request $request) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param $input |
||
| 95 | * @param string $format |
||
| 96 | * |
||
| 97 | * @return static |
||
| 98 | * |
||
| 99 | * @author Bertrand Kintanar <[email protected]> |
||
| 100 | */ |
||
| 101 | private function startOfMonth($input, $format = 'Y-m-d H:i:s') |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param $input |
||
| 110 | * @param string $format |
||
| 111 | * |
||
| 112 | * @return static |
||
| 113 | * |
||
| 114 | * @author Bertrand Kintanar <[email protected]> |
||
| 115 | */ |
||
| 116 | private function endOfMonth($input, $format = 'Y-m-d H:i:s') |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param $start |
||
| 125 | * @param $end |
||
| 126 | * |
||
| 127 | * @return string |
||
| 128 | * |
||
| 129 | * @author Bertrand Kintanar <[email protected]> |
||
| 130 | */ |
||
| 131 | private function dateRangeFormat($start, $end) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param $timelogs |
||
| 149 | * |
||
| 150 | * @return int |
||
| 151 | * |
||
| 152 | * @author Bertrand Kintanar <[email protected]> |
||
| 153 | */ |
||
| 154 | private function totalHours($timelogs) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Setup table for timelogs. |
||
| 166 | * |
||
| 167 | * @param $timelogs |
||
| 168 | * |
||
| 169 | * @return array |
||
| 170 | * |
||
| 171 | * @author Bertrand Kintanar <[email protected]> |
||
| 172 | */ |
||
| 173 | protected function setupDataTable($timelogs) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return \Dingo\Api\Http\Response |
||
| 193 | */ |
||
| 194 | public function serverTime() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Alert configuration of time in. |
||
| 203 | * |
||
| 204 | * @return string |
||
| 205 | * |
||
| 206 | * @author Harlequin Doyon <[email protected]> |
||
| 207 | * @author Bertrand Kintanar <[email protected]> |
||
| 208 | */ |
||
| 209 | View Code Duplication | public function attemptTimeIn() |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Alert HTML helper. |
||
| 230 | * |
||
| 231 | * @param string $msg |
||
| 232 | * @param string $note |
||
| 233 | * |
||
| 234 | * @return string |
||
| 235 | * |
||
| 236 | * @author Harlequin Doyon <[email protected]> |
||
| 237 | * @author Bertrand Kintanar <[email protected]> |
||
| 238 | */ |
||
| 239 | private function html($msg, $note = '') |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Alert configuration of time out. |
||
| 255 | * |
||
| 256 | * @return string |
||
| 257 | * |
||
| 258 | * @author Harlequin Doyon <[email protected]> |
||
| 259 | * @author Bertrand Kintanar <[email protected]> |
||
| 260 | */ |
||
| 261 | View Code Duplication | public function attemptTimeOut() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Save time in log in the database. |
||
| 282 | * |
||
| 283 | * @param Timelog $timelog |
||
| 284 | * |
||
| 285 | * @return \Dingo\Api\Http\Response |
||
| 286 | * |
||
| 287 | * @author Harlequin Doyon <[email protected]> |
||
| 288 | * @author Bertrand Kintanar <[email protected]> |
||
| 289 | */ |
||
| 290 | public function timeIn(Timelog $timelog) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Save time out log in the database. |
||
| 310 | * |
||
| 311 | * @param Timelog $timelog |
||
| 312 | * @param Request $request |
||
| 313 | * |
||
| 314 | * @return \Dingo\Api\Http\Response |
||
| 315 | * |
||
| 316 | * @author Harlequin Doyon <[email protected]> |
||
| 317 | * @author Bertrand Kintanar <[email protected]> |
||
| 318 | */ |
||
| 319 | public function timeOut(Timelog $timelog, Request $request) |
||
| 348 | } |
||
| 349 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: