Complex classes like BitrixModel 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 BitrixModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | abstract class BitrixModel 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 | * Constructor. |
||
39 | * |
||
40 | * @param $id |
||
41 | * @param $fields |
||
42 | */ |
||
43 | public function __construct($id = null, $fields = null) |
||
51 | |||
52 | /** |
||
53 | * Get all model attributes from cache or database. |
||
54 | * |
||
55 | * @return array |
||
56 | */ |
||
57 | public function get() |
||
63 | |||
64 | /** |
||
65 | * Load model fields from database if they are not loaded yet. |
||
66 | * |
||
67 | * @return $this |
||
68 | */ |
||
69 | public function load() |
||
77 | |||
78 | /** |
||
79 | * Get model fields from cache or database. |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | public function getFields() |
||
91 | |||
92 | /** |
||
93 | * Refresh model from database and place data to $this->fields. |
||
94 | * |
||
95 | * @return array |
||
96 | */ |
||
97 | public function refresh() |
||
101 | |||
102 | /** |
||
103 | * Refresh model fields and save them to a class field. |
||
104 | * |
||
105 | * @return array |
||
106 | */ |
||
107 | public function refreshFields() |
||
119 | |||
120 | /** |
||
121 | * Fill model fields if they are already known. |
||
122 | * Saves DB queries. |
||
123 | * |
||
124 | * @param array $fields |
||
125 | * |
||
126 | * @return void |
||
127 | */ |
||
128 | public function fill($fields) |
||
146 | |||
147 | /** |
||
148 | * Activate model. |
||
149 | * |
||
150 | * @return bool |
||
151 | */ |
||
152 | public function activate() |
||
158 | |||
159 | /** |
||
160 | * Deactivate model. |
||
161 | * |
||
162 | * @return bool |
||
163 | */ |
||
164 | public function deactivate() |
||
170 | |||
171 | /** |
||
172 | * Create new item in database. |
||
173 | * |
||
174 | * @param $fields |
||
175 | * |
||
176 | * @throws Exception |
||
177 | * |
||
178 | * @return static|bool |
||
179 | */ |
||
180 | public static function create($fields) |
||
203 | |||
204 | /** |
||
205 | * Get count of items that match $filter. |
||
206 | * |
||
207 | * @param array $filter |
||
208 | * |
||
209 | * @return int |
||
210 | */ |
||
211 | public static function count(array $filter = []) |
||
215 | |||
216 | /** |
||
217 | * Get item by its id. |
||
218 | * |
||
219 | * @param int $id |
||
220 | * |
||
221 | * @return static|bool |
||
222 | */ |
||
223 | public static function find($id) |
||
227 | |||
228 | /** |
||
229 | * Delete model. |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | public function delete() |
||
245 | |||
246 | /** |
||
247 | * Update model. |
||
248 | * |
||
249 | * @param array $fields |
||
250 | * |
||
251 | * @return bool |
||
252 | */ |
||
253 | public function update(array $fields = []) |
||
263 | |||
264 | /** |
||
265 | * Save model to database. |
||
266 | * |
||
267 | * @param array $selectedFields save only these fields instead of all. |
||
268 | * |
||
269 | * @return bool |
||
270 | */ |
||
271 | public function save($selectedFields = []) |
||
291 | |||
292 | /** |
||
293 | * Scope to get only active items. |
||
294 | * |
||
295 | * @param BaseQuery $query |
||
296 | * |
||
297 | * @return BaseQuery |
||
298 | */ |
||
299 | public function scopeActive($query) |
||
305 | |||
306 | /** |
||
307 | * Create an array of fields that will be saved to database. |
||
308 | * |
||
309 | * @param $selectedFields |
||
310 | * |
||
311 | * @return array |
||
312 | */ |
||
313 | protected function normalizeFieldsForSave($selectedFields) |
||
328 | |||
329 | /** |
||
330 | * Determine whether the field should be stopped from passing to "update". |
||
331 | * |
||
332 | * @param string $field |
||
333 | * @param mixed $value |
||
334 | * @param array $selectedFields |
||
335 | * |
||
336 | * @return bool |
||
337 | */ |
||
338 | protected function fieldShouldNotBeSaved($field, $value, $selectedFields) |
||
353 | |||
354 | /** |
||
355 | * Instantiate bitrix entity object. |
||
356 | * |
||
357 | * @throws Exception |
||
358 | * |
||
359 | * @return object |
||
360 | */ |
||
361 | public static function instantiateObject() |
||
373 | |||
374 | /** |
||
375 | * Destroy bitrix entity object. |
||
376 | * |
||
377 | * @return void |
||
378 | */ |
||
379 | public static function destroyObject() |
||
383 | |||
384 | /** |
||
385 | * Instantiate a query object for the model. |
||
386 | * |
||
387 | * @throws Exception |
||
388 | * |
||
389 | * @return BaseQuery |
||
390 | */ |
||
391 | public static function query() |
||
395 | |||
396 | /** |
||
397 | * Set current model id. |
||
398 | * |
||
399 | * @param $id |
||
400 | */ |
||
401 | protected function setId($id) |
||
406 | } |
||
407 |
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: