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 boolean $_isNewRecord is new record? */ |
||
28 | protected $_isNewRecord = false; |
||
29 | /** @var array $cacheRelations cached loads relations */ |
||
30 | protected $cacheRelations = []; |
||
31 | |||
32 | |||
33 | /** |
||
34 | * Constructor for model |
||
35 | * |
||
36 | * @access public |
||
37 | * |
||
38 | * @param IContainer $container |
||
39 | * @param boolean $new is new model? |
||
40 | * |
||
41 | * @result void |
||
42 | */ |
||
43 | public function __construct(IContainer $container, $new = true) |
||
49 | |||
50 | /** |
||
51 | * Finder by primary key |
||
52 | * |
||
53 | * @access public |
||
54 | * |
||
55 | * @param int|string $value unique value |
||
56 | * @param IContainer $container |
||
57 | * |
||
58 | * @return mixed |
||
59 | * @throws \Micro\base\Exception |
||
60 | * @static |
||
61 | */ |
||
62 | public static function findByPk($value, IContainer $container) |
||
66 | |||
67 | /** |
||
68 | * Find models by attributes |
||
69 | * |
||
70 | * @access public |
||
71 | * |
||
72 | * @param array $attributes attributes and data for search |
||
73 | * @param bool $single single or more |
||
74 | * @param IContainer $container |
||
75 | * |
||
76 | * @return mixed |
||
77 | * @throws \Micro\base\Exception |
||
78 | */ |
||
79 | public static function findByAttributes(array $attributes = [], $single = false, IContainer $container) |
||
89 | |||
90 | /** |
||
91 | * Finder data in DB |
||
92 | * |
||
93 | * @access public |
||
94 | * |
||
95 | * @param IQuery $query query to search |
||
96 | * @param boolean $single is single |
||
97 | * @param IContainer $container |
||
98 | * |
||
99 | * @return mixed One or more data |
||
100 | * @throws \Micro\base\Exception |
||
101 | * @static |
||
102 | */ |
||
103 | public static function finder(IQuery $query = null, $single = false, IContainer $container = null) |
||
112 | |||
113 | /** |
||
114 | * Find by model attribute values |
||
115 | * |
||
116 | * @access public |
||
117 | * |
||
118 | * @param bool $single Is a single? |
||
119 | * |
||
120 | * @return mixed |
||
121 | * @throws \Micro\base\Exception |
||
122 | */ |
||
123 | public function find($single = false) |
||
127 | |||
128 | /** |
||
129 | * Get attributes defined into model |
||
130 | * |
||
131 | * @access public |
||
132 | * |
||
133 | * @return array |
||
134 | */ |
||
135 | public function getAttributes() |
||
144 | |||
145 | /** |
||
146 | * Get relation data or magic properties |
||
147 | * |
||
148 | * @access public |
||
149 | * |
||
150 | * @param string $name |
||
151 | * |
||
152 | * @return mixed |
||
153 | */ |
||
154 | public function __get($name) |
||
186 | |||
187 | /** |
||
188 | * @inheritdoc |
||
189 | */ |
||
190 | public function relations() |
||
194 | |||
195 | /** |
||
196 | * Save changes |
||
197 | * |
||
198 | * @access public |
||
199 | * |
||
200 | * @param bool $validate Validated data? |
||
201 | * |
||
202 | * @return boolean |
||
203 | * @throws Exception |
||
204 | */ |
||
205 | final public function save($validate = false) |
||
223 | |||
224 | /** |
||
225 | * Is new record? |
||
226 | * |
||
227 | * @access public |
||
228 | * @return boolean |
||
229 | */ |
||
230 | public function isNewRecord() |
||
234 | |||
235 | /** |
||
236 | * Create changes |
||
237 | * |
||
238 | * @access public |
||
239 | * @return boolean |
||
240 | * @throws Exception |
||
241 | */ |
||
242 | final public function create() |
||
268 | |||
269 | /** |
||
270 | * @inheritdoc |
||
271 | */ |
||
272 | public function beforeCreate() |
||
276 | |||
277 | /** |
||
278 | * @inheritdoc |
||
279 | */ |
||
280 | public function beforeSave() |
||
284 | |||
285 | /** |
||
286 | * Merge local attributes and db attributes |
||
287 | * |
||
288 | * @access protected |
||
289 | * |
||
290 | * @return array |
||
291 | * @throws \Micro\base\Exception |
||
292 | */ |
||
293 | protected function mergeAttributesDb() |
||
312 | |||
313 | /** |
||
314 | * Check attribute exists into table |
||
315 | * |
||
316 | * @access public |
||
317 | * |
||
318 | * @param string $name Attribute name |
||
319 | * |
||
320 | * @return array |
||
321 | */ |
||
322 | public function checkAttributeExists($name) |
||
338 | |||
339 | /** |
||
340 | * @inheritdoc |
||
341 | */ |
||
342 | public function afterCreate() |
||
345 | |||
346 | /** |
||
347 | * @inheritdoc |
||
348 | */ |
||
349 | public function afterSave() |
||
352 | |||
353 | /** |
||
354 | * Update changes |
||
355 | * |
||
356 | * @access public |
||
357 | * |
||
358 | * @param string $where condition for search |
||
359 | * |
||
360 | * @throws Exception |
||
361 | * @return boolean |
||
362 | */ |
||
363 | final public function update($where = null) |
||
388 | |||
389 | /** |
||
390 | * @inheritdoc |
||
391 | */ |
||
392 | public function beforeUpdate() |
||
396 | |||
397 | /** |
||
398 | * @inheritdoc |
||
399 | */ |
||
400 | public function afterUpdate() |
||
403 | |||
404 | /** |
||
405 | * Delete changes |
||
406 | * |
||
407 | * @access public |
||
408 | * @return boolean |
||
409 | * @throws Exception |
||
410 | */ |
||
411 | final public function delete() |
||
436 | |||
437 | /** |
||
438 | * @inheritdoc |
||
439 | */ |
||
440 | public function beforeDelete() |
||
444 | |||
445 | /** |
||
446 | * @inheritdoc |
||
447 | */ |
||
448 | public function afterDelete() |
||
451 | } |
||
452 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: