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 /** MicroModel */ |
||
23 | abstract class Model extends FormModel implements IModel |
||
24 | { |
||
25 | /** @var string $primaryKey Primary key on table */ |
||
26 | public static $primaryKey = 'id'; |
||
27 | /** @var string $tableName Table name */ |
||
28 | public static $tableName; |
||
29 | |||
30 | /** @var boolean $_isNewRecord Is new record? */ |
||
31 | protected $_isNewRecord = false; |
||
32 | /** @var array $cacheRelations Cached loads relations */ |
||
33 | protected $cacheRelations = []; |
||
34 | |||
35 | |||
36 | /** |
||
37 | * Constructor for model |
||
38 | * |
||
39 | * @access public |
||
40 | * |
||
41 | * @param boolean $new is new model? |
||
42 | * |
||
43 | * @result void |
||
44 | * @throws Exception |
||
45 | */ |
||
46 | public function __construct($new = true) |
||
56 | |||
57 | /** |
||
58 | * Finder by primary key |
||
59 | * |
||
60 | * @access public |
||
61 | * |
||
62 | * @param int|string $value unique value |
||
63 | * |
||
64 | * @return mixed |
||
65 | * @throws \Micro\base\Exception |
||
66 | * @static |
||
67 | */ |
||
68 | public static function findByPk($value) |
||
72 | |||
73 | /** |
||
74 | * Find models by attributes |
||
75 | * |
||
76 | * @access public |
||
77 | * |
||
78 | * @param array $attributes attributes and data for search |
||
79 | * @param bool $single single or more |
||
80 | * |
||
81 | * @return mixed |
||
82 | * @throws \Micro\base\Exception |
||
83 | */ |
||
84 | public static function findByAttributes(array $attributes = [], $single = false) |
||
94 | |||
95 | /** |
||
96 | * Finder data in DB |
||
97 | * |
||
98 | * @access public |
||
99 | * |
||
100 | * @param IQuery $query query to search |
||
101 | * @param boolean $single is single |
||
102 | * |
||
103 | * @return mixed One or more data |
||
104 | * @throws \Micro\base\Exception |
||
105 | * @static |
||
106 | */ |
||
107 | public static function finder(IQuery $query = null, $single = false) |
||
116 | |||
117 | /** |
||
118 | * Find by model attribute values |
||
119 | * |
||
120 | * @access public |
||
121 | * |
||
122 | * @param bool $single Is a single? |
||
123 | * |
||
124 | * @return mixed |
||
125 | * @throws \Micro\base\Exception |
||
126 | */ |
||
127 | public function find($single = false) |
||
131 | |||
132 | /** |
||
133 | * Get attributes defined into model |
||
134 | * |
||
135 | * @access public |
||
136 | * |
||
137 | * @return array |
||
138 | * @throws Exception |
||
139 | */ |
||
140 | public function getAttributes() |
||
149 | |||
150 | /** |
||
151 | * Get relation data or magic properties |
||
152 | * |
||
153 | * @access public |
||
154 | * |
||
155 | * @param string $name |
||
156 | * |
||
157 | * @return mixed |
||
158 | * @throws Exception |
||
159 | */ |
||
160 | public function __get($name) |
||
196 | |||
197 | /** |
||
198 | * @param string $name |
||
199 | * @param mixed $value |
||
200 | */ |
||
201 | public function __set($name, $value) |
||
204 | |||
205 | /** |
||
206 | * @inheritdoc |
||
207 | */ |
||
208 | public function relations() |
||
212 | |||
213 | /** |
||
214 | * @param string $name |
||
215 | * @return boolean |
||
216 | */ |
||
217 | public function __isset($name) |
||
221 | |||
222 | /** |
||
223 | * Save changes |
||
224 | * |
||
225 | * @access public |
||
226 | * |
||
227 | * @param bool $validate Validated data? |
||
228 | * |
||
229 | * @return boolean |
||
230 | * @throws Exception |
||
231 | */ |
||
232 | final public function save($validate = false) |
||
250 | |||
251 | /** |
||
252 | * Is new record? |
||
253 | * |
||
254 | * @access public |
||
255 | * @return boolean |
||
256 | */ |
||
257 | public function isNewRecord() |
||
261 | |||
262 | /** |
||
263 | * Create changes |
||
264 | * |
||
265 | * @access public |
||
266 | * @return boolean |
||
267 | * @throws Exception |
||
268 | */ |
||
269 | final public function create() |
||
296 | |||
297 | /** |
||
298 | * @inheritdoc |
||
299 | */ |
||
300 | public function beforeCreate() |
||
304 | |||
305 | /** |
||
306 | * @inheritdoc |
||
307 | */ |
||
308 | public function beforeSave() |
||
312 | |||
313 | /** |
||
314 | * Merge local attributes and db attributes |
||
315 | * |
||
316 | * @access protected |
||
317 | * |
||
318 | * @return array |
||
319 | * @throws \Micro\base\Exception |
||
320 | */ |
||
321 | protected function mergeAttributesDb() |
||
340 | |||
341 | /** |
||
342 | * Check attribute exists into table |
||
343 | * |
||
344 | * @access public |
||
345 | * |
||
346 | * @param string $name Attribute name |
||
347 | * |
||
348 | * @return boolean |
||
349 | * @throws Exception |
||
350 | */ |
||
351 | public function checkAttributeExists($name) |
||
367 | |||
368 | /** |
||
369 | * @inheritdoc |
||
370 | */ |
||
371 | public function afterCreate() |
||
374 | |||
375 | /** |
||
376 | * @inheritdoc |
||
377 | */ |
||
378 | public function afterSave() |
||
381 | |||
382 | /** |
||
383 | * Update changes |
||
384 | * |
||
385 | * @access public |
||
386 | * |
||
387 | * @param string $where condition for search |
||
388 | * |
||
389 | * @throws Exception |
||
390 | * @return boolean |
||
391 | */ |
||
392 | final public function update($where = null) |
||
421 | |||
422 | /** |
||
423 | * @inheritdoc |
||
424 | */ |
||
425 | public function beforeUpdate() |
||
429 | |||
430 | /** |
||
431 | * @inheritdoc |
||
432 | */ |
||
433 | public function afterUpdate() |
||
436 | |||
437 | /** |
||
438 | * Delete changes |
||
439 | * |
||
440 | * @access public |
||
441 | * @return boolean |
||
442 | * @throws Exception |
||
443 | */ |
||
444 | final public function delete() |
||
469 | |||
470 | /** |
||
471 | * @inheritdoc |
||
472 | */ |
||
473 | public function beforeDelete() |
||
477 | |||
478 | /** |
||
479 | * @inheritdoc |
||
480 | */ |
||
481 | public function afterDelete() |
||
484 | } |
||
485 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.