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) |
|
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 | * @param array $array |
|
229 | 10 | * @param string $return_field |
|
230 | 26 | * @return mixed |
|
231 | */ |
||
232 | |||
233 | private function getCommaString($array, $return_field) |
||
243 | 6 | ||
244 | 6 | /** |
|
245 | 2 | * @param string $action |
|
246 | 6 | * @throws \yii\db\Exception |
|
247 | */ |
||
248 | protected function auditAttributes($action) |
||
266 | |||
267 | 12 | /** |
|
268 | * Save the audit trails for a create or update action |
||
269 | 12 | * |
|
270 | 12 | * @param $action |
|
271 | * @param $newAttributes |
||
272 | * @param $oldAttributes |
||
273 | * @param $entry_id |
||
274 | * @param $user_id |
||
275 | * @param $model |
||
276 | 12 | * @param $model_id |
|
277 | * @param $created |
||
278 | 12 | * @throws \yii\db\Exception |
|
279 | */ |
||
280 | protected function saveAuditTrail($action, $newAttributes, $oldAttributes, $entry_id, $user_id, $model, $model_id, $created) |
||
298 | |||
299 | /** |
||
300 | * Save the audit trails for a delete action |
||
301 | */ |
||
302 | protected function saveAuditTrailDelete() |
||
314 | |||
315 | /** |
||
316 | * @return array |
||
317 | */ |
||
318 | public function getOldAttributes() |
||
322 | |||
323 | /** |
||
324 | * @param $value |
||
325 | */ |
||
326 | public function setOldAttributes($value) |
||
330 | |||
331 | /** |
||
332 | * @return string |
||
333 | */ |
||
334 | protected function getNormalizedPk() |
||
339 | |||
340 | /** |
||
341 | * @return int|null|string |
||
342 | */ |
||
343 | protected function getUserId() |
||
347 | |||
348 | /** |
||
349 | * @return models\AuditEntry|null|static |
||
350 | * @throws \Exception |
||
351 | */ |
||
352 | protected function getAuditEntryId() |
||
363 | |||
364 | } |
||
365 |