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 array |
||
| 31 | */ |
||
| 32 | static $supportedOptions = array( |
||
| 33 | 'cache', |
||
| 34 | 'request', |
||
| 35 | 'logger', |
||
| 36 | 'profile', |
||
| 37 | 'cookieName', |
||
| 38 | 'cookiePath', |
||
| 39 | 'exit', |
||
| 40 | 'responseCodeSetter' |
||
| 41 | ); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string|object |
||
| 45 | */ |
||
| 46 | protected $profile; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var boolean |
||
| 50 | */ |
||
| 51 | protected $throwExceptions = false; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var boolean |
||
| 55 | */ |
||
| 56 | protected $logErrors = true; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $cookieName = 'bsid'; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $cookiePath = '/'; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The exit callable to use when blocking a request |
||
| 70 | * |
||
| 71 | * @var callable |
||
| 72 | */ |
||
| 73 | protected $exit; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The callable to use to set the HTTP Response Code |
||
| 77 | * |
||
| 78 | * @var callable |
||
| 79 | */ |
||
| 80 | protected $responseCodeSetter = 'http_response_code'; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var \Bouncer\Cache\CacheInterface |
||
| 84 | */ |
||
| 85 | protected $cache; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var \Bouncer\Logger\LoggerInterface |
||
| 89 | */ |
||
| 90 | protected $logger; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var Request |
||
| 94 | */ |
||
| 95 | protected $request; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | protected $response; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var array |
||
| 104 | */ |
||
| 105 | protected $analyzers = array(); |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var Identity |
||
| 109 | */ |
||
| 110 | protected $identity; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Store internal metadata |
||
| 114 | * |
||
| 115 | * @var array |
||
| 116 | */ |
||
| 117 | protected $context; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var boolean |
||
| 121 | */ |
||
| 122 | protected $started = false; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var boolean |
||
| 126 | */ |
||
| 127 | protected $ended = false; |
||
| 128 | |||
| 129 | public function __construct(array $options = array()) |
||
| 142 | |||
| 143 | /* |
||
| 144 | * Set the supported options |
||
| 145 | */ |
||
| 146 | public function setOptions(array $options = array()) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @throw Exception |
||
| 157 | */ |
||
| 158 | public function error($message) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @return \Bouncer\Cache\CacheInterface |
||
| 170 | */ |
||
| 171 | public function getCache($reportError = false) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return \Bouncer\Logger\LoggerInterface |
||
| 185 | */ |
||
| 186 | public function getLogger($reportError = false) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return Request |
||
| 200 | */ |
||
| 201 | public function getRequest() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return array |
||
| 214 | */ |
||
| 215 | public function getResponse() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | public function getUserAgent() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | public function getAddr() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return Address |
||
| 238 | */ |
||
| 239 | public function getAddress() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | public function getHeaders() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return Signature |
||
| 262 | */ |
||
| 263 | public function getSignature() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | public function getCookies() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Return the current session id (from Cookie) |
||
| 286 | * |
||
| 287 | * @return string|null |
||
| 288 | */ |
||
| 289 | public function getSessionId() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Return the protocol of the request: HTTP/1.0 or HTTP/1.1 |
||
| 298 | * |
||
| 299 | * @return string|null |
||
| 300 | */ |
||
| 301 | public function getProtocol() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @return Identity |
||
| 310 | */ |
||
| 311 | public function getIdentity() |
||
| 344 | |||
| 345 | public function getContext() |
||
| 353 | |||
| 354 | /* |
||
| 355 | * Init the context with id, time and start. |
||
| 356 | */ |
||
| 357 | public function initContext() |
||
| 364 | |||
| 365 | /* |
||
| 366 | * Complete the context with end, exec_time and memory_usage. |
||
| 367 | */ |
||
| 368 | public function completeContext() |
||
| 387 | |||
| 388 | /* |
||
| 389 | * Complete the response with status code |
||
| 390 | */ |
||
| 391 | public function completeResponse() |
||
| 404 | /* |
||
| 405 | * Register an analyzer for a given type. |
||
| 406 | * |
||
| 407 | * @param string |
||
| 408 | * @param callable |
||
| 409 | * @param int |
||
| 410 | */ |
||
| 411 | public function registerAnalyzer($type, $callable, $priority = 100) |
||
| 415 | |||
| 416 | /* |
||
| 417 | * Process Analyzers for a given type. Return the modified array or object. |
||
| 418 | * |
||
| 419 | * @param string |
||
| 420 | * @param object |
||
| 421 | * |
||
| 422 | * @return object |
||
| 423 | */ |
||
| 424 | protected function processAnalyzers($type, $value) |
||
| 435 | |||
| 436 | /* |
||
| 437 | * Start a Bouncer session |
||
| 438 | */ |
||
| 439 | public function start() |
||
| 454 | |||
| 455 | /* |
||
| 456 | * Set a cookie containing the session id |
||
| 457 | */ |
||
| 458 | public function initSession() |
||
| 471 | |||
| 472 | /* |
||
| 473 | * Throttle |
||
| 474 | * |
||
| 475 | * @param array $statuses |
||
| 476 | * @param int $minimum |
||
| 477 | * @param int $maximum |
||
| 478 | * |
||
| 479 | */ |
||
| 480 | public function throttle($minimum = 1000, $maximum = 2500) |
||
| 486 | |||
| 487 | /* |
||
| 488 | * @deprecated deprecated since version 2.1.0 |
||
| 489 | */ |
||
| 490 | public function sleep($statuses = array(), $minimum = 1000, $maximum = 2500) |
||
| 498 | |||
| 499 | /* |
||
| 500 | * Block |
||
| 501 | * |
||
| 502 | * @param string $type |
||
| 503 | * @param array $extra |
||
| 504 | * |
||
| 505 | */ |
||
| 506 | public function block($type = null, $extra = null) |
||
| 531 | |||
| 532 | /* |
||
| 533 | * @deprecated deprecated since version 2.1.0 |
||
| 534 | */ |
||
| 535 | public function ban($statuses = array()) |
||
| 545 | |||
| 546 | /* |
||
| 547 | * @param string $type |
||
| 548 | * @param array $extra |
||
| 549 | */ |
||
| 550 | public function registerEvent($type, $extra = null) |
||
| 557 | |||
| 558 | /* |
||
| 559 | * Complete the connection then attempt to log. |
||
| 560 | */ |
||
| 561 | public function end() |
||
| 580 | |||
| 581 | /* |
||
| 582 | * Log the connection to the logging backend. |
||
| 583 | */ |
||
| 584 | public function log() |
||
| 599 | |||
| 600 | // Static |
||
| 601 | |||
| 602 | public static function hash($value) |
||
| 606 | |||
| 607 | } |
||
| 608 |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: