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 |
||
| 51 | class Log implements ILogger { |
||
| 52 | |||
| 53 | /** @var string */ |
||
| 54 | private $logger; |
||
| 55 | |||
| 56 | /** @var SystemConfig */ |
||
| 57 | private $config; |
||
| 58 | |||
| 59 | /** @var boolean|null cache the result of the log condition check for the request */ |
||
| 60 | private $logConditionSatisfied = null; |
||
| 61 | |||
| 62 | /** @var Normalizer */ |
||
| 63 | private $normalizer; |
||
| 64 | |||
| 65 | protected $methodsWithSensitiveParameters = [ |
||
| 66 | // Session/User |
||
| 67 | 'login', |
||
| 68 | 'checkPassword', |
||
| 69 | 'updatePrivateKeyPassword', |
||
| 70 | 'validateUserPass', |
||
| 71 | 'loginWithPassword', |
||
| 72 | |||
| 73 | // TokenProvider |
||
| 74 | 'getToken', |
||
| 75 | 'isTokenPassword', |
||
| 76 | 'getPassword', |
||
| 77 | 'decryptPassword', |
||
| 78 | 'logClientIn', |
||
| 79 | 'generateToken', |
||
| 80 | 'validateToken', |
||
| 81 | |||
| 82 | // TwoFactorAuth |
||
| 83 | 'solveChallenge', |
||
| 84 | 'verifyChallenge', |
||
| 85 | |||
| 86 | //ICrypto |
||
| 87 | 'calculateHMAC', |
||
| 88 | 'encrypt', |
||
| 89 | 'decrypt', |
||
| 90 | |||
| 91 | //LoginController |
||
| 92 | 'tryLogin' |
||
| 93 | ]; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $logger The logger that should be used |
||
| 97 | * @param SystemConfig $config the system config object |
||
| 98 | * @param null $normalizer |
||
| 99 | */ |
||
| 100 | public function __construct($logger = null, SystemConfig $config = null, $normalizer = null) { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * System is unusable. |
||
| 125 | * |
||
| 126 | * @param string $message |
||
| 127 | * @param array $context |
||
| 128 | * @return void |
||
| 129 | */ |
||
| 130 | public function emergency($message, array $context = []) { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Action must be taken immediately. |
||
| 136 | * |
||
| 137 | * Example: Entire website down, database unavailable, etc. This should |
||
| 138 | * trigger the SMS alerts and wake you up. |
||
| 139 | * |
||
| 140 | * @param string $message |
||
| 141 | * @param array $context |
||
| 142 | * @return void |
||
| 143 | */ |
||
| 144 | public function alert($message, array $context = []) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Critical conditions. |
||
| 150 | * |
||
| 151 | * Example: Application component unavailable, unexpected exception. |
||
| 152 | * |
||
| 153 | * @param string $message |
||
| 154 | * @param array $context |
||
| 155 | * @return void |
||
| 156 | */ |
||
| 157 | public function critical($message, array $context = []) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Runtime errors that do not require immediate action but should typically |
||
| 163 | * be logged and monitored. |
||
| 164 | * |
||
| 165 | * @param string $message |
||
| 166 | * @param array $context |
||
| 167 | * @return void |
||
| 168 | */ |
||
| 169 | public function error($message, array $context = []) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Exceptional occurrences that are not errors. |
||
| 175 | * |
||
| 176 | * Example: Use of deprecated APIs, poor use of an API, undesirable things |
||
| 177 | * that are not necessarily wrong. |
||
| 178 | * |
||
| 179 | * @param string $message |
||
| 180 | * @param array $context |
||
| 181 | * @return void |
||
| 182 | */ |
||
| 183 | public function warning($message, array $context = []) { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Normal but significant events. |
||
| 189 | * |
||
| 190 | * @param string $message |
||
| 191 | * @param array $context |
||
| 192 | * @return void |
||
| 193 | */ |
||
| 194 | public function notice($message, array $context = []) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Interesting events. |
||
| 200 | * |
||
| 201 | * Example: User logs in, SQL logs. |
||
| 202 | * |
||
| 203 | * @param string $message |
||
| 204 | * @param array $context |
||
| 205 | * @return void |
||
| 206 | */ |
||
| 207 | public function info($message, array $context = []) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Detailed debug information. |
||
| 213 | * |
||
| 214 | * @param string $message |
||
| 215 | * @param array $context |
||
| 216 | * @return void |
||
| 217 | */ |
||
| 218 | public function debug($message, array $context = []) { |
||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * Logs with an arbitrary level. |
||
| 225 | * |
||
| 226 | * @param mixed $level |
||
| 227 | * @param string $message |
||
| 228 | * @param array $context |
||
| 229 | * @return void |
||
| 230 | */ |
||
| 231 | public function log($level, $message, array $context = []) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * interpolate $message as defined in PSR-3 |
||
| 323 | * @param string $message |
||
| 324 | * @param array $context |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | protected function interpolate ($message, array $context) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Logs an exception very detailed |
||
| 341 | * |
||
| 342 | * @param \Exception | \Throwable $exception |
||
| 343 | * @param array $context |
||
| 344 | * @return void |
||
| 345 | * @since 8.2.0 |
||
| 346 | */ |
||
| 347 | public function logException($exception, array $context = []) { |
||
| 364 | } |
||
| 365 |