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 | * The exit callable to use when blocking a request |
||
| 56 | * |
||
| 57 | * @var callable |
||
| 58 | */ |
||
| 59 | protected $exit; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var \Bouncer\Cache\CacheInterface |
||
| 63 | */ |
||
| 64 | protected $cache; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var \Bouncer\Logger\LoggerInterface |
||
| 68 | */ |
||
| 69 | protected $logger; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var Request |
||
| 73 | */ |
||
| 74 | protected $request; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $response; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected $analyzers = array(); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var Identity |
||
| 88 | */ |
||
| 89 | protected $identity; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Store internal metadata |
||
| 93 | * |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | protected $context; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var boolean |
||
| 100 | */ |
||
| 101 | protected $started = false; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var boolean |
||
| 105 | */ |
||
| 106 | protected $ended = false; |
||
| 107 | |||
| 108 | public function __construct(array $options = array()) |
||
| 121 | |||
| 122 | /* |
||
| 123 | * Set the supported options |
||
| 124 | */ |
||
| 125 | public function setOptions(array $options = array()) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @throw Exception |
||
| 152 | */ |
||
| 153 | public function error($message) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return \Bouncer\Cache\CacheInterface |
||
| 165 | */ |
||
| 166 | public function getCache($reportError = false) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return \Bouncer\Logger\LoggerInterface |
||
| 180 | */ |
||
| 181 | public function getLogger($reportError = false) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return Request |
||
| 195 | */ |
||
| 196 | public function getRequest() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | public function getResponse() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | public function getUserAgent() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | public function getAddr() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return Address |
||
| 233 | */ |
||
| 234 | public function getAddress() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | public function getHeaders() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @return Signature |
||
| 257 | */ |
||
| 258 | public function getSignature() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return array |
||
| 269 | */ |
||
| 270 | public function getCookies() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Return the current session id (from Cookie) |
||
| 281 | * |
||
| 282 | * @return string|null |
||
| 283 | */ |
||
| 284 | public function getSessionId() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Return the protocol of the request: HTTP/1.0 or HTTP/1.1 |
||
| 293 | * |
||
| 294 | * @return string|null |
||
| 295 | */ |
||
| 296 | public function getProtocol() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @return Identity |
||
| 305 | */ |
||
| 306 | public function getIdentity() |
||
| 339 | |||
| 340 | public function getContext() |
||
| 348 | |||
| 349 | /* |
||
| 350 | * Init the context with id, time and start. |
||
| 351 | */ |
||
| 352 | public function initContext() |
||
| 359 | |||
| 360 | /* |
||
| 361 | * Complete the context with end, exec_time and memory_usage. |
||
| 362 | */ |
||
| 363 | public function completeContext() |
||
| 382 | |||
| 383 | /* |
||
| 384 | * Complete the response with status code |
||
| 385 | */ |
||
| 386 | public function completeResponse() |
||
| 399 | /* |
||
| 400 | * Register an analyzer for a given type. |
||
| 401 | * |
||
| 402 | * @param string |
||
| 403 | * @param callable |
||
| 404 | * @param int |
||
| 405 | */ |
||
| 406 | public function registerAnalyzer($type, $callable, $priority = 100) |
||
| 410 | |||
| 411 | /* |
||
| 412 | * Process Analyzers for a given type. Return the modified array or object. |
||
| 413 | * |
||
| 414 | * @param string |
||
| 415 | * @param object |
||
| 416 | * |
||
| 417 | * @return object |
||
| 418 | */ |
||
| 419 | protected function processAnalyzers($type, $value) |
||
| 430 | |||
| 431 | /* |
||
| 432 | * Start a Bouncer session |
||
| 433 | */ |
||
| 434 | public function start() |
||
| 449 | |||
| 450 | /* |
||
| 451 | * Set a cookie containing the session id |
||
| 452 | */ |
||
| 453 | public function initSession() |
||
| 466 | |||
| 467 | /* |
||
| 468 | * Throttle |
||
| 469 | * |
||
| 470 | * @param array $statuses |
||
| 471 | * @param int $minimum |
||
| 472 | * @param int $maximum |
||
| 473 | * |
||
| 474 | */ |
||
| 475 | public function throttle($minimum = 1000, $maximum = 2500) |
||
| 481 | |||
| 482 | /* |
||
| 483 | * @deprecated deprecated since version 2.1.0 |
||
| 484 | */ |
||
| 485 | public function sleep($statuses = array(), $minimum = 1000, $maximum = 2500) |
||
| 493 | |||
| 494 | /* |
||
| 495 | * Block |
||
| 496 | * |
||
| 497 | * @param string $type |
||
| 498 | * @param array $extra |
||
| 499 | * |
||
| 500 | */ |
||
| 501 | public function block($type = null, $extra = null) |
||
| 520 | |||
| 521 | /* |
||
| 522 | * @deprecated deprecated since version 2.1.0 |
||
| 523 | */ |
||
| 524 | public function ban($statuses = array()) |
||
| 534 | |||
| 535 | /* |
||
| 536 | * @param string $type |
||
| 537 | * @param array $extra |
||
| 538 | */ |
||
| 539 | public function registerEvent($type, $extra = null) |
||
| 546 | |||
| 547 | /* |
||
| 548 | * Complete the connection then attempt to log. |
||
| 549 | */ |
||
| 550 | public function end() |
||
| 569 | |||
| 570 | /* |
||
| 571 | * Log the connection to the logging backend. |
||
| 572 | */ |
||
| 573 | public function log() |
||
| 588 | |||
| 589 | // Static |
||
| 590 | |||
| 591 | public static function forbidden() |
||
| 598 | |||
| 599 | public static function unavailable() |
||
| 606 | |||
| 607 | public static function responseStatus($code, $message) |
||
| 616 | |||
| 617 | public static function hash($value) |
||
| 621 | |||
| 622 | } |
||
| 623 |
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.