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 |
||
| 10 | abstract class BitrixModel extends ArrayableModel |
||
| 11 | { |
||
| 12 | use ModelEventsTrait; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Bitrix entity object. |
||
| 16 | * |
||
| 17 | * @var object |
||
| 18 | */ |
||
| 19 | public static $bxObject; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Corresponding object class name. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected static $objectClass = ''; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Have fields been already fetched from DB? |
||
| 30 | * |
||
| 31 | * @var bool |
||
| 32 | */ |
||
| 33 | protected $fieldsAreFetched = false; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Constructor. |
||
| 37 | * |
||
| 38 | * @param $id |
||
| 39 | * @param $fields |
||
| 40 | */ |
||
| 41 | public function __construct($id = null, $fields = null) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get all model attributes from cache or database. |
||
| 52 | * |
||
| 53 | * @return array |
||
| 54 | */ |
||
| 55 | public function get() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Load model fields from database if they are not loaded yet. |
||
| 64 | * |
||
| 65 | * @return $this |
||
| 66 | */ |
||
| 67 | public function load() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Get model fields from cache or database. |
||
| 78 | * |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | public function getFields() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Refresh model from database and place data to $this->fields. |
||
| 92 | * |
||
| 93 | * @return array |
||
| 94 | */ |
||
| 95 | public function refresh() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Refresh model fields and save them to a class field. |
||
| 102 | * |
||
| 103 | * @return array |
||
| 104 | */ |
||
| 105 | public function refreshFields() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Fill model fields if they are already known. |
||
| 120 | * Saves DB queries. |
||
| 121 | * |
||
| 122 | * @param array $fields |
||
| 123 | * |
||
| 124 | * @return void |
||
| 125 | */ |
||
| 126 | public function fill($fields) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Activate model. |
||
| 147 | * |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | public function activate() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Deactivate model. |
||
| 159 | * |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | public function deactivate() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Create new item in database. |
||
| 171 | * |
||
| 172 | * @param $fields |
||
| 173 | * |
||
| 174 | * @throws Exception |
||
| 175 | * |
||
| 176 | * @return static|bool |
||
| 177 | */ |
||
| 178 | public static function create($fields) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get count of items that match $filter. |
||
| 204 | * |
||
| 205 | * @param array $filter |
||
| 206 | * |
||
| 207 | * @return int |
||
| 208 | */ |
||
| 209 | public static function count(array $filter = []) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get item by its id. |
||
| 216 | * |
||
| 217 | * @param int $id |
||
| 218 | * |
||
| 219 | * @return static|bool |
||
| 220 | */ |
||
| 221 | public static function find($id) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Delete model. |
||
| 228 | * |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | public function delete() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Update model. |
||
| 246 | * |
||
| 247 | * @param array $fields |
||
| 248 | * |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | public function update(array $fields = []) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Save model to database. |
||
| 264 | * |
||
| 265 | * @param array $selectedFields save only these fields instead of all. |
||
| 266 | * |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | public function save($selectedFields = []) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Scope to get only active items. |
||
| 292 | * |
||
| 293 | * @param BaseQuery $query |
||
| 294 | * |
||
| 295 | * @return BaseQuery |
||
| 296 | */ |
||
| 297 | public function scopeActive($query) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Create an array of fields that will be saved to database. |
||
| 306 | * |
||
| 307 | * @param $selectedFields |
||
| 308 | * |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | protected function normalizeFieldsForSave($selectedFields) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Determine whether the field should be stopped from passing to "update". |
||
| 329 | * |
||
| 330 | * @param string $field |
||
| 331 | * @param mixed $value |
||
| 332 | * @param array $selectedFields |
||
| 333 | * |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | protected function fieldShouldNotBeSaved($field, $value, $selectedFields) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Instantiate bitrix entity object. |
||
| 354 | * |
||
| 355 | * @throws Exception |
||
| 356 | * |
||
| 357 | * @return object |
||
| 358 | */ |
||
| 359 | public static function instantiateObject() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Destroy bitrix entity object. |
||
| 374 | * |
||
| 375 | * @return void |
||
| 376 | */ |
||
| 377 | public static function destroyObject() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Instantiate a query object for the model. |
||
| 384 | * |
||
| 385 | * @throws Exception |
||
| 386 | * |
||
| 387 | * @return BaseQuery |
||
| 388 | */ |
||
| 389 | public static function query() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Set current model id. |
||
| 396 | * |
||
| 397 | * @param $id |
||
| 398 | */ |
||
| 399 | protected function setId($id) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Handle dynamic static method calls into a new query. |
||
| 407 | * |
||
| 408 | * @param string $method |
||
| 409 | * @param array $parameters |
||
| 410 | * @return mixed |
||
| 411 | */ |
||
| 412 | public static function __callStatic($method, $parameters) |
||
| 416 | } |
||
| 417 |
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: