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) |
||
| 161 | { |
||
| 162 | /** @var array $relation */ |
||
| 163 | if ($relation = $this->relations()->get($name)) { |
||
| 164 | if (empty($this->cacheRelations[$name])) { |
||
| 165 | $sql = new Query((new ConnectionInjector)->getDriver()); |
||
| 166 | |||
| 167 | if ((new ConnectionInjector)->getDriver()->getDriverType() === 'pgsql') { |
||
| 168 | $sql->addWhere('"m"."' . $relation['On'][1] . '"=:' . $relation['On'][0]); |
||
| 169 | } else { |
||
| 170 | $sql->addWhere('`m`.`' . $relation['On'][1] . '`=:' . $relation['On'][0]); |
||
| 171 | } |
||
| 172 | |||
| 173 | if ($relation['Where']) { |
||
| 174 | $sql->addWhere($relation['Where']); |
||
| 175 | } |
||
| 176 | if ($relation['Params']) { |
||
| 177 | $sql->params = $relation['Params']; |
||
| 178 | } |
||
| 179 | if ($relation['Limit'] > 0) { |
||
| 180 | $sql->limit = $relation['Limit']; |
||
| 181 | } |
||
| 182 | |||
| 183 | $sql->params[$relation['On'][0]] = $this->{$relation['On'][0]}; |
||
| 184 | |||
| 185 | /** @noinspection PhpUndefinedMethodInspection */ |
||
| 186 | $this->cacheRelations[$name] = $relation['Model']::finder($sql, !$relation['IsMany']); |
||
| 187 | } |
||
| 188 | |||
| 189 | return $this->cacheRelations[$name]; |
||
| 190 | } elseif (isset($this->$name)) { |
||
| 191 | return $this->$name; |
||
| 192 | } |
||
| 193 | |||
| 194 | return false; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @inheritdoc |
||
| 199 | */ |
||
| 200 | public function relations() |
||
| 201 | { |
||
| 202 | return new Relations; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Save changes |
||
| 207 | * |
||
| 208 | * @access public |
||
| 209 | * |
||
| 210 | * @param bool $validate Validated data? |
||
| 211 | * |
||
| 212 | * @return boolean |
||
| 213 | * @throws Exception |
||
| 214 | */ |
||
| 215 | final public function save($validate = false) |
||
| 216 | { |
||
| 217 | if ($validate && !$this->validate()) { |
||
| 218 | return false; |
||
| 219 | } |
||
| 220 | |||
| 221 | if ($this->isNewRecord()) { |
||
| 222 | return $this->create(); |
||
| 223 | } else { |
||
| 224 | if ($this->beforeSave() && $this->update()) { |
||
| 225 | $this->afterSave(); |
||
| 226 | |||
| 227 | return true; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | return false; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Is new record? |
||
| 236 | * |
||
| 237 | * @access public |
||
| 238 | * @return boolean |
||
| 239 | */ |
||
| 240 | public function isNewRecord() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Create changes |
||
| 247 | * |
||
| 248 | * @access public |
||
| 249 | * @return boolean |
||
| 250 | * @throws Exception |
||
| 251 | */ |
||
| 252 | final public function create() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @inheritdoc |
||
| 282 | */ |
||
| 283 | public function beforeCreate() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @inheritdoc |
||
| 290 | */ |
||
| 291 | public function beforeSave() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Merge local attributes and db attributes |
||
| 298 | * |
||
| 299 | * @access protected |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | * @throws \Micro\base\Exception |
||
| 303 | */ |
||
| 304 | protected function mergeAttributesDb() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Check attribute exists into table |
||
| 326 | * |
||
| 327 | * @access public |
||
| 328 | * |
||
| 329 | * @param string $name Attribute name |
||
| 330 | * |
||
| 331 | * @return boolean |
||
| 332 | * @throws Exception |
||
| 333 | */ |
||
| 334 | public function checkAttributeExists($name) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @inheritdoc |
||
| 353 | */ |
||
| 354 | public function afterCreate() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @inheritdoc |
||
| 360 | */ |
||
| 361 | public function afterSave() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Update changes |
||
| 367 | * |
||
| 368 | * @access public |
||
| 369 | * |
||
| 370 | * @param string $where condition for search |
||
| 371 | * |
||
| 372 | * @throws Exception |
||
| 373 | * @return boolean |
||
| 374 | */ |
||
| 375 | final public function update($where = null) |
||
| 376 | { |
||
| 377 | if ($this->isNewRecord()) { |
||
| 378 | return false; |
||
| 379 | } |
||
| 380 | if ($this->beforeUpdate()) { |
||
| 381 | if (!$where) { |
||
|
|
|||
| 382 | if (self::$primaryKey) { |
||
| 383 | if ((new ConnectionInjector)->getDriver()->getDriverType() === 'pgsql') { |
||
| 384 | $where .= '"' . self::$primaryKey . '" = :' . self::$primaryKey; |
||
| 385 | } else { |
||
| 386 | $where .= '`' . self::$primaryKey . '` = :' . self::$primaryKey; |
||
| 387 | } |
||
| 388 | |||
| 389 | } else { |
||
| 390 | throw new Exception('In table '.static::$tableName.' option `'.self::$primaryKey.'` not defined/not use.' |
||
| 391 | ); |
||
| 392 | } |
||
| 393 | } |
||
| 394 | |||
| 395 | if ((new ConnectionInjector)->getDriver()->update(static::$tableName, $this->mergeAttributesDb(), $where)) { |
||
| 396 | $this->afterUpdate(); |
||
| 397 | |||
| 398 | return true; |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | return false; |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @inheritdoc |
||
| 407 | */ |
||
| 408 | public function beforeUpdate() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @inheritdoc |
||
| 415 | */ |
||
| 416 | public function afterUpdate() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Delete changes |
||
| 422 | * |
||
| 423 | * @access public |
||
| 424 | * @return boolean |
||
| 425 | * @throws Exception |
||
| 426 | */ |
||
| 427 | final public function delete() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @inheritdoc |
||
| 455 | */ |
||
| 456 | public function beforeDelete() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @inheritdoc |
||
| 463 | */ |
||
| 464 | public function afterDelete() |
||
| 467 | } |
||
| 468 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: