Complex classes like AuditTrailBehavior 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 AuditTrailBehavior, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class AuditTrailBehavior extends Behavior |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Array with fields to save |
||
| 21 | * You don't need to configure both `allowed` and `ignored` |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | public $allowed = []; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Array with fields to ignore |
||
| 28 | * You don't need to configure both `allowed` and `ignored` |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | public $ignored = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Array with classes to ignore |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | public $ignoredClasses = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Timestamp attributes should, in most cases, be ignored. If both AudittrailBehavior and |
||
| 41 | * TimestampBehavior logs the created_at and updated_at fields, the data is saved twice. |
||
| 42 | * In case you want to log them, you can unset the column from this timestamp column name suggestions. |
||
| 43 | * Set to null to disable this filter and log all columns. |
||
| 44 | * @var null|array |
||
| 45 | */ |
||
| 46 | public $timestamp_fields = ['created', 'updated', 'created_at', 'updated_at', 'timestamp']; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Is the behavior is active or not |
||
| 50 | * @var boolean |
||
| 51 | */ |
||
| 52 | public $active = true; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Date format to use in stamp - set to "Y-m-d H:i:s" for datetime or "U" for timestamp |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | public $dateFormat = 'Y-m-d H:i:s'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | private $_oldAttributes = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | 57 | * Array with fields you want to override before saving the row into audit_trail table |
|
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | 57 | public $override = []; |
|
| 70 | 57 | ||
| 71 | 57 | /** |
|
| 72 | 57 | * @inheritdoc |
|
| 73 | 57 | */ |
|
| 74 | public function events() |
||
| 75 | { |
||
| 76 | return [ |
||
| 77 | ActiveRecord::EVENT_AFTER_FIND => 'afterFind', |
||
| 78 | ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert', |
||
| 79 | 45 | ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate', |
|
| 80 | ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete', |
||
| 81 | 45 | ]; |
|
| 82 | 45 | } |
|
| 83 | |||
| 84 | /** |
||
| 85 | * |
||
| 86 | */ |
||
| 87 | 24 | public function afterFind() |
|
| 91 | 24 | ||
| 92 | /** |
||
| 93 | * |
||
| 94 | */ |
||
| 95 | public function afterInsert() |
||
| 100 | 27 | ||
| 101 | /** |
||
| 102 | * |
||
| 103 | */ |
||
| 104 | public function afterUpdate() |
||
| 109 | 12 | ||
| 110 | /** |
||
| 111 | * |
||
| 112 | */ |
||
| 113 | public function afterDelete() |
||
| 118 | 57 | ||
| 119 | 9 | /** |
|
| 120 | * @param $action |
||
| 121 | * @throws \yii\db\Exception |
||
| 122 | 48 | */ |
|
| 123 | 9 | public function audit($action) |
|
| 141 | |||
| 142 | 33 | /** |
|
| 143 | 33 | * Clean attributes of fields that are not allowed or ignored. |
|
| 144 | 33 | * |
|
| 145 | 33 | * @param $attributes |
|
| 146 | * @return mixed |
||
| 147 | */ |
||
| 148 | protected function cleanAttributes($attributes) |
||
| 155 | |||
| 156 | 33 | /** |
|
| 157 | 6 | * Unset attributes which are not allowed |
|
| 158 | 6 | * |
|
| 159 | 6 | * @param $attributes |
|
| 160 | 6 | * @return mixed |
|
| 161 | 6 | */ |
|
| 162 | 6 | protected function cleanAttributesAllowed($attributes) |
|
| 173 | |||
| 174 | 33 | /** |
|
| 175 | 6 | * Unset attributes which are ignored |
|
| 176 | 6 | * |
|
| 177 | 6 | * @param $attributes |
|
| 178 | 6 | * @return mixed |
|
| 179 | 6 | */ |
|
| 180 | 6 | protected function cleanAttributesIgnored($attributes) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * attributes which need to get override with a new value |
||
| 198 | * |
||
| 199 | * @param $attributes |
||
| 200 | * @return mixed |
||
| 201 | */ |
||
| 202 | protected function cleanAttributesOverride($attributes) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param string $searchFieldValue |
||
| 223 | * @param string $queryParams |
||
| 224 | * @return mixed |
||
| 225 | */ |
||
| 226 | private function getNewOverrideValues($searchFieldValue, $queryParams) |
||
| 238 | 33 | ||
| 239 | 3 | ||
| 240 | /** |
||
| 241 | * @param string $action |
||
| 242 | 30 | * @throws \yii\db\Exception |
|
| 243 | 30 | */ |
|
| 244 | 30 | protected function auditAttributes($action) |
|
| 262 | |||
| 263 | /** |
||
| 264 | 30 | * Save the audit trails for a create or update action |
|
| 265 | * |
||
| 266 | * @param $action |
||
| 267 | 30 | * @param $newAttributes |
|
| 268 | 30 | * @param $oldAttributes |
|
| 269 | 30 | * @param $entry_id |
|
| 270 | * @param $user_id |
||
| 271 | 30 | * @param $model |
|
| 272 | 30 | * @param $model_id |
|
| 273 | 30 | * @param $created |
|
| 274 | 30 | * @throws \yii\db\Exception |
|
| 275 | */ |
||
| 276 | 30 | protected function saveAuditTrail($action, $newAttributes, $oldAttributes, $entry_id, $user_id, $model, $model_id, $created) |
|
| 294 | 6 | ||
| 295 | 6 | /** |
|
| 296 | 6 | * Save the audit trails for a delete action |
|
| 297 | 6 | */ |
|
| 298 | protected function saveAuditTrailDelete() |
||
| 310 | 57 | ||
| 311 | 36 | /** |
|
| 312 | 57 | * @return array |
|
| 313 | 57 | */ |
|
| 314 | public function getOldAttributes() |
||
| 318 | 36 | ||
| 319 | /** |
||
| 320 | 36 | * @param $value |
|
| 321 | 36 | */ |
|
| 322 | public function setOldAttributes($value) |
||
| 326 | |||
| 327 | 36 | /** |
|
| 328 | * @return string |
||
| 329 | 36 | */ |
|
| 330 | protected function getNormalizedPk() |
||
| 335 | |||
| 336 | 36 | /** |
|
| 337 | * @return int|null|string |
||
| 338 | 36 | */ |
|
| 339 | 36 | protected function getUserId() |
|
| 343 | 36 | ||
| 344 | /** |
||
| 345 | 36 | * @return models\AuditEntry|null|static |
|
| 346 | * @throws \Exception |
||
| 347 | */ |
||
| 348 | protected function getAuditEntryId() |
||
| 359 | |||
| 360 | } |
||
| 361 |