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 | /** |
||
| 47 | * This function is called from `Query::prepare`. |
||
| 48 | * You can redefine it to get desired behavior. |
||
| 49 | */ |
||
| 50 | public static function prepare(Query $query, QueryBuilderInterface $builder) |
||
| 53 | |||
| 54 | public function isScenarioDefault() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Gets a record by its primary key. |
||
| 61 | * |
||
| 62 | * @param mixed $primaryKey the primaryKey value |
||
| 63 | * @param array $options options given in this parameter are passed to API |
||
| 64 | * |
||
| 65 | * @return null|static the record instance or null if it was not found |
||
| 66 | */ |
||
| 67 | public static function get($primaryKey = null, $options = []) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * This method defines the attribute that uniquely identifies a record. |
||
| 88 | * |
||
| 89 | * The primaryKey for HiArt objects is the `id` field by default. This field is not part of the |
||
| 90 | * ActiveRecord attributes so you should never add `_id` to the list of [[attributes()|attributes]]. |
||
| 91 | * |
||
| 92 | * You may override this method to define the primary key name. |
||
| 93 | * |
||
| 94 | * Note that HiArt only supports _one_ attribute to be the primary key. However to match the signature |
||
| 95 | * of the [[\yii\db\ActiveRecordInterface|ActiveRecordInterface]] this methods returns an array instead of a |
||
| 96 | * single string. |
||
| 97 | * |
||
| 98 | * @return string[] array of primary key attributes. Only the first element of the array will be used. |
||
| 99 | */ |
||
| 100 | public static function primaryKey() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * + * The name of the main attribute |
||
| 107 | * + * |
||
| 108 | * Examples:. |
||
| 109 | * |
||
| 110 | * This will directly reference to the attribute 'name' |
||
| 111 | * ``` |
||
| 112 | * return 'name'; |
||
| 113 | * ``` |
||
| 114 | * |
||
| 115 | * This will concatenate listed attributes, separated with `delimiter` value. |
||
| 116 | * If delimiter is not set, space is used by default. |
||
| 117 | * ``` |
||
| 118 | * return ['seller', 'client', 'delimiter' => '/']; |
||
| 119 | * ``` |
||
| 120 | * |
||
| 121 | * The callable method, that will get [[$model]] and should return value of name attribute |
||
| 122 | * ``` |
||
| 123 | * return function ($model) { |
||
| 124 | * return $model->someField ? $model->name : $model->otherName; |
||
| 125 | * }; |
||
| 126 | * ``` |
||
| 127 | * |
||
| 128 | * @throws InvalidConfigException |
||
| 129 | * |
||
| 130 | * @return string|callable|array |
||
| 131 | * |
||
| 132 | * @author SilverFire |
||
| 133 | */ |
||
| 134 | public function primaryValue() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Returns the value of the primary attribute. |
||
| 141 | * |
||
| 142 | * @throws InvalidConfigException |
||
| 143 | * |
||
| 144 | * @return mixed|null |
||
| 145 | * |
||
| 146 | * @see primaryValue() |
||
| 147 | */ |
||
| 148 | public function getPrimaryValue() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Returns the list of attribute names. |
||
| 165 | * By default, this method returns all attributes mentioned in rules. |
||
| 166 | * You may override this method to change the default behavior. |
||
| 167 | * @return string[] list of attribute names |
||
| 168 | */ |
||
| 169 | 2 | public function attributes() |
|
| 170 | { |
||
| 171 | 2 | $attributes = []; |
|
| 172 | 2 | foreach ($this->rules() as $rule) { |
|
| 173 | 2 | $names = reset($rule); |
|
| 174 | 2 | if (is_string($names)) { |
|
| 175 | 2 | $names = [$names]; |
|
| 176 | } |
||
| 177 | 2 | foreach ($names as $name) { |
|
| 178 | 2 | if (substr_compare($name, '!', 0, 1) === 0) { |
|
| 179 | $name = mb_substr($name, 1); |
||
| 180 | } |
||
| 181 | 2 | $attributes[$name] = $name; |
|
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | 2 | return array_values($attributes); |
|
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @return string the name of the index this record is stored in |
||
| 190 | */ |
||
| 191 | public static function index() |
||
| 196 | |||
| 197 | public static function joinIndex() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Creates an active record instance. |
||
| 204 | * |
||
| 205 | * This method is called together with [[populateRecord()]] by [[ActiveQuery]]. |
||
| 206 | * It is not meant to be used for creating new records directly. |
||
| 207 | * |
||
| 208 | * You may override this method if the instance being created |
||
| 209 | * depends on the row data to be populated into the record. |
||
| 210 | * For example, by creating a record based on the value of a column, |
||
| 211 | * you may implement the so-called single-table inheritance mapping. |
||
| 212 | * |
||
| 213 | * @return static the newly created active record |
||
| 214 | */ |
||
| 215 | 2 | public static function instantiate($row) |
|
| 216 | { |
||
| 217 | 2 | return new static(); |
|
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return string the name of the entity of this record |
||
| 222 | */ |
||
| 223 | public static function from() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Declares the name of the model associated with this class. |
||
| 230 | * By default this method returns the class name by calling [[Inflector::camel2id()]]. |
||
| 231 | * |
||
| 232 | * @return string the module name |
||
| 233 | */ |
||
| 234 | public static function modelName() |
||
| 238 | |||
| 239 | public function insert($runValidation = true, $attributes = null, $options = []) |
||
| 240 | { |
||
| 241 | if ($runValidation && !$this->validate($attributes)) { |
||
| 242 | return false; |
||
| 243 | } |
||
| 244 | |||
| 245 | if (!$this->beforeSave(true)) { |
||
| 246 | return false; |
||
| 247 | } |
||
| 248 | |||
| 249 | $values = $this->getDirtyAttributes($attributes); |
||
| 250 | $data = array_merge($values, $options, ['id' => $this->getOldPrimaryKey()]); |
||
| 251 | $result = $this->performScenario('insert', $data); |
||
| 252 | |||
| 253 | $pk = static::primaryKey()[0]; |
||
| 254 | $this->$pk = $result['id']; |
||
| 255 | if ($pk !== 'id') { |
||
| 256 | $values[$pk] = $result['id']; |
||
| 257 | } |
||
| 258 | $changedAttributes = array_fill_keys(array_keys($values), null); |
||
| 259 | $this->setOldAttributes($values); |
||
| 260 | $this->afterSave(true, $changedAttributes); |
||
| 261 | |||
| 262 | return true; |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritdoc} |
||
| 267 | */ |
||
| 268 | public function delete($options = []) |
||
| 282 | |||
| 283 | public function update($runValidation = true, $attributeNames = null, $options = []) |
||
| 291 | |||
| 292 | protected function updateInternal($attributes = null, $options = []) |
||
| 293 | { |
||
| 294 | if (!$this->beforeSave(false)) { |
||
| 295 | return false; |
||
| 296 | } |
||
| 297 | |||
| 298 | $values = $this->getAttributes($attributes); |
||
| 299 | if (empty($values)) { |
||
| 300 | $this->afterSave(false, $values); |
||
| 301 | |||
| 302 | return 0; |
||
| 303 | } |
||
| 304 | |||
| 305 | $result = $this->performScenario('update', $values, $options); |
||
| 306 | |||
| 307 | $changedAttributes = []; |
||
| 308 | foreach ($values as $name => $value) { |
||
| 309 | $changedAttributes[$name] = $this->getOldAttribute($name); |
||
| 310 | $this->setOldAttribute($name, $value); |
||
| 311 | } |
||
| 312 | |||
| 313 | $this->afterSave(false, $changedAttributes); |
||
| 314 | |||
| 315 | return $result === false ? false : true; |
||
| 316 | } |
||
| 317 | |||
| 318 | public function performScenario($defaultScenario, $data, array $options = []) |
||
| 324 | |||
| 325 | public static function perform($action, $data, array $options = []) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Converts scenario name to action. |
||
| 332 | * @param string $default default action name |
||
| 333 | * @throws InvalidConfigException |
||
| 334 | * @throws NotSupportedException |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | public function getScenarioAction($default = '') |
||
| 338 | { |
||
| 339 | if ($this->isScenarioDefault()) { |
||
| 340 | if ($default !== '') { |
||
| 341 | $result = Inflector::id2camel($default); |
||
| 342 | } else { |
||
| 343 | throw new InvalidConfigException('Scenario not specified'); |
||
| 344 | } |
||
| 345 | } else { |
||
| 346 | $scenarioCommands = static::scenarioCommands(); |
||
| 347 | if ($action = $scenarioCommands[$this->scenario]) { |
||
| 348 | if ($action === false) { |
||
| 349 | throw new NotSupportedException('The scenario can not be saved'); |
||
| 350 | } |
||
| 351 | |||
| 352 | if (is_array($action) && $action[0] === null) { |
||
| 353 | $result = $action[1]; |
||
| 354 | } elseif (is_array($action)) { |
||
| 355 | $result = $action; |
||
| 356 | } else { |
||
| 357 | $result = Inflector::id2camel($action); |
||
| 358 | } |
||
| 359 | } else { |
||
| 360 | $result = Inflector::id2camel($this->scenario); |
||
| 361 | } |
||
| 362 | } |
||
| 363 | |||
| 364 | return is_array($result) ? implode('', $result) : $result; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Define an array of relations between scenario and API call action. |
||
| 369 | * |
||
| 370 | * Example: |
||
| 371 | * |
||
| 372 | * ``` |
||
| 373 | * [ |
||
| 374 | * 'update-name' => 'set-name', /// ModuleSetName |
||
| 375 | * 'update-related-name' => [Action::formName(), 'SetName'], /// ActionSetName |
||
| 376 | * 'update-self-case-sensetive' => [null, 'SomeSENSETIVE'] /// ModuleSomeSENSETIVE |
||
| 377 | * ] |
||
| 378 | * ~~ |
||
| 379 | * |
||
| 380 | * key string name of scenario |
||
| 381 | * value string|array |
||
| 382 | * string will be passed to [[Inflector::id2camel|id2camel]] inflator |
||
| 383 | * array - first attribute a module name, second - value |
||
| 384 | * |
||
| 385 | * Tricks: pass null as first argument of array to leave command's case unchanged (no inflator calling) |
||
| 386 | * |
||
| 387 | * @return array |
||
| 388 | */ |
||
| 389 | public function scenarioCommands() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @return bool |
||
| 396 | */ |
||
| 397 | public function getIsNewRecord() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * This method has no effect in HiArt ActiveRecord. |
||
| 404 | */ |
||
| 405 | public function optimisticLock() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Destroys the relationship in current model. |
||
| 412 | * |
||
| 413 | * This method is not supported by HiArt. |
||
| 414 | */ |
||
| 415 | public function unlinkAll($name, $delete = false) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * {@inheritdoc} |
||
| 422 | * |
||
| 423 | * @return ActiveQueryInterface|ActiveQuery the relational query object. If the relation does not exist |
||
| 424 | * and `$throwException` is false, null will be returned. |
||
| 425 | */ |
||
| 426 | public function getRelation($name, $throwException = true) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * {@inheritdoc} |
||
| 433 | * @return ActiveQuery the relational query object |
||
| 434 | */ |
||
| 435 | public function hasOne($class, $link) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * {@inheritdoc} |
||
| 442 | * @return ActiveQuery the relational query object |
||
| 443 | */ |
||
| 444 | public function hasMany($class, $link) |
||
| 448 | } |
||
| 449 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.