Conditions | 3 |
Paths | 3 |
Total Lines | 56 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
97 | public function processResponse() |
||
98 | { |
||
99 | // Merge the response array into a buffer. Unknown if this protocol does split packets or not |
||
100 | $buffer = new Buffer(implode($this->packets_response)); |
||
101 | |||
102 | // Read first character from response. It should match below |
||
103 | $header = $buffer->read(1); |
||
104 | |||
105 | // Check first character to make sure the header matches |
||
106 | if ($header !== self::ID_UNCONNECTED_PONG) { |
||
107 | throw new Exception(sprintf('The header returned "%s" does not match the expected header of "%s"!', $header, |
||
108 | self::ID_UNCONNECTED_PONG)); |
||
109 | } |
||
110 | |||
111 | // Burn the time section |
||
112 | $buffer->skip(8); |
||
113 | |||
114 | // Server GUID is next |
||
115 | $serverGUID = $buffer->readInt64(); |
||
116 | |||
117 | // Read the next set to check to make sure the "magic" matches |
||
118 | $magicCheck = $buffer->read(16); |
||
119 | |||
120 | // Magic check fails |
||
121 | if ($magicCheck !== self::OFFLINE_MESSAGE_DATA_ID) { |
||
122 | throw new Exception(sprintf('The magic value returned "%s" does not match the expected value of "%s"!', |
||
123 | $magicCheck, self::OFFLINE_MESSAGE_DATA_ID)); |
||
124 | } |
||
125 | |||
126 | // According to docs the next character is supposed to be used for a length and string for the following character for the MOTD but it appears to be implemented incorrectly |
||
127 | // Burn the next two characters instead of trying to do anything useful with them |
||
128 | $buffer->skip(2); |
||
129 | |||
130 | // Set the result to a new result instance |
||
131 | $result = new Result(); |
||
132 | |||
133 | // Here on is server information delimited by semicolons (;) |
||
134 | $info = explode(';', $buffer->getBuffer()); |
||
135 | |||
136 | $result->add('edition', $info[0]); |
||
137 | $result->add('motd_line_1', $info[1]); |
||
138 | $result->add('protocol_version', (int)$info[2]); |
||
139 | $result->add('version', $info[3]); |
||
140 | $result->add('num_players', $info[4]); |
||
141 | $result->add('max_players', $info[5]); |
||
142 | $result->add('server_uid', $info[6]); |
||
143 | $result->add('motd_line_2', $info[7]); |
||
144 | $result->add('gamemode', $info[8]); |
||
145 | $result->add('gamemode_numeric', (int)$info[9]); |
||
146 | $result->add('port_ipv4', (int)$info[10]); |
||
147 | $result->add('port_ipv6', (int)$info[11]); |
||
148 | $result->add('dedicated', 1); |
||
149 | |||
150 | unset($header, $serverGUID, $magicCheck, $info); |
||
151 | |||
152 | return $result->fetch(); |
||
153 | } |
||
155 |