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 |
||
| 22 | class ActiveRecord extends BaseActiveRecord |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Returns the database connection used by this AR class. |
||
| 26 | * By default, the "hiart" application component is used as the database connection. |
||
| 27 | * You may override this method if you want to use a different database connection. |
||
| 28 | * |
||
| 29 | * @return Connection the database connection used by this AR class |
||
| 30 | */ |
||
| 31 | public static function getDb() |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritdoc} |
||
| 38 | * @return ActiveQuery the newly created [[ActiveQuery]] instance |
||
| 39 | */ |
||
| 40 | public static function find() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * This function is called from `Query::prepare`. |
||
| 49 | * You can redefine it to get desired behavior. |
||
| 50 | */ |
||
| 51 | public static function prepare(Query $query, QueryBuilderInterface $builder) |
||
| 54 | |||
| 55 | public function isScenarioDefault() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Gets a record by its primary key. |
||
| 62 | * |
||
| 63 | * @param mixed $primaryKey the primaryKey value |
||
| 64 | * @param array $options options given in this parameter are passed to API |
||
| 65 | * |
||
| 66 | * @return null|static the record instance or null if it was not found |
||
| 67 | */ |
||
| 68 | public static function get($primaryKey = null, $options = []) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * This method defines the attribute that uniquely identifies a record. |
||
| 89 | * |
||
| 90 | * The primaryKey for HiArt objects is the `id` field by default. This field is not part of the |
||
| 91 | * ActiveRecord attributes so you should never add `_id` to the list of [[attributes()|attributes]]. |
||
| 92 | * |
||
| 93 | * You may override this method to define the primary key name. |
||
| 94 | * |
||
| 95 | * Note that HiArt only supports _one_ attribute to be the primary key. However to match the signature |
||
| 96 | * of the [[\yii\db\ActiveRecordInterface|ActiveRecordInterface]] this methods returns an array instead of a |
||
| 97 | * single string. |
||
| 98 | * |
||
| 99 | * @return string[] array of primary key attributes. Only the first element of the array will be used. |
||
| 100 | */ |
||
| 101 | public static function primaryKey() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * + * The name of the main attribute |
||
| 108 | * + * |
||
| 109 | * Examples:. |
||
| 110 | * |
||
| 111 | * This will directly reference to the attribute 'name' |
||
| 112 | * ``` |
||
| 113 | * return 'name'; |
||
| 114 | * ``` |
||
| 115 | * |
||
| 116 | * This will concatenate listed attributes, separated with `delimiter` value. |
||
| 117 | * If delimiter is not set, space is used by default. |
||
| 118 | * ``` |
||
| 119 | * return ['seller', 'client', 'delimiter' => '/']; |
||
| 120 | * ``` |
||
| 121 | * |
||
| 122 | * The callable method, that will get [[$model]] and should return value of name attribute |
||
| 123 | * ``` |
||
| 124 | * return function ($model) { |
||
| 125 | * return $model->someField ? $model->name : $model->otherName; |
||
| 126 | * }; |
||
| 127 | * ``` |
||
| 128 | * |
||
| 129 | * @throws InvalidConfigException |
||
| 130 | * |
||
| 131 | * @return string|callable|array |
||
| 132 | * |
||
| 133 | * @author SilverFire |
||
| 134 | */ |
||
| 135 | public function primaryValue() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Returns the value of the primary attribute. |
||
| 142 | * |
||
| 143 | * @throws InvalidConfigException |
||
| 144 | * |
||
| 145 | * @return mixed|null |
||
| 146 | * |
||
| 147 | * @see primaryValue() |
||
| 148 | */ |
||
| 149 | public function getPrimaryValue() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Returns the list of attribute names. |
||
| 166 | * By default, this method returns all attributes mentioned in rules. |
||
| 167 | * You may override this method to change the default behavior. |
||
| 168 | * @return string[] list of attribute names. |
||
| 169 | */ |
||
| 170 | public function attributes() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return string the name of the index this record is stored in |
||
| 191 | */ |
||
| 192 | public static function index() |
||
| 197 | |||
| 198 | public static function joinIndex() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Creates an active record instance. |
||
| 205 | * |
||
| 206 | * This method is called together with [[populateRecord()]] by [[ActiveQuery]]. |
||
| 207 | * It is not meant to be used for creating new records directly. |
||
| 208 | * |
||
| 209 | * You may override this method if the instance being created |
||
| 210 | * depends on the row data to be populated into the record. |
||
| 211 | * For example, by creating a record based on the value of a column, |
||
| 212 | * you may implement the so-called single-table inheritance mapping. |
||
| 213 | * |
||
| 214 | * @return static the newly created active record |
||
| 215 | */ |
||
| 216 | public static function instantiate($row) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @return string the name of the entity of this record |
||
| 223 | */ |
||
| 224 | public static function from() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Declares the name of the model associated with this class. |
||
| 231 | * By default this method returns the class name by calling [[Inflector::camel2id()]]. |
||
| 232 | * |
||
| 233 | * @return string the module name |
||
| 234 | */ |
||
| 235 | public static function modelName() |
||
| 239 | |||
| 240 | public function insert($runValidation = true, $attributes = null, $options = []) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * {@inheritdoc} |
||
| 268 | */ |
||
| 269 | public function delete($options = []) |
||
| 283 | |||
| 284 | public function update($runValidation = true, $attributeNames = null, $options = []) |
||
| 292 | |||
| 293 | protected function updateInternal($attributes = null, $options = []) |
||
| 318 | |||
| 319 | public function performScenario($defaultScenario, $data, array $options = []) |
||
| 325 | |||
| 326 | public static function perform($action, $data, array $options = []) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Converts scenario name to action. |
||
| 333 | * @param string $default default action name |
||
| 334 | * @throws InvalidConfigException |
||
| 335 | * @throws NotSupportedException |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | public function getScenarioAction($default = '') |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Define an array of relations between scenario and API call action. |
||
| 370 | * |
||
| 371 | * Example: |
||
| 372 | * |
||
| 373 | * ``` |
||
| 374 | * [ |
||
| 375 | * 'update-name' => 'set-name', /// ModuleSetName |
||
| 376 | * 'update-related-name' => [Action::formName(), 'SetName'], /// ActionSetName |
||
| 377 | * 'update-self-case-sensetive' => [null, 'SomeSENSETIVE'] /// ModuleSomeSENSETIVE |
||
| 378 | * ] |
||
| 379 | * ~~ |
||
| 380 | * |
||
| 381 | * key string name of scenario |
||
| 382 | * value string|array |
||
| 383 | * string will be passed to [[Inflector::id2camel|id2camel]] inflator |
||
| 384 | * array - first attribute a module name, second - value |
||
| 385 | * |
||
| 386 | * Tricks: pass null as first argument of array to leave command's case unchanged (no inflator calling) |
||
| 387 | * |
||
| 388 | * @return array |
||
| 389 | */ |
||
| 390 | public function scenarioCommands() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @return bool |
||
| 397 | */ |
||
| 398 | public function getIsNewRecord() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * This method has no effect in HiArt ActiveRecord. |
||
| 405 | */ |
||
| 406 | public function optimisticLock() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Destroys the relationship in current model. |
||
| 413 | * |
||
| 414 | * This method is not supported by HiArt. |
||
| 415 | */ |
||
| 416 | public function unlinkAll($name, $delete = false) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * {@inheritdoc} |
||
| 423 | * |
||
| 424 | * @return ActiveQueryInterface|ActiveQuery the relational query object. If the relation does not exist |
||
| 425 | * and `$throwException` is false, null will be returned. |
||
| 426 | */ |
||
| 427 | public function getRelation($name, $throwException = true) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * {@inheritdoc} |
||
| 434 | * @return ActiveQuery the relational query object |
||
| 435 | */ |
||
| 436 | public function hasOne($class, $link) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * {@inheritdoc} |
||
| 443 | * @return ActiveQuery the relational query object |
||
| 444 | */ |
||
| 445 | public function hasMany($class, $link) |
||
| 449 | } |
||
| 450 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: