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 |
||
| 49 | class Audit extends Module |
||
| 50 | { |
||
| 51 | /** |
||
| 52 | * @var string|boolean the layout that should be applied for views within this module. This refers to a view name |
||
| 53 | * relative to [[layoutPath]]. If this is not set, it means the layout value of the [[module|parent module]] |
||
| 54 | * will be taken. If this is false, layout will be disabled within this module. |
||
| 55 | */ |
||
| 56 | public $layout = 'main'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string name of the component to use for database access |
||
| 60 | */ |
||
| 61 | public $db = 'db'; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string[] Action or list of actions to track. '*' is allowed as the first or last character to use as wildcard. |
||
| 65 | */ |
||
| 66 | public $trackActions = ['*']; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string[] Action or list of actions to ignore. '*' is allowed as the first or last character to use as wildcard (eg 'debug/*'). |
||
| 70 | */ |
||
| 71 | public $ignoreActions = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var int Maximum age (in days) of the audit entries before they are truncated |
||
| 75 | */ |
||
| 76 | public $maxAge = null; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var string[] IP address or list of IP addresses with access to the viewer, null for everyone (if the IP matches) |
||
| 80 | * An IP address can contain the wildcard `*` at the end so that it matches IP addresses with the same prefix. |
||
| 81 | * For example, '192.168.*' matches all IP addresses in the segment '192.168.'. |
||
| 82 | */ |
||
| 83 | public $accessIps = null; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var string[] Role or list of roles with access to the viewer, null for everyone (if the user matches) |
||
| 87 | */ |
||
| 88 | public $accessRoles = ['admin']; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var int[] User ID or list of user IDs with access to the viewer, null for everyone (if the role matches) |
||
| 92 | */ |
||
| 93 | public $accessUsers = null; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var bool Compress extra data generated or just keep in text? For people who don't like binary data in the DB |
||
| 97 | */ |
||
| 98 | public $compressData = true; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var string The callback to use to convert a user id into an identifier (username, email, ...). Can also be html. |
||
| 102 | */ |
||
| 103 | public $userIdentifierCallback = false; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var string Will be called to translate text in the user filter into a (or more) user id's |
||
| 107 | */ |
||
| 108 | public $userFilterCallback = false; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var bool The module does batch saving of the data records by default. You can disable this if you are experiencing |
||
| 112 | * `max_allowed_packet` errors when logging huge data quantities. Records will be saved per piece instead of all at once |
||
| 113 | */ |
||
| 114 | public $batchSave = true; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var array|Panel[] list of panels that should be active/tracking/available during the auditing phase. |
||
| 118 | * If the value is a simple string, it is the identifier of an internal panel to activate (with default settings) |
||
| 119 | * If the entry is a '<key>' => '<string>|<array>' it is either a new panel or a panel override (if you specify a core id). |
||
| 120 | * It is important that the key is unique, as this is the identifier used to store any data associated with the panel. |
||
| 121 | * |
||
| 122 | * Please note: |
||
| 123 | * - If you just want to change the configuration for a core panel, use the `$panelConfiguration`, it will be merged into this one |
||
| 124 | * - If you add custom panels, please namespace them ("mynamespace/panelname"). |
||
| 125 | */ |
||
| 126 | public $panels = [ |
||
| 127 | 'audit/request', |
||
| 128 | 'audit/db', |
||
| 129 | 'audit/log', |
||
| 130 | 'audit/mail', |
||
| 131 | 'audit/profiling', |
||
| 132 | 'audit/trail', |
||
| 133 | 'audit/javascript', |
||
| 134 | // 'audit/asset', |
||
| 135 | // 'audit/config', |
||
| 136 | |||
| 137 | // These provide special functionality and get loaded to activate it |
||
| 138 | 'audit/error', // Links the extra error reporting functions (`exception()` and `errorMessage()`) |
||
| 139 | 'audit/extra', // Links the data functions (`data()`) |
||
| 140 | 'audit/curl', // Links the curl tracking function (`curlBegin()`, `curlEnd()` and `curlExec()`) |
||
| 141 | ]; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Everything you add in here will be merged with the basic panel configuration. |
||
| 145 | * This gives you an easy way to just add or modify panels/configurations without having to re-specify every panel. |
||
| 146 | * This only accepts regular definitions ('<key>' => '<array>'), but the core class will be added if needed |
||
| 147 | * Take a look at the [module configuration](docs/module-configuration.md) for more information. |
||
| 148 | */ |
||
| 149 | public $panelsMerge = []; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @var LogTarget |
||
| 153 | */ |
||
| 154 | public $logTarget; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @var array |
||
| 158 | */ |
||
| 159 | private $_corePanels = [ |
||
| 160 | // Tracking/logging panels |
||
| 161 | 'audit/request' => ['class' => 'bedezign\yii2\audit\panels\RequestPanel'], |
||
| 162 | 'audit/db' => ['class' => 'bedezign\yii2\audit\panels\DbPanel'], |
||
| 163 | 'audit/log' => ['class' => 'bedezign\yii2\audit\panels\LogPanel'], |
||
| 164 | 'audit/asset' => ['class' => 'bedezign\yii2\audit\panels\AssetPanel'], |
||
| 165 | 'audit/config' => ['class' => 'bedezign\yii2\audit\panels\ConfigPanel'], |
||
| 166 | 'audit/profiling' => ['class' => 'bedezign\yii2\audit\panels\ProfilingPanel'], |
||
| 167 | |||
| 168 | // Special other panels |
||
| 169 | 'audit/error' => ['class' => 'bedezign\yii2\audit\panels\ErrorPanel'], |
||
| 170 | 'audit/javascript' => ['class' => 'bedezign\yii2\audit\panels\JavascriptPanel'], |
||
| 171 | 'audit/trail' => ['class' => 'bedezign\yii2\audit\panels\TrailPanel'], |
||
| 172 | 'audit/mail' => ['class' => 'bedezign\yii2\audit\panels\MailPanel'], |
||
| 173 | 'audit/extra' => ['class' => 'bedezign\yii2\audit\panels\ExtraDataPanel'], |
||
| 174 | 'audit/curl' => ['class' => 'bedezign\yii2\audit\panels\CurlPanel'], |
||
| 175 | 'audit/soap' => ['class' => 'bedezign\yii2\audit\panels\SoapPanel'], |
||
| 176 | ]; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @var array |
||
| 180 | */ |
||
| 181 | private $_panelFunctions = []; |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @var \bedezign\yii2\audit\models\AuditEntry If activated this is the active entry |
||
| 185 | */ |
||
| 186 | private $_entry = null; |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @throws InvalidConfigException |
||
| 190 | */ |
||
| 191 | 114 | public function init() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Called to evaluate if the current request should be logged |
||
| 211 | * @param ActionEvent $event |
||
| 212 | */ |
||
| 213 | 55 | public function onBeforeAction($event) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * |
||
| 227 | */ |
||
| 228 | 50 | public function onAfterRequest() |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Allows panels to register functions that can be called directly on the module |
||
| 237 | * @param string $name |
||
| 238 | * @param callable $callback |
||
| 239 | */ |
||
| 240 | 114 | public function registerFunction($name, $callback) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @param \yii\debug\Panel $panel |
||
| 250 | */ |
||
| 251 | public function registerPanel(\yii\debug\Panel $panel) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param string $name |
||
| 258 | * @param array $params |
||
| 259 | * @return mixed |
||
| 260 | */ |
||
| 261 | 21 | public function __call($name, $params) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * @return \yii\db\Connection the database connection. |
||
| 271 | */ |
||
| 272 | 71 | public function getDb() |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @param bool $create |
||
| 279 | * @param bool $new |
||
| 280 | * @return AuditEntry|static |
||
| 281 | */ |
||
| 282 | 92 | public function getEntry($create = false, $new = false) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @param $user_id |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | 14 | public function getUserIdentifier($user_id) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Returns a list of all available panel identifiers |
||
| 307 | * @return string[] |
||
| 308 | */ |
||
| 309 | 1 | public function getPanelIdentifiers() |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Tries to assemble the configuration for the panels that the user wants for auditing |
||
| 316 | * @param string[] Set of panel identifiers that should be loaded |
||
| 317 | * @return Panel[] |
||
| 318 | */ |
||
| 319 | 114 | public function loadPanels($list) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * @param string $identifier |
||
| 330 | * @return null|Panel |
||
| 331 | * @throws InvalidConfigException |
||
| 332 | */ |
||
| 333 | 142 | public function getPanel($identifier) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Make sure the configured panels array is a uniform set of <identifier> => <config> entries. |
||
| 355 | * @throws InvalidConfigException |
||
| 356 | */ |
||
| 357 | 114 | protected function normalizePanelConfiguration() |
|
| 384 | |||
| 385 | /** |
||
| 386 | * @return int|null|string |
||
| 387 | */ |
||
| 388 | 118 | public static function findModuleIdentifier() |
|
| 407 | |||
| 408 | /** |
||
| 409 | * @param string $className |
||
| 410 | * @return bool|string |
||
| 411 | */ |
||
| 412 | 27 | public static function findPanelIdentifier($className) |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Verifies a route against a given list and returns whether it matches or not. |
||
| 425 | * Entries in the list are allowed to end with a '*', which means that a substring will be used for the match |
||
| 426 | * instead of a full compare. |
||
| 427 | * |
||
| 428 | * @param string $route An application rout |
||
| 429 | * @param string[] $list List of routes to compare against. |
||
| 430 | * @return bool |
||
| 431 | */ |
||
| 432 | 67 | protected function routeMatches($route, $list) |
|
| 454 | |||
| 455 | } |
||
| 456 |