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 | abstract class ModelAccessorByHandle extends ModelAccessor |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * @var ModelWithHandle[] |
||
25 | */ |
||
26 | protected $_cacheByHandle = []; |
||
27 | |||
28 | /** |
||
29 | * @return string |
||
30 | */ |
||
31 | public static function modelClassInstance(): string |
||
35 | |||
36 | /** |
||
37 | * @param $identifier |
||
38 | * @param string $toScenario |
||
39 | * @return Model|ModelWithHandle|null |
||
40 | */ |
||
41 | View Code Duplication | public function find($identifier, string $toScenario = null) |
|
53 | |||
54 | |||
55 | /******************************************* |
||
56 | * FIND/GET BY HANDLE |
||
57 | *******************************************/ |
||
58 | |||
59 | /** |
||
60 | * @param string $handle |
||
61 | * @param string|null $toScenario |
||
62 | * @return ModelWithHandle|null |
||
63 | */ |
||
64 | View Code Duplication | public function findByHandle(string $handle, string $toScenario = null) |
|
89 | |||
90 | /** |
||
91 | * @param string $handle |
||
92 | * @param string|null $toScenario |
||
93 | * @return ModelWithHandle|null |
||
94 | * @throws ModelNotFoundException |
||
95 | */ |
||
96 | View Code Duplication | public function getByHandle(string $handle, string $toScenario = null): ModelWithHandle |
|
108 | |||
109 | /** |
||
110 | * @param string $handle |
||
111 | * @param string|null $toScenario |
||
112 | * @return ModelWithHandle|null |
||
113 | */ |
||
114 | public function freshFindByHandle(string $handle, string $toScenario = null) |
||
128 | |||
129 | /** |
||
130 | * @param string $handle |
||
131 | * @param string|null $toScenario |
||
132 | * @return ModelWithHandle |
||
133 | * @throws ModelNotFoundException |
||
134 | */ |
||
135 | View Code Duplication | public function freshGetByHandle(string $handle, string $toScenario = null): ModelWithHandle |
|
147 | |||
148 | /******************************************* |
||
149 | * CACHE |
||
150 | *******************************************/ |
||
151 | |||
152 | /** |
||
153 | * @inheritdoc |
||
154 | * @return Model|ModelWithHandle|null |
||
155 | */ |
||
156 | public function findCache($identifier) |
||
168 | |||
169 | /** |
||
170 | * @inheritdoc |
||
171 | */ |
||
172 | public function addToCache(Model $model) |
||
184 | |||
185 | /** |
||
186 | * Find an existing cache by handle |
||
187 | * |
||
188 | * @param string $handle |
||
189 | * @return null |
||
190 | */ |
||
191 | public function findCacheByHandle(string $handle) |
||
202 | |||
203 | /** |
||
204 | * Identify whether in cache by handle |
||
205 | * |
||
206 | * @param string $handle |
||
207 | * @return bool |
||
208 | */ |
||
209 | private function isCachedByHandle(string $handle): bool |
||
213 | |||
214 | /** |
||
215 | * @param ModelWithHandle $model |
||
216 | * @return static |
||
217 | */ |
||
218 | protected function cacheByHandle(ModelWithHandle $model) |
||
232 | |||
233 | /** |
||
234 | * @param Record $record |
||
235 | * @return Model|ModelWithHandle|null |
||
236 | */ |
||
237 | View Code Duplication | public function findCacheByRecord(Record $record) |
|
254 | |||
255 | /******************************************* |
||
256 | * RECORD BY HANDLE |
||
257 | *******************************************/ |
||
258 | |||
259 | /** |
||
260 | * @param string $handle |
||
261 | * @param string|null $toScenario |
||
262 | * @return RecordWithHandle|null |
||
263 | */ |
||
264 | protected function findRecordByHandle(string $handle, string $toScenario = null) |
||
275 | |||
276 | |||
277 | /** |
||
278 | * @param Model $model |
||
279 | * @param bool $mirrorScenario |
||
280 | * @return RecordWithHandle|Record |
||
281 | */ |
||
282 | public function toRecord(Model $model, bool $mirrorScenario = true): Record |
||
305 | |||
306 | /******************************************* |
||
307 | * EXCEPTIONS |
||
308 | *******************************************/ |
||
309 | |||
310 | /** |
||
311 | * @param string|null $handle |
||
312 | * @throws ModelNotFoundException |
||
313 | */ |
||
314 | protected function notFoundByHandleException(string $handle = null) |
||
325 | |||
326 | } |
||
327 |
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.