Complex classes like BaseBitrixModel 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 BaseBitrixModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | abstract class BaseBitrixModel extends ArrayableModel |
||
11 | { |
||
12 | use ModelEventsTrait; |
||
13 | |||
14 | /** |
||
15 | * @var string|null |
||
16 | */ |
||
17 | protected static $currentLanguage = null; |
||
18 | |||
19 | /** |
||
20 | * Array of model fields keys that needs to be saved with next save(). |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $fieldsSelectedForSave = []; |
||
25 | |||
26 | /** |
||
27 | * Array of errors that are passed to model events. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $eventErrors = []; |
||
32 | |||
33 | /** |
||
34 | * Have fields been already fetched from DB? |
||
35 | * |
||
36 | * @var bool |
||
37 | */ |
||
38 | protected $fieldsAreFetched = false; |
||
39 | |||
40 | /** |
||
41 | * Internal part of create to avoid problems with static and inheritance |
||
42 | * |
||
43 | * @param $fields |
||
44 | * |
||
45 | * @throws LogicException |
||
46 | * |
||
47 | * @return static|bool |
||
48 | */ |
||
49 | protected static function internalCreate($fields) |
||
50 | { |
||
51 | throw new LogicException('internalCreate is not implemented'); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Save model to database. |
||
56 | * |
||
57 | * @param array $selectedFields save only these fields instead of all. |
||
58 | * |
||
59 | * @return bool |
||
60 | */ |
||
61 | abstract public function save($selectedFields = []); |
||
62 | |||
63 | /** |
||
64 | * Determine whether the field should be stopped from passing to "update". |
||
65 | * |
||
66 | * @param string $field |
||
67 | * @param mixed $value |
||
68 | * @param array $selectedFields |
||
69 | * |
||
70 | * @return bool |
||
71 | */ |
||
72 | abstract protected function fieldShouldNotBeSaved($field, $value, $selectedFields); |
||
73 | |||
74 | /** |
||
75 | * Get all model attributes from cache or database. |
||
76 | * |
||
77 | * @return array |
||
78 | */ |
||
79 | public function get() |
||
85 | |||
86 | /** |
||
87 | * Load model fields from database if they are not loaded yet. |
||
88 | * |
||
89 | * @return $this |
||
90 | */ |
||
91 | public function load() |
||
99 | |||
100 | /** |
||
101 | * Get model fields from cache or database. |
||
102 | * |
||
103 | * @return array |
||
104 | */ |
||
105 | public function getFields() |
||
113 | |||
114 | /** |
||
115 | * Refresh model from database and place data to $this->fields. |
||
116 | * |
||
117 | * @return array |
||
118 | */ |
||
119 | public function refresh() |
||
123 | |||
124 | /** |
||
125 | * Refresh model fields and save them to a class field. |
||
126 | * |
||
127 | * @return array |
||
128 | */ |
||
129 | public function refreshFields() |
||
143 | |||
144 | /** |
||
145 | * Fill model fields if they are already known. |
||
146 | * Saves DB queries. |
||
147 | * |
||
148 | * @param array $fields |
||
149 | * |
||
150 | * @return void |
||
151 | */ |
||
152 | public function fill($fields) |
||
172 | |||
173 | /** |
||
174 | * Set current model id. |
||
175 | * |
||
176 | * @param $id |
||
177 | */ |
||
178 | protected function setId($id) |
||
183 | |||
184 | /** |
||
185 | * Create new item in database. |
||
186 | * |
||
187 | * @param $fields |
||
188 | * |
||
189 | * @throws LogicException |
||
190 | * |
||
191 | * @return static|bool |
||
192 | */ |
||
193 | public static function create($fields) |
||
197 | |||
198 | /** |
||
199 | * Get count of items that match $filter. |
||
200 | * |
||
201 | * @param array $filter |
||
202 | * |
||
203 | * @return int |
||
204 | */ |
||
205 | public static function count(array $filter = []) |
||
209 | |||
210 | /** |
||
211 | * Get item by its id. |
||
212 | * |
||
213 | * @param int $id |
||
214 | * |
||
215 | * @return static|bool |
||
216 | */ |
||
217 | public static function find($id) |
||
221 | |||
222 | /** |
||
223 | * Update model. |
||
224 | * |
||
225 | * @param array $fields |
||
226 | * |
||
227 | * @return bool |
||
228 | */ |
||
229 | public function update(array $fields = []) |
||
239 | |||
240 | /** |
||
241 | * Create an array of fields that will be saved to database. |
||
242 | * |
||
243 | * @param $selectedFields |
||
244 | * |
||
245 | * @return array|null |
||
246 | */ |
||
247 | protected function normalizeFieldsForSave($selectedFields) |
||
262 | |||
263 | /** |
||
264 | * Instantiate a query object for the model. |
||
265 | * |
||
266 | * @throws LogicException |
||
267 | * |
||
268 | * @return BaseQuery |
||
269 | */ |
||
270 | public static function query() |
||
274 | |||
275 | /** |
||
276 | * Handle dynamic static method calls into a new query. |
||
277 | * |
||
278 | * @param string $method |
||
279 | * @param array $parameters |
||
280 | * @return mixed |
||
281 | */ |
||
282 | public static function __callStatic($method, $parameters) |
||
286 | |||
287 | /** |
||
288 | * Returns the value of a model property. |
||
289 | * |
||
290 | * This method will check in the following order and act accordingly: |
||
291 | * |
||
292 | * - a property defined by a getter: return the getter result |
||
293 | * |
||
294 | * Do not call this method directly as it is a PHP magic method that |
||
295 | * will be implicitly called when executing `$value = $component->property;`. |
||
296 | * @param string $name the property name |
||
297 | * @return mixed the property value |
||
298 | * @throws \Exception if the property is not defined |
||
299 | * @see __set() |
||
300 | */ |
||
301 | public function __get($name) |
||
323 | |||
324 | /** |
||
325 | * Получить запрос для релейшена по имени |
||
326 | * @param string $name - название релейшена, например `orders` для релейшена, определенного через метод getOrders() |
||
327 | * @param bool $throwException - кидать ли исключение в случае ошибки |
||
328 | * @return BaseQuery - запрос для подгрузки релейшена |
||
329 | * @throws \InvalidArgumentException |
||
330 | */ |
||
331 | public function getRelation($name, $throwException = true) |
||
354 | |||
355 | /** |
||
356 | * Reset event errors back to default. |
||
357 | */ |
||
358 | protected function resetEventErrors() |
||
362 | |||
363 | /** |
||
364 | * Declares a `has-one` relation. |
||
365 | * The declaration is returned in terms of a relational [[BaseQuery]] instance |
||
366 | * through which the related record can be queried and retrieved back. |
||
367 | * |
||
368 | * A `has-one` relation means that there is at most one related record matching |
||
369 | * the criteria set by this relation, e.g., a customer has one country. |
||
370 | * |
||
371 | * For example, to declare the `country` relation for `Customer` class, we can write |
||
372 | * the following code in the `Customer` class: |
||
373 | * |
||
374 | * ```php |
||
375 | * public function country() |
||
376 | * { |
||
377 | * return $this->hasOne(Country::className(), 'ID', 'PROPERTY_COUNTRY'); |
||
378 | * } |
||
379 | * ``` |
||
380 | * |
||
381 | * Note that in the above, the 'ID' key in the `$link` parameter refers to an attribute name |
||
382 | * in the related class `Country`, while the 'PROPERTY_COUNTRY' value refers to an attribute name |
||
383 | * in the current BaseBitrixModel class. |
||
384 | * |
||
385 | * Call methods declared in [[BaseQuery]] to further customize the relation. |
||
386 | * |
||
387 | * @param string $class the class name of the related record |
||
388 | * @param string $foreignKey |
||
389 | * @param string $localKey |
||
390 | * @return BaseQuery the relational query object. |
||
391 | */ |
||
392 | public function hasOne($class, $foreignKey, $localKey = 'ID') |
||
396 | |||
397 | /** |
||
398 | * Declares a `has-many` relation. |
||
399 | * The declaration is returned in terms of a relational [[BaseQuery]] instance |
||
400 | * through which the related record can be queried and retrieved back. |
||
401 | * |
||
402 | * A `has-many` relation means that there are multiple related records matching |
||
403 | * the criteria set by this relation, e.g., a customer has many orders. |
||
404 | * |
||
405 | * For example, to declare the `orders` relation for `Customer` class, we can write |
||
406 | * the following code in the `Customer` class: |
||
407 | * |
||
408 | * ```php |
||
409 | * public function orders() |
||
410 | * { |
||
411 | * return $this->hasMany(Order::className(), 'PROPERTY_COUNTRY_VALUE', 'ID'); |
||
412 | * } |
||
413 | * ``` |
||
414 | * |
||
415 | * Note that in the above, the 'customer_id' key in the `$link` parameter refers to |
||
416 | * an attribute name in the related class `Order`, while the 'id' value refers to |
||
417 | * an attribute name in the current BaseBitrixModel class. |
||
418 | * |
||
419 | * Call methods declared in [[BaseQuery]] to further customize the relation. |
||
420 | * |
||
421 | * @param string $class the class name of the related record |
||
422 | * @param string $foreignKey |
||
423 | * @param string $localKey |
||
424 | * @return BaseQuery the relational query object. |
||
425 | */ |
||
426 | public function hasMany($class, $foreignKey, $localKey = 'ID') |
||
430 | |||
431 | /** |
||
432 | * Creates a query instance for `has-one` or `has-many` relation. |
||
433 | * @param string $class the class name of the related record. |
||
434 | * @param string $foreignKey |
||
435 | * @param string $localKey |
||
436 | * @param bool $multiple whether this query represents a relation to more than one record. |
||
437 | * @return BaseQuery the relational query object. |
||
438 | * @see hasOne() |
||
439 | * @see hasMany() |
||
440 | */ |
||
441 | protected function createRelationQuery($class, $foreignKey, $localKey, $multiple) |
||
452 | |||
453 | /** |
||
454 | * Записать модели как связанные |
||
455 | * @param string $name - название релейшена |
||
456 | * @param Collection|BaseBitrixModel $records - связанные модели |
||
457 | * @see getRelation() |
||
458 | */ |
||
459 | public function populateRelation($name, $records) |
||
463 | |||
464 | /** |
||
465 | * Setter for currentLanguage. |
||
466 | * |
||
467 | * @param $language |
||
468 | * @return mixed |
||
469 | */ |
||
470 | public static function setCurrentLanguage($language) |
||
474 | |||
475 | /** |
||
476 | * Getter for currentLanguage. |
||
477 | * |
||
478 | * @return string |
||
479 | */ |
||
480 | public static function getCurrentLanguage() |
||
484 | |||
485 | /** |
||
486 | * Get value from language field according to current language. |
||
487 | * |
||
488 | * @param $field |
||
489 | * @return mixed |
||
490 | */ |
||
491 | protected function getValueFromLanguageField($field) |
||
497 | } |
||
498 |
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: