Conditions | 79 |
Paths | > 20000 |
Total Lines | 311 |
Code Lines | 179 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 parse($input) { |
||
98 | global $globalDebug; |
||
99 | $debug = false; |
||
100 | $result = array(); |
||
101 | $input_len = strlen($input); |
||
102 | //$split_input = str_split($input); |
||
103 | |||
104 | /* Find the end of header checking for NULL bytes while doing it. */ |
||
105 | $splitpos = strpos($input,':'); |
||
106 | |||
107 | /* Check that end was found and body has at least one byte. */ |
||
108 | if ($splitpos == 0 || $splitpos + 1 == $input_len || $splitpos === FALSE) { |
||
109 | if ($globalDebug) echo '!!! APRS invalid : '.$input."\n"; |
||
110 | return false; |
||
111 | } |
||
112 | |||
113 | if ($debug) echo 'input : '.$input."\n"; |
||
114 | /* Save header and body. */ |
||
115 | $body = substr($input,$splitpos+1,$input_len); |
||
116 | $body_len = strlen($body); |
||
|
|||
117 | $header = substr($input,0,$splitpos); |
||
118 | //$header_len = strlen($header); |
||
119 | if ($debug) echo 'header : '.$header."\n"; |
||
120 | |||
121 | /* Parse source, target and path. */ |
||
122 | //FLRDF0A52>APRS,qAS,LSTB |
||
123 | if (preg_match('/^([A-Z0-9\\-]{1,9})>(.*)$/',$header,$matches)) { |
||
124 | $ident = $matches[1]; |
||
125 | $all_elements = $matches[2]; |
||
126 | if ($debug) echo 'ident : '.$ident."\n"; |
||
127 | $result['ident'] = $ident; |
||
128 | } else return false; |
||
129 | $elements = explode(',',$all_elements); |
||
130 | $source = end($elements); |
||
131 | $result['source'] = $source; |
||
132 | foreach ($elements as $element) { |
||
133 | if (preg_match('/^([a-zA-Z0-9-]{1,9})([*]?)$/',$element)) { |
||
134 | //echo "ok"; |
||
135 | //if ($element == 'TCPIP*') return false; |
||
136 | } elseif (!preg_match('/^([0-9A-F]{32})$/',$element)) { |
||
137 | if ($debug) echo 'element : '.$element."\n"; |
||
138 | return false; |
||
139 | } |
||
140 | /* |
||
141 | } elseif (preg_match('/^([0-9A-F]{32})$/',$element)) { |
||
142 | //echo "ok"; |
||
143 | } else { |
||
144 | return false; |
||
145 | } |
||
146 | */ |
||
147 | } |
||
148 | |||
149 | $type = substr($body,0,1); |
||
150 | if ($debug) echo 'type : '.$type."\n"; |
||
151 | if ($type == ';') { |
||
152 | $result['ident'] = trim(substr($body,1,9)); |
||
153 | } elseif ($type == ',') { |
||
154 | // Invalid data or test data |
||
155 | return false; |
||
156 | } |
||
157 | |||
158 | // Check for Timestamp |
||
159 | $find = false; |
||
160 | $body_parse = substr($body,1); |
||
161 | //echo 'Body : '.$body."\n"; |
||
162 | if (preg_match('/^;(.){9}\*/',$body,$matches)) { |
||
163 | $body_parse = substr($body_parse,10); |
||
164 | $find = true; |
||
165 | //echo $body_parse."\n"; |
||
166 | } |
||
167 | if (preg_match('/^`(.*)\//',$body,$matches)) { |
||
168 | $body_parse = substr($body_parse,strlen($matches[1])-1); |
||
169 | $find = true; |
||
170 | //echo $body_parse."\n"; |
||
171 | } |
||
172 | if (preg_match("/^'(.*)\//",$body,$matches)) { |
||
173 | $body_parse = substr($body_parse,strlen($matches[1])-1); |
||
174 | $find = true; |
||
175 | //echo $body_parse."\n"; |
||
176 | } |
||
177 | if (preg_match('/^([0-9]{2})([0-9]{2})([0-9]{2})([zh\\/])/',$body_parse,$matches)) { |
||
178 | $find = true; |
||
179 | //print_r($matches); |
||
180 | $timestamp = $matches[0]; |
||
181 | if ($matches[4] == 'h') { |
||
182 | $timestamp = strtotime($matches[1].':'.$matches[2].':'.$matches[3]); |
||
183 | //echo 'timestamp : '.$timestamp.' - now : '.time()."\n"; |
||
184 | /* |
||
185 | if (time() + 3900 < $timestamp) $timestamp -= 86400; |
||
186 | elseif (time() - 82500 > $timestamp) $timestamp += 86400; |
||
187 | */ |
||
188 | } elseif ($matches[4] == 'z' || $matches[4] == '/') { |
||
189 | // This work or not ? |
||
190 | $timestamp = strtotime(date('Ym').$matches[1].' '.$matches[2].':'.$matches[3]); |
||
191 | } |
||
192 | $body_parse = substr($body_parse,7); |
||
193 | $result['timestamp'] = $timestamp; |
||
194 | //echo date('Ymd H:i:s',$timestamp); |
||
195 | } |
||
196 | if (preg_match('/^([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/',$body_parse,$matches)) { |
||
197 | $find = true; |
||
198 | $timestamp = strtotime(date('Y').$matches[1].$matches[2].' '.$matches[3].':'.$matches[4]); |
||
199 | $body_parse = substr($body_parse,8); |
||
200 | $result['timestamp'] = $timestamp; |
||
201 | //echo date('Ymd H:i:s',$timestamp); |
||
202 | } |
||
203 | //if (strlen($body_parse) > 19) { |
||
204 | if (preg_match('/^([0-9]{2})([0-7 ][0-9 ]\\.[0-9 ]{2})([NnSs])(.)([0-9]{3})([0-7 ][0-9 ]\\.[0-9 ]{2})([EeWw])(.)/',$body_parse,$matches)) { |
||
205 | $find = true; |
||
206 | // 4658.70N/00707.78Ez |
||
207 | //print_r(str_split($body_parse)); |
||
208 | |||
209 | //$latlon = $matches[0]; |
||
210 | $sind = strtoupper($matches[3]); |
||
211 | $wind = strtoupper($matches[7]); |
||
212 | $lat_deg = $matches[1]; |
||
213 | $lat_min = $matches[2]; |
||
214 | $lon_deg = $matches[5]; |
||
215 | $lon_min = $matches[6]; |
||
216 | |||
217 | //$symbol_table = $matches[4]; |
||
218 | $lat = intval($lat_deg); |
||
219 | $lon = intval($lon_deg); |
||
220 | if ($lat > 89 || $lon > 179) return false; |
||
221 | |||
222 | /* |
||
223 | $tmp_5b = str_replace('.','',$lat_min); |
||
224 | if (preg_match('/^([0-9]{0,4})( {0,4})$/',$tmp_5b,$matches)) { |
||
225 | print_r($matches); |
||
226 | } |
||
227 | */ |
||
228 | $latitude = $lat + floatval($lat_min)/60; |
||
229 | $longitude = $lon + floatval($lon_min)/60; |
||
230 | if ($sind == 'S') $latitude = 0-$latitude; |
||
231 | if ($wind == 'W') $longitude = 0-$longitude; |
||
232 | $result['latitude'] = $latitude; |
||
233 | $result['longitude'] = $longitude; |
||
234 | $body_parse = substr($body_parse,18); |
||
235 | $body_parse_len = strlen($body_parse); |
||
236 | } |
||
237 | $body_parse_len = strlen($body_parse); |
||
238 | if ($body_parse_len > 0) { |
||
239 | /* |
||
240 | if (!isset($result['timestamp']) && !isset($result['latitude'])) { |
||
241 | $body_split = str_split($body); |
||
242 | $symbol_code = $body_split[0]; |
||
243 | $body_parse = substr($body,1); |
||
244 | $body_parse_len = strlen($body_parse); |
||
245 | } else { |
||
246 | */ |
||
247 | /* |
||
248 | if ($find === false) { |
||
249 | $body_split = str_split($body); |
||
250 | $symbol_code = $body_split[0]; |
||
251 | $body_parse = substr($body,1); |
||
252 | $body_parse_len = strlen($body_parse); |
||
253 | } else { |
||
254 | */ |
||
255 | if ($find) { |
||
256 | $body_split = str_split($body_parse); |
||
257 | $symbol_code = $body_split[0]; |
||
258 | //} |
||
259 | //echo $body_parse; |
||
260 | if ($type != ';' && $type != '>') { |
||
261 | $body_parse = substr($body_parse,1); |
||
262 | $body_parse_len = strlen($body_parse); |
||
263 | $result['symbol_code'] = $symbol_code; |
||
264 | if (isset($this->symbols[$symbol_code])) $result['symbol'] = $this->symbols[$symbol_code]; |
||
265 | if ($symbol_code != '_') { |
||
266 | } |
||
267 | //$body_parse = substr($body_parse,1); |
||
268 | //$body_parse = trim($body_parse); |
||
269 | //$body_parse_len = strlen($body_parse); |
||
270 | if ($body_parse_len >= 7) { |
||
271 | |||
272 | if (preg_match('/^([0-9\\. ]{3})\\/([0-9\\. ]{3})/',$body_parse)) { |
||
273 | $course = substr($body_parse,0,3); |
||
274 | $tmp_s = intval($course); |
||
275 | if ($tmp_s >= 1 && $tmp_s <= 360) $result['heading'] = intval($course); |
||
276 | $speed = substr($body_parse,4,3); |
||
277 | if ($speed != '...') { |
||
278 | $result['speed'] = round($speed*1.852); |
||
279 | } |
||
280 | $body_parse = substr($body_parse,7); |
||
281 | } |
||
282 | // Check PHGR, PHG, RNG |
||
283 | } |
||
284 | /* |
||
285 | else if ($body_parse_len > 0) { |
||
286 | $rest = $body_parse; |
||
287 | } |
||
288 | */ |
||
289 | if (strlen($body_parse) > 0) { |
||
290 | if (preg_match('/\\/A=(-[0-9]{5}|[0-9]{6})/',$body_parse,$matches)) { |
||
291 | $altitude = intval($matches[1]); |
||
292 | //$result['altitude'] = round($altitude*0.3048); |
||
293 | $result['altitude'] = $altitude; |
||
294 | //$body_parse = trim(substr($body_parse,strlen($matches[0]))); |
||
295 | $body_parse = trim(preg_replace('/\\/A=(-[0-9]{5}|[0-9]{6})/','',$body_parse)); |
||
296 | } |
||
297 | } |
||
298 | |||
299 | // Telemetry |
||
300 | /* |
||
301 | if (preg_match('/^([0-9]+),(-?)([0-9]{1,6}|[0-9]+\\.[0-9]+|\\.[0-9]+)?,(-?)([0-9]{1,6}|[0-9]+\\.[0-9]+|\\.[0-9]+)?,(-?)([0-9]{1,6}|[0-9]+\\.[0-9]+|\\.[0-9]+)?,(-?)([0-9]{1,6}|[0-9]+\\.[0-9]+|\\.[0-9]+)?,(-?)([0-9]{1,6}|[0-9]+\\.[0-9]+|\\.[0-9]+)?,([01]{0,8})/',$body_parse,$matches)) { |
||
302 | // Nothing yet... |
||
303 | } |
||
304 | */ |
||
305 | // DAO |
||
306 | if (preg_match('/^!([0-9A-Z]{3})/',$body_parse,$matches)) { |
||
307 | $dao = $matches[1]; |
||
308 | if (preg_match('/^([A-Z])([0-9]{2})/',$dao)) { |
||
309 | $dao_split = str_split($dao); |
||
310 | $lat_off = (($dao_split[1])-48.0)*0.001/60.0; |
||
311 | $lon_off = (($dao_split[2])-48.0)*0.001/60.0; |
||
312 | |||
313 | if ($result['latitude'] < 0) $result['latitude'] -= $lat_off; |
||
314 | else $result['latitude'] += $lat_off; |
||
315 | if ($result['longitude'] < 0) $result['longitude'] -= $lon_off; |
||
316 | else $result['longitude'] += $lon_off; |
||
317 | } |
||
318 | $body_parse = substr($body_parse,6); |
||
319 | } |
||
320 | |||
321 | if (preg_match('/CS=([0-9A-Z]*)/',$body_parse,$matches)) { |
||
322 | $result['callsign'] = $matches[1]; |
||
323 | } |
||
324 | if (preg_match('/SQ=([0-9]{4})/',$body_parse,$matches)) { |
||
325 | $result['squawk'] = $matches[1]; |
||
326 | } |
||
327 | if (preg_match('/AI=([0-9A-Z]{4})/',$body_parse,$matches)) { |
||
328 | $result['aircraft_icao'] = $matches[1]; |
||
329 | } |
||
330 | // OGN comment |
||
331 | // echo "Before OGN : ".$body_parse."\n"; |
||
332 | //if (preg_match('/^id([0-9A-F]{8}) ([+-])([0-9]{3,4})fpm ([+-])([0-9.]{3,4})rot (.*)$/',$body_parse,$matches)) { |
||
333 | if (preg_match('/^id([0-9A-F]{8})/',$body_parse,$matches)) { |
||
334 | $id = $matches[1]; |
||
335 | //$mode = substr($id,0,2); |
||
336 | $address = substr($id,2); |
||
337 | //print_r($matches); |
||
338 | $addressType = (intval(substr($id,0,2),16))&3; |
||
339 | if ($addressType == 0) $result['addresstype'] = "RANDOM"; |
||
340 | elseif ($addressType == 1) $result['addresstype'] = "ICAO"; |
||
341 | elseif ($addressType == 2) $result['addresstype'] = "FLARM"; |
||
342 | elseif ($addressType == 3) $result['addresstype'] = "OGN"; |
||
343 | $aircraftType = $this->urshift(((intval(substr($id,0,2),16)) & 0b1111100),2); |
||
344 | $result['aircrafttype_code'] = $aircraftType; |
||
345 | if ($aircraftType == 0) $result['aircrafttype'] = "UNKNOWN"; |
||
346 | elseif ($aircraftType == 1) $result['aircrafttype'] = "GLIDER"; |
||
347 | elseif ($aircraftType == 2) $result['aircrafttype'] = "TOW_PLANE"; |
||
348 | elseif ($aircraftType == 3) $result['aircrafttype'] = "HELICOPTER_ROTORCRAFT"; |
||
349 | elseif ($aircraftType == 4) $result['aircrafttype'] = "PARACHUTE"; |
||
350 | elseif ($aircraftType == 5) $result['aircrafttype'] = "DROP_PLANE"; |
||
351 | elseif ($aircraftType == 6) $result['aircrafttype'] = "HANG_GLIDER"; |
||
352 | elseif ($aircraftType == 7) $result['aircrafttype'] = "PARA_GLIDER"; |
||
353 | elseif ($aircraftType == 8) $result['aircrafttype'] = "POWERED_AIRCRAFT"; |
||
354 | elseif ($aircraftType == 9) $result['aircrafttype'] = "JET_AIRCRAFT"; |
||
355 | elseif ($aircraftType == 10) $result['aircrafttype'] = "UFO"; |
||
356 | elseif ($aircraftType == 11) $result['aircrafttype'] = "BALLOON"; |
||
357 | elseif ($aircraftType == 12) $result['aircrafttype'] = "AIRSHIP"; |
||
358 | elseif ($aircraftType == 13) $result['aircrafttype'] = "UAV"; |
||
359 | elseif ($aircraftType == 15) $result['aircrafttype'] = "STATIC_OBJECT"; |
||
360 | $stealth = (intval(substr($id,0,2), 16) & 0b10000000) != 0; |
||
361 | $result['stealth'] = $stealth; |
||
362 | $result['address'] = $address; |
||
363 | } |
||
364 | |||
365 | //Comment |
||
366 | $result['comment'] = trim($body_parse); |
||
367 | } else { |
||
368 | // parse weather |
||
369 | //$body_parse = substr($body_parse,1); |
||
370 | //$body_parse_len = strlen($body_parse); |
||
371 | |||
372 | if (preg_match('/^_{0,1}([0-9 \\.\\-]{3})\\/([0-9 \\.]{3})g([0-9 \\.]+)t(-{0,1}[0-9 \\.]+)/',$body_parse,$matches)) { |
||
373 | $result['wind_dir'] = intval($matches[1]); |
||
374 | $result['wind_speed'] = round(intval($matches[2])*1.60934,1); |
||
375 | $result['wind_gust'] = round(intval($matches[3])*1.60934,1); |
||
376 | $result['temp'] = round(5/9*((intval($matches[4]))-32),1); |
||
377 | $body_parse = substr($body_parse,strlen($matches[0])+1); |
||
378 | } elseif (preg_match('/^_{0,1}c([0-9 \\.\\-]{3})s([0-9 \\.]{3})g([0-9 \\.]+)t(-{0,1}[0-9 \\.]+)/',$body_parse,$matches)) { |
||
379 | $result['wind_dir'] = intval($matches[1]); |
||
380 | $result['wind_speed'] = round($matches[2]*1.60934,1); |
||
381 | $result['wind_gust'] = round($matches[3]*1.60934,1); |
||
382 | $result['temp'] = round(5/9*(($matches[4])-32),1); |
||
383 | $body_parse = substr($body_parse,strlen($matches[0])+1); |
||
384 | } elseif (preg_match('/^_{0,1}([0-9 \\.\\-]{3})\\/([0-9 \\.]{3})t(-{0,1}[0-9 \\.]+)/',$body_parse,$matches)) { |
||
385 | $result['wind_dir'] = intval($matches[1]); |
||
386 | $result['wind_speed'] = round($matches[2]*1.60934,1); |
||
387 | $result['wind_gust'] = round($matches[3]*1.60934,1); |
||
388 | $body_parse = substr($body_parse,strlen($matches[0])+1); |
||
389 | } elseif (preg_match('/^_{0,1}([0-9 \\.\\-]{3})\\/([0-9 \\.]{3})g([0-9 \\.]+)/',$body_parse,$matches)) { |
||
390 | $result['wind_dir'] = intval($matches[1]); |
||
391 | $result['wind_speed'] = round($matches[2]*1.60934,1); |
||
392 | $result['wind_gust'] = round($matches[3]*1.60934,1); |
||
393 | $body_parse = substr($body_parse,strlen($matches[0])+1); |
||
394 | } |
||
395 | if (!isset($result['temp']) && strlen($body_parse) > 0 && preg_match('/^g([0-9]+)t(-?[0-9 \\.]{1,3})/',$body_parse,$matches)) { |
||
396 | $result['temp'] = round(5/9*(($matches[1])-32),1); |
||
397 | } |
||
398 | } |
||
399 | } else $result['comment'] = trim($body_parse); |
||
400 | |||
401 | } |
||
402 | //} |
||
403 | if (isset($result['latitude'])) $result['latitude'] = round($result['latitude'],4); |
||
404 | if (isset($result['longitude'])) $result['longitude'] = round($result['longitude'],4); |
||
405 | if ($debug) print_r($result); |
||
406 | return $result; |
||
407 | } |
||
408 | |||
518 | ?> |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.