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 | * Bitrix entity object. |
||
18 | * |
||
19 | * @var object |
||
20 | */ |
||
21 | public static $bxObject; |
||
22 | |||
23 | /** |
||
24 | * Corresponding object class name. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected static $objectClass = ''; |
||
29 | |||
30 | /** |
||
31 | * Have fields been already fetched from DB? |
||
32 | * |
||
33 | * @var bool |
||
34 | */ |
||
35 | protected $fieldsAreFetched = false; |
||
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 void |
||
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|bool |
||
153 | */ |
||
154 | public static function create($fields) |
||
177 | |||
178 | /** |
||
179 | * Get item by its id. |
||
180 | * |
||
181 | * @param int $id |
||
182 | * |
||
183 | * @return static|bool |
||
184 | */ |
||
185 | public static function find($id) |
||
189 | |||
190 | /** |
||
191 | * Delete model. |
||
192 | * |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function delete() |
||
207 | |||
208 | /** |
||
209 | * Update model. |
||
210 | * |
||
211 | * @param array $fields |
||
212 | * |
||
213 | * @return bool |
||
214 | */ |
||
215 | public function update(array $fields = []) |
||
225 | |||
226 | /** |
||
227 | * Save model to database. |
||
228 | * |
||
229 | * @param array $selectedFields save only these fields instead of all. |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | public function save($selectedFields = []) |
||
253 | |||
254 | /** |
||
255 | * Scope to get only active items. |
||
256 | * |
||
257 | * @param BaseQuery $query |
||
258 | * |
||
259 | * @return BaseQuery |
||
260 | */ |
||
261 | public function scopeActive($query) |
||
267 | |||
268 | /** |
||
269 | * Create an array of fields that will be saved to database. |
||
270 | * |
||
271 | * @param $selectedFields |
||
272 | * |
||
273 | * @return array |
||
274 | */ |
||
275 | protected function normalizeFieldsForSave($selectedFields) |
||
290 | |||
291 | /** |
||
292 | * Determine whether the field should be stopped from passing to "update". |
||
293 | * |
||
294 | * @param string $field |
||
295 | * @param mixed $value |
||
296 | * @param array $selectedFields |
||
297 | * |
||
298 | * @return bool |
||
299 | */ |
||
300 | protected function fieldShouldNotBeSaved($field, $value, $selectedFields) |
||
315 | |||
316 | /** |
||
317 | * Instantiate bitrix entity object. |
||
318 | * |
||
319 | * @throws Exception |
||
320 | * |
||
321 | * @return object |
||
322 | */ |
||
323 | public static function instantiateObject() |
||
335 | |||
336 | /** |
||
337 | * Destroy bitrix entity object. |
||
338 | * |
||
339 | * @return void |
||
340 | */ |
||
341 | public static function destroyObject() |
||
345 | |||
346 | /** |
||
347 | * Instantiate a query object for the model. |
||
348 | * |
||
349 | * @throws Exception |
||
350 | * |
||
351 | * @return BaseQuery |
||
352 | */ |
||
353 | public static function query() |
||
357 | |||
358 | /** |
||
359 | * Set current model id. |
||
360 | * |
||
361 | * @param $id |
||
362 | */ |
||
363 | protected function setId($id) |
||
368 | } |
||
369 |
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: