Complex classes like BaseModel 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 BaseModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | abstract class BaseModel extends ArrayableModel |
||
13 | { |
||
14 | use ModelEventsTrait; |
||
15 | |||
16 | /** |
||
17 | * The loaded relationships for the model. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $relations = []; |
||
22 | |||
23 | /** |
||
24 | * Have fields been already fetched from DB? |
||
25 | * |
||
26 | * @var bool |
||
27 | */ |
||
28 | protected $fieldsAreFetched = false; |
||
29 | |||
30 | /** |
||
31 | * List of additional params that can modify query. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | protected static $additionalQueryModifiers = []; |
||
36 | |||
37 | /** |
||
38 | * Refresh model from database and place data to $this->fields. |
||
39 | * |
||
40 | * @return array |
||
41 | */ |
||
42 | abstract public function refresh(); |
||
43 | |||
44 | /** |
||
45 | * Refresh model fields from database and place them to $this->fields. |
||
46 | * |
||
47 | * @return array |
||
48 | */ |
||
49 | abstract public function refreshFields(); |
||
50 | |||
51 | /** |
||
52 | * Constructor. |
||
53 | * |
||
54 | * @param $id |
||
55 | * @param $fields |
||
56 | */ |
||
57 | public function __construct($id = null, $fields = null) |
||
65 | |||
66 | /** |
||
67 | * Get all model attributes from cache or database. |
||
68 | * |
||
69 | * @return array |
||
70 | */ |
||
71 | public function get() |
||
79 | |||
80 | /** |
||
81 | * Get user groups from cache or database. |
||
82 | * |
||
83 | * @return array |
||
84 | */ |
||
85 | public function getFields() |
||
93 | |||
94 | /** |
||
95 | * Fill model fields if they are already known. |
||
96 | * Saves DB queries. |
||
97 | * |
||
98 | * @param array $fields |
||
99 | * |
||
100 | * @return null |
||
101 | */ |
||
102 | public function fill($fields) |
||
120 | |||
121 | /** |
||
122 | * Activate model. |
||
123 | * |
||
124 | * @return bool |
||
125 | */ |
||
126 | public function activate() |
||
132 | |||
133 | /** |
||
134 | * Deactivate model. |
||
135 | * |
||
136 | * @return bool |
||
137 | */ |
||
138 | public function deactivate() |
||
144 | |||
145 | /** |
||
146 | * Create new item in database. |
||
147 | * |
||
148 | * @param $fields |
||
149 | * |
||
150 | * @throws Exception |
||
151 | * |
||
152 | * @return static |
||
153 | */ |
||
154 | public static function create($fields) |
||
177 | |||
178 | /** |
||
179 | * Get count of items that match $filter. |
||
180 | * |
||
181 | * @param array $filter |
||
182 | * |
||
183 | * @return int |
||
184 | */ |
||
185 | public static function count(array $filter = []) |
||
189 | |||
190 | /** |
||
191 | * Get item by its id. |
||
192 | * |
||
193 | * @param int $id |
||
194 | * |
||
195 | * @return static |
||
196 | */ |
||
197 | public static function find($id) |
||
201 | |||
202 | /** |
||
203 | * Create a new query and apply modifiers according to $params. |
||
204 | * |
||
205 | * @param array $params |
||
206 | * |
||
207 | * @return BaseQuery |
||
208 | */ |
||
209 | protected static function createQueryWithModifiers($params) |
||
231 | |||
232 | /** |
||
233 | * Get item by its id. |
||
234 | * |
||
235 | * @param int $id |
||
236 | * |
||
237 | * @return static |
||
238 | */ |
||
239 | public static function getById($id) |
||
243 | |||
244 | /** |
||
245 | * Get list of items. |
||
246 | * |
||
247 | * @param array $params |
||
248 | * |
||
249 | * @return static[] |
||
250 | */ |
||
251 | public static function getList($params = []) |
||
257 | |||
258 | /** |
||
259 | * Get first item that match $params. |
||
260 | * |
||
261 | * @param array $params |
||
262 | * |
||
263 | * @return static |
||
264 | */ |
||
265 | public static function first($params = []) |
||
271 | |||
272 | /** |
||
273 | * Delete model. |
||
274 | * |
||
275 | * @return bool |
||
276 | */ |
||
277 | public function delete() |
||
289 | |||
290 | /** |
||
291 | * Update model. |
||
292 | * |
||
293 | * @param array $fields |
||
294 | * |
||
295 | * @return bool |
||
296 | */ |
||
297 | public function update(array $fields = []) |
||
307 | |||
308 | /** |
||
309 | * Save model to database. |
||
310 | * |
||
311 | * @param array $selectedFields save only these fields instead of all. |
||
312 | * |
||
313 | * @return bool |
||
314 | */ |
||
315 | public function save($selectedFields = []) |
||
335 | |||
336 | /** |
||
337 | * Scope to get only active items. |
||
338 | * |
||
339 | * @param BaseQuery $query |
||
340 | * |
||
341 | * @return BaseQuery |
||
342 | */ |
||
343 | public function scopeActive($query) |
||
349 | |||
350 | /** |
||
351 | * Create an array of fields that will be saved to database. |
||
352 | * |
||
353 | * @param $selectedFields |
||
354 | * |
||
355 | * @return array |
||
356 | */ |
||
357 | protected function normalizeFieldsForSave($selectedFields) |
||
372 | |||
373 | /** |
||
374 | * Determine whether the field should be stopped from passing to "update". |
||
375 | * |
||
376 | * @param string $field |
||
377 | * @param mixed $value |
||
378 | * @param array $selectedFields |
||
379 | * |
||
380 | * @return bool |
||
381 | */ |
||
382 | protected function fieldShouldNotBeSaved($field, $value, $selectedFields) |
||
397 | |||
398 | /** |
||
399 | * Instantiate bitrix entity object. |
||
400 | * |
||
401 | * @throws Exception |
||
402 | * |
||
403 | * @return object |
||
404 | */ |
||
405 | public static function instantiateObject() |
||
417 | |||
418 | /** |
||
419 | * Destroy bitrix entity object. |
||
420 | * |
||
421 | * @return void |
||
422 | */ |
||
423 | public static function destroyObject() |
||
427 | |||
428 | /** |
||
429 | * Instantiate a query object for the model. |
||
430 | * |
||
431 | * @throws Exception |
||
432 | * |
||
433 | * @return BaseQuery |
||
434 | */ |
||
435 | public static function query() |
||
439 | |||
440 | /** |
||
441 | * Set current model id. |
||
442 | * |
||
443 | * @param $id |
||
444 | */ |
||
445 | protected function setId($id) |
||
450 | |||
451 | /** |
||
452 | * Determine if the given relation is loaded. |
||
453 | * |
||
454 | * @param string $key |
||
455 | * |
||
456 | * @return bool |
||
457 | */ |
||
458 | public function relationIsLoaded($key) |
||
462 | |||
463 | /** |
||
464 | * Get a relationship value from a method. |
||
465 | * |
||
466 | * @param string $method |
||
467 | * |
||
468 | * @return mixed |
||
469 | */ |
||
470 | protected function getRelationshipFromMethod($method) |
||
480 | |||
481 | /** |
||
482 | * Dynamically retrieve fields on the model. |
||
483 | * |
||
484 | * @param string $key |
||
485 | * |
||
486 | * @return mixed |
||
487 | */ |
||
488 | public function __get($key) |
||
502 | } |
||
503 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: