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 | * Returns the list of attribute names. |
||
| 99 | * By default, this method returns all attributes mentioned in rules. |
||
| 100 | * You may override this method to change the default behavior. |
||
| 101 | * @return string[] list of attribute names |
||
| 102 | */ |
||
| 103 | public function attributes() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Creates an active record instance. |
||
| 124 | * |
||
| 125 | * This method is called together with [[populateRecord()]] by [[ActiveQuery]]. |
||
| 126 | * It is not meant to be used for creating new records directly. |
||
| 127 | * |
||
| 128 | * You may override this method if the instance being created |
||
| 129 | * depends on the row data to be populated into the record. |
||
| 130 | * For example, by creating a record based on the value of a column, |
||
| 131 | * you may implement the so-called single-table inheritance mapping. |
||
| 132 | * |
||
| 133 | * @return static the newly created active record |
||
| 134 | */ |
||
| 135 | public static function instantiate($row) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return string the name of the entity of this record |
||
| 142 | */ |
||
| 143 | public static function from() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Declares the name of the model associated with this class. |
||
| 150 | * By default this method returns the class name by calling [[Inflector::camel2id()]]. |
||
| 151 | * |
||
| 152 | * @return string the module name |
||
| 153 | */ |
||
| 154 | public static function modelName() |
||
| 158 | |||
| 159 | public function insert($runValidation = true, $attributes = null, $options = []) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * {@inheritdoc} |
||
| 187 | */ |
||
| 188 | public function delete($options = []) |
||
| 202 | |||
| 203 | public function update($runValidation = true, $attributeNames = null, $options = []) |
||
| 211 | |||
| 212 | protected function updateInternal($attributes = null, $options = []) |
||
| 237 | |||
| 238 | public function performScenario($defaultScenario, $data, array $options = []) |
||
| 244 | |||
| 245 | public static function perform($action, $data, array $options = []) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Converts scenario name to action. |
||
| 252 | * @param string $default default action name |
||
| 253 | * @throws InvalidConfigException |
||
| 254 | * @throws NotSupportedException |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | public function getScenarioAction($default = '') |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Define an array of relations between scenario and API call action. |
||
| 289 | * |
||
| 290 | * Example: |
||
| 291 | * |
||
| 292 | * ``` |
||
| 293 | * [ |
||
| 294 | * 'update-name' => 'set-name', /// ModuleSetName |
||
| 295 | * 'update-related-name' => [Action::formName(), 'SetName'], /// ActionSetName |
||
| 296 | * 'update-self-case-sensetive' => [null, 'SomeSENSETIVE'] /// ModuleSomeSENSETIVE |
||
| 297 | * ] |
||
| 298 | * ~~ |
||
| 299 | * |
||
| 300 | * key string name of scenario |
||
| 301 | * value string|array |
||
| 302 | * string will be passed to [[Inflector::id2camel|id2camel]] inflator |
||
| 303 | * array - first attribute a module name, second - value |
||
| 304 | * |
||
| 305 | * Tricks: pass null as first argument of array to leave command's case unchanged (no inflator calling) |
||
| 306 | * |
||
| 307 | * @return array |
||
| 308 | */ |
||
| 309 | public function scenarioCommands() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | public function getIsNewRecord() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * This method has no effect in HiArt ActiveRecord. |
||
| 324 | */ |
||
| 325 | public function optimisticLock() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Destroys the relationship in current model. |
||
| 332 | * |
||
| 333 | * This method is not supported by HiArt. |
||
| 334 | */ |
||
| 335 | public function unlinkAll($name, $delete = false) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * {@inheritdoc} |
||
| 342 | * |
||
| 343 | * @return ActiveQueryInterface|ActiveQuery the relational query object. If the relation does not exist |
||
| 344 | * and `$throwException` is false, null will be returned. |
||
| 345 | */ |
||
| 346 | public function getRelation($name, $throwException = true) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * {@inheritdoc} |
||
| 353 | * @return ActiveQuery the relational query object |
||
| 354 | */ |
||
| 355 | public function hasOne($class, $link) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * {@inheritdoc} |
||
| 362 | * @return ActiveQuery the relational query object |
||
| 363 | */ |
||
| 364 | public function hasMany($class, $link) |
||
| 368 | } |
||
| 369 |
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: