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 |
||
| 14 | class Bouncer |
||
| 15 | { |
||
| 16 | |||
| 17 | const NICE = 'nice'; |
||
| 18 | const OK = 'ok'; |
||
| 19 | const SUSPICIOUS = 'suspicious'; |
||
| 20 | const BAD = 'bad'; |
||
| 21 | |||
| 22 | const ROBOT = 'robot'; |
||
| 23 | const BROWSER = 'browser'; |
||
| 24 | const UNKNOWN = 'unknown'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string|object |
||
| 28 | */ |
||
| 29 | protected $profile; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var boolean |
||
| 33 | */ |
||
| 34 | protected $throwExceptions = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var boolean |
||
| 38 | */ |
||
| 39 | protected $logErrors = true; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var boolean |
||
| 43 | */ |
||
| 44 | protected $cookieName = 'bsid'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var \Bouncer\Cache\CacheInterface |
||
| 48 | */ |
||
| 49 | protected $cache; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var \Bouncer\Logger\LoggerInterface |
||
| 53 | */ |
||
| 54 | protected $logger; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var Request |
||
| 58 | */ |
||
| 59 | protected $request; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $analyzers = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var Identity |
||
| 68 | */ |
||
| 69 | protected $identity; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Store metadata about the handling of the request |
||
| 73 | * |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected $connection = array(); |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var boolean |
||
| 80 | */ |
||
| 81 | protected $started = false; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var boolean |
||
| 85 | */ |
||
| 86 | protected $ended = false; |
||
| 87 | |||
| 88 | public function __construct(array $options = array()) |
||
| 89 | { |
||
| 90 | if (!empty($options)) { |
||
| 91 | $this->setOptions($options); |
||
| 92 | } |
||
| 93 | |||
| 94 | // Load Profile |
||
| 95 | if (!$this->profile) { |
||
| 96 | $this->profile = new \Bouncer\Profile\Standard; |
||
| 97 | } |
||
| 98 | call_user_func_array(array($this->profile, 'load'), array($this)); |
||
| 99 | } |
||
| 100 | |||
| 101 | /* |
||
| 102 | * Set the supported options |
||
| 103 | */ |
||
| 104 | public function setOptions(array $options = array()) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @throw Exception |
||
| 122 | */ |
||
| 123 | public function error($message) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return \Bouncer\Cache\CacheInterface |
||
| 135 | */ |
||
| 136 | public function getCache($reportError = false) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return \Bouncer\Logger\LoggerInterface |
||
| 150 | */ |
||
| 151 | public function getLogger($reportError = false) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return Request |
||
| 165 | */ |
||
| 166 | public function getRequest() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function getUserAgent() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | public function getAddr() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | public function getHeaders() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | public function getCookies() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Return the current session id |
||
| 225 | * |
||
| 226 | * @return string|null |
||
| 227 | */ |
||
| 228 | public function getSession() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Return the protocol of the request: HTTP/1.0 or HTTP/1.1 |
||
| 237 | * |
||
| 238 | * @return string|null |
||
| 239 | */ |
||
| 240 | public function getProtocol() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return Identity |
||
| 249 | */ |
||
| 250 | public function getIdentity() |
||
| 307 | |||
| 308 | public function getConnection() |
||
| 316 | |||
| 317 | /* |
||
| 318 | * Init the connection with id, time and start. |
||
| 319 | */ |
||
| 320 | public function initConnection() |
||
| 327 | |||
| 328 | /* |
||
| 329 | * Complete the connection with end, exec_time, memory_usage and response_status. |
||
| 330 | */ |
||
| 331 | public function completeConnection() |
||
| 356 | |||
| 357 | /* |
||
| 358 | * Register an analyzer for a given type. |
||
| 359 | * |
||
| 360 | * @param string |
||
| 361 | * @param callable |
||
| 362 | * @param int |
||
| 363 | */ |
||
| 364 | public function registerAnalyzer($type, callable $callable, $priority = 100) |
||
| 368 | |||
| 369 | /* |
||
| 370 | * Process Analyzers for a given type. Return the modified array or object. |
||
| 371 | * |
||
| 372 | * @param string |
||
| 373 | * @param array|object |
||
| 374 | * |
||
| 375 | * @return array|object |
||
| 376 | */ |
||
| 377 | protected function processAnalyzers($type, $value) |
||
| 388 | |||
| 389 | /* |
||
| 390 | * Start a Bouncer session |
||
| 391 | */ |
||
| 392 | public function start() |
||
| 407 | |||
| 408 | /* |
||
| 409 | * Set a cookie containing the session id |
||
| 410 | */ |
||
| 411 | public function initSession() |
||
| 423 | |||
| 424 | /* |
||
| 425 | * Throttle if Identity status is suspicious. |
||
| 426 | */ |
||
| 427 | public function throttle() |
||
| 448 | |||
| 449 | /* |
||
| 450 | * Ban if Identity status is bad. |
||
| 451 | */ |
||
| 452 | public function ban() |
||
| 460 | |||
| 461 | /* |
||
| 462 | * Complete the connection then attempt to log. |
||
| 463 | */ |
||
| 464 | public function end() |
||
| 482 | |||
| 483 | /* |
||
| 484 | * Log the connection to the logging backend. |
||
| 485 | */ |
||
| 486 | public function log() |
||
| 497 | |||
| 498 | // Static |
||
| 499 | |||
| 500 | public static function unavailable() |
||
| 509 | |||
| 510 | public static function hash($string) |
||
| 514 | |||
| 515 | } |
||
| 516 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: