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 |
||
| 10 | class ProgramRepository |
||
| 11 | { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var VisitorModel |
||
| 15 | */ |
||
| 16 | protected $visitorModel; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var ProgramModel |
||
| 20 | */ |
||
| 21 | protected $programModel; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param VisitorModel $visitorModel |
||
| 25 | * @param ProgramModel $programModel |
||
| 26 | */ |
||
| 27 | public function __construct( |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param int $meetingId |
||
| 37 | */ |
||
| 38 | public function setMeetingId(int $meetingId): self |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @return array |
||
| 48 | */ |
||
| 49 | public function all(): array |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param int $id |
||
| 56 | * @return Nette\Database\Table\ActiveRow |
||
| 57 | */ |
||
| 58 | public function find(int $id): ActiveRow |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param int $visitorId |
||
| 65 | * @return array |
||
| 66 | */ |
||
| 67 | public function findByVisitorId(int $visitorId): array |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param int $blockId |
||
| 74 | * @return array |
||
| 75 | */ |
||
| 76 | public function findByBlockId(int $blockId): array |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param int $programId |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | public function findTutor(int $programId): ActiveRow |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param int $programId |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | public function findVisitors(int $programId): array |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param Nette\Utils\ArrayHash $program |
||
| 101 | * @return boolean |
||
| 102 | */ |
||
| 103 | public function create(ArrayHash $program) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param Nette\Utils\ArrayHash $program |
||
| 112 | * @return boolean |
||
| 113 | */ |
||
| 114 | public function update(int $id, ArrayHash $program) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param int $id |
||
| 123 | * @return boolean |
||
| 124 | */ |
||
| 125 | public function delete(int $id) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param int $programId |
||
| 132 | * @return int |
||
| 133 | */ |
||
| 134 | public function countVisitors(int $programId): int |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param int $visitorId |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | View Code Duplication | public function assembleFormPrograms(int $visitorId): array |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @param Nette\Utils\ArrayHash $program |
||
| 161 | * @return Nette\Utils\ArrayHash |
||
| 162 | */ |
||
| 163 | protected function transformDisplayInRegValue(ArrayHash $program): ArrayHash |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @return ProgramModel |
||
| 176 | */ |
||
| 177 | protected function getProgramModel() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param ProgramModel $model |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | protected function setProgramModel(ProgramModel $model): self |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return VisitorModel |
||
| 195 | */ |
||
| 196 | protected function getVisitorModel(): VisitorModel |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param VisitorModel $model |
||
| 203 | * @return self |
||
| 204 | */ |
||
| 205 | protected function setVisitorModel(VisitorModel $model): self |
||
| 211 | |||
| 212 | } |
||
| 213 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.