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 | /* Static Section */ |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Holds the instance of itself |
||
| 51 | * |
||
| 52 | * @type self |
||
| 53 | */ |
||
| 54 | protected static $instance = null; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Create a new instance of this class |
||
| 58 | * |
||
| 59 | * @return \GameQ\GameQ |
||
| 60 | */ |
||
| 61 | 1 | public static function factory() |
|
| 62 | { |
||
| 63 | |||
| 64 | // Create a new instance |
||
| 65 | 1 | self::$instance = new self(); |
|
| 66 | |||
| 67 | // Return this new instance |
||
| 68 | 1 | return self::$instance; |
|
| 69 | } |
||
| 70 | |||
| 71 | /* Dynamic Section */ |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Default options |
||
| 75 | * |
||
| 76 | * @type array |
||
| 77 | */ |
||
| 78 | protected $options = [ |
||
| 79 | 'debug' => false, |
||
| 80 | 'timeout' => 3, // Seconds |
||
| 81 | 'filters' => [ 'normalize' => [ ] ], |
||
| 82 | // Advanced settings |
||
| 83 | 'stream_timeout' => 200000, // See http://www.php.net/manual/en/function.stream-select.php for more info |
||
| 84 | 'write_wait' => 500, |
||
| 85 | // How long (in micro-seconds) to pause between writing to server sockets, helps cpu usage |
||
| 86 | |||
| 87 | // Used for generating protocol test data |
||
| 88 | 'capture_packets_file' => null, |
||
| 89 | ]; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Array of servers being queried |
||
| 93 | * |
||
| 94 | * @type array |
||
| 95 | */ |
||
| 96 | protected $servers = [ ]; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * The query library to use. Default is Native |
||
| 100 | * |
||
| 101 | * @type string |
||
| 102 | */ |
||
| 103 | protected $queryLibrary = 'GameQ\\Query\\Native'; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Holds the instance of the queryLibrary |
||
| 107 | * |
||
| 108 | * @type \GameQ\Query\Core|null |
||
| 109 | */ |
||
| 110 | protected $query = null; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * GameQ constructor. |
||
| 114 | * |
||
| 115 | * Do some checks as needed so this will operate |
||
| 116 | */ |
||
| 117 | 143 | public function __construct() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Get an option's value |
||
| 128 | * |
||
| 129 | * @param mixed $option |
||
| 130 | * |
||
| 131 | * @return mixed|null |
||
| 132 | */ |
||
| 133 | 137 | public function __get($option) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Set an option's value |
||
| 141 | * |
||
| 142 | * @param mixed $option |
||
| 143 | * @param mixed $value |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | 139 | public function __set($option, $value) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Chainable call to __set, uses set as the actual setter |
||
| 157 | * |
||
| 158 | * @param mixed $var |
||
| 159 | * @param mixed $value |
||
| 160 | * |
||
| 161 | * @return $this |
||
| 162 | */ |
||
| 163 | 139 | public function setOption($var, $value) |
|
| 164 | { |
||
| 165 | |||
| 166 | // Use magic |
||
| 167 | 139 | $this->{$var} = $value; |
|
| 168 | |||
| 169 | 139 | return $this; // Make chainable |
|
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Add a single server |
||
| 174 | * |
||
| 175 | * @param array $server_info |
||
| 176 | * |
||
| 177 | * @return $this |
||
| 178 | */ |
||
| 179 | 2 | public function addServer(array $server_info = [ ]) |
|
| 180 | { |
||
| 181 | |||
| 182 | // Add and validate the server |
||
| 183 | 2 | $this->servers[ uniqid() ] = new Server($server_info); |
|
| 184 | |||
| 185 | 2 | return $this; // Make calls chainable |
|
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Add multiple servers in a single call |
||
| 190 | * |
||
| 191 | * @param array $servers |
||
| 192 | * |
||
| 193 | * @return $this |
||
| 194 | */ |
||
| 195 | 2 | public function addServers(array $servers = [ ]) |
|
| 196 | { |
||
| 197 | |||
| 198 | // Loop through all the servers and add them |
||
| 199 | 2 | foreach ($servers as $server_info) { |
|
| 200 | 2 | $this->addServer($server_info); |
|
| 201 | } |
||
| 202 | |||
| 203 | 2 | return $this; // Make calls chainable |
|
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Add a set of servers from a file or an array of files. |
||
| 208 | * Supported formats: |
||
| 209 | * JSON |
||
| 210 | * |
||
| 211 | * @param array $files |
||
| 212 | * |
||
| 213 | * @return $this |
||
| 214 | * @throws \Exception |
||
| 215 | */ |
||
| 216 | 1 | public function addServersFromFiles($files = [ ]) |
|
| 217 | { |
||
| 218 | |||
| 219 | // Since we expect an array let us turn a string (i.e. single file) into an array |
||
| 220 | 1 | if (!is_array($files)) { |
|
| 221 | 1 | $files = [ $files ]; |
|
| 222 | } |
||
| 223 | |||
| 224 | // Iterate over the file(s) and add them |
||
| 225 | 1 | foreach ($files as $file) { |
|
| 226 | // Check to make sure the file exists and we can read it |
||
| 227 | 1 | if (!file_exists($file) || !is_readable($file)) { |
|
| 228 | 1 | continue; |
|
| 229 | } |
||
| 230 | |||
| 231 | // See if this file is JSON |
||
| 232 | 1 | if (($servers = json_decode(file_get_contents($file), true)) === null |
|
| 233 | 1 | && json_last_error() !== JSON_ERROR_NONE |
|
| 234 | ) { |
||
| 235 | // Type not supported |
||
| 236 | 1 | continue; |
|
| 237 | } |
||
| 238 | |||
| 239 | // Add this list of servers |
||
| 240 | 1 | $this->addServers($servers); |
|
| 241 | } |
||
| 242 | |||
| 243 | 1 | return $this; |
|
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Clear all of the defined servers |
||
| 248 | * |
||
| 249 | * @return $this |
||
| 250 | */ |
||
| 251 | 2 | public function clearServers() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Add a filter to the processing list |
||
| 262 | * |
||
| 263 | * @param string $filterName |
||
| 264 | * @param array $options |
||
| 265 | * |
||
| 266 | * @return $this |
||
| 267 | */ |
||
| 268 | 3 | public function addFilter($filterName, $options = [ ]) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Remove a filter from processing |
||
| 279 | * |
||
| 280 | * @param string $filterName |
||
| 281 | * |
||
| 282 | * @return $this |
||
| 283 | */ |
||
| 284 | 139 | public function removeFilter($filterName) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Main method used to actually process all of the added servers and return the information |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | * @throws \Exception |
||
| 303 | */ |
||
| 304 | public function process() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Do server challenges, where required |
||
| 348 | */ |
||
| 349 | protected function doChallenges() |
||
| 350 | { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Run the actual queries and get the response(s) |
||
| 435 | */ |
||
| 436 | protected function doQueries() |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Parse the response for a specific server |
||
| 538 | * |
||
| 539 | * @param \GameQ\Server $server |
||
| 540 | * |
||
| 541 | * @return array |
||
| 542 | * @throws \Exception |
||
| 543 | */ |
||
| 544 | 136 | protected function doParseResponse(Server $server) |
|
| 591 | |||
| 592 | /** |
||
| 593 | * Apply any filters to the results |
||
| 594 | * |
||
| 595 | * @param array $results |
||
| 596 | * @param \GameQ\Server $server |
||
| 597 | * |
||
| 598 | * @return array |
||
| 599 | */ |
||
| 600 | 2 | protected function doApplyFilters(array $results, Server $server) |
|
| 623 | } |
||
| 624 |