Complex classes like Audit 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 Audit, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | class Audit extends Module |
||
49 | { |
||
50 | /** |
||
51 | * @var string|boolean the layout that should be applied for views within this module. This refers to a view name |
||
52 | * relative to [[layoutPath]]. If this is not set, it means the layout value of the [[module|parent module]] |
||
53 | * will be taken. If this is false, layout will be disabled within this module. |
||
54 | */ |
||
55 | public $layout = 'main'; |
||
56 | |||
57 | /** |
||
58 | * @var string name of the component to use for database access |
||
59 | */ |
||
60 | public $db = 'db'; |
||
61 | |||
62 | /** |
||
63 | * @var string[] Action or list of actions to track. '*' is allowed as the first or last character to use as wildcard. |
||
64 | */ |
||
65 | public $trackActions = ['*']; |
||
66 | |||
67 | /** |
||
68 | * @var string[] Action or list of actions to ignore. '*' is allowed as the first or last character to use as wildcard (eg 'debug/*'). |
||
69 | */ |
||
70 | public $ignoreActions = []; |
||
71 | |||
72 | /** |
||
73 | * @var int Maximum age (in days) of the audit entries before they are truncated |
||
74 | */ |
||
75 | public $maxAge = null; |
||
76 | |||
77 | /** |
||
78 | * @var string[] IP address or list of IP addresses with access to the viewer, null for everyone (if the IP matches) |
||
79 | * An IP address can contain the wildcard `*` at the end so that it matches IP addresses with the same prefix. |
||
80 | * For example, '192.168.*' matches all IP addresses in the segment '192.168.'. |
||
81 | */ |
||
82 | public $accessIps = null; |
||
83 | |||
84 | /** |
||
85 | * @var string[] Role or list of roles with access to the viewer, null for everyone (if the user matches) |
||
86 | */ |
||
87 | public $accessRoles = ['admin']; |
||
88 | |||
89 | /** |
||
90 | * @var int[] User ID or list of user IDs with access to the viewer, null for everyone (if the role matches) |
||
91 | */ |
||
92 | public $accessUsers = null; |
||
93 | |||
94 | /** |
||
95 | * @var bool Compress extra data generated or just keep in text? For people who don't like binary data in the DB |
||
96 | */ |
||
97 | public $compressData = true; |
||
98 | |||
99 | /** |
||
100 | * @var string The callback to use to convert a user id into an identifier (username, email, ...). Can also be html. |
||
101 | */ |
||
102 | public $userIdentifierCallback = false; |
||
103 | |||
104 | /** |
||
105 | * @var string The callback to get a user id. |
||
106 | */ |
||
107 | public $userIdCallback = false; |
||
108 | |||
109 | /** |
||
110 | * @var string Will be called to translate text in the user filter into a (or more) user id's |
||
111 | */ |
||
112 | public $userFilterCallback = false; |
||
113 | |||
114 | /** |
||
115 | * @var bool The module does batch saving of the data records by default. You can disable this if you are experiencing |
||
116 | * `max_allowed_packet` errors when logging huge data quantities. Records will be saved per piece instead of all at once |
||
117 | */ |
||
118 | public $batchSave = true; |
||
119 | |||
120 | /** |
||
121 | * @var array|Panel[] list of panels that should be active/tracking/available during the auditing phase. |
||
122 | * If the value is a simple string, it is the identifier of an internal panel to activate (with default settings) |
||
123 | * If the entry is a '<key>' => '<string>|<array>' it is either a new panel or a panel override (if you specify a core id). |
||
124 | * It is important that the key is unique, as this is the identifier used to store any data associated with the panel. |
||
125 | * |
||
126 | * Please note: |
||
127 | * - If you just want to change the configuration for a core panel, use the `$panelConfiguration`, it will be merged into this one |
||
128 | * - If you add custom panels, please namespace them ("mynamespace/panelname"). |
||
129 | */ |
||
130 | public $panels = [ |
||
131 | 'audit/request', |
||
132 | 'audit/db', |
||
133 | 'audit/log', |
||
134 | 'audit/mail', |
||
135 | 'audit/profiling', |
||
136 | 'audit/trail', |
||
137 | 'audit/javascript', |
||
138 | // 'audit/asset', |
||
139 | // 'audit/config', |
||
140 | |||
141 | // These provide special functionality and get loaded to activate it |
||
142 | 'audit/error', // Links the extra error reporting functions (`exception()` and `errorMessage()`) |
||
143 | 'audit/extra', // Links the data functions (`data()`) |
||
144 | 'audit/curl', // Links the curl tracking function (`curlBegin()`, `curlEnd()` and `curlExec()`) |
||
145 | ]; |
||
146 | |||
147 | /** |
||
148 | * Everything you add in here will be merged with the basic panel configuration. |
||
149 | * This gives you an easy way to just add or modify panels/configurations without having to re-specify every panel. |
||
150 | * This only accepts regular definitions ('<key>' => '<array>'), but the core class will be added if needed |
||
151 | * Take a look at the [module configuration](docs/module-configuration.md) for more information. |
||
152 | */ |
||
153 | public $panelsMerge = []; |
||
154 | |||
155 | /** |
||
156 | * @var LogTarget |
||
157 | */ |
||
158 | public $logTarget; |
||
159 | |||
160 | /** |
||
161 | * @var array |
||
162 | */ |
||
163 | private $_corePanels = [ |
||
164 | // Tracking/logging panels |
||
165 | 'audit/request' => ['class' => 'bedezign\yii2\audit\panels\RequestPanel'], |
||
166 | 'audit/db' => ['class' => 'bedezign\yii2\audit\panels\DbPanel'], |
||
167 | 'audit/log' => ['class' => 'bedezign\yii2\audit\panels\LogPanel'], |
||
168 | 'audit/asset' => ['class' => 'bedezign\yii2\audit\panels\AssetPanel'], |
||
169 | 'audit/config' => ['class' => 'bedezign\yii2\audit\panels\ConfigPanel'], |
||
170 | 'audit/profiling' => ['class' => 'bedezign\yii2\audit\panels\ProfilingPanel'], |
||
171 | |||
172 | // Special other panels |
||
173 | 'audit/error' => ['class' => 'bedezign\yii2\audit\panels\ErrorPanel'], |
||
174 | 'audit/javascript' => ['class' => 'bedezign\yii2\audit\panels\JavascriptPanel'], |
||
175 | 'audit/trail' => ['class' => 'bedezign\yii2\audit\panels\TrailPanel'], |
||
176 | 'audit/mail' => ['class' => 'bedezign\yii2\audit\panels\MailPanel'], |
||
177 | 'audit/extra' => ['class' => 'bedezign\yii2\audit\panels\ExtraDataPanel'], |
||
178 | 'audit/curl' => ['class' => 'bedezign\yii2\audit\panels\CurlPanel'], |
||
179 | 'audit/soap' => ['class' => 'bedezign\yii2\audit\panels\SoapPanel'], |
||
180 | ]; |
||
181 | |||
182 | /** |
||
183 | * @var array |
||
184 | */ |
||
185 | private $_panelFunctions = []; |
||
186 | |||
187 | /** |
||
188 | * @var \bedezign\yii2\audit\models\AuditEntry If activated this is the active entry |
||
189 | */ |
||
190 | private $_entry = null; |
||
191 | 280 | ||
192 | /** |
||
193 | 114 | * @throws InvalidConfigException |
|
194 | 280 | */ |
|
195 | public function init() |
||
212 | |||
213 | 54 | /** |
|
214 | * Called to evaluate if the current request should be logged |
||
215 | 42 | * @param ActionEvent $event |
|
216 | 9 | */ |
|
217 | public function onBeforeAction($event) |
||
228 | 50 | ||
229 | /** |
||
230 | 50 | * |
|
231 | 18 | */ |
|
232 | 18 | public function onAfterRequest() |
|
238 | |||
239 | /** |
||
240 | 114 | * Allows panels to register functions that can be called directly on the module |
|
241 | * @param string $name |
||
242 | 114 | * @param callable $callback |
|
243 | 114 | */ |
|
244 | public function registerFunction($name, $callback) |
||
251 | |||
252 | /** |
||
253 | * @param \yii\debug\Panel $panel |
||
254 | */ |
||
255 | public function registerPanel(\yii\debug\Panel $panel) |
||
259 | |||
260 | /** |
||
261 | 21 | * @param string $name |
|
262 | * @param array $params |
||
263 | 21 | * @return mixed |
|
264 | 7 | */ |
|
265 | public function __call($name, $params) |
||
272 | 70 | ||
273 | /** |
||
274 | 70 | * @return \yii\db\Connection the database connection. |
|
275 | */ |
||
276 | public function getDb() |
||
280 | |||
281 | /** |
||
282 | 91 | * @param bool $create |
|
283 | * @param bool $new |
||
284 | 91 | * @return AuditEntry|static |
|
285 | 39 | */ |
|
286 | 39 | public function getEntry($create = false, $new = false) |
|
297 | |||
298 | /** |
||
299 | 12 | * @param $user_id |
|
300 | 1 | * @return string |
|
301 | */ |
||
302 | 11 | public function getUserIdentifier($user_id) |
|
312 | |||
313 | /** |
||
314 | * @return int|mixed|null|string |
||
315 | */ |
||
316 | public function getUserId() |
||
323 | 114 | ||
324 | 114 | /** |
|
325 | 280 | * Returns a list of all available panel identifiers |
|
326 | * @return string[] |
||
327 | */ |
||
328 | public function getPanelIdentifiers() |
||
332 | |||
333 | 142 | /** |
|
334 | * Tries to assemble the configuration for the panels that the user wants for auditing |
||
335 | 114 | * @param string[] Set of panel identifiers that should be loaded |
|
336 | 114 | * @return Panel[] |
|
337 | 114 | */ |
|
338 | 1 | public function loadPanels($list) |
|
346 | 114 | ||
347 | 114 | /** |
|
348 | * @param string $identifier |
||
349 | * @return null|Panel |
||
350 | 46 | * @throws InvalidConfigException |
|
351 | */ |
||
352 | public function getPanel($identifier) |
||
371 | 114 | ||
372 | 114 | /** |
|
373 | * Make sure the configured panels array is a uniform set of <identifier> => <config> entries. |
||
374 | * @throws InvalidConfigException |
||
375 | 114 | */ |
|
376 | 114 | protected function normalizePanelConfiguration() |
|
403 | 278 | ||
404 | 162 | /** |
|
405 | 6 | * @return int|null|string |
|
406 | */ |
||
407 | public static function findModuleIdentifier() |
||
426 | |||
427 | /** |
||
428 | * @param string $className |
||
429 | * @return bool|string |
||
430 | */ |
||
431 | public static function findPanelIdentifier($className) |
||
441 | 8 | ||
442 | /** |
||
443 | 62 | * Verifies a route against a given list and returns whether it matches or not. |
|
444 | 6 | * Entries in the list are allowed to end with a '*', which means that a substring will be used for the match |
|
445 | 6 | * instead of a full compare. |
|
446 | 14 | * |
|
447 | 2 | * @param string $route An application rout |
|
448 | * @param string[] $list List of routes to compare against. |
||
449 | 50 | * @return bool |
|
450 | 26 | */ |
|
451 | 36 | protected function routeMatches($route, $list) |
|
473 | |||
474 | } |
||
475 |