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 string |
||
| 43 | */ |
||
| 44 | protected $cookieName = 'bsid'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $cookiePath = '/'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var \Bouncer\Cache\CacheInterface |
||
| 53 | */ |
||
| 54 | protected $cache; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var \Bouncer\Logger\LoggerInterface |
||
| 58 | */ |
||
| 59 | protected $logger; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var Request |
||
| 63 | */ |
||
| 64 | protected $request; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $analyzers = array(); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var Identity |
||
| 73 | */ |
||
| 74 | protected $identity; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Store metadata about the handling of the request |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $connection = array(); |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var boolean |
||
| 85 | */ |
||
| 86 | protected $started = false; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var boolean |
||
| 90 | */ |
||
| 91 | protected $ended = false; |
||
| 92 | |||
| 93 | public function __construct(array $options = array()) |
||
| 105 | |||
| 106 | /* |
||
| 107 | * Set the supported options |
||
| 108 | */ |
||
| 109 | public function setOptions(array $options = array()) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @throw Exception |
||
| 133 | */ |
||
| 134 | public function error($message) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return \Bouncer\Cache\CacheInterface |
||
| 146 | */ |
||
| 147 | public function getCache($reportError = false) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @return \Bouncer\Logger\LoggerInterface |
||
| 161 | */ |
||
| 162 | public function getLogger($reportError = false) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @return Request |
||
| 176 | */ |
||
| 177 | public function getRequest() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | public function getUserAgent() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | public function getAddr() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return array |
||
| 206 | */ |
||
| 207 | public function getHeaders() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @return array |
||
| 224 | */ |
||
| 225 | public function getCookies() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Return the current session id (from Cookie) |
||
| 236 | * |
||
| 237 | * @return string|null |
||
| 238 | */ |
||
| 239 | public function getSession() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Return the protocol of the request: HTTP/1.0 or HTTP/1.1 |
||
| 248 | * |
||
| 249 | * @return string|null |
||
| 250 | */ |
||
| 251 | public function getProtocol() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @return Identity |
||
| 260 | */ |
||
| 261 | public function getIdentity() |
||
| 318 | |||
| 319 | public function getConnection() |
||
| 327 | |||
| 328 | /* |
||
| 329 | * Init the connection with id, time and start. |
||
| 330 | */ |
||
| 331 | public function initConnection() |
||
| 338 | |||
| 339 | /* |
||
| 340 | * Complete the connection with end, exec_time, memory_usage and response_status. |
||
| 341 | */ |
||
| 342 | public function completeConnection() |
||
| 369 | |||
| 370 | /* |
||
| 371 | * Register an analyzer for a given type. |
||
| 372 | * |
||
| 373 | * @param string |
||
| 374 | * @param callable |
||
| 375 | * @param int |
||
| 376 | */ |
||
| 377 | public function registerAnalyzer($type, $callable, $priority = 100) |
||
| 381 | |||
| 382 | /* |
||
| 383 | * Process Analyzers for a given type. Return the modified array or object. |
||
| 384 | * |
||
| 385 | * @param string |
||
| 386 | * @param array|object |
||
| 387 | * |
||
| 388 | * @return array|object |
||
| 389 | */ |
||
| 390 | protected function processAnalyzers($type, $value) |
||
| 401 | |||
| 402 | /* |
||
| 403 | * Start a Bouncer session |
||
| 404 | */ |
||
| 405 | public function start() |
||
| 420 | |||
| 421 | /* |
||
| 422 | * Set a cookie containing the session id |
||
| 423 | */ |
||
| 424 | public function initSession() |
||
| 436 | |||
| 437 | /* |
||
| 438 | * Sleep if Identity status is of a certain value. |
||
| 439 | * |
||
| 440 | * @param array |
||
| 441 | * @param int |
||
| 442 | * @param int |
||
| 443 | * |
||
| 444 | */ |
||
| 445 | public function sleep($statuses = array(), $minimum = 1000, $maximum = 2500) |
||
| 455 | |||
| 456 | /* |
||
| 457 | * Ban if Identity status is of a certain value. |
||
| 458 | */ |
||
| 459 | public function ban($statuses = array()) |
||
| 468 | |||
| 469 | /* |
||
| 470 | * Complete the connection then attempt to log. |
||
| 471 | */ |
||
| 472 | public function end() |
||
| 490 | |||
| 491 | /* |
||
| 492 | * Log the connection to the logging backend. |
||
| 493 | */ |
||
| 494 | public function log() |
||
| 505 | |||
| 506 | // Static |
||
| 507 | |||
| 508 | public static function forbidden() |
||
| 516 | |||
| 517 | public static function unavailable() |
||
| 525 | |||
| 526 | public static function responseStatus($code, $message) |
||
| 535 | |||
| 536 | public static function hash($string) |
||
| 540 | |||
| 541 | } |
||
| 542 |
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: