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 = '\Bouncer\Profile\Standard'; |
||
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()) |
||
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 string|object |
||
165 | */ |
||
166 | public function getProfile() |
||
170 | |||
171 | /** |
||
172 | * @return Request |
||
173 | */ |
||
174 | public function getRequest() |
||
184 | |||
185 | /** |
||
186 | * @return string |
||
187 | */ |
||
188 | public function getUserAgent() |
||
192 | |||
193 | /** |
||
194 | * @return string |
||
195 | */ |
||
196 | public function getAddr() |
||
200 | |||
201 | /** |
||
202 | * @return array |
||
203 | */ |
||
204 | public function getHeaders() |
||
218 | |||
219 | /** |
||
220 | * @return array |
||
221 | */ |
||
222 | public function getCookies() |
||
230 | |||
231 | /** |
||
232 | * Return the current session id |
||
233 | * |
||
234 | * @return string|null |
||
235 | */ |
||
236 | public function getSession() |
||
242 | |||
243 | /** |
||
244 | * Return the protocol of the request: HTTP/1.0 or HTTP/1.1 |
||
245 | * |
||
246 | * @return string|null |
||
247 | */ |
||
248 | public function getProtocol() |
||
254 | |||
255 | /** |
||
256 | * @return Identity |
||
257 | */ |
||
258 | public function getIdentity() |
||
315 | |||
316 | public function getConnection() |
||
324 | |||
325 | /* |
||
326 | * Init the connection with id, time and start. |
||
327 | */ |
||
328 | public function initConnection() |
||
335 | |||
336 | /* |
||
337 | * Complete the connection with end, exec_time, memory_usage and response_status. |
||
338 | */ |
||
339 | public function completeConnection() |
||
364 | |||
365 | /* |
||
366 | * Register an analyzer for a given type. |
||
367 | * |
||
368 | * @param string |
||
369 | * @param callable |
||
370 | * @param int |
||
371 | */ |
||
372 | public function registerAnalyzer($type, callable $callable, $priority = 100) |
||
376 | |||
377 | /* |
||
378 | * Process Analyzers for a given type. Return the modified array or object. |
||
379 | * |
||
380 | * @param string |
||
381 | * @param array|object |
||
382 | * |
||
383 | * @return array|object |
||
384 | */ |
||
385 | protected function processAnalyzers($type, $value) |
||
396 | |||
397 | /* |
||
398 | * Start a Bouncer session |
||
399 | */ |
||
400 | public function start() |
||
415 | |||
416 | /* |
||
417 | * Set a cookie containing the session id |
||
418 | */ |
||
419 | public function initSession() |
||
431 | |||
432 | /* |
||
433 | * Throttle if Identity status is suspicious. |
||
434 | */ |
||
435 | public function throttle() |
||
456 | |||
457 | /* |
||
458 | * Ban if Identity status is bad. |
||
459 | */ |
||
460 | public function ban() |
||
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 unavailable() |
||
517 | |||
518 | public static function hash($string) |
||
522 | |||
523 | } |
||
524 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: