Complex classes like BaseModel 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 BaseModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | abstract class BaseModel 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 | * Refresh model from database and place data to $this->fields. |
||
| 39 | * |
||
| 40 | * @return array |
||
| 41 | */ |
||
| 42 | abstract public function refresh(); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Refresh model fields from database and place them to $this->fields. |
||
| 46 | * |
||
| 47 | * @return array |
||
| 48 | */ |
||
| 49 | abstract public function refreshFields(); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Constructor. |
||
| 53 | * |
||
| 54 | * @param $id |
||
| 55 | * @param $fields |
||
| 56 | */ |
||
| 57 | public function __construct($id = null, $fields = null) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get all model attributes from cache or database. |
||
| 68 | * |
||
| 69 | * @return array |
||
| 70 | */ |
||
| 71 | public function get() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Get user groups from cache or database. |
||
| 82 | * |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | public function getFields() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Fill model fields if they are already known. |
||
| 96 | * Saves DB queries. |
||
| 97 | * |
||
| 98 | * @param array $fields |
||
| 99 | * |
||
| 100 | * @return null |
||
| 101 | */ |
||
| 102 | public function fill($fields) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Activate model. |
||
| 123 | * |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | public function activate() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Deactivate model. |
||
| 135 | * |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | public function deactivate() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Create new item in database. |
||
| 147 | * |
||
| 148 | * @param $fields |
||
| 149 | * |
||
| 150 | * @throws Exception |
||
| 151 | * |
||
| 152 | * @return static |
||
| 153 | */ |
||
| 154 | public static function create($fields) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @deprecated in favour of `static::query()->count()` |
||
| 180 | * |
||
| 181 | * Get count of items that match $filter. |
||
| 182 | * |
||
| 183 | * @param array $filter |
||
| 184 | * |
||
| 185 | * @return int |
||
| 186 | */ |
||
| 187 | public static function count(array $filter = []) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get item by its id. |
||
| 194 | * |
||
| 195 | * @param int $id |
||
| 196 | * |
||
| 197 | * @return static |
||
| 198 | */ |
||
| 199 | public static function find($id) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * |
||
| 206 | * Get item by its id. |
||
| 207 | * |
||
| 208 | * @param int $id |
||
| 209 | * |
||
| 210 | * @return static |
||
| 211 | */ |
||
| 212 | public static function getById($id) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Delete model. |
||
| 219 | * |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | public function delete() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Update model. |
||
| 237 | * |
||
| 238 | * @param array $fields |
||
| 239 | * |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | public function update(array $fields = []) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Save model to database. |
||
| 255 | * |
||
| 256 | * @param array $selectedFields save only these fields instead of all. |
||
| 257 | * |
||
| 258 | * @return bool |
||
| 259 | */ |
||
| 260 | public function save($selectedFields = []) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Scope to get only active items. |
||
| 283 | * |
||
| 284 | * @param BaseQuery $query |
||
| 285 | * |
||
| 286 | * @return BaseQuery |
||
| 287 | */ |
||
| 288 | public function scopeActive($query) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Create an array of fields that will be saved to database. |
||
| 297 | * |
||
| 298 | * @param $selectedFields |
||
| 299 | * |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | protected function normalizeFieldsForSave($selectedFields) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Determine whether the field should be stopped from passing to "update". |
||
| 320 | * |
||
| 321 | * @param string $field |
||
| 322 | * @param mixed $value |
||
| 323 | * @param array $selectedFields |
||
| 324 | * |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | protected function fieldShouldNotBeSaved($field, $value, $selectedFields) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Instantiate bitrix entity object. |
||
| 345 | * |
||
| 346 | * @throws Exception |
||
| 347 | * |
||
| 348 | * @return object |
||
| 349 | */ |
||
| 350 | public static function instantiateObject() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Destroy bitrix entity object. |
||
| 365 | * |
||
| 366 | * @return void |
||
| 367 | */ |
||
| 368 | public static function destroyObject() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Instantiate a query object for the model. |
||
| 375 | * |
||
| 376 | * @throws Exception |
||
| 377 | * |
||
| 378 | * @return BaseQuery |
||
| 379 | */ |
||
| 380 | public static function query() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Set current model id. |
||
| 387 | * |
||
| 388 | * @param $id |
||
| 389 | */ |
||
| 390 | protected function setId($id) |
||
| 395 | } |
||
| 396 |
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: