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 |
||
17 | class AuditTrailBehavior extends Behavior |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * Array with fields to save |
||
22 | * You don't need to configure both `allowed` and `ignored` |
||
23 | * @var array |
||
24 | */ |
||
25 | public $allowed = []; |
||
26 | |||
27 | /** |
||
28 | * Array with fields to ignore |
||
29 | * You don't need to configure both `allowed` and `ignored` |
||
30 | * @var array |
||
31 | */ |
||
32 | public $ignored = []; |
||
33 | |||
34 | /** |
||
35 | * Array with classes to ignore |
||
36 | * @var array |
||
37 | */ |
||
38 | public $ignoredClasses = []; |
||
39 | |||
40 | /** |
||
41 | * Timestamp attributes should, in most cases, be ignored. If both AudittrailBehavior and |
||
42 | * TimestampBehavior logs the created_at and updated_at fields, the data is saved twice. |
||
43 | * In case you want to log them, you can unset the column from this timestamp column name suggestions. |
||
44 | * Set to null to disable this filter and log all columns. |
||
45 | * @var null|array |
||
46 | */ |
||
47 | public $timestamp_fields = ['created', 'updated', 'created_at', 'updated_at', 'timestamp']; |
||
48 | |||
49 | /** |
||
50 | * Is the behavior is active or not |
||
51 | * @var boolean |
||
52 | */ |
||
53 | public $active = true; |
||
54 | |||
55 | /** |
||
56 | * Date format to use in stamp - set to "Y-m-d H:i:s" for datetime or "U" for timestamp |
||
57 | * @var string |
||
58 | */ |
||
59 | public $dateFormat = 'Y-m-d H:i:s'; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | private $_oldAttributes = []; |
||
65 | |||
66 | 57 | /** |
|
67 | * Array with fields you want to override before saving the row into audit_trail table |
||
68 | * @var array |
||
69 | 57 | */ |
|
70 | 57 | public $override = []; |
|
71 | 57 | ||
72 | 57 | /** |
|
73 | 57 | * @inheritdoc |
|
74 | */ |
||
75 | public function events() |
||
84 | |||
85 | /** |
||
86 | * |
||
87 | 24 | */ |
|
88 | public function afterFind() |
||
92 | |||
93 | /** |
||
94 | * |
||
95 | */ |
||
96 | 27 | public function afterInsert() |
|
101 | |||
102 | /** |
||
103 | * |
||
104 | */ |
||
105 | 12 | public function afterUpdate() |
|
110 | |||
111 | /** |
||
112 | * |
||
113 | */ |
||
114 | public function afterDelete() |
||
119 | 9 | ||
120 | /** |
||
121 | * @param $action |
||
122 | 48 | * @throws \yii\db\Exception |
|
123 | 9 | */ |
|
124 | public function audit($action) |
||
142 | 33 | ||
143 | 33 | /** |
|
144 | 33 | * Clean attributes of fields that are not allowed or ignored. |
|
145 | 33 | * |
|
146 | * @param $attributes |
||
147 | * @return mixed |
||
148 | */ |
||
149 | protected function cleanAttributes($attributes) |
||
156 | 33 | ||
157 | 6 | /** |
|
158 | 6 | * Unset attributes which are not allowed |
|
159 | 6 | * |
|
160 | 6 | * @param $attributes |
|
161 | 6 | * @return mixed |
|
162 | 6 | */ |
|
163 | 33 | protected function cleanAttributesAllowed($attributes) |
|
174 | 33 | ||
175 | 6 | /** |
|
176 | 6 | * Unset attributes which are ignored |
|
177 | 6 | * |
|
178 | 6 | * @param $attributes |
|
179 | 6 | * @return mixed |
|
180 | 6 | */ |
|
181 | 33 | protected function cleanAttributesIgnored($attributes) |
|
196 | |||
197 | /** |
||
198 | * attributes which need to get override with a new value |
||
199 | * |
||
200 | * @param $attributes |
||
201 | * @return mixed |
||
202 | */ |
||
203 | protected function cleanAttributesOverride($attributes) |
||
221 | |||
222 | /** |
||
223 | * @param string $searchFieldValue |
||
224 | * @param string $queryParams |
||
225 | * @return mixed |
||
226 | */ |
||
227 | private function getNewOverrideValues($searchFieldValue, $queryParams) |
||
239 | 3 | ||
240 | |||
241 | /** |
||
242 | 30 | * @param string $action |
|
243 | 30 | * @throws \yii\db\Exception |
|
244 | 30 | */ |
|
245 | 30 | protected function auditAttributes($action) |
|
273 | 30 | ||
274 | 30 | /** |
|
275 | * Save the audit trails for a create or update action |
||
276 | 30 | * |
|
277 | 30 | * @param $action |
|
278 | 30 | * @param $newAttributes |
|
279 | 30 | * @param $oldAttributes |
|
280 | 30 | * @param $entry_id |
|
281 | 30 | * @param $user_id |
|
282 | * @param $model |
||
283 | * @param $model_id |
||
284 | * @param $created |
||
285 | * @throws \yii\db\Exception |
||
286 | 6 | */ |
|
287 | protected function saveAuditTrail($action, $newAttributes, $oldAttributes, $entry_id, $user_id, $model, $model_id, $created) |
||
305 | |||
306 | /** |
||
307 | * Save the audit trails for a delete action |
||
308 | */ |
||
309 | protected function saveAuditTrailDelete() |
||
321 | 36 | ||
322 | /** |
||
323 | * @return array |
||
324 | */ |
||
325 | public function getOldAttributes() |
||
329 | 36 | ||
330 | /** |
||
331 | * @param $value |
||
332 | */ |
||
333 | public function setOldAttributes($value) |
||
337 | |||
338 | 36 | /** |
|
339 | 36 | * @return string |
|
340 | */ |
||
341 | protected function getNormalizedPk() |
||
346 | |||
347 | /** |
||
348 | * @return int|null|string |
||
349 | */ |
||
350 | protected function getUserId() |
||
354 | |||
355 | /** |
||
356 | * @return models\AuditEntry|null|static |
||
357 | * @throws \Exception |
||
358 | */ |
||
359 | protected function getAuditEntryId() |
||
370 | |||
371 | } |
||
372 |