Complex classes like Application 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 Application, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Application extends \PHPDaemon\Core\AppInstance |
||
| 13 | { |
||
| 14 | public $wss; |
||
| 15 | protected $redis; |
||
| 16 | protected $sessions; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Set Redis instance |
||
| 20 | * @param \PHPDaemon\Clients\Redis\Pool $redis |
||
| 21 | * @return $this |
||
| 22 | */ |
||
| 23 | public function setRedis(\PHPDaemon\Clients\Redis\Pool $redis) |
||
| 28 | |||
| 29 | /** |
||
| 30 | * getLocalSubscribersCount |
||
| 31 | * @param string $chan |
||
| 32 | * @return integer |
||
| 33 | */ |
||
| 34 | public function getLocalSubscribersCount($chan) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * subscribe |
||
| 41 | * @param string $chan [@todo description] |
||
| 42 | * @param callable $cb [@todo description] |
||
| 43 | * @param callable $opcb [@todo description] |
||
|
|
|||
| 44 | * @callback $cb ( ) |
||
| 45 | * @callback $opcb ( ) |
||
| 46 | * @return void |
||
| 47 | */ |
||
| 48 | public function subscribe($chan, $cb, $opcb = null) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * setnx |
||
| 55 | * @param string $key [@todo description] |
||
| 56 | * @param mixed $value [@todo description] |
||
| 57 | * @param callable $cb [@todo description] |
||
| 58 | * @callback $cb ( ) |
||
| 59 | * @return void |
||
| 60 | */ |
||
| 61 | public function setnx($key, $value, $cb = null) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * setkey |
||
| 68 | * @param string $key [@todo description] |
||
| 69 | * @param mixed $value [@todo description] |
||
| 70 | * @param callable $cb [@todo description] |
||
| 71 | * @callback $cb ( ) |
||
| 72 | * @return void |
||
| 73 | */ |
||
| 74 | public function setkey($key, $value, $cb = null) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * getkey |
||
| 81 | * @param string $key [@todo description] |
||
| 82 | * @param callable $cb [@todo description] |
||
| 83 | * @callback $cb ( ) |
||
| 84 | * @return void |
||
| 85 | */ |
||
| 86 | public function getkey($key, $cb = null) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * expire |
||
| 93 | * @param string $key [@todo description] |
||
| 94 | * @param integer $seconds [@todo description] |
||
| 95 | * @param callable $cb [@todo description] |
||
| 96 | * @callback $cb ( ) |
||
| 97 | * @return void |
||
| 98 | */ |
||
| 99 | public function expire($key, $seconds, $cb = null) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * unsubscribe |
||
| 106 | * @param string $chan [@todo description] |
||
| 107 | * @param callable $cb [@todo description] |
||
| 108 | * @param callable $opcb [@todo description] |
||
| 109 | * @callback $cb ( ) |
||
| 110 | * @callback $opcb ( ) |
||
| 111 | * @return void |
||
| 112 | */ |
||
| 113 | public function unsubscribe($chan, $cb, $opcb = null) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * unsubscribeReal |
||
| 120 | * @param string $chan [@todo description] |
||
| 121 | * @param callable $opcb [@todo description] |
||
| 122 | * @callback $opcb ( ) |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | public function unsubscribeReal($chan, $opcb = null) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * publish |
||
| 132 | * @param string $chan [@todo description] |
||
| 133 | * @param callable $cb [@todo description] |
||
| 134 | * @param callable $opcb [@todo description] |
||
| 135 | * @callback $cb ( ) |
||
| 136 | * @callback $opcb ( ) |
||
| 137 | * @return void |
||
| 138 | */ |
||
| 139 | public function publish($chan, $cb, $opcb = null) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Called when the worker is ready to go |
||
| 146 | * @return void |
||
| 147 | */ |
||
| 148 | public function onReady() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * attachWss |
||
| 160 | * @param \PHPDaemon\Network\Pool $wss |
||
| 161 | * @return boolean |
||
| 162 | */ |
||
| 163 | public function attachWss($wss) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * onFinish |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | public function onFinish() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * detachWss |
||
| 187 | * @param object $wss [@todo description] |
||
| 188 | * @return boolean |
||
| 189 | */ |
||
| 190 | public function detachWss($wss) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * wsHandler |
||
| 202 | * @param object $ws [@todo description] |
||
| 203 | * @param string $path [@todo description] |
||
| 204 | * @param object $client [@todo description] |
||
| 205 | * @param callable $state [@todo description] |
||
| 206 | * @callback $state ( object $route ) |
||
| 207 | * @return boolean |
||
| 208 | */ |
||
| 209 | public function wsHandler($ws, $path, $client, $state) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * beginSession |
||
| 237 | * @param string $path [@todo description] |
||
| 238 | * @param string $sessId [@todo description] |
||
| 239 | * @param string $server [@todo description] |
||
| 240 | * @return object |
||
| 241 | */ |
||
| 242 | public function beginSession($path, $sessId, $server) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * getRouteOptions |
||
| 260 | * @param string $path [@todo description] |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | public function getRouteOptions($path) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * endSession |
||
| 283 | * @param Session $session |
||
| 284 | * @return void |
||
| 285 | */ |
||
| 286 | public function endSession($session) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Creates Request. |
||
| 293 | * @param object $req Request. |
||
| 294 | * @param object $upstream Upstream application instance. |
||
| 295 | * @return object Request. |
||
| 296 | */ |
||
| 297 | public function beginRequest($req, $upstream) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * callMethod |
||
| 361 | * @param string $method [@todo description] |
||
| 362 | * @param object $req [@todo description] |
||
| 363 | * @param object $upstream [@todo description] |
||
| 364 | * @return object |
||
| 365 | */ |
||
| 366 | public function callMethod($method, $req, $upstream) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Setting default config options |
||
| 381 | * @return array|bool |
||
| 382 | */ |
||
| 383 | protected function getConfigDefaults() |
||
| 414 | } |
||
| 415 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.