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 |
||
19 | class AuditTrailBehavior extends \yii\base\Behavior |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * Array with fields to save |
||
24 | * You don't need to configure both `allowed` and `ignored` |
||
25 | * @var array |
||
26 | */ |
||
27 | public $allowed = []; |
||
28 | |||
29 | /** |
||
30 | * Array with fields to ignore |
||
31 | * You don't need to configure both `allowed` and `ignored` |
||
32 | * @var array |
||
33 | */ |
||
34 | public $ignored = []; |
||
35 | |||
36 | /** |
||
37 | * Array with classes to ignore |
||
38 | * @var array |
||
39 | */ |
||
40 | public $ignoredClasses = []; |
||
41 | |||
42 | /** |
||
43 | * Is the behavior is active or not |
||
44 | * @var boolean |
||
45 | */ |
||
46 | public $active = true; |
||
47 | |||
48 | /** |
||
49 | * Date format to use in stamp - set to "Y-m-d H:i:s" for datetime or "U" for timestamp |
||
50 | * @var string |
||
51 | */ |
||
52 | public $dateFormat = 'Y-m-d H:i:s'; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | private $_oldAttributes = []; |
||
58 | |||
59 | /** |
||
60 | 304 | * Array with fields you want to override before saving the row into audit_trail table |
|
61 | * @var array |
||
62 | */ |
||
63 | 304 | public $override = []; |
|
64 | 304 | ||
65 | 304 | ||
66 | 304 | /** |
|
67 | 114 | * @inheritdoc |
|
68 | */ |
||
69 | public function events() |
||
78 | |||
79 | /** |
||
80 | * |
||
81 | 24 | */ |
|
82 | public function afterFind() |
||
86 | |||
87 | /** |
||
88 | * |
||
89 | */ |
||
90 | 23 | public function afterInsert() |
|
95 | |||
96 | /** |
||
97 | * |
||
98 | */ |
||
99 | 12 | public function afterUpdate() |
|
104 | |||
105 | /** |
||
106 | * |
||
107 | */ |
||
108 | public function afterDelete() |
||
113 | 9 | ||
114 | /** |
||
115 | * @param $action |
||
116 | 16 | * @throws \yii\db\Exception |
|
117 | 9 | */ |
|
118 | public function audit($action) |
||
136 | 11 | ||
137 | 11 | /** |
|
138 | 11 | * Clean attributes of fields that are not allowed or ignored. |
|
139 | * |
||
140 | * @param $attributes |
||
141 | * @return mixed |
||
142 | */ |
||
143 | protected function cleanAttributes($attributes) |
||
150 | 2 | ||
151 | 2 | /** |
|
152 | 2 | * Unset attributes which are not allowed |
|
153 | 2 | * |
|
154 | 2 | * @param $attributes |
|
155 | 2 | * @return mixed |
|
156 | 11 | */ |
|
157 | protected function cleanAttributesAllowed($attributes) |
||
168 | 2 | ||
169 | 2 | /** |
|
170 | 2 | * Unset attributes which are ignored |
|
171 | 2 | * |
|
172 | 2 | * @param $attributes |
|
173 | 2 | * @return mixed |
|
174 | 11 | */ |
|
175 | protected function cleanAttributesIgnored($attributes) |
||
186 | |||
187 | 11 | /** |
|
188 | 3 | * attributes which need to get override with a new value |
|
189 | * |
||
190 | * @param $attributes |
||
191 | 10 | * @return mixed |
|
192 | 10 | */ |
|
193 | 10 | protected function cleanAttributesOverride($attributes) |
|
210 | |||
211 | /** |
||
212 | * @param string $searchFieldValue |
||
213 | 26 | * @param string $queryParams |
|
214 | * @return mixed |
||
215 | */ |
||
216 | 26 | private function getNewOverrideValues($searchFieldValue, $queryParams) |
|
228 | 10 | ||
229 | 10 | ||
230 | 26 | /** |
|
231 | * @param string $action |
||
232 | * @throws \yii\db\Exception |
||
233 | */ |
||
234 | protected function auditAttributes($action) |
||
252 | |||
253 | 29 | /** |
|
254 | * Save the audit trails for a create or update action |
||
255 | * |
||
256 | * @param $action |
||
257 | * @param $newAttributes |
||
258 | * @param $oldAttributes |
||
259 | 27 | * @param $entry_id |
|
260 | * @param $user_id |
||
261 | 27 | * @param $model |
|
262 | 27 | * @param $model_id |
|
263 | * @param $created |
||
264 | * @throws \yii\db\Exception |
||
265 | */ |
||
266 | protected function saveAuditTrail($action, $newAttributes, $oldAttributes, $entry_id, $user_id, $model, $model_id, $created) |
||
284 | |||
285 | 32 | /** |
|
286 | * Save the audit trails for a delete action |
||
287 | 12 | */ |
|
288 | 32 | protected function saveAuditTrailDelete() |
|
300 | |||
301 | /** |
||
302 | * @return array |
||
303 | */ |
||
304 | public function getOldAttributes() |
||
308 | |||
309 | /** |
||
310 | * @param $value |
||
311 | */ |
||
312 | public function setOldAttributes($value) |
||
316 | |||
317 | /** |
||
318 | * @return string |
||
319 | */ |
||
320 | protected function getNormalizedPk() |
||
325 | |||
326 | /** |
||
327 | * @return int|null|string |
||
328 | */ |
||
329 | protected function getUserId() |
||
333 | |||
334 | /** |
||
335 | * @return models\AuditEntry|null|static |
||
336 | * @throws \Exception |
||
337 | */ |
||
338 | protected function getAuditEntryId() |
||
349 | |||
350 | } |
||
351 |