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()) |
||
| 100 | |||
| 101 | /* |
||
| 102 | * Set the supported options |
||
| 103 | */ |
||
| 104 | public function setOptions(array $options = array()) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @throw Exception |
||
| 125 | */ |
||
| 126 | public function error($message) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @return \Bouncer\Cache\CacheInterface |
||
| 138 | */ |
||
| 139 | public function getCache($reportError = false) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @return \Bouncer\Logger\LoggerInterface |
||
| 153 | */ |
||
| 154 | public function getLogger($reportError = false) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return Request |
||
| 168 | */ |
||
| 169 | public function getRequest() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | public function getUserAgent() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | public function getAddr() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | public function getHeaders() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | public function getCookies() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Return the current session id (from Cookie) |
||
| 228 | * |
||
| 229 | * @return string|null |
||
| 230 | */ |
||
| 231 | public function getSession() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Return the protocol of the request: HTTP/1.0 or HTTP/1.1 |
||
| 240 | * |
||
| 241 | * @return string|null |
||
| 242 | */ |
||
| 243 | public function getProtocol() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return Identity |
||
| 252 | */ |
||
| 253 | public function getIdentity() |
||
| 310 | |||
| 311 | public function getConnection() |
||
| 319 | |||
| 320 | /* |
||
| 321 | * Init the connection with id, time and start. |
||
| 322 | */ |
||
| 323 | public function initConnection() |
||
| 330 | |||
| 331 | /* |
||
| 332 | * Complete the connection with end, exec_time, memory_usage and response_status. |
||
| 333 | */ |
||
| 334 | public function completeConnection() |
||
| 361 | |||
| 362 | /* |
||
| 363 | * Register an analyzer for a given type. |
||
| 364 | * |
||
| 365 | * @param string |
||
| 366 | * @param callable |
||
| 367 | * @param int |
||
| 368 | */ |
||
| 369 | public function registerAnalyzer($type, $callable, $priority = 100) |
||
| 373 | |||
| 374 | /* |
||
| 375 | * Process Analyzers for a given type. Return the modified array or object. |
||
| 376 | * |
||
| 377 | * @param string |
||
| 378 | * @param array|object |
||
| 379 | * |
||
| 380 | * @return array|object |
||
| 381 | */ |
||
| 382 | protected function processAnalyzers($type, $value) |
||
| 393 | |||
| 394 | /* |
||
| 395 | * Start a Bouncer session |
||
| 396 | */ |
||
| 397 | public function start() |
||
| 412 | |||
| 413 | /* |
||
| 414 | * Set a cookie containing the session id |
||
| 415 | */ |
||
| 416 | public function initSession() |
||
| 428 | |||
| 429 | /* |
||
| 430 | * Sleep if Identity status is of a certain value. |
||
| 431 | * |
||
| 432 | * @param array |
||
| 433 | * @param int |
||
| 434 | * @param int |
||
| 435 | * |
||
| 436 | */ |
||
| 437 | public function sleep($statuses = array(), $minimum = 1000, $maximum = 2500) |
||
| 447 | |||
| 448 | /* |
||
| 449 | * Ban if Identity status is of a certain value. |
||
| 450 | */ |
||
| 451 | public function ban($statuses = array()) |
||
| 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 forbidden() |
||
| 508 | |||
| 509 | public static function unavailable() |
||
| 517 | |||
| 518 | public static function response_status($code, $message) |
||
| 527 | |||
| 528 | public static function hash($string) |
||
| 532 | |||
| 533 | } |
||
| 534 |
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: