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