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 |
||
24 | trait ModelById |
||
25 | { |
||
26 | |||
27 | use Model; |
||
28 | |||
29 | /** |
||
30 | * @var ModelWithId[] |
||
31 | */ |
||
32 | protected $_cacheById = []; |
||
33 | |||
34 | /** |
||
35 | * @param Record $record |
||
36 | * @param string|null $toScenario |
||
37 | * @return BaseModel|ModelWithId |
||
38 | */ |
||
39 | abstract protected function findByRecord(Record $record, string $toScenario = null): BaseModel; |
||
40 | |||
41 | /** |
||
42 | * @param array $config |
||
43 | * @param string|null $toScenario |
||
44 | * @return BaseModel|ModelWithId |
||
45 | */ |
||
46 | abstract public function create($config = [], string $toScenario = null): BaseModel; |
||
47 | |||
48 | /******************************************* |
||
49 | * FIND/GET BY ID |
||
50 | *******************************************/ |
||
51 | |||
52 | /** |
||
53 | * @param int $id |
||
54 | * @param string|null $toScenario |
||
55 | * @return ModelWithId|null |
||
56 | */ |
||
57 | View Code Duplication | public function findById(int $id, string $toScenario = null) |
|
81 | |||
82 | /** |
||
83 | * @param int $id |
||
84 | * @param string|null $toScenario |
||
85 | * @return ModelWithId|null |
||
86 | * @throws ModelNotFoundException |
||
87 | */ |
||
88 | View Code Duplication | public function getById(int $id, string $toScenario = null): ModelWithId |
|
100 | |||
101 | /** |
||
102 | * @param int $id |
||
103 | * @param string|null $toScenario |
||
104 | * @return ModelWithId|null |
||
105 | */ |
||
106 | public function freshFindById(int $id, string $toScenario = null) |
||
119 | |||
120 | /** |
||
121 | * @param int $id |
||
122 | * @param string|null $toScenario |
||
123 | * @return ModelWithId |
||
124 | * @throws ModelNotFoundException |
||
125 | */ |
||
126 | View Code Duplication | public function freshGetById(int $id, string $toScenario = null): ModelWithId |
|
138 | |||
139 | /******************************************* |
||
140 | * CACHE |
||
141 | *******************************************/ |
||
142 | |||
143 | /** |
||
144 | * Find an existing cache by id |
||
145 | * |
||
146 | * @param int $id |
||
147 | * @return null |
||
148 | */ |
||
149 | public function findCacheById(int $id) |
||
160 | |||
161 | /** |
||
162 | * Identify whether in cache by id |
||
163 | * |
||
164 | * @param int $id |
||
165 | * @return bool |
||
166 | */ |
||
167 | private function isCachedById(int $id): bool |
||
171 | |||
172 | |||
173 | /** |
||
174 | * @param ModelWithId $model |
||
175 | * @return static |
||
176 | */ |
||
177 | protected function cacheById(ModelWithId $model) |
||
195 | |||
196 | /** |
||
197 | * @param RecordWithId $record |
||
198 | * @return ModelWithId|null |
||
199 | */ |
||
200 | protected function findCacheByRecordById(RecordWithId $record) |
||
212 | |||
213 | /******************************************* |
||
214 | * RECORD BY ID |
||
215 | *******************************************/ |
||
216 | |||
217 | /** |
||
218 | * @param int $id |
||
219 | * @param string|null $toScenario |
||
220 | * @return RecordWithId|null |
||
221 | */ |
||
222 | public function findRecordById(int $id, string $toScenario = null) |
||
233 | |||
234 | /** |
||
235 | * @param int $id |
||
236 | * @param string|null $toScenario |
||
237 | * @return RecordWithId|null |
||
238 | */ |
||
239 | public function getRecordById(int $id, string $toScenario = null) |
||
251 | |||
252 | |||
253 | /** |
||
254 | * @param ModelWithId $model |
||
255 | * @param bool $mirrorScenario |
||
256 | * @return RecordWithId |
||
257 | */ |
||
258 | protected function toRecordById(ModelWithId $model, bool $mirrorScenario = true): RecordWithId |
||
272 | |||
273 | /******************************************* |
||
274 | * EXCEPTIONS |
||
275 | *******************************************/ |
||
276 | |||
277 | /** |
||
278 | * @param int|null $id |
||
279 | * @throws ModelNotFoundException |
||
280 | */ |
||
281 | protected function notFoundByIdException(int $id = null) |
||
292 | |||
293 | /** |
||
294 | * @param int|null $id |
||
295 | * @throws RecordNotFoundException |
||
296 | */ |
||
297 | protected function notFoundRecordByIdException(int $id = null) |
||
308 | |||
309 | } |
||
310 |
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.