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 | |||
| 47 | /** |
||
| 48 | * Current version |
||
| 49 | */ |
||
| 50 | const VERSION = '3.0.0-alpha2'; |
||
| 51 | |||
| 52 | /* Static Section */ |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Holds the instance of itself |
||
| 56 | * |
||
| 57 | * @type self |
||
| 58 | */ |
||
| 59 | protected static $instance = null; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Create a new instance of this class |
||
| 63 | * |
||
| 64 | * @return \GameQ\GameQ |
||
| 65 | */ |
||
| 66 | 1 | public static function factory() |
|
| 75 | |||
| 76 | /* Dynamic Section */ |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Default options |
||
| 80 | * |
||
| 81 | * @type array |
||
| 82 | */ |
||
| 83 | protected $options = [ |
||
| 84 | 'debug' => false, |
||
| 85 | 'timeout' => 3, // Seconds |
||
| 86 | 'filters' => [ 'normalize' => [ ] ], |
||
| 87 | // Advanced settings |
||
| 88 | 'stream_timeout' => 200000, // See http://www.php.net/manual/en/function.stream-select.php for more info |
||
| 89 | 'write_wait' => 500, |
||
| 90 | // How long (in micro-seconds) to pause between writing to server sockets, helps cpu usage |
||
| 91 | |||
| 92 | // Used for generating protocol test data |
||
| 93 | 'capture_packets_file' => null, |
||
| 94 | ]; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Array of servers being queried |
||
| 98 | * |
||
| 99 | * @type array |
||
| 100 | */ |
||
| 101 | protected $servers = [ ]; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * The query library to use. Default is Native |
||
| 105 | * |
||
| 106 | * @type string |
||
| 107 | */ |
||
| 108 | protected $queryLibrary = 'GameQ\\Query\\Native'; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Holds the instance of the queryLibrary |
||
| 112 | * |
||
| 113 | * @type \GameQ\Query\Core|null |
||
| 114 | */ |
||
| 115 | protected $query = null; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * GameQ constructor. |
||
| 119 | * |
||
| 120 | * Do some checks as needed so this will operate |
||
| 121 | */ |
||
| 122 | 137 | public function __construct() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Get an option's value |
||
| 133 | * |
||
| 134 | * @param mixed $option |
||
| 135 | * |
||
| 136 | * @return mixed|null |
||
| 137 | */ |
||
| 138 | 131 | public function __get($option) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Set an option's value |
||
| 146 | * |
||
| 147 | * @param mixed $option |
||
| 148 | * @param mixed $value |
||
| 149 | * |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | 133 | public function __set($option, $value) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Chainable call to __set, uses set as the actual setter |
||
| 162 | * |
||
| 163 | * @param mixed $var |
||
| 164 | * @param mixed $value |
||
| 165 | * |
||
| 166 | * @return $this |
||
| 167 | */ |
||
| 168 | 133 | public function setOption($var, $value) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Add a single server |
||
| 179 | * |
||
| 180 | * @param array $server_info |
||
| 181 | * |
||
| 182 | * @return $this |
||
| 183 | */ |
||
| 184 | 2 | public function addServer(array $server_info = [ ]) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Add multiple servers in a single call |
||
| 195 | * |
||
| 196 | * @param array $servers |
||
| 197 | * |
||
| 198 | * @return $this |
||
| 199 | */ |
||
| 200 | 2 | public function addServers(array $servers = [ ]) |
|
| 201 | { |
||
| 202 | |||
| 203 | // Loop through all the servers and add them |
||
| 204 | 2 | foreach ($servers as $server_info) { |
|
| 205 | 2 | $this->addServer($server_info); |
|
| 206 | } |
||
| 207 | |||
| 208 | 2 | return $this; // Make calls chainable |
|
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Add a set of servers from a file or an array of files. |
||
| 213 | * Supported formats: |
||
| 214 | * JSON |
||
| 215 | * |
||
| 216 | * @param array $files |
||
| 217 | * |
||
| 218 | * @return $this |
||
| 219 | * @throws \Exception |
||
| 220 | */ |
||
| 221 | 1 | public function addServersFromFiles($files = [ ]) |
|
| 222 | { |
||
| 223 | |||
| 224 | // Since we expect an array let us turn a string (i.e. single file) into an array |
||
| 225 | 1 | if (!is_array($files)) { |
|
| 226 | 1 | $files = [ $files ]; |
|
| 227 | } |
||
| 228 | |||
| 229 | // Iterate over the file(s) and add them |
||
| 230 | 1 | foreach ($files as $file) { |
|
| 231 | // Check to make sure the file exists and we can read it |
||
| 232 | 1 | if (!file_exists($file) || !is_readable($file)) { |
|
| 233 | 1 | continue; |
|
| 234 | } |
||
| 235 | |||
| 236 | // See if this file is JSON |
||
| 237 | 1 | if (($servers = json_decode(file_get_contents($file), true)) === null |
|
| 238 | 1 | && json_last_error() !== JSON_ERROR_NONE |
|
| 239 | ) { |
||
| 240 | // Type not supported |
||
| 241 | 1 | continue; |
|
| 242 | } |
||
| 243 | |||
| 244 | // Add this list of servers |
||
| 245 | 1 | $this->addServers($servers); |
|
| 246 | } |
||
| 247 | |||
| 248 | 1 | return $this; |
|
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Clear all of the defined servers |
||
| 253 | * |
||
| 254 | * @return $this |
||
| 255 | */ |
||
| 256 | 2 | public function clearServers() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Add a filter to the processing list |
||
| 267 | * |
||
| 268 | * @param string $filterName |
||
| 269 | * @param array $options |
||
| 270 | * |
||
| 271 | * @return $this |
||
| 272 | */ |
||
| 273 | 3 | public function addFilter($filterName, $options = [ ]) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Remove a filter from processing |
||
| 284 | * |
||
| 285 | * @param string $filterName |
||
| 286 | * |
||
| 287 | * @return $this |
||
| 288 | */ |
||
| 289 | 133 | public function removeFilter($filterName) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Main method used to actually process all of the added servers and return the information |
||
| 305 | * |
||
| 306 | * @return array |
||
| 307 | * @throws \Exception |
||
| 308 | */ |
||
| 309 | public function process() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Do server challenges, where required |
||
| 353 | */ |
||
| 354 | protected function doChallenges() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Run the actual queries and get the response(s) |
||
| 441 | */ |
||
| 442 | protected function doQueries() |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Parse the response for a specific server |
||
| 544 | * |
||
| 545 | * @param \GameQ\Server $server |
||
| 546 | * |
||
| 547 | * @return array |
||
| 548 | * @throws \Exception |
||
| 549 | */ |
||
| 550 | 130 | protected function doParseResponse(Server $server) |
|
| 597 | |||
| 598 | /** |
||
| 599 | * Apply any filters to the results |
||
| 600 | * |
||
| 601 | * @param array $results |
||
| 602 | * @param \GameQ\Server $server |
||
| 603 | * |
||
| 604 | * @return array |
||
| 605 | */ |
||
| 606 | 2 | protected function doApplyFilters(array $results, Server $server) |
|
| 629 | } |
||
| 630 |