Complex classes like RecordManager 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 RecordManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | abstract class RecordManager |
||
19 | { |
||
20 | use NameWorksTrait; |
||
21 | use ActiveRecordsTrait; |
||
22 | |||
23 | /** |
||
24 | * Collection class for current record manager |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $collectionClass = null; |
||
29 | |||
30 | protected $helpers = []; |
||
31 | |||
32 | /** |
||
33 | * @var null|string |
||
34 | */ |
||
35 | protected $urlPK = null; |
||
36 | |||
37 | /** |
||
38 | * Model class name |
||
39 | * @var null|string |
||
40 | */ |
||
41 | protected $model = null; |
||
42 | |||
43 | /** |
||
44 | * @var null|string |
||
45 | */ |
||
46 | protected $controller = null; |
||
47 | |||
48 | /** |
||
49 | * @var null|string |
||
50 | */ |
||
51 | protected $modelNamespacePath = null; |
||
52 | |||
53 | protected $registry = null; |
||
54 | |||
55 | /** |
||
56 | * Overloads findByRecord, findByField, deleteByRecord, deleteByField, countByRecord, countByField |
||
57 | * |
||
58 | * @example findByCategory(Category $item) |
||
59 | * @example deleteByProduct(Product $item) |
||
60 | * @example findByIdUser(2) |
||
61 | * @example deleteByTitle(array('Lorem ipsum', 'like')) |
||
62 | * @example countByIdCategory(1) |
||
63 | * |
||
64 | * @param string $name |
||
65 | * @param array $arguments |
||
66 | * |
||
67 | * @return mixed |
||
68 | */ |
||
69 | public function __call($name, $arguments) |
||
89 | |||
90 | /** |
||
91 | * @param string $name |
||
92 | * @param $arguments |
||
93 | * @return bool |
||
94 | */ |
||
95 | protected function isCallUrl($name, $arguments) |
||
107 | |||
108 | /** |
||
109 | * @param string $action |
||
110 | * @param array $params |
||
111 | * @param null $module |
||
112 | * @return string|null |
||
113 | */ |
||
114 | public function compileURL($action, $params = [], $module = null) |
||
146 | |||
147 | /** |
||
148 | * @return string |
||
149 | */ |
||
150 | 13 | public function getController() |
|
158 | |||
159 | /** |
||
160 | * @param null|string $controller |
||
161 | */ |
||
162 | 13 | public function setController($controller) |
|
166 | |||
167 | 13 | protected function initController() |
|
176 | |||
177 | /** |
||
178 | * @return string |
||
179 | */ |
||
180 | 8 | protected function generateControllerNamespaced() |
|
187 | |||
188 | /** |
||
189 | * @return string |
||
190 | */ |
||
191 | 8 | public function getModelNamespacePath() |
|
199 | |||
200 | 8 | public function initModelNamespacePath() |
|
210 | |||
211 | /** |
||
212 | * @return string |
||
213 | */ |
||
214 | 8 | protected function generateModelNamespacePathFromClassName() |
|
225 | |||
226 | /** |
||
227 | * @return string |
||
228 | */ |
||
229 | 8 | public function getRootNamespace() |
|
236 | |||
237 | /** |
||
238 | * @return string |
||
239 | */ |
||
240 | 5 | protected function generateControllerGeneric() |
|
246 | |||
247 | /** |
||
248 | * @param string $name |
||
249 | * @return \Nip\Helpers\AbstractHelper |
||
250 | */ |
||
251 | public function getHelper($name) |
||
255 | |||
256 | /** |
||
257 | * @return string |
||
258 | */ |
||
259 | public function getModelNamespace() |
||
263 | |||
264 | /** |
||
265 | * When searching by primary key, look for items in current registry before |
||
266 | * fetching them from the database |
||
267 | * |
||
268 | * @param array $pk_list |
||
269 | * @return RecordCollection |
||
270 | */ |
||
271 | public function findByPrimary($pk_list = []) |
||
301 | |||
302 | /** |
||
303 | * @return RecordCollection |
||
304 | */ |
||
305 | 1 | public function newCollection() |
|
314 | |||
315 | /** |
||
316 | * @return string |
||
317 | */ |
||
318 | 2 | public function getCollectionClass() |
|
326 | |||
327 | /** |
||
328 | * @param string $collectionClass |
||
329 | */ |
||
330 | 2 | public function setCollectionClass($collectionClass) |
|
334 | |||
335 | 2 | protected function initCollectionClass() |
|
339 | |||
340 | /** |
||
341 | * @return string |
||
342 | */ |
||
343 | 2 | protected function generateCollectionClass() |
|
347 | |||
348 | /** |
||
349 | * @return \Nip\Collections\Registry |
||
350 | */ |
||
351 | public function getRegistry() |
||
359 | |||
360 | /** |
||
361 | * Factory |
||
362 | * |
||
363 | * @return Record |
||
364 | * @param array $data [optional] |
||
365 | */ |
||
366 | public function getNew($data = []) |
||
381 | |||
382 | /** |
||
383 | * @param array $data |
||
384 | * @return Record |
||
385 | */ |
||
386 | public function getNewRecordFromDB($data = []) |
||
393 | |||
394 | /** |
||
395 | * @param array $data |
||
396 | * @return Record |
||
397 | */ |
||
398 | public function getNewRecord($data = []) |
||
408 | |||
409 | /** |
||
410 | * @return string |
||
411 | */ |
||
412 | 1 | public function getModel() |
|
420 | |||
421 | /** |
||
422 | * @param null $model |
||
423 | */ |
||
424 | 1 | public function setModel($model) |
|
428 | |||
429 | protected function inflectModel() |
||
436 | |||
437 | /** |
||
438 | * @param string $class |
||
439 | * @return string |
||
440 | */ |
||
441 | 1 | public function generateModelClass($class = null) |
|
460 | |||
461 | /** |
||
462 | * @return \Nip\Request |
||
463 | */ |
||
464 | public function getRequest() |
||
468 | |||
469 | public function __wakeup() |
||
473 | |||
474 | /** |
||
475 | * @param Record $item |
||
476 | * @return bool|false|Record |
||
477 | */ |
||
478 | public function exists(Record $item) |
||
500 | |||
501 | /** |
||
502 | * @return null |
||
503 | */ |
||
504 | public function getUniqueFields() |
||
512 | |||
513 | /** |
||
514 | * @return array|null |
||
515 | */ |
||
516 | public function initUniqueFields() |
||
532 | |||
533 | /** |
||
534 | * Finds one Record using params array |
||
535 | * |
||
536 | * @param array $params |
||
537 | * @return Record|null |
||
538 | */ |
||
539 | public function findOneByParams(array $params = []) |
||
549 | |||
550 | /** |
||
551 | * Finds Records using params array |
||
552 | * |
||
553 | * @param array $params |
||
554 | * @return RecordCollection |
||
555 | */ |
||
556 | public function findByParams($params = []) |
||
562 | |||
563 | /** |
||
564 | * @return RecordCollection |
||
565 | */ |
||
566 | public function getAll() |
||
574 | |||
575 | /** |
||
576 | * @return RecordCollection |
||
577 | */ |
||
578 | public function findAll() |
||
582 | |||
583 | /** |
||
584 | * @param int $count |
||
585 | * @return RecordCollection |
||
586 | */ |
||
587 | public function findLast($count = 9) |
||
593 | |||
594 | /** |
||
595 | * Inserts a Record into the database |
||
596 | * @param Record $model |
||
597 | * @param array|bool $onDuplicate |
||
598 | * @return integer |
||
599 | */ |
||
600 | public function insert($model, $onDuplicate = false) |
||
607 | |||
608 | /** |
||
609 | * @param Record $model |
||
610 | * @param $onDuplicate |
||
611 | * @return InsertQuery |
||
612 | */ |
||
613 | public function insertQuery($model, $onDuplicate) |
||
626 | |||
627 | /** |
||
628 | * @param Record $model |
||
629 | * @return array |
||
630 | */ |
||
631 | public function getQueryModelData($model) |
||
644 | |||
645 | /** |
||
646 | * The name of the field used as a foreign key in other tables |
||
647 | * @return string |
||
648 | */ |
||
649 | 8 | public function getPrimaryFK() |
|
657 | |||
658 | 6 | public function initPrimaryFK() |
|
662 | |||
663 | /** |
||
664 | * @param string $foreignKey |
||
665 | */ |
||
666 | 6 | public function setForeignKey($foreignKey) |
|
670 | |||
671 | /** |
||
672 | * @return string |
||
673 | */ |
||
674 | 5 | public function generatePrimaryFK() |
|
680 | |||
681 | /** |
||
682 | * @param $fk |
||
683 | */ |
||
684 | 3 | public function setPrimaryFK($fk) |
|
688 | |||
689 | /** |
||
690 | * The name of the field used as a foreign key in other tables |
||
691 | * @return string |
||
692 | */ |
||
693 | public function getUrlPK() |
||
701 | |||
702 | /** |
||
703 | * @param $name |
||
704 | * @return bool |
||
705 | */ |
||
706 | public function hasField($name) |
||
715 | |||
716 | /** |
||
717 | * @return array |
||
718 | */ |
||
719 | public function getFullTextFields() |
||
731 | |||
732 | /** |
||
733 | * Sets model and database table from the class name |
||
734 | */ |
||
735 | protected function inflect() |
||
739 | } |
||
740 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: