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 | * Array of model fields keys that needs to be saved with next save(). |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $fieldsSelectedForSave = []; |
||
20 | |||
21 | /** |
||
22 | * Array of errors that are passed to model events. |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $eventErrors = []; |
||
27 | |||
28 | /** |
||
29 | * Have fields been already fetched from DB? |
||
30 | * |
||
31 | * @var bool |
||
32 | */ |
||
33 | protected $fieldsAreFetched = false; |
||
34 | |||
35 | /** |
||
36 | * @var array - Array related models indexed by the relation names |
||
37 | */ |
||
38 | public $related = []; |
||
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) |
||
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() |
||
141 | |||
142 | /** |
||
143 | * Fill model fields if they are already known. |
||
144 | * Saves DB queries. |
||
145 | * |
||
146 | * @param array $fields |
||
147 | * |
||
148 | * @return void |
||
149 | */ |
||
150 | public function fill($fields) |
||
168 | |||
169 | /** |
||
170 | * Set current model id. |
||
171 | * |
||
172 | * @param $id |
||
173 | */ |
||
174 | protected function setId($id) |
||
179 | |||
180 | /** |
||
181 | * Create new item in database. |
||
182 | * |
||
183 | * @param $fields |
||
184 | * |
||
185 | * @throws LogicException |
||
186 | * |
||
187 | * @return static|bool |
||
188 | */ |
||
189 | public static function create($fields) |
||
193 | |||
194 | /** |
||
195 | * Get count of items that match $filter. |
||
196 | * |
||
197 | * @param array $filter |
||
198 | * |
||
199 | * @return int |
||
200 | */ |
||
201 | public static function count(array $filter = []) |
||
205 | |||
206 | /** |
||
207 | * Get item by its id. |
||
208 | * |
||
209 | * @param int $id |
||
210 | * |
||
211 | * @return static|bool |
||
212 | */ |
||
213 | public static function find($id) |
||
217 | |||
218 | /** |
||
219 | * Update model. |
||
220 | * |
||
221 | * @param array $fields |
||
222 | * |
||
223 | * @return bool |
||
224 | */ |
||
225 | public function update(array $fields = []) |
||
235 | |||
236 | /** |
||
237 | * Create an array of fields that will be saved to database. |
||
238 | * |
||
239 | * @param $selectedFields |
||
240 | * |
||
241 | * @return array |
||
242 | */ |
||
243 | protected function normalizeFieldsForSave($selectedFields) |
||
258 | |||
259 | /** |
||
260 | * Instantiate a query object for the model. |
||
261 | * |
||
262 | * @throws LogicException |
||
263 | * |
||
264 | * @return BaseQuery |
||
265 | */ |
||
266 | public static function query() |
||
270 | |||
271 | /** |
||
272 | * Handle dynamic static method calls into a new query. |
||
273 | * |
||
274 | * @param string $method |
||
275 | * @param array $parameters |
||
276 | * @return mixed |
||
277 | */ |
||
278 | public static function __callStatic($method, $parameters) |
||
282 | |||
283 | /** |
||
284 | * Returns the value of a model property. |
||
285 | * |
||
286 | * This method will check in the following order and act accordingly: |
||
287 | * |
||
288 | * - a property defined by a getter: return the getter result |
||
289 | * |
||
290 | * Do not call this method directly as it is a PHP magic method that |
||
291 | * will be implicitly called when executing `$value = $component->property;`. |
||
292 | * @param string $name the property name |
||
293 | * @return mixed the property value |
||
294 | * @throws \Exception if the property is not defined |
||
295 | * @see __set() |
||
296 | */ |
||
297 | public function __get($name) |
||
319 | |||
320 | /** |
||
321 | * Получить запрос для релейшена по имени |
||
322 | * @param string $name - название релейшена, например `orders` для релейшена, определенного через метод getOrders() |
||
323 | * @param bool $throwException - кидать ли исключение в случае ошибки |
||
324 | * @return BaseQuery - запрос для подгрузки релейшена |
||
325 | * @throws \InvalidArgumentException |
||
326 | */ |
||
327 | public function getRelation($name, $throwException = true) |
||
350 | |||
351 | /** |
||
352 | * Reset event errors back to default. |
||
353 | */ |
||
354 | protected function resetEventErrors() |
||
358 | |||
359 | /** |
||
360 | * Declares a `has-one` relation. |
||
361 | * The declaration is returned in terms of a relational [[BaseQuery]] instance |
||
362 | * through which the related record can be queried and retrieved back. |
||
363 | * |
||
364 | * A `has-one` relation means that there is at most one related record matching |
||
365 | * the criteria set by this relation, e.g., a customer has one country. |
||
366 | * |
||
367 | * For example, to declare the `country` relation for `Customer` class, we can write |
||
368 | * the following code in the `Customer` class: |
||
369 | * |
||
370 | * ```php |
||
371 | * public function country() |
||
372 | * { |
||
373 | * return $this->hasOne(Country::className(), 'ID', 'PROPERTY_COUNTRY'); |
||
374 | * } |
||
375 | * ``` |
||
376 | * |
||
377 | * Note that in the above, the 'ID' key in the `$link` parameter refers to an attribute name |
||
378 | * in the related class `Country`, while the 'PROPERTY_COUNTRY' value refers to an attribute name |
||
379 | * in the current BaseBitrixModel class. |
||
380 | * |
||
381 | * Call methods declared in [[BaseQuery]] to further customize the relation. |
||
382 | * |
||
383 | * @param string $class the class name of the related record |
||
384 | * @param string $foreignKey |
||
385 | * @param string $localKey |
||
386 | * @return BaseQuery the relational query object. |
||
387 | */ |
||
388 | public function hasOne($class, $foreignKey, $localKey = 'ID') |
||
392 | |||
393 | /** |
||
394 | * Declares a `has-many` relation. |
||
395 | * The declaration is returned in terms of a relational [[BaseQuery]] instance |
||
396 | * through which the related record can be queried and retrieved back. |
||
397 | * |
||
398 | * A `has-many` relation means that there are multiple related records matching |
||
399 | * the criteria set by this relation, e.g., a customer has many orders. |
||
400 | * |
||
401 | * For example, to declare the `orders` relation for `Customer` class, we can write |
||
402 | * the following code in the `Customer` class: |
||
403 | * |
||
404 | * ```php |
||
405 | * public function orders() |
||
406 | * { |
||
407 | * return $this->hasMany(Order::className(), 'PROPERTY_COUNTRY_VALUE', 'ID'); |
||
408 | * } |
||
409 | * ``` |
||
410 | * |
||
411 | * Note that in the above, the 'customer_id' key in the `$link` parameter refers to |
||
412 | * an attribute name in the related class `Order`, while the 'id' value refers to |
||
413 | * an attribute name in the current BaseBitrixModel class. |
||
414 | * |
||
415 | * Call methods declared in [[BaseQuery]] to further customize the relation. |
||
416 | * |
||
417 | * @param string $class the class name of the related record |
||
418 | * @param string $foreignKey |
||
419 | * @param string $localKey |
||
420 | * @return BaseQuery the relational query object. |
||
421 | */ |
||
422 | public function hasMany($class, $foreignKey, $localKey = 'ID') |
||
426 | |||
427 | /** |
||
428 | * Creates a query instance for `has-one` or `has-many` relation. |
||
429 | * @param string $class the class name of the related record. |
||
430 | * @param string $foreignKey |
||
431 | * @param string $localKey |
||
432 | * @param bool $multiple whether this query represents a relation to more than one record. |
||
433 | * @return BaseQuery the relational query object. |
||
434 | * @see hasOne() |
||
435 | * @see hasMany() |
||
436 | */ |
||
437 | protected function createRelationQuery($class, $foreignKey, $localKey, $multiple) |
||
448 | |||
449 | /** |
||
450 | * Записать модели как связанные |
||
451 | * @param string $name - название релейшена |
||
452 | * @param Collection|BaseBitrixModel $records - связанные модели |
||
453 | * @see getRelation() |
||
454 | */ |
||
455 | public function populateRelation($name, $records) |
||
459 | } |
||
460 |
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: