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