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:
Complex classes like Model often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Model, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | abstract class Model extends Component |
||
27 | { |
||
28 | |||
29 | use traits\Model; |
||
30 | |||
31 | /** |
||
32 | * @var BaseModel[] |
||
33 | */ |
||
34 | protected $_cacheAll; |
||
35 | |||
36 | |||
37 | /******************************************* |
||
38 | * MODEL CLASSES |
||
39 | *******************************************/ |
||
40 | |||
41 | /** |
||
42 | * @return string |
||
43 | */ |
||
44 | public abstract static function modelClass(): string; |
||
45 | |||
46 | /** |
||
47 | * @return string |
||
48 | */ |
||
49 | public static function modelClassInstance(): string |
||
53 | |||
54 | /******************************************* |
||
55 | * CREATE |
||
56 | *******************************************/ |
||
57 | |||
58 | /** |
||
59 | * @param array $config |
||
60 | * @param string|null $toScenario |
||
61 | * @throws InvalidConfigException |
||
62 | * @return BaseModel |
||
63 | */ |
||
64 | View Code Duplication | public function create($config = [], string $toScenario = null): BaseModel |
|
87 | |||
88 | /** |
||
89 | * @param Record $record |
||
90 | * @param string|null $toScenario |
||
91 | * @throws InvalidConfigException |
||
92 | * @return BaseModel |
||
93 | */ |
||
94 | protected function createFromRecord(Record $record, string $toScenario = null): BaseModel |
||
113 | |||
114 | /******************************************* |
||
115 | * FIND/GET ALL |
||
116 | *******************************************/ |
||
117 | |||
118 | /** |
||
119 | * @param string $toScenario |
||
120 | * @return BaseModel[] |
||
121 | */ |
||
122 | View Code Duplication | public function findAll(string $toScenario = null) |
|
146 | |||
147 | /** |
||
148 | * @param string $toScenario |
||
149 | * @return BaseModel[] |
||
150 | * @throws ModelNotFoundException |
||
151 | */ |
||
152 | public function getAll(string $toScenario = null): array |
||
164 | |||
165 | /******************************************* |
||
166 | * FIND/GET |
||
167 | *******************************************/ |
||
168 | |||
169 | /** |
||
170 | * @param $identifier |
||
171 | * @param string $toScenario |
||
172 | * @return BaseModel|null |
||
173 | */ |
||
174 | public function find($identifier, string $toScenario = null) |
||
196 | |||
197 | /** |
||
198 | * @param $identifier |
||
199 | * @param string $toScenario |
||
200 | * @return BaseModel |
||
201 | * @throws ModelNotFoundException |
||
202 | */ |
||
203 | public function get($identifier, string $toScenario = null): BaseModel |
||
216 | |||
217 | /******************************************* |
||
218 | * FIND/GET BY QUERY |
||
219 | *******************************************/ |
||
220 | |||
221 | /** |
||
222 | * @param QueryInterface $query |
||
223 | * @param string $toScenario |
||
224 | * @return BaseModel[] |
||
225 | */ |
||
226 | View Code Duplication | public function findAllByQuery(QueryInterface $query, string $toScenario = null): array |
|
238 | |||
239 | /** |
||
240 | * @param QueryInterface $query |
||
241 | * @param string $toScenario |
||
242 | * @return BaseModel|null |
||
243 | */ |
||
244 | View Code Duplication | public function findByQuery(QueryInterface $query, string $toScenario = null) |
|
255 | |||
256 | /******************************************* |
||
257 | * FIND/GET BY CONDITION |
||
258 | *******************************************/ |
||
259 | |||
260 | /** |
||
261 | * @param $condition |
||
262 | * @param string $toScenario |
||
263 | * @return BaseModel[] |
||
264 | */ |
||
265 | public function findAllByCondition($condition, string $toScenario = null): array |
||
282 | |||
283 | /** |
||
284 | * @param $condition |
||
285 | * @param string $toScenario |
||
286 | * @return BaseModel[] |
||
287 | * @throws ModelNotFoundException |
||
288 | */ |
||
289 | public function getAllByCondition($condition, string $toScenario = null): array |
||
301 | |||
302 | /** |
||
303 | * @param $condition |
||
304 | * @param string $toScenario |
||
305 | * @return BaseModel|null |
||
306 | */ |
||
307 | View Code Duplication | public function findByCondition($condition, string $toScenario = null) |
|
318 | |||
319 | /** |
||
320 | * @param $condition |
||
321 | * @param string $toScenario |
||
322 | * @return BaseModel |
||
323 | * @throws ModelNotFoundException |
||
324 | */ |
||
325 | public function getByCondition($condition, string $toScenario = null): BaseModel |
||
337 | |||
338 | /******************************************* |
||
339 | * FIND/GET BY CRITERIA |
||
340 | *******************************************/ |
||
341 | |||
342 | /** |
||
343 | * @param $criteria |
||
344 | * @param string $toScenario |
||
345 | * @return BaseModel[] |
||
346 | */ |
||
347 | View Code Duplication | public function findAllByCriteria($criteria, string $toScenario = null): array |
|
365 | |||
366 | /** |
||
367 | * @param $criteria |
||
368 | * @param string $toScenario |
||
369 | * @return BaseModel[] |
||
370 | * @throws ModelNotFoundException |
||
371 | */ |
||
372 | public function getAllByCriteria($criteria, string $toScenario = null): array |
||
384 | |||
385 | /** |
||
386 | * @param $criteria |
||
387 | * @param string $toScenario |
||
388 | * @return BaseModel|null |
||
389 | */ |
||
390 | View Code Duplication | public function findByCriteria($criteria, string $toScenario = null) |
|
401 | |||
402 | /** |
||
403 | * @param $criteria |
||
404 | * @param string $toScenario |
||
405 | * @return BaseModel |
||
406 | * @throws ModelNotFoundException |
||
407 | */ |
||
408 | public function getByCriteria($criteria, string $toScenario = null): BaseModel |
||
420 | |||
421 | |||
422 | /******************************************* |
||
423 | * FIND/GET BY RECORD |
||
424 | *******************************************/ |
||
425 | |||
426 | /** |
||
427 | * @param Record $record |
||
428 | * @param string $toScenario |
||
429 | * @return BaseModel |
||
430 | */ |
||
431 | View Code Duplication | public function findByRecord(Record $record, string $toScenario = null): BaseModel |
|
448 | |||
449 | /** |
||
450 | * @param Record $record |
||
451 | * @param string $toScenario |
||
452 | * @return BaseModel |
||
453 | */ |
||
454 | public function getByRecord(Record $record, string $toScenario = null): BaseModel |
||
458 | |||
459 | /** |
||
460 | * @param BaseModel $model |
||
461 | * @return Record|null |
||
462 | */ |
||
463 | public function findRecordByModel(BaseModel $model) |
||
467 | |||
468 | /** |
||
469 | * @param BaseModel $model |
||
470 | * @return Record |
||
471 | * @throws RecordNotFoundException |
||
472 | */ |
||
473 | public function getRecordByModel(BaseModel $model) |
||
485 | |||
486 | /******************************************* |
||
487 | * CACHE |
||
488 | *******************************************/ |
||
489 | |||
490 | /** |
||
491 | * @param $identifier |
||
492 | * @return BaseModel|null |
||
493 | */ |
||
494 | public function findCache($identifier) |
||
506 | |||
507 | /** |
||
508 | * @param Record $record |
||
509 | * @return BaseModel|null |
||
510 | */ |
||
511 | public function findCacheByRecord(Record $record) |
||
515 | |||
516 | /** |
||
517 | * @param BaseModel $model |
||
518 | * @return static |
||
519 | */ |
||
520 | public function addToCache(BaseModel $model) |
||
524 | |||
525 | |||
526 | /******************************************* |
||
527 | * EXCEPTIONS |
||
528 | *******************************************/ |
||
529 | |||
530 | /** |
||
531 | * @throws ModelNotFoundException |
||
532 | */ |
||
533 | protected function notFoundException() |
||
543 | |||
544 | /** |
||
545 | * @param null $criteria |
||
546 | * @throws ModelNotFoundException |
||
547 | */ |
||
548 | protected function notFoundByCriteriaException($criteria = null) |
||
559 | |||
560 | /** |
||
561 | * @param null $condition |
||
562 | * @throws ModelNotFoundException |
||
563 | */ |
||
564 | protected function notFoundByConditionException($condition = null) |
||
575 | |||
576 | } |
||
577 |