| Conditions | 37 |
| Paths | 525 |
| Total Lines | 111 |
| Code Lines | 84 |
| 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 |
||
| 5 | public function parse($buffer) { |
||
| 6 | // Not yet finished, no CRC checks |
||
| 7 | //echo $buffer."\n"; |
||
| 8 | $data = array(); |
||
| 9 | $typehex = substr($buffer,0,1); |
||
| 10 | if ($typehex == '*' || $typehex == ':') $hex = substr($buffer,1,-1); |
||
| 11 | elseif ($typehex == '@' || $typehex == '%') $hex = substr($buffer,13,-13); |
||
| 12 | else $hex = substr($buffer,1,-1); |
||
| 13 | $bin = gmp_strval( gmp_init($hex,16), 2); |
||
| 14 | //if (strlen($hex) == 28 && $this->parityCheck($hex,$bin)) { |
||
| 15 | if (strlen($hex) == 28) { |
||
| 16 | $df = intval(substr($bin,0,5),2); |
||
| 17 | //$ca = intval(substr($bin,5,3),2); |
||
| 18 | // Only support DF17 for now |
||
| 19 | //if ($df == 17 || ($df == 18 && ($ca == 0 || $ca == 1 || $ca == 6))) { |
||
| 20 | if (($df == 17 || $df == 18) && ($this->parityCheck($hex,$bin) || $typehex == '@')) { |
||
| 21 | $icao = substr($hex,2,6); |
||
| 22 | $data['hex'] = $icao; |
||
| 23 | $tc = intval(substr($bin,32,5),2); |
||
| 24 | if ($tc >= 1 && $tc <= 4) { |
||
| 25 | //callsign |
||
| 26 | $csbin = substr($bin,40,56); |
||
| 27 | $charset = str_split('#ABCDEFGHIJKLMNOPQRSTUVWXYZ#####_###############0123456789######'); |
||
| 28 | $cs = ''; |
||
| 29 | $cs .= $charset[intval(substr($csbin,0,6),2)]; |
||
| 30 | $cs .= $charset[intval(substr($csbin,6,6),2)]; |
||
| 31 | $cs .= $charset[intval(substr($csbin,12,6),2)]; |
||
| 32 | $cs .= $charset[intval(substr($csbin,18,6),2)]; |
||
| 33 | $cs .= $charset[intval(substr($csbin,24,6),2)]; |
||
| 34 | $cs .= $charset[intval(substr($csbin,30,6),2)]; |
||
| 35 | $cs .= $charset[intval(substr($csbin,36,6),2)]; |
||
| 36 | $cs .= $charset[intval(substr($csbin,42,6),2)]; |
||
| 37 | $cs = str_replace('_','',$cs); |
||
| 38 | $cs = str_replace('#','',$cs); |
||
| 39 | $callsign = $cs; |
||
| 40 | $data['ident'] = $callsign; |
||
| 41 | } elseif ($tc >= 9 && $tc <= 18) { |
||
| 42 | // Check Q-bit |
||
| 43 | $q = substr($bin,47,1); |
||
| 44 | if ($q) { |
||
| 45 | $n = intval(substr($bin,40,7).substr($bin,48,4),2); |
||
| 46 | $alt = $n*25-1000; |
||
| 47 | $data['altitude'] = $alt; |
||
| 48 | } |
||
| 49 | // Check odd/even flag |
||
| 50 | $oe = substr($bin,53,1); |
||
| 51 | //if ($oe) => odd else even |
||
| 52 | // 131072 is 2^17 since CPR latitude and longitude are encoded in 17 bits. |
||
| 53 | $cprlat = intval(substr($bin,54,17),2)/131072.0; |
||
| 54 | $cprlon = intval(substr($bin,71,17),2)/131072.0; |
||
| 55 | if ($oe == 0) $this::$latlon[$icao] = array('latitude' => $cprlat,'longitude' => $cprlon,'created' => time()); |
||
| 56 | elseif (isset($this::$latlon[$icao]) && (time() - $this::$latlon[$icao]['created']) < 10) { |
||
| 57 | $cprlat_odd = $cprlat; |
||
| 58 | $cprlon_odd = $cprlon; |
||
| 59 | $cprlat_even = $this::$latlon[$icao]['latitude']; |
||
| 60 | $cprlon_even = $this::$latlon[$icao]['longitude']; |
||
| 61 | |||
| 62 | $j = 59*$cprlat_even-60*$cprlat_odd+0.5; |
||
| 63 | $lat_even = (360.0/60)*($j%60+$cprlat_even); |
||
| 64 | $lat_odd = (360.0/59)*($j%59+$cprlat_odd); |
||
| 65 | if ($lat_even >= 270) $lat_even = $lat_even - 360; |
||
| 66 | if ($lat_odd >= 270) $lat_odd = $lat_odd - 360; |
||
| 67 | // check latitude zone |
||
| 68 | if ($this->cprNL($lat_even) == $this->cprNL($lat_odd)) { |
||
| 69 | if ($this::$latlon[$icao]['created'] > time()) { |
||
| 70 | $ni = $this->cprN($lat_even,0); |
||
| 71 | $m = floor($cprlon_even*($this->cprNL($lat_even)-1) - $cprlon_odd * $this->cprNL($lat_even)+0.5); |
||
| 72 | $lon = (360.0/$ni)*($m%$ni+$cprlon_even); |
||
| 73 | $lat = $lat_even; |
||
| 74 | if ($lon > 180) $lon = $lon -360; |
||
| 75 | if ($lat > -91 && $lat < 91 && $lon > -181 && $lon < 181) { |
||
| 76 | //if ($globalDebug) echo 'cs : '.$cs.' - hex : '.$hex.' - lat : '.$lat.' - lon : '.$lon; |
||
| 77 | $data['latitude'] = $lat; |
||
| 78 | $data['longitude'] = $lon; |
||
| 79 | } |
||
| 80 | } else { |
||
| 81 | $ni = $this->cprN($lat_odd,1); |
||
| 82 | $m = floor($cprlon_even*($this->cprNL($lat_odd)-1) - $cprlon_odd * $this->cprNL($lat_odd)+0.5); |
||
| 83 | $lon = (360.0/$ni)*($m%$ni+$cprlon_odd); |
||
| 84 | $lat = $lat_odd; |
||
| 85 | if ($lon > 180) $lon = $lon -360; |
||
| 86 | if ($lat > -91 && $lat < 91 && $lon > -181 && $lon < 181) { |
||
| 87 | //if ($globalDebug) echo 'icao : '.$icao.' - hex : '.$hex.' - lat : '.$lat.' - lon : '.$lon.' second'."\n"; |
||
| 88 | $data['latitude'] = $lat; |
||
| 89 | $data['longitude'] = $lon; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | } else echo "Not cprNL"; |
||
| 93 | unset($this::$latlon[$icao]); |
||
| 94 | } |
||
| 95 | } elseif ($tc == 19) { |
||
| 96 | // speed & heading |
||
| 97 | $v_ew_dir = intval(substr($bin,45,1)); |
||
| 98 | $v_ew = intval(substr($bin,46,10),2); |
||
| 99 | $v_ns_dir = intval(substr($bin,56,1)); |
||
| 100 | $v_ns = intval(substr($bin,57,10),2); |
||
| 101 | if ($v_ew_dir) $v_ew = -1*$v_ew; |
||
| 102 | if ($v_ns_dir) $v_ns = -1*$v_ns; |
||
| 103 | $speed = sqrt($v_ns*$v_ns+$v_ew*$v_ew); |
||
| 104 | $heading = atan2($v_ew,$v_ns)*360.0/(2*pi()); |
||
| 105 | if ($heading <0) $heading = $heading+360; |
||
| 106 | $data['speed'] = $speed; |
||
| 107 | $data['heading'] = $heading; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | if (isset($data)) { |
||
| 111 | //print_r($data); |
||
| 112 | return $data; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 283 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.