Complex classes like Bouncer 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 Bouncer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Bouncer |
||
| 18 | { |
||
| 19 | |||
| 20 | const NICE = 'nice'; |
||
| 21 | const OK = 'ok'; |
||
| 22 | const SUSPICIOUS = 'suspicious'; |
||
| 23 | const BAD = 'bad'; |
||
| 24 | |||
| 25 | const ROBOT = 'robot'; |
||
| 26 | const BROWSER = 'browser'; |
||
| 27 | const UNKNOWN = 'unknown'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string|object |
||
| 31 | */ |
||
| 32 | protected $profile; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var boolean |
||
| 36 | */ |
||
| 37 | protected $throwExceptions = false; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var boolean |
||
| 41 | */ |
||
| 42 | protected $logErrors = true; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $cookieName = 'bsid'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $cookiePath = '/'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var \Bouncer\Cache\CacheInterface |
||
| 56 | */ |
||
| 57 | protected $cache; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var \Bouncer\Logger\LoggerInterface |
||
| 61 | */ |
||
| 62 | protected $logger; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var Request |
||
| 66 | */ |
||
| 67 | protected $request; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $response; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | protected $analyzers = array(); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var Identity |
||
| 81 | */ |
||
| 82 | protected $identity; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Store internal metadata |
||
| 86 | * |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | protected $context; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var boolean |
||
| 93 | */ |
||
| 94 | protected $started = false; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var boolean |
||
| 98 | */ |
||
| 99 | protected $ended = false; |
||
| 100 | |||
| 101 | public function __construct(array $options = array()) |
||
| 114 | |||
| 115 | /* |
||
| 116 | * Set the supported options |
||
| 117 | */ |
||
| 118 | public function setOptions(array $options = array()) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @throw Exception |
||
| 142 | */ |
||
| 143 | public function error($message) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @return \Bouncer\Cache\CacheInterface |
||
| 155 | */ |
||
| 156 | public function getCache($reportError = false) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @return \Bouncer\Logger\LoggerInterface |
||
| 170 | */ |
||
| 171 | public function getLogger($reportError = false) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return Request |
||
| 185 | */ |
||
| 186 | public function getRequest() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | public function getResponse() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | public function getUserAgent() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | public function getAddr() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @return Address |
||
| 223 | */ |
||
| 224 | public function getAddress() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | public function getHeaders() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @return Signature |
||
| 247 | */ |
||
| 248 | public function getSignature() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | public function getCookies() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Return the current session id (from Cookie) |
||
| 271 | * |
||
| 272 | * @return string|null |
||
| 273 | */ |
||
| 274 | public function getSessionId() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Return the protocol of the request: HTTP/1.0 or HTTP/1.1 |
||
| 283 | * |
||
| 284 | * @return string|null |
||
| 285 | */ |
||
| 286 | public function getProtocol() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @return Identity |
||
| 295 | */ |
||
| 296 | public function getIdentity() |
||
| 329 | |||
| 330 | public function getContext() |
||
| 338 | |||
| 339 | /* |
||
| 340 | * Init the context with id, time and start. |
||
| 341 | */ |
||
| 342 | public function initContext() |
||
| 349 | |||
| 350 | /* |
||
| 351 | * Complete the context with end, exec_time and memory_usage. |
||
| 352 | */ |
||
| 353 | public function completeContext() |
||
| 372 | |||
| 373 | /* |
||
| 374 | * Complete the response with status code |
||
| 375 | */ |
||
| 376 | public function completeResponse() |
||
| 389 | /* |
||
| 390 | * Register an analyzer for a given type. |
||
| 391 | * |
||
| 392 | * @param string |
||
| 393 | * @param callable |
||
| 394 | * @param int |
||
| 395 | */ |
||
| 396 | public function registerAnalyzer($type, $callable, $priority = 100) |
||
| 400 | |||
| 401 | /* |
||
| 402 | * Process Analyzers for a given type. Return the modified array or object. |
||
| 403 | * |
||
| 404 | * @param string |
||
| 405 | * @param object |
||
| 406 | * |
||
| 407 | * @return object |
||
| 408 | */ |
||
| 409 | protected function processAnalyzers($type, $value) |
||
| 420 | |||
| 421 | /* |
||
| 422 | * Start a Bouncer session |
||
| 423 | */ |
||
| 424 | public function start() |
||
| 439 | |||
| 440 | /* |
||
| 441 | * Set a cookie containing the session id |
||
| 442 | */ |
||
| 443 | public function initSession() |
||
| 456 | |||
| 457 | /* |
||
| 458 | * Sleep if Identity status is of a certain value. |
||
| 459 | * |
||
| 460 | * @param array $statuses |
||
| 461 | * @param int $minimum |
||
| 462 | * @param int $maximum |
||
| 463 | * |
||
| 464 | */ |
||
| 465 | public function sleep($statuses = array(), $minimum = 1000, $maximum = 2500) |
||
| 475 | |||
| 476 | /* |
||
| 477 | * Ban if Identity status is of a certain value. |
||
| 478 | */ |
||
| 479 | public function ban($statuses = array()) |
||
| 488 | |||
| 489 | /* |
||
| 490 | * Complete the connection then attempt to log. |
||
| 491 | */ |
||
| 492 | public function end() |
||
| 511 | |||
| 512 | /* |
||
| 513 | * Log the connection to the logging backend. |
||
| 514 | */ |
||
| 515 | public function log() |
||
| 530 | |||
| 531 | // Static |
||
| 532 | |||
| 533 | public static function forbidden() |
||
| 541 | |||
| 542 | public static function unavailable() |
||
| 550 | |||
| 551 | public static function responseStatus($code, $message) |
||
| 560 | |||
| 561 | public static function hash($value) |
||
| 565 | |||
| 566 | } |
||
| 567 |
This checks looks for assignemnts to variables using the
list(...)function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$aand$care used. There was no need to assign$b.Instead, the list call could have been.