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 | 304 | public $override = []; |
|
61 | |||
62 | |||
63 | 304 | /** |
|
64 | 304 | * @inheritdoc |
|
65 | 304 | */ |
|
66 | 304 | public function events() |
|
75 | 15 | ||
76 | 37 | /** |
|
77 | * |
||
78 | */ |
||
79 | public function afterFind() |
||
83 | 8 | ||
84 | 8 | /** |
|
85 | 24 | * |
|
86 | */ |
||
87 | public function afterInsert() |
||
92 | 9 | ||
93 | 9 | /** |
|
94 | 23 | * |
|
95 | */ |
||
96 | public function afterUpdate() |
||
101 | 4 | ||
102 | 4 | /** |
|
103 | 12 | * |
|
104 | */ |
||
105 | public function afterDelete() |
||
110 | |||
111 | /** |
||
112 | 53 | * @param $action |
|
113 | 9 | * @throws \yii\db\Exception |
|
114 | */ |
||
115 | public function audit($action) |
||
133 | |||
134 | 11 | /** |
|
135 | * Clean attributes of fields that are not allowed or ignored. |
||
136 | 11 | * |
|
137 | 11 | * @param $attributes |
|
138 | 11 | * @return mixed |
|
139 | */ |
||
140 | protected function cleanAttributes($attributes) |
||
147 | 11 | ||
148 | /** |
||
149 | 11 | * Unset attributes which are not allowed |
|
150 | 2 | * |
|
151 | 2 | * @param $attributes |
|
152 | 2 | * @return mixed |
|
153 | 2 | */ |
|
154 | 2 | protected function cleanAttributesAllowed($attributes) |
|
165 | 11 | ||
166 | /** |
||
167 | 11 | * Unset attributes which are ignored |
|
168 | 2 | * |
|
169 | 2 | * @param $attributes |
|
170 | 2 | * @return mixed |
|
171 | 2 | */ |
|
172 | 2 | protected function cleanAttributesIgnored($attributes) |
|
183 | |||
184 | 11 | /** |
|
185 | 11 | * attributes which need to get override with a new value |
|
186 | * |
||
187 | 11 | * @param $attributes |
|
188 | 3 | * @return mixed |
|
189 | */ |
||
190 | protected function cleanAttributesOverride($attributes) |
||
208 | |||
209 | /** |
||
210 | * @param string $searchFieldValue |
||
211 | * @param string $queryParams |
||
212 | * @return mixed |
||
213 | 26 | */ |
|
214 | private function getNewOverrideValues($searchFieldValue, $queryParams) |
||
226 | 26 | ||
227 | 10 | ||
228 | 10 | /** |
|
229 | 10 | * @param string $action |
|
230 | 26 | * @throws \yii\db\Exception |
|
231 | */ |
||
232 | protected function auditAttributes($action) |
||
260 | |||
261 | 27 | /** |
|
262 | 27 | * Save the audit trails for a create or update action |
|
263 | * |
||
264 | * @param $action |
||
265 | * @param $newAttributes |
||
266 | * @param $oldAttributes |
||
267 | 12 | * @param $entry_id |
|
268 | * @param $user_id |
||
269 | 12 | * @param $model |
|
270 | 12 | * @param $model_id |
|
271 | * @param $created |
||
272 | * @throws \yii\db\Exception |
||
273 | */ |
||
274 | protected function saveAuditTrail($action, $newAttributes, $oldAttributes, $entry_id, $user_id, $model, $model_id, $created) |
||
292 | |||
293 | /** |
||
294 | 12 | * Save the audit trails for a delete action |
|
295 | */ |
||
296 | protected function saveAuditTrailDelete() |
||
308 | |||
309 | /** |
||
310 | * @return array |
||
311 | */ |
||
312 | public function getOldAttributes() |
||
316 | |||
317 | /** |
||
318 | * @param $value |
||
319 | */ |
||
320 | public function setOldAttributes($value) |
||
324 | |||
325 | /** |
||
326 | * @return string |
||
327 | */ |
||
328 | protected function getNormalizedPk() |
||
333 | |||
334 | /** |
||
335 | * @return int|null|string |
||
336 | */ |
||
337 | protected function getUserId() |
||
341 | |||
342 | /** |
||
343 | * @return models\AuditEntry|null|static |
||
344 | * @throws \Exception |
||
345 | */ |
||
346 | protected function getAuditEntryId() |
||
357 | |||
358 | } |
||
359 |