Complex classes like ActiveRecord 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 ActiveRecord, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class ActiveRecord extends BaseActiveRecord |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Returns the database connection used by this AR class. |
||
| 25 | * By default, the "hiart" application component is used as the database connection. |
||
| 26 | * You may override this method if you want to use a different database connection. |
||
| 27 | * |
||
| 28 | * @return AbstractConnection the database connection used by this AR class |
||
| 29 | */ |
||
| 30 | public static function getDb() |
||
| 34 | |||
| 35 | /** |
||
| 36 | * {@inheritdoc} |
||
| 37 | * @return ActiveQuery the newly created [[ActiveQuery]] instance |
||
| 38 | */ |
||
| 39 | 2 | public static function find() |
|
| 45 | |||
| 46 | public function isScenarioDefault() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Gets a record by its primary key. |
||
| 53 | * |
||
| 54 | * @param mixed $primaryKey the primaryKey value |
||
| 55 | * @param array $options options given in this parameter are passed to API |
||
| 56 | * |
||
| 57 | * @return null|static the record instance or null if it was not found |
||
| 58 | */ |
||
| 59 | public static function get($primaryKey = null, $options = []) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * This method defines the attribute that uniquely identifies a record. |
||
| 80 | * |
||
| 81 | * The primaryKey for HiArt objects is the `id` field by default. This field is not part of the |
||
| 82 | * ActiveRecord attributes so you should never add `_id` to the list of [[attributes()|attributes]]. |
||
| 83 | * |
||
| 84 | * You may override this method to define the primary key name. |
||
| 85 | * |
||
| 86 | * Note that HiArt only supports _one_ attribute to be the primary key. However to match the signature |
||
| 87 | * of the [[\yii\db\ActiveRecordInterface|ActiveRecordInterface]] this methods returns an array instead of a |
||
| 88 | * single string. |
||
| 89 | * |
||
| 90 | * @return string[] array of primary key attributes. Only the first element of the array will be used. |
||
| 91 | */ |
||
| 92 | public static function primaryKey() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * + * The name of the main attribute |
||
| 99 | * + * |
||
| 100 | * Examples:. |
||
| 101 | * |
||
| 102 | * This will directly reference to the attribute 'name' |
||
| 103 | * ``` |
||
| 104 | * return 'name'; |
||
| 105 | * ``` |
||
| 106 | * |
||
| 107 | * This will concatenate listed attributes, separated with `delimiter` value. |
||
| 108 | * If delimiter is not set, space is used by default. |
||
| 109 | * ``` |
||
| 110 | * return ['seller', 'client', 'delimiter' => '/']; |
||
| 111 | * ``` |
||
| 112 | * |
||
| 113 | * The callable method, that will get [[$model]] and should return value of name attribute |
||
| 114 | * ``` |
||
| 115 | * return function ($model) { |
||
| 116 | * return $model->someField ? $model->name : $model->otherName; |
||
| 117 | * }; |
||
| 118 | * ``` |
||
| 119 | * |
||
| 120 | * @throws InvalidConfigException |
||
| 121 | * |
||
| 122 | * @return string|callable|array |
||
| 123 | * |
||
| 124 | * @author SilverFire |
||
| 125 | */ |
||
| 126 | public function primaryValue() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Returns the value of the primary attribute. |
||
| 133 | * |
||
| 134 | * @throws InvalidConfigException |
||
| 135 | * |
||
| 136 | * @return mixed|null |
||
| 137 | * |
||
| 138 | * @see primaryValue() |
||
| 139 | */ |
||
| 140 | public function getPrimaryValue() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Returns the list of attribute names. |
||
| 157 | * By default, this method returns all attributes mentioned in rules. |
||
| 158 | * You may override this method to change the default behavior. |
||
| 159 | * @return string[] list of attribute names |
||
| 160 | */ |
||
| 161 | public function attributes() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @return string the name of the index this record is stored in |
||
| 182 | */ |
||
| 183 | public static function index() |
||
| 188 | |||
| 189 | public static function joinIndex() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Creates an active record instance. |
||
| 196 | * |
||
| 197 | * This method is called together with [[populateRecord()]] by [[ActiveQuery]]. |
||
| 198 | * It is not meant to be used for creating new records directly. |
||
| 199 | * |
||
| 200 | * You may override this method if the instance being created |
||
| 201 | * depends on the row data to be populated into the record. |
||
| 202 | * For example, by creating a record based on the value of a column, |
||
| 203 | * you may implement the so-called single-table inheritance mapping. |
||
| 204 | * |
||
| 205 | * @return static the newly created active record |
||
| 206 | */ |
||
| 207 | public static function instantiate($row) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return string the name of the entity of this record |
||
| 214 | */ |
||
| 215 | public static function from() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Declares the name of the model associated with this class. |
||
| 222 | * By default this method returns the class name by calling [[Inflector::camel2id()]]. |
||
| 223 | * |
||
| 224 | * @return string the module name |
||
| 225 | */ |
||
| 226 | public static function modelName() |
||
| 230 | |||
| 231 | public function insert($runValidation = true, $attributes = null, $options = []) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * {@inheritdoc} |
||
| 259 | */ |
||
| 260 | public function delete($options = []) |
||
| 274 | |||
| 275 | public function update($runValidation = true, $attributeNames = null, $options = []) |
||
| 283 | |||
| 284 | protected function updateInternal($attributes = null, $options = []) |
||
| 309 | |||
| 310 | public function performScenario($defaultScenario, $data, array $options = []) |
||
| 316 | |||
| 317 | public static function perform($action, $data, array $options = []) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Converts scenario name to action. |
||
| 324 | * @param string $default default action name |
||
| 325 | * @throws InvalidConfigException |
||
| 326 | * @throws NotSupportedException |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | public function getScenarioAction($default = '') |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Define an array of relations between scenario and API call action. |
||
| 361 | * |
||
| 362 | * Example: |
||
| 363 | * |
||
| 364 | * ``` |
||
| 365 | * [ |
||
| 366 | * 'update-name' => 'set-name', /// ModuleSetName |
||
| 367 | * 'update-related-name' => [Action::formName(), 'SetName'], /// ActionSetName |
||
| 368 | * 'update-self-case-sensetive' => [null, 'SomeSENSETIVE'] /// ModuleSomeSENSETIVE |
||
| 369 | * ] |
||
| 370 | * ~~ |
||
| 371 | * |
||
| 372 | * key string name of scenario |
||
| 373 | * value string|array |
||
| 374 | * string will be passed to [[Inflector::id2camel|id2camel]] inflator |
||
| 375 | * array - first attribute a module name, second - value |
||
| 376 | * |
||
| 377 | * Tricks: pass null as first argument of array to leave command's case unchanged (no inflator calling) |
||
| 378 | * |
||
| 379 | * @return array |
||
| 380 | */ |
||
| 381 | public function scenarioCommands() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @return bool |
||
| 388 | */ |
||
| 389 | public function getIsNewRecord() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * This method has no effect in HiArt ActiveRecord. |
||
| 396 | */ |
||
| 397 | public function optimisticLock() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Destroys the relationship in current model. |
||
| 404 | * |
||
| 405 | * This method is not supported by HiArt. |
||
| 406 | */ |
||
| 407 | public function unlinkAll($name, $delete = false) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * {@inheritdoc} |
||
| 414 | * |
||
| 415 | * @return ActiveQueryInterface|ActiveQuery the relational query object. If the relation does not exist |
||
| 416 | * and `$throwException` is false, null will be returned. |
||
| 417 | */ |
||
| 418 | public function getRelation($name, $throwException = true) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * {@inheritdoc} |
||
| 425 | * @return ActiveQuery the relational query object |
||
| 426 | */ |
||
| 427 | public function hasOne($class, $link) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * {@inheritdoc} |
||
| 434 | * @return ActiveQuery the relational query object |
||
| 435 | */ |
||
| 436 | public function hasMany($class, $link) |
||
| 440 | } |
||
| 441 |
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: