Conditions | 8 |
Paths | 8 |
Total Lines | 58 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
135 | public function processResponse() |
||
136 | { |
||
137 | /* Ensure there is a reply from the "Metaserver" */ |
||
138 | if (empty($this->packets_response)) { |
||
139 | return []; |
||
140 | } |
||
141 | |||
142 | // Implode and rip out the JSON |
||
143 | preg_match('/\{(.*)\}/ms', implode('', $this->packets_response), $matches); |
||
144 | |||
145 | // Return should be JSON, let's validate |
||
146 | if (!isset($matches[0]) || ($json = json_decode($matches[0])) === null) { |
||
147 | throw new Exception(__METHOD__ . " JSON response from Stationeers Metaserver is invalid."); |
||
148 | } |
||
149 | |||
150 | // By default no server is found |
||
151 | $server = null; |
||
152 | |||
153 | // Find the server on this list by iterating over the entire list. |
||
154 | foreach ($json->GameSessions as $serverEntry) { |
||
155 | // Server information passed matches an entry on this list |
||
156 | if ($serverEntry->Address === $this->realIp && (int)$serverEntry->Port === $this->realPortQuery) { |
||
157 | $server = $serverEntry; |
||
158 | break; |
||
159 | } |
||
160 | } |
||
161 | |||
162 | /* Send to the garbage collector */ |
||
163 | unset($matches, $serverEntry, $json); |
||
164 | |||
165 | /* Ensure the provided Server has been found in the list provided by the "Metaserver" */ |
||
166 | if (! $server) { |
||
167 | throw new Exception(sprintf( |
||
168 | '%s Unable to find the server "%s:%d" in the Stationeer Metaservers server list', |
||
169 | __METHOD__, |
||
170 | $this->realIp, |
||
171 | $this->realPortQuery |
||
172 | )); |
||
173 | } |
||
174 | |||
175 | /* Build the Result from the parsed JSON */ |
||
176 | $result = new Result(); |
||
177 | $result->add('dedicated', 1); // Server is always dedicated |
||
178 | $result->add('hostname', $server->Name); |
||
179 | $result->add('gq_address', $server->Address); |
||
180 | $result->add('gq_port_query', $server->Port); |
||
181 | $result->add('version', $server->Version); |
||
182 | $result->add('map', $server->MapName); |
||
183 | $result->add('uptime', $server->UpTime); |
||
184 | $result->add('password', (int)$server->Password); |
||
185 | $result->add('numplayers', $server->Players); |
||
186 | $result->add('maxplayers', $server->MaxPlayers); |
||
187 | $result->add('type', $server->Type); |
||
188 | |||
189 | /* Send to the garbage collector */ |
||
190 | unset($server); |
||
191 | |||
192 | return $result->fetch(); |
||
193 | } |
||
195 |