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 void |
||
| 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|bool |
||
| 153 | */ |
||
| 154 | public static function create($fields) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get count of items that match $filter. |
||
| 180 | * |
||
| 181 | * @param array $filter |
||
| 182 | * |
||
| 183 | * @return int |
||
| 184 | */ |
||
| 185 | public static function count(array $filter = []) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get item by its id. |
||
| 192 | * |
||
| 193 | * @param int $id |
||
| 194 | * |
||
| 195 | * @return static|bool |
||
| 196 | */ |
||
| 197 | public static function find($id) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Delete model. |
||
| 204 | * |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | public function delete() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Update model. |
||
| 222 | * |
||
| 223 | * @param array $fields |
||
| 224 | * |
||
| 225 | * @return bool |
||
| 226 | */ |
||
| 227 | public function update(array $fields = []) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Save model to database. |
||
| 240 | * |
||
| 241 | * @param array $selectedFields save only these fields instead of all. |
||
| 242 | * |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | public function save($selectedFields = []) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Scope to get only active items. |
||
| 268 | * |
||
| 269 | * @param BaseQuery $query |
||
| 270 | * |
||
| 271 | * @return BaseQuery |
||
| 272 | */ |
||
| 273 | public function scopeActive($query) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Create an array of fields that will be saved to database. |
||
| 282 | * |
||
| 283 | * @param $selectedFields |
||
| 284 | * |
||
| 285 | * @return array |
||
| 286 | */ |
||
| 287 | protected function normalizeFieldsForSave($selectedFields) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Determine whether the field should be stopped from passing to "update". |
||
| 305 | * |
||
| 306 | * @param string $field |
||
| 307 | * @param mixed $value |
||
| 308 | * @param array $selectedFields |
||
| 309 | * |
||
| 310 | * @return bool |
||
| 311 | */ |
||
| 312 | protected function fieldShouldNotBeSaved($field, $value, $selectedFields) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Instantiate bitrix entity object. |
||
| 330 | * |
||
| 331 | * @throws Exception |
||
| 332 | * |
||
| 333 | * @return object |
||
| 334 | */ |
||
| 335 | public static function instantiateObject() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Destroy bitrix entity object. |
||
| 350 | * |
||
| 351 | * @return void |
||
| 352 | */ |
||
| 353 | public static function destroyObject() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Instantiate a query object for the model. |
||
| 360 | * |
||
| 361 | * @throws Exception |
||
| 362 | * |
||
| 363 | * @return BaseQuery |
||
| 364 | */ |
||
| 365 | public static function query() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Set current model id. |
||
| 372 | * |
||
| 373 | * @param $id |
||
| 374 | */ |
||
| 375 | protected function setId($id) |
||
| 380 | } |
||
| 381 |
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: