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 |
||
| 34 | class TimelogController extends BaseController |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var TimelogRepository |
||
| 38 | */ |
||
| 39 | public $timelog; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var Employee |
||
| 43 | */ |
||
| 44 | public $employee; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var int |
||
| 48 | */ |
||
| 49 | public $rows_per_page = 10; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * TimelogController constructor. |
||
| 53 | * |
||
| 54 | * @param Employee $employee |
||
| 55 | */ |
||
| 56 | 6 | public function __construct(Employee $employee) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * @param Request $request |
||
| 64 | * |
||
| 65 | * @return \Dingo\Api\Http\Response |
||
| 66 | * |
||
| 67 | * @author Bertrand Kintanar <[email protected]> |
||
| 68 | */ |
||
| 69 | public function index(Request $request) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param $input |
||
| 109 | * @param string $format |
||
| 110 | * |
||
| 111 | * @return static |
||
| 112 | * |
||
| 113 | * @author Bertrand Kintanar <[email protected]> |
||
| 114 | */ |
||
| 115 | private function startOfMonth($input, $format = 'Y-m-d H:i:s') |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param $input |
||
| 124 | * @param string $format |
||
| 125 | * |
||
| 126 | * @return static |
||
| 127 | * |
||
| 128 | * @author Bertrand Kintanar <[email protected]> |
||
| 129 | */ |
||
| 130 | 4 | private function endOfMonth($input, $format = 'Y-m-d H:i:s') |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @param $start |
||
| 139 | * @param $end |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | * |
||
| 143 | * @author Bertrand Kintanar <[email protected]> |
||
| 144 | */ |
||
| 145 | private function dateRangeFormat($start, $end) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param $timelogs |
||
| 163 | * |
||
| 164 | * @return int |
||
| 165 | * |
||
| 166 | * @author Bertrand Kintanar <[email protected]> |
||
| 167 | */ |
||
| 168 | private function totalHours($timelogs) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Setup table for timelogs. |
||
| 180 | * |
||
| 181 | * @param $timelogs |
||
| 182 | * |
||
| 183 | * @return array |
||
| 184 | * |
||
| 185 | * @author Bertrand Kintanar <[email protected]> |
||
| 186 | */ |
||
| 187 | protected function setupDataTable($timelogs) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return \Dingo\Api\Http\Response |
||
| 207 | */ |
||
| 208 | public function serverTime() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Alert configuration of time in. |
||
| 217 | * |
||
| 218 | * @return string |
||
| 219 | * |
||
| 220 | * @author Harlequin Doyon <[email protected]> |
||
| 221 | * @author Bertrand Kintanar <[email protected]> |
||
| 222 | */ |
||
| 223 | 2 | View Code Duplication | public function attemptTimeIn() |
| 241 | |||
| 242 | /** |
||
| 243 | * Alert HTML helper. |
||
| 244 | * |
||
| 245 | * @param string $msg |
||
| 246 | * @param string $note |
||
| 247 | * |
||
| 248 | * @return string |
||
| 249 | * |
||
| 250 | * @author Harlequin Doyon <[email protected]> |
||
| 251 | * @author Bertrand Kintanar <[email protected]> |
||
| 252 | */ |
||
| 253 | 2 | private function html($msg, $note = '') |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Alert configuration of time out. |
||
| 269 | * |
||
| 270 | * @return string |
||
| 271 | * |
||
| 272 | * @author Harlequin Doyon <[email protected]> |
||
| 273 | * @author Bertrand Kintanar <[email protected]> |
||
| 274 | */ |
||
| 275 | View Code Duplication | public function attemptTimeOut() |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Save time in log in the database. |
||
| 296 | * |
||
| 297 | * @param Timelog $timelog |
||
| 298 | * |
||
| 299 | * @return \Dingo\Api\Http\Response |
||
| 300 | * |
||
| 301 | * @author Harlequin Doyon <[email protected]> |
||
| 302 | * @author Bertrand Kintanar <[email protected]> |
||
| 303 | */ |
||
| 304 | 2 | public function timeIn(Timelog $timelog) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Save time out log in the database. |
||
| 324 | * |
||
| 325 | * @param Timelog $timelog |
||
| 326 | * @param Request $request |
||
| 327 | * |
||
| 328 | * @return \Dingo\Api\Http\Response |
||
| 329 | * |
||
| 330 | * @author Harlequin Doyon <[email protected]> |
||
| 331 | * @author Bertrand Kintanar <[email protected]> |
||
| 332 | */ |
||
| 333 | public function timeOut(Timelog $timelog, Request $request) |
||
| 360 | } |
||
| 361 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.