| Conditions | 86 |
| Paths | > 20000 |
| Total Lines | 338 |
| 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'] = 'modes'; |
||
| 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'] == 'modes') { |
||
| 162 | $result['address'] = 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 | if ($type != '') { |
||
| 273 | $body_parse = substr($body_parse,1); |
||
| 274 | $body_parse_len = strlen($body_parse); |
||
| 275 | $result['symbol_code'] = $symbol_code; |
||
| 276 | if (isset($this->symbols[$symbol_code])) $result['symbol'] = $this->symbols[$symbol_code]; |
||
| 277 | if ($symbol_code != '_') { |
||
| 278 | } |
||
| 279 | //$body_parse = substr($body_parse,1); |
||
| 280 | //$body_parse = trim($body_parse); |
||
| 281 | //$body_parse_len = strlen($body_parse); |
||
| 282 | if ($body_parse_len >= 7) { |
||
| 283 | |||
| 284 | if (preg_match('/^([0-9\\. ]{3})\\/([0-9\\. ]{3})/',$body_parse)) { |
||
| 285 | $course = substr($body_parse,0,3); |
||
| 286 | $tmp_s = intval($course); |
||
| 287 | if ($tmp_s >= 1 && $tmp_s <= 360) $result['heading'] = intval($course); |
||
| 288 | $speed = substr($body_parse,4,3); |
||
| 289 | if ($speed != '...') { |
||
| 290 | //$result['speed'] = round($speed*1.852); |
||
| 291 | $result['speed'] = intval($speed); |
||
| 292 | } |
||
| 293 | $body_parse = substr($body_parse,7); |
||
| 294 | } |
||
| 295 | // Check PHGR, PHG, RNG |
||
| 296 | } |
||
| 297 | /* |
||
| 298 | else if ($body_parse_len > 0) { |
||
| 299 | $rest = $body_parse; |
||
| 300 | } |
||
| 301 | */ |
||
| 302 | if (strlen($body_parse) > 0) { |
||
| 303 | if (preg_match('/\\/A=(-[0-9]{5}|[0-9]{6})/',$body_parse,$matches)) { |
||
| 304 | $altitude = intval($matches[1]); |
||
| 305 | //$result['altitude'] = round($altitude*0.3048); |
||
| 306 | $result['altitude'] = $altitude; |
||
| 307 | //$body_parse = trim(substr($body_parse,strlen($matches[0]))); |
||
| 308 | $body_parse = trim(preg_replace('/\\/A=(-[0-9]{5}|[0-9]{6})/','',$body_parse)); |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | // Telemetry |
||
| 313 | /* |
||
| 314 | 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)) { |
||
| 315 | // Nothing yet... |
||
| 316 | } |
||
| 317 | */ |
||
| 318 | // DAO |
||
| 319 | |||
| 320 | if (preg_match('/^!([0-9A-Z]{3})/',$body_parse,$matches)) { |
||
| 321 | |||
| 322 | $dao = $matches[1]; |
||
| 323 | if (preg_match('/^([A-Z])([0-9]{2})/',$dao)) { |
||
| 324 | $dao_split = str_split($dao); |
||
| 325 | $lat_off = (($dao_split[1])-48.0)*0.001/60.0; |
||
| 326 | $lon_off = (($dao_split[2])-48.0)*0.001/60.0; |
||
| 327 | |||
| 328 | if ($result['latitude'] < 0) $result['latitude'] -= $lat_off; |
||
| 329 | else $result['latitude'] += $lat_off; |
||
| 330 | if ($result['longitude'] < 0) $result['longitude'] -= $lon_off; |
||
| 331 | else $result['longitude'] += $lon_off; |
||
| 332 | } |
||
| 333 | |||
| 334 | $body_parse = substr($body_parse,6); |
||
| 335 | } |
||
| 336 | |||
| 337 | if (preg_match('/CS=([0-9A-Z]*)/',$body_parse,$matches)) { |
||
| 338 | $result['ident'] = $matches[1]; |
||
| 339 | } |
||
| 340 | if (preg_match('/SQ=([0-9]{4})/',$body_parse,$matches)) { |
||
| 341 | $result['squawk'] = $matches[1]; |
||
| 342 | } |
||
| 343 | if (preg_match('/AI=([0-9A-Z]{4})/',$body_parse,$matches)) { |
||
| 344 | $result['aircraft_icao'] = $matches[1]; |
||
| 345 | } |
||
| 346 | if (preg_match('/TI=([0-9]*)/',$body_parse,$matches)) { |
||
| 347 | $result['typeid'] = $matches[1]; |
||
| 348 | } |
||
| 349 | if (preg_match('/IMO=([0-9]{7})/',$body_parse,$matches)) { |
||
| 350 | $result['imo'] = $matches[1]; |
||
| 351 | } |
||
| 352 | if (preg_match('/AD=([0-9]*)/',$body_parse,$matches)) { |
||
| 353 | $result['arrival_date'] = $matches[1]; |
||
| 354 | } |
||
| 355 | if (preg_match('/AC=([0-9A-Z]*)/',$body_parse,$matches)) { |
||
| 356 | $result['arrival_code'] = $matches[1]; |
||
| 357 | } |
||
| 358 | // OGN comment |
||
| 359 | // echo "Before OGN : ".$body_parse."\n"; |
||
| 360 | //if (preg_match('/^id([0-9A-F]{8}) ([+-])([0-9]{3,4})fpm ([+-])([0-9.]{3,4})rot (.*)$/',$body_parse,$matches)) { |
||
| 361 | if (preg_match('/^id([0-9A-F]{8})/',$body_parse,$matches)) { |
||
| 362 | $id = $matches[1]; |
||
| 363 | //$mode = substr($id,0,2); |
||
| 364 | $address = substr($id,2); |
||
| 365 | //print_r($matches); |
||
| 366 | $addressType = (intval(substr($id,0,2),16))&3; |
||
| 367 | if ($addressType == 0) $result['addresstype'] = "RANDOM"; |
||
| 368 | elseif ($addressType == 1) $result['addresstype'] = "ICAO"; |
||
| 369 | elseif ($addressType == 2) $result['addresstype'] = "FLARM"; |
||
| 370 | elseif ($addressType == 3) $result['addresstype'] = "OGN"; |
||
| 371 | $aircraftType = $this->urshift(((intval(substr($id,0,2),16)) & 0b1111100),2); |
||
| 372 | $result['aircrafttype_code'] = $aircraftType; |
||
| 373 | if ($aircraftType == 0) $result['aircrafttype'] = "UNKNOWN"; |
||
| 374 | elseif ($aircraftType == 1) $result['aircrafttype'] = "GLIDER"; |
||
| 375 | elseif ($aircraftType == 2) $result['aircrafttype'] = "TOW_PLANE"; |
||
| 376 | elseif ($aircraftType == 3) $result['aircrafttype'] = "HELICOPTER_ROTORCRAFT"; |
||
| 377 | elseif ($aircraftType == 4) $result['aircrafttype'] = "PARACHUTE"; |
||
| 378 | elseif ($aircraftType == 5) $result['aircrafttype'] = "DROP_PLANE"; |
||
| 379 | elseif ($aircraftType == 6) $result['aircrafttype'] = "HANG_GLIDER"; |
||
| 380 | elseif ($aircraftType == 7) $result['aircrafttype'] = "PARA_GLIDER"; |
||
| 381 | elseif ($aircraftType == 8) $result['aircrafttype'] = "POWERED_AIRCRAFT"; |
||
| 382 | elseif ($aircraftType == 9) $result['aircrafttype'] = "JET_AIRCRAFT"; |
||
| 383 | elseif ($aircraftType == 10) $result['aircrafttype'] = "UFO"; |
||
| 384 | elseif ($aircraftType == 11) $result['aircrafttype'] = "BALLOON"; |
||
| 385 | elseif ($aircraftType == 12) $result['aircrafttype'] = "AIRSHIP"; |
||
| 386 | elseif ($aircraftType == 13) $result['aircrafttype'] = "UAV"; |
||
| 387 | elseif ($aircraftType == 15) $result['aircrafttype'] = "STATIC_OBJECT"; |
||
| 388 | $stealth = (intval(substr($id,0,2), 16) & 0b10000000) != 0; |
||
| 389 | $result['stealth'] = $stealth; |
||
| 390 | $result['address'] = $address; |
||
| 391 | } |
||
| 392 | |||
| 393 | //Comment |
||
| 394 | $result['comment'] = trim($body_parse); |
||
| 395 | } else { |
||
| 396 | // parse weather |
||
| 397 | //$body_parse = substr($body_parse,1); |
||
| 398 | //$body_parse_len = strlen($body_parse); |
||
| 399 | |||
| 400 | if (preg_match('/^_{0,1}([0-9 \\.\\-]{3})\\/([0-9 \\.]{3})g([0-9 \\.]+)t(-{0,1}[0-9 \\.]+)/',$body_parse,$matches)) { |
||
| 401 | $result['wind_dir'] = intval($matches[1]); |
||
| 402 | $result['wind_speed'] = round(intval($matches[2])*1.60934,1); |
||
| 403 | $result['wind_gust'] = round(intval($matches[3])*1.60934,1); |
||
| 404 | $result['temp'] = round(5/9*((intval($matches[4]))-32),1); |
||
| 405 | $body_parse = substr($body_parse,strlen($matches[0])+1); |
||
| 406 | } elseif (preg_match('/^_{0,1}c([0-9 \\.\\-]{3})s([0-9 \\.]{3})g([0-9 \\.]+)t(-{0,1}[0-9 \\.]+)/',$body_parse,$matches)) { |
||
| 407 | $result['wind_dir'] = intval($matches[1]); |
||
| 408 | $result['wind_speed'] = round($matches[2]*1.60934,1); |
||
| 409 | $result['wind_gust'] = round($matches[3]*1.60934,1); |
||
| 410 | $result['temp'] = round(5/9*(($matches[4])-32),1); |
||
| 411 | $body_parse = substr($body_parse,strlen($matches[0])+1); |
||
| 412 | } elseif (preg_match('/^_{0,1}([0-9 \\.\\-]{3})\\/([0-9 \\.]{3})t(-{0,1}[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 | } elseif (preg_match('/^_{0,1}([0-9 \\.\\-]{3})\\/([0-9 \\.]{3})g([0-9 \\.]+)/',$body_parse,$matches)) { |
||
| 418 | $result['wind_dir'] = intval($matches[1]); |
||
| 419 | $result['wind_speed'] = round($matches[2]*1.60934,1); |
||
| 420 | $result['wind_gust'] = round($matches[3]*1.60934,1); |
||
| 421 | $body_parse = substr($body_parse,strlen($matches[0])+1); |
||
| 422 | } |
||
| 423 | if (!isset($result['temp']) && strlen($body_parse) > 0 && preg_match('/^g([0-9]+)t(-?[0-9 \\.]{1,3})/',$body_parse,$matches)) { |
||
| 424 | $result['temp'] = round(5/9*(($matches[1])-32),1); |
||
| 425 | } |
||
| 426 | } |
||
| 427 | } else $result['comment'] = trim($body_parse); |
||
| 428 | |||
| 429 | } |
||
| 430 | //} |
||
| 431 | if (isset($result['latitude'])) $result['latitude'] = round($result['latitude'],4); |
||
| 432 | if (isset($result['longitude'])) $result['longitude'] = round($result['longitude'],4); |
||
| 433 | if ($debug) print_r($result); |
||
| 434 | return $result; |
||
| 435 | } |
||
| 436 | |||
| 555 | ?> |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.