Complex classes like ActiveRecordsTrait 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 ActiveRecordsTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | trait ActiveRecordsTrait |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * @var Connection |
||
25 | */ |
||
26 | protected $connection = null; |
||
27 | |||
28 | /** |
||
29 | * @var null|string |
||
30 | */ |
||
31 | protected $table = null; |
||
32 | |||
33 | protected $tableStructure = null; |
||
34 | |||
35 | /** |
||
36 | * @var null|string |
||
37 | */ |
||
38 | protected $primaryKey = null; |
||
39 | protected $fields = null; |
||
40 | protected $uniqueFields = null; |
||
41 | |||
42 | /** |
||
43 | * @var null|string |
||
44 | */ |
||
45 | protected $foreignKey = null; |
||
46 | |||
47 | /** |
||
48 | * @return SelectQuery |
||
49 | */ |
||
50 | public function newSelectQuery() |
||
54 | |||
55 | /** |
||
56 | * Factory |
||
57 | * @param string $type |
||
58 | * @return Query|SelectQuery|InsertQuery|DeleteQuery|UpdateQuery |
||
59 | */ |
||
60 | public function newQuery($type = 'select') |
||
69 | |||
70 | /** |
||
71 | * @return Connection |
||
72 | */ |
||
73 | 2 | public function getDB() |
|
83 | |||
84 | 1 | protected function initDB() |
|
88 | |||
89 | /** |
||
90 | * @param Connection $connection |
||
91 | * @return $this |
||
92 | */ |
||
93 | 21 | public function setDB($connection) |
|
99 | |||
100 | /** |
||
101 | * @return Connection |
||
102 | */ |
||
103 | 1 | protected function newDbConnection() |
|
107 | |||
108 | 2 | public function checkDB() |
|
114 | |||
115 | /** |
||
116 | * @return bool |
||
117 | */ |
||
118 | 2 | public function hasDB() |
|
122 | |||
123 | /** |
||
124 | * @return string |
||
125 | */ |
||
126 | 2 | public function getTable() |
|
134 | |||
135 | /** |
||
136 | * @param null $table |
||
137 | */ |
||
138 | 21 | public function setTable($table) |
|
142 | |||
143 | 1 | protected function initTable() |
|
147 | |||
148 | /** |
||
149 | * @return string |
||
150 | */ |
||
151 | 1 | protected function generateTable() |
|
155 | |||
156 | /** |
||
157 | * @return string |
||
158 | */ |
||
159 | 2 | public function getFullNameTable() |
|
165 | |||
166 | /** |
||
167 | * @return null |
||
168 | */ |
||
169 | public function getFields() |
||
177 | |||
178 | public function initFields() |
||
183 | |||
184 | /** |
||
185 | * @return mixed |
||
186 | */ |
||
187 | protected function getTableStructure() |
||
195 | |||
196 | /** |
||
197 | * @param null $tableStructure |
||
198 | */ |
||
199 | 1 | public function setTableStructure($tableStructure) |
|
203 | |||
204 | protected function initTableStructure() |
||
208 | |||
209 | /** |
||
210 | * @return InsertQuery |
||
211 | */ |
||
212 | public function newInsertQuery() |
||
216 | |||
217 | /** |
||
218 | * Updates a Record's database entry |
||
219 | * @param Record $model |
||
220 | * @return bool|Result |
||
221 | */ |
||
222 | public function update(Record $model) |
||
232 | |||
233 | /** |
||
234 | * @param Record $model |
||
235 | * @return bool|UpdateQuery |
||
236 | */ |
||
237 | public function updateQuery(Record $model) |
||
259 | |||
260 | /** |
||
261 | * @return string |
||
262 | */ |
||
263 | 7 | public function getPrimaryKey() |
|
271 | |||
272 | /** |
||
273 | * @param null|string $primaryKey |
||
274 | */ |
||
275 | 7 | public function setPrimaryKey($primaryKey) |
|
279 | |||
280 | protected function initPrimaryKey() |
||
284 | |||
285 | /** |
||
286 | * @return string |
||
287 | */ |
||
288 | public function generatePrimaryKey() |
||
301 | |||
302 | /** |
||
303 | * @return UpdateQuery |
||
304 | */ |
||
305 | public function newUpdateQuery() |
||
309 | |||
310 | /** |
||
311 | * Saves a Record's database entry |
||
312 | * @param Record $model |
||
313 | * @return mixed |
||
314 | */ |
||
315 | public function save(Record $model) |
||
345 | |||
346 | /** |
||
347 | * Delete a Record's database entry |
||
348 | * |
||
349 | * @param mixed|Record $input |
||
350 | */ |
||
351 | public function delete($input) |
||
367 | |||
368 | /** |
||
369 | * @return DeleteQuery |
||
370 | */ |
||
371 | public function newDeleteQuery() |
||
375 | |||
376 | /** |
||
377 | * Delete a Record's database entry |
||
378 | * @param array $params |
||
379 | * @return $this |
||
380 | */ |
||
381 | public function deleteByParams($params = []) |
||
410 | |||
411 | /** |
||
412 | * Returns paginated results |
||
413 | * @param Paginator $paginator |
||
414 | * @param array $params |
||
415 | * @return mixed |
||
416 | */ |
||
417 | public function paginate(Paginator $paginator, $params = []) |
||
433 | |||
434 | /** |
||
435 | * @param array $params |
||
436 | * @return SelectQuery |
||
437 | */ |
||
438 | public function paramsToQuery($params = []) |
||
447 | |||
448 | /** |
||
449 | * @param array $params |
||
450 | */ |
||
451 | protected function injectParams(&$params = []) |
||
454 | |||
455 | /** |
||
456 | * Checks the registry before fetching from the database |
||
457 | * @param mixed $primary |
||
458 | * @return Record |
||
459 | */ |
||
460 | public function findOne($primary) |
||
481 | |||
482 | /** |
||
483 | * @param Query $query |
||
484 | * @param array $params |
||
485 | * @return bool |
||
486 | */ |
||
487 | public function findOneByQuery($query, $params = []) |
||
497 | |||
498 | /** |
||
499 | * @param $query |
||
500 | * @param array $params |
||
501 | * @return RecordCollection |
||
502 | */ |
||
503 | public function findByQuery($query, $params = []) |
||
526 | |||
527 | /** |
||
528 | * @param bool|array $where |
||
529 | * @return int |
||
530 | */ |
||
531 | public function count($where = false) |
||
535 | |||
536 | /** |
||
537 | * Counts all the Record entries in the database |
||
538 | * @param array $params |
||
539 | * @return int |
||
540 | */ |
||
541 | public function countByParams($params = []) |
||
549 | |||
550 | /** |
||
551 | * Counts all the Record entries in the database |
||
552 | * @param Query $query |
||
553 | * @return int |
||
554 | */ |
||
555 | public function countByQuery($query) |
||
569 | |||
570 | /** |
||
571 | * @param $data |
||
572 | * @return mixed |
||
573 | */ |
||
574 | public function cleanData($data) |
||
578 | |||
579 | /** |
||
580 | * @param $name |
||
581 | * @param $arguments |
||
582 | * @return RecordCollection|null |
||
583 | */ |
||
584 | protected function isCallDatabaseOperation($name, $arguments) |
||
611 | } |
||
612 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.