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 |
||
| 13 | 1 | class VisitorRepository |
|
| 14 | { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var VisitorModel |
||
| 18 | */ |
||
| 19 | protected $visitorModel; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var MealModel |
||
| 23 | */ |
||
| 24 | protected $mealModel; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var BlockModel |
||
| 28 | */ |
||
| 29 | protected $blockModel; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var ProgramRepository |
||
| 33 | */ |
||
| 34 | protected $programRepository; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param VisitorModel $visitor |
||
| 38 | * @param MealModel $meal |
||
| 39 | * @param BlockModel $block |
||
| 40 | * @param ProgramRepository $program |
||
| 41 | */ |
||
| 42 | public function __construct( |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param int $meetingId |
||
| 56 | * @return self |
||
| 57 | */ |
||
| 58 | public function setMeeting(int $meetingId): self |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param int $id |
||
| 67 | * @return boolean |
||
| 68 | */ |
||
| 69 | public function setChecked(int $id): bool |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param int $id |
||
| 76 | * @return boolean |
||
| 77 | */ |
||
| 78 | public function setUnchecked(int $id): bool |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Return visitor by id |
||
| 85 | * |
||
| 86 | * @param int $id |
||
| 87 | * @return ActiveRow |
||
| 88 | */ |
||
| 89 | public function findById($id) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param int $id |
||
| 96 | * @return ArrayHash |
||
| 97 | */ |
||
| 98 | public function findExpandedById(int $id): ArrayHash |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Return visitor by guid |
||
| 115 | * |
||
| 116 | * @param string $guid |
||
| 117 | * @return Nette\Database\Table\ActiveRow|bool |
||
| 118 | */ |
||
| 119 | public function findByGuid($guid) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param string $guid |
||
| 126 | * @return array |
||
| 127 | */ |
||
| 128 | public function findExpandedByGuid(string $guid): array |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param string $value |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | public function findBySearch(string $value = ''): array |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param int $id |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | public function findRecipients($id): array |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param array $data |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | public function create($data) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param integer $id |
||
| 177 | * @param array $data |
||
| 178 | * @return integer |
||
| 179 | */ |
||
| 180 | View Code Duplication | public function update($id, $values) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param integer $id |
||
| 201 | * @param array $data |
||
| 202 | * @return integer |
||
| 203 | */ |
||
| 204 | View Code Duplication | public function updateByGuid($guid, $values) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @param integer $id |
||
| 226 | * @return boolean |
||
| 227 | */ |
||
| 228 | public function delete($id) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Counts visitors |
||
| 235 | * |
||
| 236 | * @return int |
||
| 237 | */ |
||
| 238 | public function count(): int |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param int $id |
||
| 245 | * @return string |
||
| 246 | * @throws \Exception |
||
| 247 | */ |
||
| 248 | public function payCostCharge($id) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param int $id |
||
| 255 | * @return string |
||
| 256 | * @throws \Exception |
||
| 257 | */ |
||
| 258 | public function payAdvanceCharge($id) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param string $name |
||
| 265 | * @param string $surname |
||
| 266 | * @param string $birthday |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | public function calculateCode4Bank(string $name, string $surname, string $birthday): string |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param int $visitorId |
||
| 278 | * @return array |
||
| 279 | */ |
||
| 280 | View Code Duplication | public function assembleFormPrograms(int $visitorId): array |
|
| 295 | |||
| 296 | /** |
||
| 297 | * @param array $data |
||
| 298 | * @param array $fields |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | protected function filterFields($data, array $fields) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param array $data |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | protected function filterProgramFields($data) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param string|DateTime $datetime |
||
| 327 | * @return DateTime |
||
| 328 | */ |
||
| 329 | protected function convertToDateTime($datetime): DateTime |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @return BlockModel |
||
| 340 | */ |
||
| 341 | protected function getBlockModel(): BlockModel |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param BlockModel $model |
||
| 348 | * @return $this |
||
| 349 | */ |
||
| 350 | protected function setBlockModel(BlockModel $model): self |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @return MealModel |
||
| 359 | */ |
||
| 360 | protected function getMealModel(): MealModel |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param MealModel $model |
||
| 367 | * @return $this |
||
| 368 | */ |
||
| 369 | protected function setMealModel(MealModel $model): self |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @return VisitorModel |
||
| 378 | */ |
||
| 379 | protected function getVisitorModel(): VisitorModel |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param VisitorModel $model |
||
| 386 | * @return self |
||
| 387 | */ |
||
| 388 | protected function setVisitorModel(VisitorModel $model): self |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @return ProgramRepository |
||
| 397 | */ |
||
| 398 | protected function getProgramRepository() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param ProgramRepository $repository |
||
| 405 | * @return self |
||
| 406 | */ |
||
| 407 | protected function setProgramRepository(ProgramRepository $repository): self |
||
| 413 | |||
| 414 | } |
||
| 415 |