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 | * Array with fields you want to override before saving the row into audit_trail table |
||
67 | * @var array |
||
68 | */ |
||
69 | public $override = []; |
||
70 | |||
71 | /** |
||
72 | * @inheritdoc |
||
73 | */ |
||
74 | 38 | public function events() |
|
83 | |||
84 | /** |
||
85 | * |
||
86 | */ |
||
87 | 30 | public function afterFind() |
|
91 | |||
92 | /** |
||
93 | * |
||
94 | */ |
||
95 | 16 | public function afterInsert() |
|
100 | |||
101 | /** |
||
102 | * |
||
103 | */ |
||
104 | 18 | public function afterUpdate() |
|
109 | |||
110 | /** |
||
111 | * |
||
112 | */ |
||
113 | 8 | public function afterDelete() |
|
118 | |||
119 | /** |
||
120 | * @param $action |
||
121 | * @throws \yii\db\Exception |
||
122 | */ |
||
123 | 38 | public function audit($action) |
|
141 | |||
142 | /** |
||
143 | * Clean attributes of fields that are not allowed or ignored. |
||
144 | * |
||
145 | * @param $attributes |
||
146 | * @return mixed |
||
147 | */ |
||
148 | 22 | protected function cleanAttributes($attributes) |
|
155 | |||
156 | /** |
||
157 | * Unset attributes which are not allowed |
||
158 | * |
||
159 | * @param $attributes |
||
160 | * @return mixed |
||
161 | */ |
||
162 | 22 | protected function cleanAttributesAllowed($attributes) |
|
173 | |||
174 | /** |
||
175 | * Unset attributes which are ignored |
||
176 | * |
||
177 | * @param $attributes |
||
178 | * @return mixed |
||
179 | */ |
||
180 | 22 | 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 | 22 | 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 | |||
239 | |||
240 | /** |
||
241 | * @param string $action |
||
242 | * @throws \yii\db\Exception |
||
243 | */ |
||
244 | 22 | protected function auditAttributes($action) |
|
262 | |||
263 | /** |
||
264 | * Save the audit trails for a create or update action |
||
265 | * |
||
266 | * @param $action |
||
267 | * @param $newAttributes |
||
268 | * @param $oldAttributes |
||
269 | * @param $entry_id |
||
270 | * @param $user_id |
||
271 | * @param $model |
||
272 | * @param $model_id |
||
273 | * @param $created |
||
274 | * @throws \yii\db\Exception |
||
275 | */ |
||
276 | 24 | protected function saveAuditTrail($action, $newAttributes, $oldAttributes, $entry_id, $user_id, $model, $model_id, $created) |
|
294 | |||
295 | /** |
||
296 | * Save the audit trails for a delete action |
||
297 | */ |
||
298 | 24 | protected function saveAuditTrailDelete() |
|
310 | |||
311 | /** |
||
312 | * @return array |
||
313 | */ |
||
314 | 22 | public function getOldAttributes() |
|
318 | |||
319 | /** |
||
320 | * @param $value |
||
321 | */ |
||
322 | 38 | public function setOldAttributes($value) |
|
326 | |||
327 | /** |
||
328 | * @return string |
||
329 | */ |
||
330 | 24 | protected function getNormalizedPk() |
|
335 | |||
336 | /** |
||
337 | * @return int|null|string |
||
338 | */ |
||
339 | 24 | protected function getUserId() |
|
343 | |||
344 | /** |
||
345 | * @return models\AuditEntry|null|static |
||
346 | * @throws \Exception |
||
347 | */ |
||
348 | 24 | protected function getAuditEntryId() |
|
359 | |||
360 | } |
||
361 |