Complex classes like BaseModel 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 BaseModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | abstract class BaseModel extends Model implements BaseModelInterface |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * @var array|null old attribute values indexed by attribute names. |
||
| 41 | * This is `null` if the record [[isNewRecord|is new]]. |
||
| 42 | */ |
||
| 43 | private $_oldAttributes; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var array attribute values indexed by attribute names |
||
| 47 | */ |
||
| 48 | private $_attributes = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @codeCoverageIgnore |
||
| 52 | */ |
||
| 53 | protected static function getDataService() |
||
| 54 | { |
||
| 55 | throw new NotSupportedException('You must implement BaseModel::getDataService()'); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @return string attribute key of the object |
||
| 60 | * @since 1.0.0 |
||
| 61 | */ |
||
| 62 | abstract public function key(); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Definition of model attributes ['attributeName' => 'type', ...] where type can be : |
||
| 66 | * * bool |
||
| 67 | * * string |
||
| 68 | * * float |
||
| 69 | * * int |
||
| 70 | * As database are not always able to retain datatype, this will be used by the service layer to |
||
| 71 | * cast data |
||
| 72 | * @return array definition of model attributes |
||
| 73 | * @since 1.0.0 |
||
| 74 | */ |
||
| 75 | abstract public function attributesDefinition(); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Returns the list of all attribute names of the model. |
||
| 79 | * The default implementation will return all column names defined in scenario. |
||
| 80 | * @return array list of attribute names. |
||
| 81 | * @codeCoverageIgnore |
||
| 82 | */ |
||
| 83 | public function attributes() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Initializes the object. |
||
| 90 | * This method is called at the end of the constructor. |
||
| 91 | * The default implementation will trigger an [[EVENT_INIT]] event. |
||
| 92 | * If you override this method, make sure you call the parent implementation at the end |
||
| 93 | * to ensure triggering of the event. |
||
| 94 | */ |
||
| 95 | 53 | public function init() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Repopulates this record with the latest data. |
||
| 103 | * @return boolean whether the row still exists in the database. If true, the latest data |
||
| 104 | * will be populated to this active record. Otherwise, this record will remain unchanged. |
||
| 105 | */ |
||
| 106 | public function refresh() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Returns the key value of the object |
||
| 121 | * @return mixed|null |
||
| 122 | * @since 1.0.0 |
||
| 123 | */ |
||
| 124 | 49 | public function getKey() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Returns the old key value of the object |
||
| 132 | * @return mixed|null |
||
| 133 | * @since 1.0.0 |
||
| 134 | */ |
||
| 135 | 20 | public function getOldKey() |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Returns a value indicating whether the model has an attribute with the specified name. |
||
| 143 | * @param string $name the name of the attribute |
||
| 144 | * @return boolean whether the model has an attribute with the specified name. |
||
| 145 | */ |
||
| 146 | 49 | public function hasAttribute($name) |
|
| 150 | /** |
||
| 151 | * Returns the named attribute value. |
||
| 152 | * If this record is the result of a query and the attribute is not loaded, |
||
| 153 | * null will be returned. |
||
| 154 | * @param string $name the attribute name |
||
| 155 | * @return mixed the attribute value. Null if the attribute is not set or does not exist. |
||
| 156 | * @see hasAttribute() |
||
| 157 | */ |
||
| 158 | public function getAttribute($name) |
||
| 162 | /** |
||
| 163 | * Sets the named attribute value. |
||
| 164 | * @param string $name the attribute name |
||
| 165 | * @param mixed $value the attribute value. |
||
| 166 | * @throws InvalidArgumentException if the named attribute does not exist. |
||
| 167 | * @see hasAttribute() |
||
| 168 | */ |
||
| 169 | 42 | public function setAttribute($name, $value) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Sets the old attribute values. |
||
| 180 | * All existing old attribute values will be discarded. |
||
| 181 | * @param array|null $values old attribute values to be set. |
||
| 182 | * If set to `null` this record is considered to be [[isNewRecord|new]]. |
||
| 183 | */ |
||
| 184 | 49 | public function setOldAttributes($values) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Returns the old attribute values. |
||
| 191 | * @return array the old attribute values (name-value pairs) |
||
| 192 | */ |
||
| 193 | 9 | public function getOldAttributes() |
|
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * Sets the old value of the named attribute. |
||
| 201 | * @param string $name the attribute name |
||
| 202 | * @param mixed $value the old attribute value. |
||
| 203 | * @throws InvalidArgumentException if the named attribute does not exist. |
||
| 204 | * @see hasAttribute() |
||
| 205 | */ |
||
| 206 | 9 | public function setOldAttribute($name, $value) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Returns the old value of the named attribute. |
||
| 217 | * If this record is the result of a query and the attribute is not loaded, |
||
| 218 | * null will be returned. |
||
| 219 | * @param string $name the attribute name |
||
| 220 | * @return mixed the old attribute value. Null if the attribute is not loaded before |
||
| 221 | * or does not exist. |
||
| 222 | * @see hasAttribute() |
||
| 223 | */ |
||
| 224 | public function getOldAttribute($name) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Returns a value indicating whether the current record is new. |
||
| 231 | * @return boolean whether the record is new and should be inserted when calling [[save()]]. |
||
| 232 | */ |
||
| 233 | 49 | public function getIsNewRecord() |
|
| 237 | /** |
||
| 238 | * Sets the value indicating whether the record is new. |
||
| 239 | * @param boolean $value whether the record is new and should be inserted when calling [[save()]]. |
||
| 240 | * @see getIsNewRecord() |
||
| 241 | */ |
||
| 242 | 12 | public function setIsNewRecord($value) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Marks an attribute dirty. |
||
| 249 | * This method may be called to force updating a record when calling [[update()]], |
||
| 250 | * even if there is no change being made to the record. |
||
| 251 | * @param string $name the attribute name |
||
| 252 | */ |
||
| 253 | public function markAttributeDirty($name) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Returns a value indicating whether the named attribute has been changed. |
||
| 260 | * @param string $name the name of the attribute. |
||
| 261 | * @param boolean $identical whether the comparison of new and old value is made for |
||
| 262 | * identical values using `===`, defaults to `true`. Otherwise `==` is used for comparison. |
||
| 263 | * This parameter is available since version 2.0.4. |
||
| 264 | * @return boolean whether the attribute has been changed |
||
| 265 | */ |
||
| 266 | public function isAttributeChanged($name, $identical = true) |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * Returns the attribute values that have been modified since they are loaded or saved most recently. |
||
| 282 | * |
||
| 283 | * The comparison of new and old values is made for identical values using `===`. |
||
| 284 | * |
||
| 285 | * @param string[]|null $names the names of the attributes whose values may be returned if they are |
||
| 286 | * changed recently. If null, [[attributes()]] will be used. |
||
| 287 | * @return array the changed attribute values (name-value pairs) |
||
| 288 | */ |
||
| 289 | 49 | public function getDirtyAttributes($names = null) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * PHP getter magic method. |
||
| 314 | * This method is overridden so that attributes and related objects can be accessed like properties. |
||
| 315 | * |
||
| 316 | * @param string $name property name |
||
| 317 | * @throws \yii\base\InvalidArgumentException if relation name is wrong |
||
| 318 | * @return mixed property value |
||
| 319 | * @see getAttribute() |
||
| 320 | */ |
||
| 321 | 49 | public function __get($name) |
|
| 331 | /** |
||
| 332 | * PHP setter magic method. |
||
| 333 | * This method is overridden so that AR attributes can be accessed like properties. |
||
| 334 | * @param string $name property name |
||
| 335 | * @param mixed $value property value |
||
| 336 | */ |
||
| 337 | 49 | public function __set($name, $value) |
|
| 345 | /** |
||
| 346 | * Checks if a property value is null. |
||
| 347 | * This method overrides the parent implementation by checking if the named attribute is null or not. |
||
| 348 | * @param string $name the property name or the event name |
||
| 349 | * @return boolean whether the property value is null |
||
| 350 | */ |
||
| 351 | 30 | public function __isset($name) |
|
| 359 | /** |
||
| 360 | * Sets a component property to be null. |
||
| 361 | * This method overrides the parent implementation by clearing |
||
| 362 | * the specified attribute value. |
||
| 363 | * @param string $name the property name or the event name |
||
| 364 | */ |
||
| 365 | public function __unset($name) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Populates the model with the data from end user. |
||
| 376 | * The data to be loaded is `$data[formName]`, where `formName` refers to the value of [[formName()]]. |
||
| 377 | * If [[formName()]] is empty, the whole `$data` array will be used to populate the model. |
||
| 378 | * The data being populated is subject to the safety check by [[setAttributes()]]. |
||
| 379 | * @param array $data the data array. This is usually `$_POST` or `$_GET`, but can also be any valid array |
||
| 380 | * supplied by end user. |
||
| 381 | * @param string $formName the form name to be used for loading the data into the model. |
||
| 382 | * If not set, [[formName()]] will be used. |
||
| 383 | * @return boolean whether the model is successfully populated with some data. |
||
| 384 | */ |
||
| 385 | public function loadIds($data, $formName=null) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * This method is called when the AR object is created and populated with the query result. |
||
| 397 | * The default implementation will trigger an [[EVENT_AFTER_FIND]] event. |
||
| 398 | * When overriding this method, make sure you call the parent implementation to ensure the |
||
| 399 | * event is triggered. |
||
| 400 | */ |
||
| 401 | 42 | public function afterFind() |
|
| 405 | |||
| 406 | /** |
||
| 407 | * This method is called at the beginning of inserting or updating a record. |
||
| 408 | * The default implementation will trigger an [[EVENT_BEFORE_INSERT]] event when `$insert` is true, |
||
| 409 | * or an [[EVENT_BEFORE_UPDATE]] event if `$insert` is false. |
||
| 410 | * When overriding this method, make sure you call the parent implementation like the following: |
||
| 411 | * |
||
| 412 | * ```php |
||
| 413 | * public function beforeSave($insert) |
||
| 414 | * { |
||
| 415 | * if (parent::beforeSave($insert)) { |
||
| 416 | * // ...custom code here... |
||
| 417 | * return true; |
||
| 418 | * } else { |
||
| 419 | * return false; |
||
| 420 | * } |
||
| 421 | * } |
||
| 422 | * ``` |
||
| 423 | * |
||
| 424 | * @param boolean $insert whether this method called while inserting a record. |
||
| 425 | * If false, it means the method is called while updating a record. |
||
| 426 | * @return boolean whether the insertion or updating should continue. |
||
| 427 | * If false, the insertion or updating will be cancelled. |
||
| 428 | */ |
||
| 429 | 49 | public function beforeSave($insert) |
|
| 435 | |||
| 436 | /** |
||
| 437 | * This method is called at the end of inserting or updating a record. |
||
| 438 | * The default implementation will trigger an [[EVENT_AFTER_INSERT]] event when `$insert` is true, |
||
| 439 | * or an [[EVENT_AFTER_UPDATE]] event if `$insert` is false. The event class used is [[AfterSaveEvent]]. |
||
| 440 | * When overriding this method, make sure you call the parent implementation so that |
||
| 441 | * the event is triggered. |
||
| 442 | * @param boolean $insert whether this method called while inserting a record. |
||
| 443 | * If false, it means the method is called while updating a record. |
||
| 444 | * @param array $changedAttributes The old values of attributes that had changed and were saved. |
||
| 445 | * You can use this parameter to take action based on the changes made for example send an email |
||
| 446 | * when the password had changed or implement audit trail that tracks all the changes. |
||
| 447 | * `$changedAttributes` gives you the old attribute values while the active record (`$this`) has |
||
| 448 | * already the new, updated values. |
||
| 449 | */ |
||
| 450 | 49 | public function afterSave($insert, $changedAttributes) |
|
| 456 | |||
| 457 | /** |
||
| 458 | * This method is invoked before deleting a record. |
||
| 459 | * The default implementation raises the [[EVENT_BEFORE_DELETE]] event. |
||
| 460 | * When overriding this method, make sure you call the parent implementation like the following: |
||
| 461 | * |
||
| 462 | * ```php |
||
| 463 | * public function beforeDelete() |
||
| 464 | * { |
||
| 465 | * if (parent::beforeDelete()) { |
||
| 466 | * // ...custom code here... |
||
| 467 | * return true; |
||
| 468 | * } else { |
||
| 469 | * return false; |
||
| 470 | * } |
||
| 471 | * } |
||
| 472 | * ``` |
||
| 473 | * |
||
| 474 | * @return boolean whether the record should be deleted. Defaults to true. |
||
| 475 | */ |
||
| 476 | 12 | public function beforeDelete() |
|
| 482 | /** |
||
| 483 | * This method is invoked after deleting a record. |
||
| 484 | * The default implementation raises the [[EVENT_AFTER_DELETE]] event. |
||
| 485 | * You may override this method to do postprocessing after the record is deleted. |
||
| 486 | * Make sure you call the parent implementation so that the event is raised properly. |
||
| 487 | */ |
||
| 488 | 12 | public function afterDelete() |
|
| 492 | } |
||
| 493 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.