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 | * Is the behavior is active or not |
||
| 41 | * @var boolean |
||
| 42 | */ |
||
| 43 | public $active = true; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Date format to use in stamp - set to "Y-m-d H:i:s" for datetime or "U" for timestamp |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | public $dateFormat = 'Y-m-d H:i:s'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private $_oldAttributes = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Array with fields you want to override before saving the row into audit_trail table |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | public $override = []; |
||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * @inheritdoc |
||
| 65 | */ |
||
| 66 | 57 | public function events() |
|
| 75 | |||
| 76 | /** |
||
| 77 | * |
||
| 78 | */ |
||
| 79 | 45 | public function afterFind() |
|
| 83 | |||
| 84 | /** |
||
| 85 | * |
||
| 86 | */ |
||
| 87 | 24 | public function afterInsert() |
|
| 92 | |||
| 93 | /** |
||
| 94 | * |
||
| 95 | */ |
||
| 96 | 27 | public function afterUpdate() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * |
||
| 104 | */ |
||
| 105 | 12 | public function afterDelete() |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @param $action |
||
| 113 | * @throws \yii\db\Exception |
||
| 114 | */ |
||
| 115 | 57 | public function audit($action) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Clean attributes of fields that are not allowed or ignored. |
||
| 136 | * |
||
| 137 | * @param $attributes |
||
| 138 | * @return mixed |
||
| 139 | */ |
||
| 140 | 33 | protected function cleanAttributes($attributes) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Unset attributes which are not allowed |
||
| 150 | * |
||
| 151 | * @param $attributes |
||
| 152 | * @return mixed |
||
| 153 | */ |
||
| 154 | 33 | protected function cleanAttributesAllowed($attributes) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Unset attributes which are ignored |
||
| 168 | * |
||
| 169 | * @param $attributes |
||
| 170 | * @return mixed |
||
| 171 | */ |
||
| 172 | 33 | protected function cleanAttributesIgnored($attributes) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * attributes which need to get override with a new value |
||
| 186 | * |
||
| 187 | * @param $attributes |
||
| 188 | * @return mixed |
||
| 189 | */ |
||
| 190 | 33 | protected function cleanAttributesOverride($attributes) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * @param string $searchFieldValue |
||
| 211 | * @param string $queryParams |
||
| 212 | * @return mixed |
||
| 213 | */ |
||
| 214 | private function getNewOverrideValues($searchFieldValue, $queryParams) |
||
| 226 | |||
| 227 | |||
| 228 | /** |
||
| 229 | * @param string $action |
||
| 230 | * @throws \yii\db\Exception |
||
| 231 | */ |
||
| 232 | 33 | protected function auditAttributes($action) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Save the audit trails for a create or update action |
||
| 253 | * |
||
| 254 | * @param $action |
||
| 255 | * @param $newAttributes |
||
| 256 | * @param $oldAttributes |
||
| 257 | * @param $entry_id |
||
| 258 | * @param $user_id |
||
| 259 | * @param $model |
||
| 260 | * @param $model_id |
||
| 261 | * @param $created |
||
| 262 | * @throws \yii\db\Exception |
||
| 263 | */ |
||
| 264 | 30 | protected function saveAuditTrail($action, $newAttributes, $oldAttributes, $entry_id, $user_id, $model, $model_id, $created) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Save the audit trails for a delete action |
||
| 285 | */ |
||
| 286 | 6 | protected function saveAuditTrailDelete() |
|
| 298 | |||
| 299 | /** |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | 33 | public function getOldAttributes() |
|
| 306 | |||
| 307 | /** |
||
| 308 | * @param $value |
||
| 309 | */ |
||
| 310 | 57 | public function setOldAttributes($value) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | 36 | protected function getNormalizedPk() |
|
| 323 | |||
| 324 | /** |
||
| 325 | * @return int|null|string |
||
| 326 | */ |
||
| 327 | 36 | protected function getUserId() |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @return models\AuditEntry|null|static |
||
| 334 | * @throws \Exception |
||
| 335 | */ |
||
| 336 | 36 | protected function getAuditEntryId() |
|
| 347 | |||
| 348 | } |
||
| 349 |