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 |
||
| 41 | class GameQ |
||
| 42 | { |
||
| 43 | /* |
||
| 44 | * Constants |
||
| 45 | */ |
||
| 46 | const PROTOCOLS_DIRECTORY = __DIR__ . '/Protocols'; |
||
| 47 | |||
| 48 | /* Static Section */ |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Holds the instance of itself |
||
| 52 | * |
||
| 53 | * @type self |
||
| 54 | */ |
||
| 55 | protected static $instance = null; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Create a new instance of this class |
||
| 59 | * |
||
| 60 | * @return \GameQ\GameQ |
||
| 61 | */ |
||
| 62 | 1 | public static function factory() |
|
| 71 | |||
| 72 | /* Dynamic Section */ |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Default options |
||
| 76 | * |
||
| 77 | * @type array |
||
| 78 | */ |
||
| 79 | protected $options = [ |
||
| 80 | 'debug' => false, |
||
| 81 | 'timeout' => 3, // Seconds |
||
| 82 | 'filters' => [ |
||
| 83 | // Default normalize |
||
| 84 | 'normalize_d751713988987e9331980363e24189ce' => [ |
||
| 85 | 'filter' => 'normalize', |
||
| 86 | 'options' => [], |
||
| 87 | ], |
||
| 88 | ], |
||
| 89 | // Advanced settings |
||
| 90 | 'stream_timeout' => 200000, // See http://www.php.net/manual/en/function.stream-select.php for more info |
||
| 91 | 'write_wait' => 500, |
||
| 92 | // How long (in micro-seconds) to pause between writing to server sockets, helps cpu usage |
||
| 93 | |||
| 94 | // Used for generating protocol test data |
||
| 95 | 'capture_packets_file' => null, |
||
| 96 | ]; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Array of servers being queried |
||
| 100 | * |
||
| 101 | * @type array |
||
| 102 | */ |
||
| 103 | protected $servers = []; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * The query library to use. Default is Native |
||
| 107 | * |
||
| 108 | * @type string |
||
| 109 | */ |
||
| 110 | protected $queryLibrary = 'GameQ\\Query\\Native'; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Holds the instance of the queryLibrary |
||
| 114 | * |
||
| 115 | * @type \GameQ\Query\Core|null |
||
| 116 | */ |
||
| 117 | protected $query = null; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * GameQ constructor. |
||
| 121 | * |
||
| 122 | * Do some checks as needed so this will operate |
||
| 123 | */ |
||
| 124 | 224 | public function __construct() |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Get an option's value |
||
| 135 | * |
||
| 136 | * @param mixed $option |
||
| 137 | * |
||
| 138 | * @return mixed|null |
||
| 139 | */ |
||
| 140 | 217 | public function __get($option) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Set an option's value |
||
| 148 | * |
||
| 149 | * @param mixed $option |
||
| 150 | * @param mixed $value |
||
| 151 | * |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | 219 | public function __set($option, $value) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Chainable call to __set, uses set as the actual setter |
||
| 164 | * |
||
| 165 | * @param mixed $var |
||
| 166 | * @param mixed $value |
||
| 167 | * |
||
| 168 | * @return $this |
||
| 169 | */ |
||
| 170 | 219 | public function setOption($var, $value) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Add a single server |
||
| 181 | * |
||
| 182 | * @param array $server_info |
||
| 183 | * |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | 2 | public function addServer(array $server_info = []) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Add multiple servers in a single call |
||
| 197 | * |
||
| 198 | * @param array $servers |
||
| 199 | * |
||
| 200 | * @return $this |
||
| 201 | */ |
||
| 202 | 2 | public function addServers(array $servers = []) |
|
| 203 | { |
||
| 204 | |||
| 205 | // Loop through all the servers and add them |
||
| 206 | 2 | foreach ($servers as $server_info) { |
|
| 207 | 2 | $this->addServer($server_info); |
|
| 208 | 2 | } |
|
| 209 | |||
| 210 | 2 | return $this; // Make calls chainable |
|
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Add a set of servers from a file or an array of files. |
||
| 215 | * Supported formats: |
||
| 216 | * JSON |
||
| 217 | * |
||
| 218 | * @param array $files |
||
| 219 | * |
||
| 220 | * @return $this |
||
| 221 | * @throws \Exception |
||
| 222 | */ |
||
| 223 | 1 | public function addServersFromFiles($files = []) |
|
| 224 | { |
||
| 225 | |||
| 226 | // Since we expect an array let us turn a string (i.e. single file) into an array |
||
| 227 | 1 | if (!is_array($files)) { |
|
| 228 | 1 | $files = [$files]; |
|
| 229 | 1 | } |
|
| 230 | |||
| 231 | // Iterate over the file(s) and add them |
||
| 232 | 1 | foreach ($files as $file) { |
|
| 233 | // Check to make sure the file exists and we can read it |
||
| 234 | 1 | if (!file_exists($file) || !is_readable($file)) { |
|
| 235 | 1 | continue; |
|
| 236 | } |
||
| 237 | |||
| 238 | // See if this file is JSON |
||
| 239 | 1 | if (($servers = json_decode(file_get_contents($file), true)) === null |
|
| 240 | 1 | && json_last_error() !== JSON_ERROR_NONE |
|
| 241 | 1 | ) { |
|
| 242 | // Type not supported |
||
| 243 | 1 | continue; |
|
| 244 | } |
||
| 245 | |||
| 246 | // Add this list of servers |
||
| 247 | 1 | $this->addServers($servers); |
|
| 248 | 1 | } |
|
| 249 | |||
| 250 | 1 | return $this; |
|
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Clear all of the defined servers |
||
| 255 | * |
||
| 256 | * @return $this |
||
| 257 | */ |
||
| 258 | 2 | public function clearServers() |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Add a filter to the processing list |
||
| 269 | * |
||
| 270 | * @param string $filterName |
||
| 271 | * @param array $options |
||
| 272 | * |
||
| 273 | * @return $this |
||
| 274 | */ |
||
| 275 | 3 | public function addFilter($filterName, $options = []) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Remove an added filter |
||
| 293 | * |
||
| 294 | * @param string $filterHash |
||
| 295 | * |
||
| 296 | * @return $this |
||
| 297 | */ |
||
| 298 | 217 | public function removeFilter($filterHash) |
|
| 299 | { |
||
| 300 | // Make lower case |
||
| 301 | 217 | $filterHash = strtolower($filterHash); |
|
| 302 | |||
| 303 | // Remove this filter if it has been defined |
||
| 304 | 217 | if (array_key_exists($filterHash, $this->options['filters'])) { |
|
| 305 | 3 | unset($this->options['filters'][$filterHash]); |
|
| 306 | 3 | } |
|
| 307 | |||
| 308 | 217 | unset($filterHash); |
|
| 309 | |||
| 310 | 217 | return $this; |
|
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Return the list of applied filters |
||
| 315 | * |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | public function listFilters() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Main method used to actually process all of the added servers and return the information |
||
| 325 | * |
||
| 326 | * @return array |
||
| 327 | * @throws \Exception |
||
| 328 | */ |
||
| 329 | public function process() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Do server challenges, where required |
||
| 373 | */ |
||
| 374 | protected function doChallenges() |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Run the actual queries and get the response(s) |
||
| 460 | */ |
||
| 461 | protected function doQueries() |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Parse the response for a specific server |
||
| 563 | * |
||
| 564 | * @param \GameQ\Server $server |
||
| 565 | * |
||
| 566 | * @return array |
||
| 567 | * @throws \Exception |
||
| 568 | */ |
||
| 569 | 216 | protected function doParseResponse(Server $server) |
|
| 616 | |||
| 617 | /** |
||
| 618 | * Apply any filters to the results |
||
| 619 | * |
||
| 620 | * @param array $results |
||
| 621 | * @param \GameQ\Server $server |
||
| 622 | * |
||
| 623 | * @return array |
||
| 624 | */ |
||
| 625 | 2 | protected function doApplyFilters(array $results, Server $server) |
|
| 648 | } |
||
| 649 |