Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like HLModel 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 HLModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class HLModel extends ArrayableModel |
||
| 12 | { |
||
| 13 | use ModelEventsTrait; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Bitrix entity object. |
||
| 17 | * |
||
| 18 | * @var object |
||
| 19 | */ |
||
| 20 | public static $bxObject; |
||
| 21 | |||
| 22 | protected static $tableName; |
||
| 23 | |||
| 24 | |||
| 25 | /** |
||
| 26 | * Have fields been already fetched from DB? |
||
| 27 | * |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | protected $fieldsAreFetched = false; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Constructor. |
||
| 34 | * |
||
| 35 | * @param $id |
||
| 36 | * @param $fields |
||
| 37 | */ |
||
| 38 | public function __construct($id = null, $fields = null) |
||
| 46 | |||
| 47 | static public function tableName() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get all model attributes from cache or database. |
||
| 54 | * |
||
| 55 | * @return array |
||
| 56 | */ |
||
| 57 | public function get() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Load model fields from database if they are not loaded yet. |
||
| 66 | * |
||
| 67 | * @return $this |
||
| 68 | */ |
||
| 69 | public function load() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Get model fields from cache or database. |
||
| 80 | * |
||
| 81 | * @return array |
||
| 82 | */ |
||
| 83 | public function getFields() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Refresh model from database and place data to $this->fields. |
||
| 94 | * |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | public function refresh() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Refresh model fields and save them to a class field. |
||
| 104 | * |
||
| 105 | * @return array |
||
| 106 | */ |
||
| 107 | View Code Duplication | public function refreshFields() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Fill model fields if they are already known. |
||
| 122 | * Saves DB queries. |
||
| 123 | * |
||
| 124 | * @param array $fields |
||
| 125 | * |
||
| 126 | * @return void |
||
| 127 | */ |
||
| 128 | View Code Duplication | public function fill($fields) |
|
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * Create new item in database. |
||
| 150 | * |
||
| 151 | * @param $fields |
||
| 152 | * |
||
| 153 | * @throws Exception |
||
| 154 | * |
||
| 155 | * @return static|bool |
||
| 156 | */ |
||
| 157 | public static function create($fields) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Internal part of create to avoid problems with static and inheritance |
||
| 164 | * |
||
| 165 | * @param $fields |
||
| 166 | * |
||
| 167 | * @throws Exception |
||
| 168 | * |
||
| 169 | * @return static|bool |
||
| 170 | */ |
||
| 171 | protected static function internalCreate($fields) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get count of items that match $filter. |
||
| 193 | * |
||
| 194 | * @param array $filter |
||
| 195 | * |
||
| 196 | * @return int |
||
| 197 | */ |
||
| 198 | public static function count(array $filter = []) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get item by its id. |
||
| 205 | * |
||
| 206 | * @param int $id |
||
| 207 | * |
||
| 208 | * @return static|bool |
||
| 209 | */ |
||
| 210 | public static function find($id) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Delete model. |
||
| 217 | * |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | View Code Duplication | public function delete() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Update model. |
||
| 235 | * |
||
| 236 | * @param array $fields |
||
| 237 | * |
||
| 238 | * @return bool |
||
| 239 | */ |
||
| 240 | View Code Duplication | public function update(array $fields = []) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Save model to database. |
||
| 253 | * |
||
| 254 | * @param array $selectedFields save only these fields instead of all. |
||
| 255 | * |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | public function save($selectedFields = []) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Create an array of fields that will be saved to database. |
||
| 277 | * |
||
| 278 | * @param $selectedFields |
||
| 279 | * |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | View Code Duplication | protected function normalizeFieldsForSave($selectedFields) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Determine whether the field should be stopped from passing to "update". |
||
| 300 | * |
||
| 301 | * @param string $field |
||
| 302 | * @param mixed $value |
||
| 303 | * @param array $selectedFields |
||
| 304 | * |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | protected function fieldShouldNotBeSaved($field, $value, $selectedFields) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Instantiate bitrix entity object. |
||
| 320 | * |
||
| 321 | * @throws Exception |
||
| 322 | * |
||
| 323 | * @return object |
||
| 324 | */ |
||
| 325 | public static function instantiateObject() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Destroy bitrix entity object. |
||
| 338 | * |
||
| 339 | * @return void |
||
| 340 | */ |
||
| 341 | public static function destroyObject() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Instantiate a query object for the model. |
||
| 348 | * |
||
| 349 | * @throws Exception |
||
| 350 | * |
||
| 351 | * @return BaseQuery |
||
| 352 | */ |
||
| 353 | public static function query() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Set current model id. |
||
| 360 | * |
||
| 361 | * @param $id |
||
| 362 | */ |
||
| 363 | protected function setId($id) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Handle dynamic static method calls into a new query. |
||
| 371 | * |
||
| 372 | * @param string $method |
||
| 373 | * @param array $parameters |
||
| 374 | * @return mixed |
||
| 375 | */ |
||
| 376 | public static function __callStatic($method, $parameters) |
||
| 380 | } |