Complex classes like Model 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 Model, and based on these observations, apply Extract Interface, too.
| 1 | <?php /** MicroModel */ |
||
| 23 | abstract class Model extends FormModel implements IModel |
||
| 24 | { |
||
| 25 | /** @var string $primaryKey Primary key on table */ |
||
| 26 | public static $primaryKey = 'id'; |
||
| 27 | /** @var string $tableName Table name */ |
||
| 28 | public static $tableName; |
||
| 29 | |||
| 30 | /** @var boolean $_isNewRecord Is new record? */ |
||
| 31 | protected $_isNewRecord = false; |
||
| 32 | /** @var array $cacheRelations Cached loads relations */ |
||
| 33 | protected $cacheRelations = []; |
||
| 34 | |||
| 35 | |||
| 36 | /** |
||
| 37 | * Constructor for model |
||
| 38 | * |
||
| 39 | * @access public |
||
| 40 | * |
||
| 41 | * @param boolean $new is new model? |
||
| 42 | * |
||
| 43 | * @result void |
||
| 44 | * @throws Exception |
||
| 45 | */ |
||
| 46 | public function __construct($new = true) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Finder by primary key |
||
| 59 | * |
||
| 60 | * @access public |
||
| 61 | * |
||
| 62 | * @param int|string $value unique value |
||
| 63 | * |
||
| 64 | * @return mixed |
||
| 65 | * @throws \Micro\base\Exception |
||
| 66 | * @static |
||
| 67 | */ |
||
| 68 | public static function findByPk($value) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Find models by attributes |
||
| 75 | * |
||
| 76 | * @access public |
||
| 77 | * |
||
| 78 | * @param array $attributes attributes and data for search |
||
| 79 | * @param bool $single single or more |
||
| 80 | * |
||
| 81 | * @return mixed |
||
| 82 | * @throws \Micro\base\Exception |
||
| 83 | */ |
||
| 84 | public static function findByAttributes(array $attributes = [], $single = false) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Finder data in DB |
||
| 97 | * |
||
| 98 | * @access public |
||
| 99 | * |
||
| 100 | * @param IQuery $query query to search |
||
| 101 | * @param boolean $single is single |
||
| 102 | * |
||
| 103 | * @return mixed One or more data |
||
| 104 | * @throws \Micro\base\Exception |
||
| 105 | * @static |
||
| 106 | */ |
||
| 107 | public static function finder(IQuery $query = null, $single = false) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Find by model attribute values |
||
| 119 | * |
||
| 120 | * @access public |
||
| 121 | * |
||
| 122 | * @param bool $single Is a single? |
||
| 123 | * |
||
| 124 | * @return mixed |
||
| 125 | * @throws \Micro\base\Exception |
||
| 126 | */ |
||
| 127 | public function find($single = false) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get attributes defined into model |
||
| 134 | * |
||
| 135 | * @access public |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | * @throws Exception |
||
| 139 | */ |
||
| 140 | public function getAttributes() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Get relation data or magic properties |
||
| 152 | * |
||
| 153 | * @access public |
||
| 154 | * |
||
| 155 | * @param string $name |
||
| 156 | * |
||
| 157 | * @return mixed |
||
| 158 | * @throws Exception |
||
| 159 | */ |
||
| 160 | public function __get($name) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @inheritdoc |
||
| 195 | */ |
||
| 196 | public function relations() |
||
| 197 | { |
||
| 198 | return new Relations; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Save changes |
||
| 203 | * |
||
| 204 | * @access public |
||
| 205 | * |
||
| 206 | * @param bool $validate Validated data? |
||
| 207 | * |
||
| 208 | * @return boolean |
||
| 209 | * @throws Exception |
||
| 210 | */ |
||
| 211 | final public function save($validate = false) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Is new record? |
||
| 232 | * |
||
| 233 | * @access public |
||
| 234 | * @return boolean |
||
| 235 | */ |
||
| 236 | public function isNewRecord() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Create changes |
||
| 243 | * |
||
| 244 | * @access public |
||
| 245 | * @return boolean |
||
| 246 | * @throws Exception |
||
| 247 | */ |
||
| 248 | final public function create() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @inheritdoc |
||
| 277 | */ |
||
| 278 | public function beforeCreate() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @inheritdoc |
||
| 285 | */ |
||
| 286 | public function beforeSave() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Merge local attributes and db attributes |
||
| 293 | * |
||
| 294 | * @access protected |
||
| 295 | * |
||
| 296 | * @return array |
||
| 297 | * @throws \Micro\base\Exception |
||
| 298 | */ |
||
| 299 | protected function mergeAttributesDb() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Check attribute exists into table |
||
| 321 | * |
||
| 322 | * @access public |
||
| 323 | * |
||
| 324 | * @param string $name Attribute name |
||
| 325 | * |
||
| 326 | * @return boolean |
||
| 327 | * @throws Exception |
||
| 328 | */ |
||
| 329 | public function checkAttributeExists($name) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @inheritdoc |
||
| 348 | */ |
||
| 349 | public function afterCreate() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @inheritdoc |
||
| 355 | */ |
||
| 356 | public function afterSave() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Update changes |
||
| 362 | * |
||
| 363 | * @access public |
||
| 364 | * |
||
| 365 | * @param string $where condition for search |
||
| 366 | * |
||
| 367 | * @throws Exception |
||
| 368 | * @return boolean |
||
| 369 | */ |
||
| 370 | final public function update($where = null) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @inheritdoc |
||
| 402 | */ |
||
| 403 | public function beforeUpdate() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @inheritdoc |
||
| 410 | */ |
||
| 411 | public function afterUpdate() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Delete changes |
||
| 417 | * |
||
| 418 | * @access public |
||
| 419 | * @return boolean |
||
| 420 | * @throws Exception |
||
| 421 | */ |
||
| 422 | final public function delete() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @inheritdoc |
||
| 450 | */ |
||
| 451 | public function beforeDelete() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @inheritdoc |
||
| 458 | */ |
||
| 459 | public function afterDelete() |
||
| 462 | } |
||
| 463 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: