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 string[] Action or list of actions to track if they cause an error. '*' is allowed as the first or last character to use as wildcard. |
||
74 | */ |
||
75 | public $trackErrorActions = ['*']; |
||
76 | |||
77 | /** |
||
78 | * @var string[] Action or list of actions to ignore if they cause an error. '*' is allowed as the first or last character to use as wildcard (eg 'debug/*'). |
||
79 | */ |
||
80 | public $ignoreErrorActions = []; |
||
81 | |||
82 | /** |
||
83 | * @var int Maximum age (in days) of the audit entries before they are truncated |
||
84 | */ |
||
85 | public $maxAge = null; |
||
86 | |||
87 | /** |
||
88 | * @var string[] IP address or list of IP addresses with access to the viewer, null for everyone (if the IP matches) |
||
89 | * An IP address can contain the wildcard `*` at the end so that it matches IP addresses with the same prefix. |
||
90 | * For example, '192.168.*' matches all IP addresses in the segment '192.168.'. |
||
91 | */ |
||
92 | public $accessIps = null; |
||
93 | |||
94 | /** |
||
95 | * @var string[] Role or list of roles with access to the viewer, null for everyone (if the user matches) |
||
96 | */ |
||
97 | public $accessRoles = ['admin']; |
||
98 | |||
99 | /** |
||
100 | * @var int[] User ID or list of user IDs with access to the viewer, null for everyone (if the role matches) |
||
101 | */ |
||
102 | public $accessUsers = null; |
||
103 | |||
104 | /** |
||
105 | * @var bool Compress extra data generated or just keep in text? For people who don't like binary data in the DB |
||
106 | */ |
||
107 | public $compressData = true; |
||
108 | |||
109 | /** |
||
110 | * @var string The callback to use to convert a user id into an identifier (username, email, ...). Can also be html. |
||
111 | */ |
||
112 | public $userIdentifierCallback = false; |
||
113 | |||
114 | /** |
||
115 | * @var string The callback to get a user id. |
||
116 | */ |
||
117 | public $userIdCallback = false; |
||
118 | |||
119 | /** |
||
120 | * @var string Will be called to translate text in the user filter into a (or more) user id's |
||
121 | */ |
||
122 | public $userFilterCallback = false; |
||
123 | |||
124 | /** |
||
125 | * @var bool The module does batch saving of the data records by default. You can disable this if you are experiencing |
||
126 | * `max_allowed_packet` errors when logging huge data quantities. Records will be saved per piece instead of all at once |
||
127 | */ |
||
128 | public $batchSave = true; |
||
129 | |||
130 | /** |
||
131 | * @var array Default log levels to filter and process |
||
132 | */ |
||
133 | public $logConfig = ['levels' => ['error', 'warning', 'info', 'profile']]; |
||
134 | |||
135 | |||
136 | /** |
||
137 | * @var array|Panel[] list of panels that should be active/tracking/available during the auditing phase. |
||
138 | * If the value is a simple string, it is the identifier of an internal panel to activate (with default settings) |
||
139 | * If the entry is a '<key>' => '<string>|<array>' it is either a new panel or a panel override (if you specify a core id). |
||
140 | * It is important that the key is unique, as this is the identifier used to store any data associated with the panel. |
||
141 | * |
||
142 | * Please note: |
||
143 | * - If you just want to change the configuration for a core panel, use the `$panelConfiguration`, it will be merged into this one |
||
144 | * - If you add custom panels, please namespace them ("mynamespace/panelname"). |
||
145 | */ |
||
146 | public $panels = [ |
||
147 | 'audit/request', |
||
148 | 'audit/db', |
||
149 | 'audit/log', |
||
150 | 'audit/mail', |
||
151 | 'audit/profiling', |
||
152 | 'audit/trail', |
||
153 | 'audit/javascript', |
||
154 | // 'audit/asset', |
||
155 | // 'audit/config', |
||
156 | |||
157 | // These provide special functionality and get loaded to activate it |
||
158 | 'audit/error', // Links the extra error reporting functions (`exception()` and `errorMessage()`) |
||
159 | 'audit/extra', // Links the data functions (`data()`) |
||
160 | 'audit/curl', // Links the curl tracking function (`curlBegin()`, `curlEnd()` and `curlExec()`) |
||
161 | ]; |
||
162 | |||
163 | /** |
||
164 | * Everything you add in here will be merged with the basic panel configuration. |
||
165 | * This gives you an easy way to just add or modify panels/configurations without having to re-specify every panel. |
||
166 | * This only accepts regular definitions ('<key>' => '<array>'), but the core class will be added if needed |
||
167 | * Take a look at the [module configuration](docs/module-configuration.md) for more information. |
||
168 | */ |
||
169 | public $panelsMerge = []; |
||
170 | |||
171 | /** |
||
172 | * @var LogTarget |
||
173 | */ |
||
174 | public $logTarget; |
||
175 | |||
176 | // Things required to keep the module yii2-debug compatible |
||
177 | /* @see \yii\debug\Module::$traceLine (since 2.0.7) */ |
||
178 | public $traceLine = \yii\debug\Module::DEFAULT_IDE_TRACELINE; |
||
179 | /* @see \yii\debug\Module::$tracePathMappings (since 2.1.6) */ |
||
180 | public $tracePathMappings = []; |
||
181 | |||
182 | /** |
||
183 | * @var array |
||
184 | */ |
||
185 | private $_corePanels = [ |
||
186 | // Tracking/logging panels |
||
187 | 'audit/request' => ['class' => 'bedezign\yii2\audit\panels\RequestPanel'], |
||
188 | 'audit/db' => ['class' => 'bedezign\yii2\audit\panels\DbPanel'], |
||
189 | 'audit/log' => ['class' => 'bedezign\yii2\audit\panels\LogPanel'], |
||
190 | 'audit/asset' => ['class' => 'bedezign\yii2\audit\panels\AssetPanel'], |
||
191 | 'audit/config' => ['class' => 'bedezign\yii2\audit\panels\ConfigPanel'], |
||
192 | 'audit/profiling' => ['class' => 'bedezign\yii2\audit\panels\ProfilingPanel'], |
||
193 | |||
194 | // Special other panels |
||
195 | 'audit/error' => ['class' => 'bedezign\yii2\audit\panels\ErrorPanel'], |
||
196 | 'audit/javascript' => ['class' => 'bedezign\yii2\audit\panels\JavascriptPanel'], |
||
197 | 'audit/trail' => ['class' => 'bedezign\yii2\audit\panels\TrailPanel'], |
||
198 | 'audit/mail' => ['class' => 'bedezign\yii2\audit\panels\MailPanel'], |
||
199 | 'audit/extra' => ['class' => 'bedezign\yii2\audit\panels\ExtraDataPanel'], |
||
200 | 'audit/curl' => ['class' => 'bedezign\yii2\audit\panels\CurlPanel'], |
||
201 | 'audit/soap' => ['class' => 'bedezign\yii2\audit\panels\SoapPanel'], |
||
202 | ]; |
||
203 | |||
204 | /** |
||
205 | * @var array |
||
206 | 78 | */ |
|
207 | private $_panelFunctions = []; |
||
208 | 78 | ||
209 | 78 | /** |
|
210 | * @var \bedezign\yii2\audit\models\AuditEntry If activated this is the active entry |
||
211 | */ |
||
212 | private $_entry = null; |
||
213 | 78 | ||
214 | 78 | private static $_me = null; |
|
215 | |||
216 | /** |
||
217 | * @throws InvalidConfigException |
||
218 | */ |
||
219 | 78 | public function init() |
|
242 | 9 | ||
243 | public function shouldTrack($event, $isError = false) |
||
257 | 57 | ||
258 | 57 | /** |
|
259 | 57 | * Called to evaluate if the current request should be logged |
|
260 | * @param ActionEvent $event |
||
261 | */ |
||
262 | public function onBeforeAction($event) |
||
271 | 78 | ||
272 | 78 | /** |
|
273 | * |
||
274 | */ |
||
275 | public function onAfterRequest() |
||
281 | |||
282 | /** |
||
283 | * Allows panels to register functions that can be called directly on the module |
||
284 | * @param string $name |
||
285 | * @param callable $callback |
||
286 | */ |
||
287 | 21 | public function registerFunction($name, $callback) |
|
294 | |||
295 | public function hasMethod($name, $checkBehaviors = true) |
||
302 | |||
303 | /** |
||
304 | * @param \yii\debug\Panel $panel |
||
305 | */ |
||
306 | public function registerPanel(\yii\debug\Panel $panel) |
||
310 | 171 | ||
311 | 171 | /** |
|
312 | 171 | * @param string $name |
|
313 | 171 | * @param array $params |
|
314 | 117 | * @return mixed |
|
315 | 117 | */ |
|
316 | 171 | public function __call($name, $params) |
|
323 | |||
324 | 18 | public function activateLogTarget() |
|
335 | |||
336 | /** |
||
337 | * @return \yii\db\Connection the database connection. |
||
338 | 120 | */ |
|
339 | public function getDb() |
||
343 | 117 | ||
344 | public static function getInstance() |
||
361 | |||
362 | 78 | ||
363 | 78 | /** |
|
364 | 78 | * @param bool $create |
|
365 | 78 | * @param bool $new |
|
366 | 78 | * @return AuditEntry |
|
367 | */ |
||
368 | public function getEntry($create = false, $new = false) |
||
379 | 3 | ||
380 | 3 | /** |
|
381 | * @param AuditEntry $entry |
||
382 | 114 | */ |
|
383 | 114 | public function setEntry($entry) |
|
387 | 78 | ||
388 | 78 | /** |
|
389 | * @param $user_id |
||
390 | * @return string |
||
391 | 54 | */ |
|
392 | public function getUserIdentifier($user_id) |
||
402 | 78 | ||
403 | /** |
||
404 | 78 | * @return int|mixed|null|string |
|
405 | 78 | */ |
|
406 | 78 | public function getUserId() |
|
413 | 78 | ||
414 | /** |
||
415 | * Returns a list of all available panel identifiers |
||
416 | 78 | * @return string[] |
|
417 | 78 | */ |
|
418 | public function getPanelIdentifiers() |
||
422 | |||
423 | 78 | /** |
|
424 | 78 | * Tries to assemble the configuration for the panels that the user wants for auditing |
|
425 | * @param string[] Set of panel identifiers that should be loaded |
||
426 | * @return Panel[] |
||
427 | */ |
||
428 | public function loadPanels($list) |
||
436 | 78 | ||
437 | 78 | /** |
|
438 | 78 | * @param string $identifier |
|
439 | * @return null|Panel |
||
440 | 3 | * @throws InvalidConfigException |
|
441 | */ |
||
442 | 81 | public function getPanel($identifier) |
|
461 | |||
462 | /** |
||
463 | * Make sure the configured panels array is a uniform set of <identifier> => <config> entries. |
||
464 | * @throws InvalidConfigException |
||
465 | */ |
||
466 | protected function normalizePanelConfiguration() |
||
493 | 36 | ||
494 | /** |
||
495 | * @return int|null|string |
||
496 | */ |
||
497 | public static function findModuleIdentifier() |
||
516 | |||
517 | /** |
||
518 | * @param string $className |
||
519 | * @return bool|string |
||
520 | */ |
||
521 | public static function findPanelIdentifier($className) |
||
531 | |||
532 | /** |
||
533 | * Verifies a route against a given list and returns whether it matches or not. |
||
534 | * Entries in the list are allowed to end with a '*', which means that a substring will be used for the match |
||
535 | * instead of a full compare. |
||
536 | * |
||
537 | * @param string $route An application rout |
||
538 | * @param string[] $list List of routes to compare against. |
||
539 | * @return bool |
||
540 | */ |
||
541 | protected function routeMatches($route, $list) |
||
563 | |||
564 | } |
||
565 |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: