Complex classes like Connection 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 Connection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Connection |
||
| 34 | { |
||
| 35 | |||
| 36 | // private $address = "http://karma.mania-exchange.com/api2/"; |
||
|
|
|||
| 37 | private $address = "http://localhost/index.html?"; |
||
| 38 | |||
| 39 | private $connected = false; |
||
| 40 | |||
| 41 | private $sessionKey = null; |
||
| 42 | |||
| 43 | private $sessionSeed = null; |
||
| 44 | |||
| 45 | private $apiKey = ""; |
||
| 46 | |||
| 47 | /** @var MXRating */ |
||
| 48 | private $ratings = null; |
||
| 49 | /** |
||
| 50 | * @var Dispatcher |
||
| 51 | */ |
||
| 52 | private $dispatcher; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Connection constructor. |
||
| 56 | * |
||
| 57 | * @param Dispatcher $dispatcher |
||
| 58 | */ |
||
| 59 | public function __construct(Dispatcher $dispatcher, Http $http) |
||
| 60 | { |
||
| 61 | $this->dispatcher = $dispatcher; |
||
| 62 | $this->http = $http; |
||
| 63 | } |
||
| 64 | |||
| 65 | |||
| 66 | /** |
||
| 67 | * connect to MX karma |
||
| 68 | * |
||
| 69 | * @param string $serverLogin |
||
| 70 | * @param string $apiKey |
||
| 71 | */ |
||
| 72 | public function connect($serverLogin, $apiKey) |
||
| 73 | { |
||
| 74 | $this->apiKey = $apiKey; |
||
| 75 | |||
| 76 | $params = array( |
||
| 77 | "serverLogin" => $serverLogin, |
||
| 78 | "applicationIdentifier" => "eXpansion 2.0.0.0", |
||
| 79 | "testMode" => "true", |
||
| 80 | ); |
||
| 81 | |||
| 82 | $this->http->get( |
||
| 83 | $this->buildUrl("startSession", $params), |
||
| 84 | array($this, "xConnect") |
||
| 85 | ); |
||
| 86 | } |
||
| 87 | |||
| 88 | public function xConnect( $answer) |
||
| 89 | { |
||
| 90 | |||
| 91 | $data = $this->getObject($answer); |
||
| 92 | |||
| 93 | if ($data === null) { |
||
| 94 | return; |
||
| 95 | } |
||
| 96 | |||
| 97 | $this->sessionKey = $data->sessionKey; |
||
| 98 | $this->sessionSeed = $data->sessionSeed; |
||
| 99 | |||
| 100 | $outHash = hash("sha512", ($this->apiKey.$this->sessionSeed)); |
||
| 101 | |||
| 102 | $params = array("sessionKey" => $this->sessionKey, "activationHash" => $outHash); |
||
| 103 | $this->http->call( |
||
| 104 | $this->buildUrl("activateSession", $params), |
||
| 105 | array($this, "xActivate"), |
||
| 106 | array(), |
||
| 107 | "ManiaLive - eXpansionPluginPack", |
||
| 108 | "application/json" |
||
| 109 | ); |
||
| 110 | } |
||
| 111 | |||
| 112 | public function xActivate($answer, $httpCode) |
||
| 130 | |||
| 131 | public function getRatings($players = array(), $getVotesOnly = false) |
||
| 154 | |||
| 155 | public function saveVotes(\Maniaplanet\DedicatedServer\Structures\Map $map, $time, $votes) |
||
| 181 | |||
| 182 | public function xSaveVotes($answer, $httpCode) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param $answer |
||
| 200 | * @param $httpCode |
||
| 201 | * |
||
| 202 | * @return MxRating|null |
||
| 203 | */ |
||
| 204 | public function xGetRatings($answer, $httpCode) |
||
| 222 | |||
| 223 | public function getGameMode() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param string $data json data |
||
| 236 | * |
||
| 237 | * @return null |
||
| 238 | */ |
||
| 239 | public function getObject($data) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param object $object |
||
| 253 | */ |
||
| 254 | public function handleErrors($obj) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param string $method |
||
| 313 | * @param array $params |
||
| 314 | * |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | private function buildUrl($method, $params) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | public function isConnected() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param $url |
||
| 334 | * @param callable $callback |
||
| 335 | */ |
||
| 336 | public function httpGet($url, callable $callback) |
||
| 346 | |||
| 347 | } |
||
| 348 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.