Complex classes like Log 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 Log, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 54 | class Log implements ILogger { |
||
| 55 | |||
| 56 | /** @var string */ |
||
| 57 | private $logger; |
||
| 58 | |||
| 59 | /** @var SystemConfig */ |
||
| 60 | private $config; |
||
| 61 | |||
| 62 | /** @var boolean|null cache the result of the log condition check for the request */ |
||
| 63 | private $logConditionSatisfied = null; |
||
| 64 | |||
| 65 | /** @var Normalizer */ |
||
| 66 | private $normalizer; |
||
| 67 | |||
| 68 | /** @var IRegistry */ |
||
| 69 | private $crashReporters; |
||
| 70 | |||
| 71 | protected $methodsWithSensitiveParameters = [ |
||
| 72 | // Session/User |
||
| 73 | 'completeLogin', |
||
| 74 | 'login', |
||
| 75 | 'checkPassword', |
||
| 76 | 'checkPasswordNoLogging', |
||
| 77 | 'loginWithPassword', |
||
| 78 | 'updatePrivateKeyPassword', |
||
| 79 | 'validateUserPass', |
||
| 80 | 'loginWithToken', |
||
| 81 | '{closure}', |
||
| 82 | |||
| 83 | // TokenProvider |
||
| 84 | 'getToken', |
||
| 85 | 'isTokenPassword', |
||
| 86 | 'getPassword', |
||
| 87 | 'decryptPassword', |
||
| 88 | 'logClientIn', |
||
| 89 | 'generateToken', |
||
| 90 | 'validateToken', |
||
| 91 | |||
| 92 | // TwoFactorAuth |
||
| 93 | 'solveChallenge', |
||
| 94 | 'verifyChallenge', |
||
| 95 | |||
| 96 | // ICrypto |
||
| 97 | 'calculateHMAC', |
||
| 98 | 'encrypt', |
||
| 99 | 'decrypt', |
||
| 100 | |||
| 101 | // LoginController |
||
| 102 | 'tryLogin', |
||
| 103 | 'confirmPassword', |
||
| 104 | |||
| 105 | // LDAP |
||
| 106 | 'bind', |
||
| 107 | 'areCredentialsValid', |
||
| 108 | 'invokeLDAPMethod', |
||
| 109 | |||
| 110 | // Encryption |
||
| 111 | 'storeKeyPair', |
||
| 112 | 'setupUser', |
||
| 113 | ]; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param string $logger The logger that should be used |
||
| 117 | * @param SystemConfig $config the system config object |
||
| 118 | * @param Normalizer|null $normalizer |
||
| 119 | * @param IRegistry|null $registry |
||
| 120 | */ |
||
| 121 | public function __construct($logger = null, SystemConfig $config = null, $normalizer = null, IRegistry $registry = null) { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * System is unusable. |
||
| 147 | * |
||
| 148 | * @param string $message |
||
| 149 | * @param array $context |
||
| 150 | * @return void |
||
| 151 | */ |
||
| 152 | public function emergency(string $message, array $context = []) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Action must be taken immediately. |
||
| 158 | * |
||
| 159 | * Example: Entire website down, database unavailable, etc. This should |
||
| 160 | * trigger the SMS alerts and wake you up. |
||
| 161 | * |
||
| 162 | * @param string $message |
||
| 163 | * @param array $context |
||
| 164 | * @return void |
||
| 165 | */ |
||
| 166 | public function alert(string $message, array $context = []) { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Critical conditions. |
||
| 172 | * |
||
| 173 | * Example: Application component unavailable, unexpected exception. |
||
| 174 | * |
||
| 175 | * @param string $message |
||
| 176 | * @param array $context |
||
| 177 | * @return void |
||
| 178 | */ |
||
| 179 | public function critical(string $message, array $context = []) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Runtime errors that do not require immediate action but should typically |
||
| 185 | * be logged and monitored. |
||
| 186 | * |
||
| 187 | * @param string $message |
||
| 188 | * @param array $context |
||
| 189 | * @return void |
||
| 190 | */ |
||
| 191 | public function error(string $message, array $context = []) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Exceptional occurrences that are not errors. |
||
| 197 | * |
||
| 198 | * Example: Use of deprecated APIs, poor use of an API, undesirable things |
||
| 199 | * that are not necessarily wrong. |
||
| 200 | * |
||
| 201 | * @param string $message |
||
| 202 | * @param array $context |
||
| 203 | * @return void |
||
| 204 | */ |
||
| 205 | public function warning(string $message, array $context = []) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Normal but significant events. |
||
| 211 | * |
||
| 212 | * @param string $message |
||
| 213 | * @param array $context |
||
| 214 | * @return void |
||
| 215 | */ |
||
| 216 | public function notice(string $message, array $context = []) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Interesting events. |
||
| 222 | * |
||
| 223 | * Example: User logs in, SQL logs. |
||
| 224 | * |
||
| 225 | * @param string $message |
||
| 226 | * @param array $context |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | public function info(string $message, array $context = []) { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Detailed debug information. |
||
| 235 | * |
||
| 236 | * @param string $message |
||
| 237 | * @param array $context |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | public function debug(string $message, array $context = []) { |
||
| 243 | |||
| 244 | |||
| 245 | /** |
||
| 246 | * Logs with an arbitrary level. |
||
| 247 | * |
||
| 248 | * @param int $level |
||
| 249 | * @param string $message |
||
| 250 | * @param array $context |
||
| 251 | * @return void |
||
| 252 | */ |
||
| 253 | public function log(int $level, string $message, array $context = []) { |
||
| 271 | |||
| 272 | private function getLogLevel($context) { |
||
| 326 | |||
| 327 | private function filterTrace(array $trace) { |
||
| 344 | |||
| 345 | private function removeValuesFromArgs($args, $values) { |
||
| 355 | |||
| 356 | private function serializeException(\Throwable $exception) { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Logs an exception very detailed |
||
| 379 | * |
||
| 380 | * @param \Exception|\Throwable $exception |
||
| 381 | * @param array $context |
||
| 382 | * @return void |
||
| 383 | * @since 8.2.0 |
||
| 384 | */ |
||
| 385 | public function logException(\Throwable $exception, array $context = []) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @param string $app |
||
| 411 | * @param string|array $entry |
||
| 412 | * @param int $level |
||
| 413 | */ |
||
| 414 | protected function writeLog(string $app, $entry, int $level) { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @param string $logType |
||
| 420 | * @return string |
||
| 421 | * @internal |
||
| 422 | */ |
||
| 423 | public static function getLogClass(string $logType): string { |
||
| 439 | } |
||
| 440 |
This check marks calls to
isset(...)orempty(...)that are found before the variable itself is defined. These will always have the same result.This is likely the result of code being shifted around. Consider removing these calls.