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 |
||
40 | class GameQ |
||
41 | { |
||
42 | /* |
||
43 | * Constants |
||
44 | */ |
||
45 | |||
46 | /** |
||
47 | * Current version |
||
48 | */ |
||
49 | const VERSION = '3.0.0-alpha2'; |
||
50 | |||
51 | /* Static Section */ |
||
52 | |||
53 | /** |
||
54 | * Holds the instance of itself |
||
55 | * |
||
56 | * @type self |
||
57 | */ |
||
58 | protected static $instance = null; |
||
59 | |||
60 | /** |
||
61 | * Create a new instance of this class |
||
62 | * |
||
63 | * @return \GameQ\GameQ |
||
64 | */ |
||
65 | 1 | public static function factory() |
|
74 | |||
75 | /* Dynamic Section */ |
||
76 | |||
77 | /** |
||
78 | * Default options |
||
79 | * |
||
80 | * @type array |
||
81 | */ |
||
82 | protected $options = [ |
||
83 | 'debug' => false, |
||
84 | 'timeout' => 3, // Seconds |
||
85 | 'filters' => [ 'normalize' => [ ] ], |
||
86 | // Advanced settings |
||
87 | 'stream_timeout' => 200000, // See http://www.php.net/manual/en/function.stream-select.php for more info |
||
88 | 'write_wait' => 500, |
||
89 | // How long (in micro-seconds) to pause between writing to server sockets, helps cpu usage |
||
90 | |||
91 | // Used for generating protocol test data |
||
92 | 'capture_packets_file' => null, |
||
93 | ]; |
||
94 | |||
95 | /** |
||
96 | * Array of servers being queried |
||
97 | * |
||
98 | * @type array |
||
99 | */ |
||
100 | protected $servers = [ ]; |
||
101 | |||
102 | /** |
||
103 | * The query library to use. Default is Native |
||
104 | * |
||
105 | * @type string |
||
106 | */ |
||
107 | protected $queryLibrary = 'GameQ\\Query\\Native'; |
||
108 | |||
109 | /** |
||
110 | * Holds the instance of the queryLibrary |
||
111 | * |
||
112 | * @type \GameQ\Query\Core|null |
||
113 | */ |
||
114 | protected $query = null; |
||
115 | |||
116 | /** |
||
117 | * Get an option's value |
||
118 | * |
||
119 | * @param mixed $option |
||
120 | * |
||
121 | * @return mixed|null |
||
122 | */ |
||
123 | 119 | public function __get($option) |
|
128 | |||
129 | /** |
||
130 | * Set an option's value |
||
131 | * |
||
132 | * @param mixed $option |
||
133 | * @param mixed $value |
||
134 | * |
||
135 | * @return bool |
||
136 | */ |
||
137 | 121 | public function __set($option, $value) |
|
144 | |||
145 | /** |
||
146 | * Chainable call to __set, uses set as the actual setter |
||
147 | * |
||
148 | * @param mixed $var |
||
149 | * @param mixed $value |
||
150 | * |
||
151 | * @return $this |
||
152 | */ |
||
153 | 121 | public function setOption($var, $value) |
|
161 | |||
162 | /** |
||
163 | * Add a single server |
||
164 | * |
||
165 | * @param array $server_info |
||
166 | * |
||
167 | * @return $this |
||
168 | */ |
||
169 | 2 | public function addServer(array $server_info = [ ]) |
|
177 | |||
178 | /** |
||
179 | * Add multiple servers in a single call |
||
180 | * |
||
181 | * @param array $servers |
||
182 | * |
||
183 | * @return $this |
||
184 | */ |
||
185 | 2 | public function addServers(array $servers = [ ]) |
|
186 | { |
||
187 | |||
188 | // Loop through all the servers and add them |
||
189 | 2 | foreach ($servers as $server_info) { |
|
190 | 2 | $this->addServer($server_info); |
|
191 | 2 | } |
|
192 | |||
193 | 2 | return $this; // Make calls chainable |
|
194 | } |
||
195 | |||
196 | /** |
||
197 | * Add a set of servers from a file or an array of files. |
||
198 | * Supported formats: |
||
199 | * JSON |
||
200 | * |
||
201 | * @param array $files |
||
202 | * |
||
203 | * @return $this |
||
204 | * @throws \Exception |
||
205 | */ |
||
206 | 1 | public function addServersFromFiles($files = [ ]) |
|
207 | { |
||
208 | |||
209 | // Since we expect an array let us turn a string (i.e. single file) into an array |
||
210 | 1 | if (!is_array($files)) { |
|
211 | 1 | $files = [ $files ]; |
|
212 | 1 | } |
|
213 | |||
214 | // Iterate over the file(s) and add them |
||
215 | 1 | foreach ($files as $file) { |
|
216 | // Check to make sure the file exists and we can read it |
||
217 | 1 | if (!file_exists($file) || !is_readable($file)) { |
|
218 | 1 | continue; |
|
219 | } |
||
220 | |||
221 | // See if this file is JSON |
||
222 | 1 | if (($servers = json_decode(file_get_contents($file), true)) === null |
|
223 | 1 | && json_last_error() !== JSON_ERROR_NONE |
|
224 | 1 | ) { |
|
225 | // Type not supported |
||
226 | 1 | continue; |
|
227 | } |
||
228 | |||
229 | // Add this list of servers |
||
230 | 1 | $this->addServers($servers); |
|
231 | 1 | } |
|
232 | |||
233 | 1 | return $this; |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * Clear all of the defined servers |
||
238 | * |
||
239 | * @return $this |
||
240 | */ |
||
241 | 2 | public function clearServers() |
|
249 | |||
250 | /** |
||
251 | * Add a filter to the processing list |
||
252 | * |
||
253 | * @param string $filterName |
||
254 | * @param array $options |
||
255 | * |
||
256 | * @return $this |
||
257 | */ |
||
258 | 3 | public function addFilter($filterName, $options = [ ]) |
|
266 | |||
267 | /** |
||
268 | * Remove a filter from processing |
||
269 | * |
||
270 | * @param string $filterName |
||
271 | * |
||
272 | * @return $this |
||
273 | */ |
||
274 | 121 | public function removeFilter($filterName) |
|
287 | |||
288 | /** |
||
289 | * Main method used to actually process all of the added servers and return the information |
||
290 | * |
||
291 | * @return array |
||
292 | * @throws \Exception |
||
293 | */ |
||
294 | public function process() |
||
295 | { |
||
296 | |||
297 | // Initialize the query library we are using |
||
298 | $class = new \ReflectionClass($this->queryLibrary); |
||
299 | |||
300 | // Set the query pointer to the new instance of the library |
||
301 | $this->query = $class->newInstance(); |
||
302 | |||
303 | unset($class); |
||
304 | |||
305 | // Define the return |
||
306 | $results = [ ]; |
||
307 | |||
308 | // @todo: Add break up into loop to split large arrays into smaller chunks |
||
309 | |||
310 | // Do server challenge(s) first, if any |
||
311 | $this->doChallenges(); |
||
312 | |||
313 | // Do packets for server(s) and get query responses |
||
314 | $this->doQueries(); |
||
315 | |||
316 | // Now we should have some information to process for each server |
||
317 | foreach ($this->servers as $server) { |
||
318 | /* @var $server \GameQ\Server */ |
||
319 | |||
320 | // Parse the responses for this server |
||
321 | $result = $this->doParseResponse($server); |
||
322 | |||
323 | // Apply the filters |
||
324 | $result = array_merge($result, $this->doApplyFilters($result, $server)); |
||
325 | |||
326 | // Sort the keys so they are alphabetical and nicer to look at |
||
327 | ksort($result); |
||
328 | |||
329 | // Add the result to the results array |
||
330 | $results[$server->id()] = $result; |
||
331 | } |
||
332 | |||
333 | return $results; |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Do server challenges, where required |
||
338 | */ |
||
339 | protected function doChallenges() |
||
415 | |||
416 | /** |
||
417 | * Run the actual queries and get the response(s) |
||
418 | */ |
||
419 | protected function doQueries() |
||
509 | |||
510 | /** |
||
511 | * Parse the response for a specific server |
||
512 | * |
||
513 | * @param \GameQ\Server $server |
||
514 | * |
||
515 | * @return array |
||
516 | * @throws \Exception |
||
517 | */ |
||
518 | 118 | protected function doParseResponse(Server $server) |
|
565 | |||
566 | /** |
||
567 | * Apply any filters to the results |
||
568 | * |
||
569 | * @param array $results |
||
570 | * @param \GameQ\Server $server |
||
571 | * |
||
572 | * @return array |
||
573 | */ |
||
574 | 2 | protected function doApplyFilters(array $results, Server $server) |
|
597 | } |
||
598 |