Conditions | 67 |
Paths | > 20000 |
Total Lines | 95 |
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 |
||
29 | function add($line) { |
||
30 | global $globalDebug, $globalServerUserID; |
||
31 | date_default_timezone_set('UTC'); |
||
32 | //if (isset($line['format_source']) && ($line['format_source'] === 'sbs' || $line['format_source'] === 'tsv' || $line['format_source'] === 'raw' || $line['format_source'] === 'deltadbtxt' || $line['format_source'] === 'aprs')) { |
||
33 | if (isset($line['format_source'])) { |
||
34 | if(is_array($line) && isset($line['hex'])) { |
||
35 | if ($line['hex'] != '' && $line['hex'] != '00000' && $line['hex'] != '000000' && $line['hex'] != '111111' && ctype_xdigit($line['hex']) && strlen($line['hex']) === 6) { |
||
36 | $data['hex'] = trim($line['hex']); |
||
37 | if (preg_match('/^(\d{4}(?:\-\d{2}){2} \d{2}(?:\:\d{2}){2})$/',$line['datetime'])) { |
||
38 | $data['datetime'] = $line['datetime']; |
||
39 | } else $data['datetime'] = date('Y-m-d H:i:s'); |
||
40 | if (!isset($line['aircraft_icao'])) { |
||
41 | $Spotter = new Spotter(); |
||
42 | $aircraft_icao = $Spotter->getAllAircraftType($data['hex']); |
||
43 | $Spotter->db = null; |
||
44 | if ($aircraft_icao == '' && isset($line['aircraft_type'])) { |
||
45 | if ($line['aircraft_type'] == 'PARA_GLIDER') $aircraft_icao = 'GLID'; |
||
46 | elseif ($line['aircraft_type'] == 'HELICOPTER_ROTORCRAFT') $aircraft_icao = 'UHEL'; |
||
47 | elseif ($line['aircraft_type'] == 'TOW_PLANE') $aircraft_icao = 'TOWPLANE'; |
||
48 | elseif ($line['aircraft_type'] == 'POWERED_AIRCRAFT') $aircraft_icao = 'POWAIRC'; |
||
49 | } |
||
50 | $data['aircraft_icao'] = $aircraft_icao; |
||
51 | } else $data['aircraft_icao'] = $line['aircraft_icao']; |
||
52 | //if ($globalDebug) echo "*********** New aircraft hex : ".$data['hex']." ***********\n"; |
||
53 | } |
||
54 | if (isset($line['registration']) && $line['registration'] != '') { |
||
55 | $data['registration'] = $line['registration']; |
||
56 | } else $data['registration'] = null; |
||
57 | if (isset($line['waypoints']) && $line['waypoints'] != '') { |
||
58 | $data['waypoints'] = $line['waypoints']; |
||
59 | } else $data['waypoints'] = null; |
||
60 | if (isset($line['ident']) && $line['ident'] != '' && $line['ident'] != '????????' && $line['ident'] != '00000000' && preg_match('/^[a-zA-Z0-9]+$/', $line['ident'])) { |
||
61 | $data['ident'] = trim($line['ident']); |
||
62 | } else $data['ident'] = null; |
||
63 | if (isset($line['latitude']) && isset($line['longitude']) && $line['latitude'] != '' && $line['longitude'] != '') { |
||
64 | if (isset($line['latitude']) && $line['latitude'] != '' && $line['latitude'] != 0 && $line['latitude'] < 91 && $line['latitude'] > -90) { |
||
65 | $data['latitude'] = $line['latitude']; |
||
66 | } else $data['latitude'] = null; |
||
67 | if (isset($line['longitude']) && $line['longitude'] != '' && $line['longitude'] != 0 && $line['longitude'] < 360 && $line['longitude'] > -180) { |
||
68 | if ($line['longitude'] > 180) $line['longitude'] = $line['longitude'] - 360; |
||
69 | $data['longitude'] = $line['longitude']; |
||
70 | } else $data['longitude'] = null; |
||
71 | } else { |
||
72 | $data['latitude'] = null; |
||
73 | $data['longitude'] = null; |
||
74 | } |
||
75 | if (isset($line['verticalrate']) && $line['verticalrate'] != '') { |
||
76 | $data['verticalrate'] = $line['verticalrate']; |
||
77 | } else $data['verticalrate'] = null; |
||
78 | if (isset($line['emergency']) && $line['emergency'] != '') { |
||
79 | $data['emergency'] = $line['emergency']; |
||
80 | } else $data['emergency'] = null; |
||
81 | if (isset($line['ground']) && $line['ground'] != '') { |
||
82 | $data['ground'] = $line['ground']; |
||
83 | } else $data['ground'] = null; |
||
84 | if (isset($line['speed']) && $line['speed'] != '') { |
||
85 | $data['speed'] = round($line['speed']); |
||
86 | } else $data['speed'] = null; |
||
87 | if (isset($line['squawk']) && $line['squawk'] != '') { |
||
88 | $data['squawk'] = $line['squawk']; |
||
89 | } else $data['squawk'] = null; |
||
90 | if (isset($line['altitude']) && $line['altitude'] != '') { |
||
91 | $data['altitude'] = round($line['altitude']); |
||
92 | } else $data['altitude'] = null; |
||
93 | if (isset($line['heading']) && $line['heading'] != '') { |
||
94 | $data['heading'] = round($line['heading']); |
||
95 | } else $data['heading'] = null; |
||
96 | if (isset($line['source_name']) && $line['source_name'] != '') { |
||
97 | $data['source_name'] = $line['source_name']; |
||
98 | } else $data['source_name'] = null; |
||
99 | if (isset($line['over_country']) && $line['over_country'] != '') { |
||
100 | $data['over_country'] = $line['over_country']; |
||
101 | } else $data['over_country'] = null; |
||
102 | if (isset($line['noarchive']) && $line['noarchive']) { |
||
103 | $data['noarchive'] = true; |
||
104 | } else $data['noarchive'] = false; |
||
105 | $data['format_source'] = $line['format_source']; |
||
106 | if (isset($line['id_source'])) $id_source = $line['id_source']; |
||
107 | if (isset($data['hex'])) { |
||
108 | echo '.'; |
||
109 | $id_user = $globalServerUserID; |
||
110 | if ($id_user == NULL) $id_user = 1; |
||
111 | if (!isset($id_source)) $id_source = 1; |
||
112 | $query = 'INSERT INTO spotter_temp (id_user,datetime,hex,ident,latitude,longitude,verticalrate,speed,squawk,altitude,heading,registration,aircraft_icao,waypoints,id_source,noarchive,format_source,source_name,over_country) VALUES (:id_user,:datetime,:hex,:ident,:latitude,:longitude,:verticalrate,:speed,:squawk,:altitude,:heading,:registration,:aircraft_icao,:waypoints,:id_source,:noarchive, :format_source, :source_name, :over_country)'; |
||
113 | $query_values = array(':id_user' => $id_user,':datetime' => $data['datetime'],':hex' => $data['hex'],':ident' => $data['ident'],':latitude' => $data['latitude'],':longitude' => $data['longitude'],':verticalrate' => $data['verticalrate'],':speed' => $data['speed'],':squawk' => $data['squawk'],':altitude' => $data['altitude'],':heading' => $data['heading'],':registration' => $data['registration'],':aircraft_icao' => $data['aircraft_icao'],':waypoints' => $data['waypoints'],':id_source' => $id_source,':noarchive' => $data['noarchive'], ':format_source' => $data['format_source'], ':source_name' => $data['source_name'],':over_country' => $data['over_country']); |
||
114 | try { |
||
115 | $sth = $this->dbs['server']->prepare($query); |
||
116 | $sth->execute($query_values); |
||
117 | } catch(PDOException $e) { |
||
118 | return "error : ".$e->getMessage(); |
||
119 | } |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | } |
||
126 |
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.