Complex classes like GameQ 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 GameQ, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class GameQ |
||
| 43 | { |
||
| 44 | /* |
||
| 45 | * Constants |
||
| 46 | */ |
||
| 47 | |||
| 48 | /* Static Section */ |
||
| 49 | |||
| 50 | const PROTOCOLS = [ |
||
| 51 | Protocols\Aa3::class, |
||
| 52 | Protocols\Aapg::class, |
||
| 53 | Protocols\Arkse::class, |
||
| 54 | Protocols\Arma3::class, |
||
| 55 | Protocols\Armedassault2oa::class, |
||
| 56 | Protocols\Ase::class, |
||
| 57 | Protocols\Batt1944::class, |
||
| 58 | Protocols\Bf2::class, |
||
| 59 | Protocols\Bf3::class, |
||
| 60 | Protocols\Bf4::class, |
||
| 61 | Protocols\Bf1942::class, |
||
| 62 | Protocols\Bfbc2::class, |
||
| 63 | Protocols\Bfh::class, |
||
| 64 | Protocols\Cod2::class, |
||
| 65 | Protocols\Cod4::class, |
||
| 66 | Protocols\Codmw3::class, |
||
| 67 | Protocols\Conanexiles::class, |
||
| 68 | Protocols\Crysiswars::class, |
||
| 69 | Protocols\Cs15::class, |
||
| 70 | Protocols\Cs16::class, |
||
| 71 | Protocols\Cscz::class, |
||
| 72 | Protocols\Csgo::class, |
||
| 73 | Protocols\Css::class, |
||
| 74 | Protocols\Dayz::class, |
||
| 75 | Protocols\Dayzmod::class, |
||
| 76 | Protocols\Dod::class, |
||
| 77 | Protocols\Dods::class, |
||
| 78 | Protocols\Dow::class, |
||
| 79 | Protocols\Eco::class, |
||
| 80 | Protocols\Egs::class, |
||
| 81 | Protocols\Etqw::class, |
||
| 82 | Protocols\Ffe::class, |
||
| 83 | Protocols\Ffow::class, |
||
| 84 | Protocols\Gamespy::class, |
||
| 85 | Protocols\Gamespy3::class, |
||
| 86 | Protocols\Gamespy4::class, |
||
| 87 | Protocols\Gmod::class, |
||
| 88 | Protocols\Grav::class, |
||
| 89 | Protocols\Gta5m::class, |
||
| 90 | Protocols\Gtan::class, |
||
| 91 | Protocols\Hl2dm::class, |
||
| 92 | Protocols\Http::class, |
||
| 93 | Protocols\Insurgency::class, |
||
| 94 | Protocols\Jediacademy::class, |
||
| 95 | Protocols\Jedioutcast::class, |
||
| 96 | Protocols\Justcause2::class, |
||
| 97 | Protocols\Justcause3::class, |
||
| 98 | Protocols\Killingfloor::class, |
||
| 99 | Protocols\Killingfloor2::class, |
||
| 100 | Protocols\L4d::class, |
||
| 101 | Protocols\L4d2::class, |
||
| 102 | Protocols\Lhmp::class, |
||
| 103 | Protocols\Minecraft::class, |
||
| 104 | Protocols\Minecraftpe::class, |
||
| 105 | Protocols\Mohaa::class, |
||
| 106 | Protocols\Mta::class, |
||
| 107 | Protocols\Mumble::class, |
||
| 108 | Protocols\Ns2::class, |
||
| 109 | Protocols\Projectrealitybf2::class, |
||
| 110 | Protocols\Quake2::class, |
||
| 111 | Protocols\Quake3::class, |
||
| 112 | Protocols\Quakelive::class, |
||
| 113 | Protocols\Redorchestra2::class, |
||
| 114 | Protocols\Risingstorm2::class, |
||
| 115 | Protocols\Rust::class, |
||
| 116 | Protocols\Samp::class, |
||
| 117 | Protocols\Sevendaystodie::class, |
||
| 118 | Protocols\Ship::class, |
||
| 119 | Protocols\Soldat::class, |
||
| 120 | Protocols\Source::class, |
||
| 121 | Protocols\Spaceengineers::class, |
||
| 122 | Protocols\Squad::class, |
||
| 123 | Protocols\Starmade::class, |
||
| 124 | Protocols\Teamspeak2::class, |
||
| 125 | Protocols\Teamspeak3::class, |
||
| 126 | Protocols\Teeworlds::class, |
||
| 127 | Protocols\Terraria::class, |
||
| 128 | Protocols\Tf2::class, |
||
| 129 | Protocols\Theforrest::class, |
||
| 130 | Protocols\Tibia::class, |
||
| 131 | Protocols\Tshock::class, |
||
| 132 | Protocols\Unreal2::class, |
||
| 133 | Protocols\Unturned::class, |
||
| 134 | Protocols\Ut::class, |
||
| 135 | Protocols\Ut3::class, |
||
| 136 | Protocols\Ut2004::class, |
||
| 137 | Protocols\Ventrilo::class, |
||
| 138 | Protocols\Warsow::class, |
||
| 139 | Protocols\Won::class, |
||
| 140 | Protocols\Wurm::class |
||
| 141 | ]; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Holds the instance of itself |
||
| 145 | * |
||
| 146 | * @type self |
||
| 147 | */ |
||
| 148 | protected static $instance = null; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Create a new instance of this class |
||
| 152 | * |
||
| 153 | * @return \GameQ\GameQ |
||
| 154 | */ |
||
| 155 | 1 | public static function factory() |
|
| 164 | |||
| 165 | /* Dynamic Section */ |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Default options |
||
| 169 | * |
||
| 170 | * @type array |
||
| 171 | */ |
||
| 172 | protected $options = [ |
||
| 173 | 'debug' => false, |
||
| 174 | 'timeout' => 3, // Seconds |
||
| 175 | 'filters' => [ |
||
| 176 | // Default normalize |
||
| 177 | 'normalize_d751713988987e9331980363e24189ce' => [ |
||
| 178 | 'filter' => 'normalize', |
||
| 179 | 'options' => [], |
||
| 180 | ], |
||
| 181 | ], |
||
| 182 | // Advanced settings |
||
| 183 | 'stream_timeout' => 200000, // See http://www.php.net/manual/en/function.stream-select.php for more info |
||
| 184 | 'write_wait' => 500, |
||
| 185 | // How long (in micro-seconds) to pause between writing to server sockets, helps cpu usage |
||
| 186 | |||
| 187 | // Used for generating protocol test data |
||
| 188 | 'capture_packets_file' => null, |
||
| 189 | ]; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Array of servers being queried |
||
| 193 | * |
||
| 194 | * @type array |
||
| 195 | */ |
||
| 196 | protected $servers = []; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * The query library to use. Default is Native |
||
| 200 | * |
||
| 201 | * @type string |
||
| 202 | */ |
||
| 203 | protected $queryLibrary = 'GameQ\\Query\\Native'; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Holds the instance of the queryLibrary |
||
| 207 | * |
||
| 208 | * @type \GameQ\Query\Core|null |
||
| 209 | */ |
||
| 210 | protected $query = null; |
||
| 211 | |||
| 212 | /** |
||
| 213 | * GameQ constructor. |
||
| 214 | * |
||
| 215 | * Do some checks as needed so this will operate |
||
| 216 | */ |
||
| 217 | 223 | public function __construct() |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Get an option's value |
||
| 228 | * |
||
| 229 | * @param mixed $option |
||
| 230 | * |
||
| 231 | * @return mixed|null |
||
| 232 | */ |
||
| 233 | 216 | public function __get($option) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Set an option's value |
||
| 241 | * |
||
| 242 | * @param mixed $option |
||
| 243 | * @param mixed $value |
||
| 244 | * |
||
| 245 | * @return bool |
||
| 246 | */ |
||
| 247 | 218 | public function __set($option, $value) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Chainable call to __set, uses set as the actual setter |
||
| 257 | * |
||
| 258 | * @param mixed $var |
||
| 259 | * @param mixed $value |
||
| 260 | * |
||
| 261 | * @return $this |
||
| 262 | */ |
||
| 263 | 218 | public function setOption($var, $value) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Add a single server |
||
| 274 | * |
||
| 275 | * @param array $server_info |
||
| 276 | * |
||
| 277 | * @return $this |
||
| 278 | */ |
||
| 279 | 2 | public function addServer(array $server_info = []) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Add multiple servers in a single call |
||
| 290 | * |
||
| 291 | * @param array $servers |
||
| 292 | * |
||
| 293 | * @return $this |
||
| 294 | */ |
||
| 295 | 2 | public function addServers(array $servers = []) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Add a set of servers from a file or an array of files. |
||
| 308 | * Supported formats: |
||
| 309 | * JSON |
||
| 310 | * |
||
| 311 | * @param array $files |
||
| 312 | * |
||
| 313 | * @return $this |
||
| 314 | * @throws \Exception |
||
| 315 | */ |
||
| 316 | 1 | public function addServersFromFiles($files = []) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Clear all of the defined servers |
||
| 348 | * |
||
| 349 | * @return $this |
||
| 350 | */ |
||
| 351 | 2 | public function clearServers() |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Add a filter to the processing list |
||
| 362 | * |
||
| 363 | * @param string $filterName |
||
| 364 | * @param array $options |
||
| 365 | * |
||
| 366 | * @return $this |
||
| 367 | */ |
||
| 368 | 3 | public function addFilter($filterName, $options = []) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Remove an added filter |
||
| 386 | * |
||
| 387 | * @param string $filterHash |
||
| 388 | * |
||
| 389 | * @return $this |
||
| 390 | */ |
||
| 391 | 216 | public function removeFilter($filterHash) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Return the list of applied filters |
||
| 408 | * |
||
| 409 | * @return array |
||
| 410 | */ |
||
| 411 | public function listFilters() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Main method used to actually process all of the added servers and return the information |
||
| 418 | * |
||
| 419 | * @return array |
||
| 420 | * @throws \Exception |
||
| 421 | */ |
||
| 422 | public function process() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Do server challenges, where required |
||
| 466 | */ |
||
| 467 | protected function doChallenges() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Run the actual queries and get the response(s) |
||
| 553 | */ |
||
| 554 | protected function doQueries() |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Parse the response for a specific server |
||
| 656 | * |
||
| 657 | * @param \GameQ\Server $server |
||
| 658 | * |
||
| 659 | * @return array |
||
| 660 | * @throws \Exception |
||
| 661 | */ |
||
| 662 | 215 | protected function doParseResponse(Server $server) |
|
| 709 | |||
| 710 | /** |
||
| 711 | * Apply any filters to the results |
||
| 712 | * |
||
| 713 | * @param array $results |
||
| 714 | * @param \GameQ\Server $server |
||
| 715 | * |
||
| 716 | * @return array |
||
| 717 | */ |
||
| 718 | 2 | protected function doApplyFilters(array $results, Server $server) |
|
| 741 | } |
||
| 742 |