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() |
|
| 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' => [ |
||
| 82 | // Default normalize |
||
| 83 | 'normalize_d751713988987e9331980363e24189ce' => [ |
||
| 84 | 'filter' => 'normalize', |
||
| 85 | 'options' => [], |
||
| 86 | ], |
||
| 87 | ], |
||
| 88 | // Advanced settings |
||
| 89 | 'stream_timeout' => 200000, // See http://www.php.net/manual/en/function.stream-select.php for more info |
||
| 90 | 'write_wait' => 500, |
||
| 91 | // How long (in micro-seconds) to pause between writing to server sockets, helps cpu usage |
||
| 92 | |||
| 93 | // Used for generating protocol test data |
||
| 94 | 'capture_packets_file' => null, |
||
| 95 | ]; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Array of servers being queried |
||
| 99 | * |
||
| 100 | * @type array |
||
| 101 | */ |
||
| 102 | protected $servers = []; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * The query library to use. Default is Native |
||
| 106 | * |
||
| 107 | * @type string |
||
| 108 | */ |
||
| 109 | protected $queryLibrary = 'GameQ\\Query\\Native'; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Holds the instance of the queryLibrary |
||
| 113 | * |
||
| 114 | * @type \GameQ\Query\Core|null |
||
| 115 | */ |
||
| 116 | protected $query = null; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * GameQ constructor. |
||
| 120 | * |
||
| 121 | * Do some checks as needed so this will operate |
||
| 122 | */ |
||
| 123 | 201 | public function __construct() |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Get an option's value |
||
| 134 | * |
||
| 135 | * @param mixed $option |
||
| 136 | * |
||
| 137 | * @return mixed|null |
||
| 138 | */ |
||
| 139 | 194 | public function __get($option) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Set an option's value |
||
| 147 | * |
||
| 148 | * @param mixed $option |
||
| 149 | * @param mixed $value |
||
| 150 | * |
||
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | 196 | public function __set($option, $value) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Chainable call to __set, uses set as the actual setter |
||
| 163 | * |
||
| 164 | * @param mixed $var |
||
| 165 | * @param mixed $value |
||
| 166 | * |
||
| 167 | * @return $this |
||
| 168 | */ |
||
| 169 | 196 | public function setOption($var, $value) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Add a single server |
||
| 180 | * |
||
| 181 | * @param array $server_info |
||
| 182 | * |
||
| 183 | * @return $this |
||
| 184 | */ |
||
| 185 | 2 | public function addServer(array $server_info = []) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Add multiple servers in a single call |
||
| 196 | * |
||
| 197 | * @param array $servers |
||
| 198 | * |
||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | 2 | public function addServers(array $servers = []) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Add a set of servers from a file or an array of files. |
||
| 214 | * Supported formats: |
||
| 215 | * JSON |
||
| 216 | * |
||
| 217 | * @param array $files |
||
| 218 | * |
||
| 219 | * @return $this |
||
| 220 | * @throws \Exception |
||
| 221 | */ |
||
| 222 | 1 | public function addServersFromFiles($files = []) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Clear all of the defined servers |
||
| 254 | * |
||
| 255 | * @return $this |
||
| 256 | */ |
||
| 257 | 2 | public function clearServers() |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Add a filter to the processing list |
||
| 268 | * |
||
| 269 | * @param string $filterName |
||
| 270 | * @param array $options |
||
| 271 | * |
||
| 272 | * @return $this |
||
| 273 | */ |
||
| 274 | 3 | public function addFilter($filterName, $options = []) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Remove an added filter |
||
| 292 | * |
||
| 293 | * @param string $filterHash |
||
| 294 | * |
||
| 295 | * @return $this |
||
| 296 | */ |
||
| 297 | 194 | public function removeFilter($filterHash) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Return the list of applied filters |
||
| 314 | * |
||
| 315 | * @return array |
||
| 316 | */ |
||
| 317 | public function listFilters() |
||
| 318 | { |
||
| 319 | return $this->options['filters']; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Main method used to actually process all of the added servers and return the information |
||
| 324 | * |
||
| 325 | * @return array |
||
| 326 | * @throws \Exception |
||
| 327 | */ |
||
| 328 | public function process() |
||
| 329 | { |
||
| 330 | |||
| 331 | // Initialize the query library we are using |
||
| 332 | $class = new \ReflectionClass($this->queryLibrary); |
||
| 333 | |||
| 334 | // Set the query pointer to the new instance of the library |
||
| 335 | $this->query = $class->newInstance(); |
||
| 336 | |||
| 337 | unset($class); |
||
| 338 | |||
| 339 | // Define the return |
||
| 340 | $results = []; |
||
| 341 | |||
| 342 | // @todo: Add break up into loop to split large arrays into smaller chunks |
||
| 343 | |||
| 344 | // Do server challenge(s) first, if any |
||
| 345 | $this->doChallenges(); |
||
| 346 | |||
| 347 | // Do packets for server(s) and get query responses |
||
| 348 | $this->doQueries(); |
||
| 349 | |||
| 350 | // Now we should have some information to process for each server |
||
| 351 | foreach ($this->servers as $server) { |
||
| 352 | /* @var $server \GameQ\Server */ |
||
| 353 | |||
| 354 | // Parse the responses for this server |
||
| 355 | $result = $this->doParseResponse($server); |
||
| 356 | |||
| 357 | // Apply the filters |
||
| 358 | $result = array_merge($result, $this->doApplyFilters($result, $server)); |
||
| 359 | |||
| 360 | // Sort the keys so they are alphabetical and nicer to look at |
||
| 361 | ksort($result); |
||
| 362 | |||
| 363 | // Add the result to the results array |
||
| 364 | $results[$server->id()] = $result; |
||
| 365 | } |
||
| 366 | |||
| 367 | return $results; |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Do server challenges, where required |
||
| 372 | */ |
||
| 373 | protected function doChallenges() |
||
| 374 | { |
||
| 375 | |||
| 376 | // Initialize the sockets for reading |
||
| 377 | $sockets = []; |
||
| 378 | |||
| 379 | // By default we don't have any challenges to process |
||
| 380 | $server_challenge = false; |
||
| 381 | |||
| 382 | // Do challenge packets |
||
| 383 | foreach ($this->servers as $server_id => $server) { |
||
| 384 | /* @var $server \GameQ\Server */ |
||
| 385 | |||
| 386 | // This protocol has a challenge packet that needs to be sent |
||
| 387 | if ($server->protocol()->hasChallenge()) { |
||
| 388 | // We have a challenge, set the flag |
||
| 389 | $server_challenge = true; |
||
| 390 | |||
| 391 | // Let's make a clone of the query class |
||
| 392 | $socket = clone $this->query; |
||
| 393 | |||
| 394 | // Set the information for this query socket |
||
| 395 | $socket->set( |
||
| 396 | $server->protocol()->transport(), |
||
| 397 | $server->ip, |
||
| 398 | $server->port_query, |
||
| 399 | $this->timeout |
||
| 400 | ); |
||
| 401 | |||
| 402 | try { |
||
| 403 | // Now write the challenge packet to the socket. |
||
| 404 | $socket->write($server->protocol()->getPacket(Protocol::PACKET_CHALLENGE)); |
||
| 405 | |||
| 406 | // Add the socket information so we can reference it easily |
||
| 407 | $sockets[(int)$socket->get()] = [ |
||
| 408 | 'server_id' => $server_id, |
||
| 409 | 'socket' => $socket, |
||
| 410 | ]; |
||
| 411 | } catch (QueryException $e) { |
||
| 412 | // Check to see if we are in debug, if so bubble up the exception |
||
| 413 | if ($this->debug) { |
||
| 414 | throw new \Exception($e->getMessage(), $e->getCode(), $e); |
||
| 415 | } |
||
| 416 | } |
||
| 417 | |||
| 418 | unset($socket); |
||
| 419 | |||
| 420 | // Let's sleep shortly so we are not hammering out calls rapid fire style hogging cpu |
||
| 421 | usleep($this->write_wait); |
||
| 422 | } |
||
| 423 | } |
||
| 424 | |||
| 425 | // We have at least one server with a challenge, we need to listen for responses |
||
| 426 | if ($server_challenge) { |
||
| 427 | // Now we need to listen for and grab challenge response(s) |
||
| 428 | $responses = call_user_func_array( |
||
| 429 | [$this->query, 'getResponses'], |
||
| 430 | [$sockets, $this->timeout, $this->stream_timeout] |
||
| 431 | ); |
||
| 432 | |||
| 433 | // Iterate over the challenge responses |
||
| 434 | foreach ($responses as $socket_id => $response) { |
||
| 435 | // Back out the server_id we need to update the challenge response for |
||
| 436 | $server_id = $sockets[$socket_id]['server_id']; |
||
| 437 | |||
| 438 | // Make this into a buffer so it is easier to manipulate |
||
| 439 | $challenge = new Buffer(implode('', $response)); |
||
| 440 | |||
| 441 | // Grab the server instance |
||
| 442 | /* @var $server \GameQ\Server */ |
||
| 443 | $server = $this->servers[$server_id]; |
||
| 444 | |||
| 445 | // Apply the challenge |
||
| 446 | $server->protocol()->challengeParseAndApply($challenge); |
||
| 447 | |||
| 448 | // Add this socket to be reused, has to be reused in GameSpy3 for example |
||
| 449 | $server->socketAdd($sockets[$socket_id]['socket']); |
||
| 450 | |||
| 451 | // Clear |
||
| 452 | unset($server); |
||
| 453 | } |
||
| 454 | } |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Run the actual queries and get the response(s) |
||
| 459 | */ |
||
| 460 | protected function doQueries() |
||
| 461 | { |
||
| 462 | |||
| 463 | // Initialize the array of sockets |
||
| 464 | $sockets = []; |
||
| 465 | |||
| 466 | // Iterate over the server list |
||
| 467 | foreach ($this->servers as $server_id => $server) { |
||
| 468 | /* @var $server \GameQ\Server */ |
||
| 469 | |||
| 470 | // Invoke the beforeSend method |
||
| 471 | $server->protocol()->beforeSend($server); |
||
| 472 | |||
| 473 | // Get all the non-challenge packets we need to send |
||
| 474 | $packets = $server->protocol()->getPacket('!' . Protocol::PACKET_CHALLENGE); |
||
| 475 | |||
| 476 | if (count($packets) == 0) { |
||
| 477 | // Skip nothing else to do for some reason. |
||
| 478 | continue; |
||
| 479 | } |
||
| 480 | |||
| 481 | // Try to use an existing socket |
||
| 482 | if (($socket = $server->socketGet()) === null) { |
||
| 483 | // Let's make a clone of the query class |
||
| 484 | $socket = clone $this->query; |
||
| 485 | |||
| 486 | // Set the information for this query socket |
||
| 487 | $socket->set( |
||
| 488 | $server->protocol()->transport(), |
||
| 489 | $server->ip, |
||
| 490 | $server->port_query, |
||
| 491 | $this->timeout |
||
| 492 | ); |
||
| 493 | } |
||
| 494 | |||
| 495 | try { |
||
| 496 | // Iterate over all the packets we need to send |
||
| 497 | foreach ($packets as $packet_data) { |
||
| 498 | // Now write the packet to the socket. |
||
| 499 | $socket->write($packet_data); |
||
| 500 | |||
| 501 | // Let's sleep shortly so we are not hammering out calls rapid fire style |
||
| 502 | usleep($this->write_wait); |
||
| 503 | } |
||
| 504 | |||
| 505 | unset($packets); |
||
| 506 | |||
| 507 | // Add the socket information so we can reference it easily |
||
| 508 | $sockets[(int)$socket->get()] = [ |
||
| 509 | 'server_id' => $server_id, |
||
| 510 | 'socket' => $socket, |
||
| 511 | ]; |
||
| 512 | } catch (QueryException $e) { |
||
| 513 | // Check to see if we are in debug, if so bubble up the exception |
||
| 514 | if ($this->debug) { |
||
| 515 | throw new \Exception($e->getMessage(), $e->getCode(), $e); |
||
| 516 | } |
||
| 517 | |||
| 518 | break; |
||
| 519 | } |
||
| 520 | |||
| 521 | // Clean up the sockets, if any left over |
||
| 522 | $server->socketCleanse(); |
||
| 523 | } |
||
| 524 | |||
| 525 | // Now we need to listen for and grab response(s) |
||
| 526 | $responses = call_user_func_array( |
||
| 527 | [$this->query, 'getResponses'], |
||
| 528 | [$sockets, $this->timeout, $this->stream_timeout] |
||
| 529 | ); |
||
| 530 | |||
| 531 | // Iterate over the responses |
||
| 532 | foreach ($responses as $socket_id => $response) { |
||
| 533 | // Back out the server_id |
||
| 534 | $server_id = $sockets[$socket_id]['server_id']; |
||
| 535 | |||
| 536 | // Grab the server instance |
||
| 537 | /* @var $server \GameQ\Server */ |
||
| 538 | $server = $this->servers[$server_id]; |
||
| 539 | |||
| 540 | // Save the response from this packet |
||
| 541 | $server->protocol()->packetResponse($response); |
||
| 542 | |||
| 543 | unset($server); |
||
| 544 | } |
||
| 545 | |||
| 546 | // Now we need to close all of the sockets |
||
| 547 | foreach ($sockets as $socketInfo) { |
||
| 548 | /* @var $socket \GameQ\Query\Core */ |
||
| 549 | $socket = $socketInfo['socket']; |
||
| 550 | |||
| 551 | // Close the socket |
||
| 552 | $socket->close(); |
||
| 553 | |||
| 554 | unset($socket); |
||
| 555 | } |
||
| 556 | |||
| 557 | unset($sockets); |
||
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Parse the response for a specific server |
||
| 562 | * |
||
| 563 | * @param \GameQ\Server $server |
||
| 564 | * |
||
| 565 | * @return array |
||
| 566 | * @throws \Exception |
||
| 567 | */ |
||
| 568 | 193 | protected function doParseResponse(Server $server) |
|
| 615 | |||
| 616 | /** |
||
| 617 | * Apply any filters to the results |
||
| 618 | * |
||
| 619 | * @param array $results |
||
| 620 | * @param \GameQ\Server $server |
||
| 621 | * |
||
| 622 | * @return array |
||
| 623 | */ |
||
| 624 | 2 | protected function doApplyFilters(array $results, Server $server) |
|
| 647 | } |
||
| 648 |