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) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Internal part of create to avoid problems with static and inheritance |
||
| 185 | * |
||
| 186 | * @param $fields |
||
| 187 | * |
||
| 188 | * @throws Exception |
||
| 189 | * |
||
| 190 | * @return static|bool |
||
| 191 | */ |
||
| 192 | protected static function internalCreate($fields) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Get count of items that match $filter. |
||
| 218 | * |
||
| 219 | * @param array $filter |
||
| 220 | * |
||
| 221 | * @return int |
||
| 222 | */ |
||
| 223 | public static function count(array $filter = []) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get item by its id. |
||
| 230 | * |
||
| 231 | * @param int $id |
||
| 232 | * |
||
| 233 | * @return static|bool |
||
| 234 | */ |
||
| 235 | public static function find($id) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Delete model. |
||
| 242 | * |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | public function delete() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Update model. |
||
| 260 | * |
||
| 261 | * @param array $fields |
||
| 262 | * |
||
| 263 | * @return bool |
||
| 264 | */ |
||
| 265 | public function update(array $fields = []) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Save model to database. |
||
| 278 | * |
||
| 279 | * @param array $selectedFields save only these fields instead of all. |
||
| 280 | * |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | public function save($selectedFields = []) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Scope to get only active items. |
||
| 306 | * |
||
| 307 | * @param BaseQuery $query |
||
| 308 | * |
||
| 309 | * @return BaseQuery |
||
| 310 | */ |
||
| 311 | public function scopeActive($query) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Create an array of fields that will be saved to database. |
||
| 320 | * |
||
| 321 | * @param $selectedFields |
||
| 322 | * |
||
| 323 | * @return array |
||
| 324 | */ |
||
| 325 | protected function normalizeFieldsForSave($selectedFields) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Determine whether the field should be stopped from passing to "update". |
||
| 343 | * |
||
| 344 | * @param string $field |
||
| 345 | * @param mixed $value |
||
| 346 | * @param array $selectedFields |
||
| 347 | * |
||
| 348 | * @return bool |
||
| 349 | */ |
||
| 350 | protected function fieldShouldNotBeSaved($field, $value, $selectedFields) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Instantiate bitrix entity object. |
||
| 368 | * |
||
| 369 | * @throws Exception |
||
| 370 | * |
||
| 371 | * @return object |
||
| 372 | */ |
||
| 373 | public static function instantiateObject() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Destroy bitrix entity object. |
||
| 388 | * |
||
| 389 | * @return void |
||
| 390 | */ |
||
| 391 | public static function destroyObject() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Instantiate a query object for the model. |
||
| 398 | * |
||
| 399 | * @throws Exception |
||
| 400 | * |
||
| 401 | * @return BaseQuery |
||
| 402 | */ |
||
| 403 | public static function query() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Set current model id. |
||
| 410 | * |
||
| 411 | * @param $id |
||
| 412 | */ |
||
| 413 | protected function setId($id) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Handle dynamic static method calls into a new query. |
||
| 421 | * |
||
| 422 | * @param string $method |
||
| 423 | * @param array $parameters |
||
| 424 | * @return mixed |
||
| 425 | */ |
||
| 426 | public static function __callStatic($method, $parameters) |
||
| 430 | } |
||
| 431 |
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: