Complex classes like Accident often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Accident, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Accident { |
||
| 8 | public $db; |
||
| 9 | |||
| 10 | public function __construct($dbc = null) { |
||
| 11 | $Connection = new Connection($dbc); |
||
| 12 | $this->db = $Connection->db(); |
||
| 13 | } |
||
| 14 | |||
| 15 | |||
| 16 | public function get() { |
||
| 17 | $query = 'SELECT DISTINCT registration FROM accidents ORDER BY date DESC'; |
||
| 18 | $sth = $this->db->prepare($query); |
||
| 19 | $sth->execute(); |
||
| 20 | $result = $sth->fetchAll(PDO::FETCH_ASSOC); |
||
| 21 | return $result; |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Get Accidents data from DB |
||
| 26 | * |
||
| 27 | * @return Array Return Accidents data in array |
||
| 28 | */ |
||
| 29 | public function getAccidentData($limit = '',$type = '',$date = '') { |
||
| 30 | global $globalURL, $globalDBdriver; |
||
| 31 | $Image = new Image($this->db); |
||
| 32 | $Spotter = new Spotter($this->db); |
||
| 33 | $Translation = new Translation($this->db); |
||
| 34 | $date = filter_var($date,FILTER_SANITIZE_STRING); |
||
| 35 | date_default_timezone_set('UTC'); |
||
| 36 | $result = array(); |
||
| 37 | $limit_query = ''; |
||
| 38 | if ($limit != "") |
||
| 39 | { |
||
| 40 | $limit_array = explode(",", $limit); |
||
| 41 | $limit_array[0] = filter_var($limit_array[0],FILTER_SANITIZE_NUMBER_INT); |
||
| 42 | $limit_array[1] = filter_var($limit_array[1],FILTER_SANITIZE_NUMBER_INT); |
||
| 43 | if ($limit_array[0] >= 0 && $limit_array[1] >= 0) |
||
| 44 | { |
||
| 45 | $limit_query = " LIMIT ".$limit_array[1]." OFFSET ".$limit_array[0]; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | if ($type != '') { |
||
| 50 | if ($date != '') { |
||
| 51 | if (preg_match("/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/",$date)) { |
||
| 52 | $query = "SELECT * FROM accidents WHERE accidents_id IN (SELECT max(accidents_id) FROM accidents WHERE type = :type AND date = :date GROUP BY registration) ORDER BY date DESC".$limit_query; |
||
| 53 | } else { |
||
| 54 | $date = $date.'%'; |
||
| 55 | $query = "SELECT * FROM accidents WHERE accidents_id IN (SELECT max(accidents_id) FROM accidents WHERE type = :type AND to_char(date,'YYYY-MM-DD') LIKE :date GROUP BY registration) ORDER BY date DESC".$limit_query; |
||
| 56 | } |
||
| 57 | $query_values = array(':type' => $type,':date' => $date); |
||
| 58 | } else { |
||
| 59 | $query = "SELECT * FROM accidents WHERE accidents_id IN (SELECT max(accidents_id) FROM accidents WHERE type = :type GROUP BY registration) ORDER BY date DESC".$limit_query; |
||
| 60 | $query_values = array(':type' => $type); |
||
| 61 | } |
||
| 62 | } else { |
||
| 63 | if ($date != '') { |
||
| 64 | if (preg_match("/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/",$date)) { |
||
| 65 | $query = "SELECT * FROM accidents WHERE accidents_id IN (SELECT max(accidents_id) FROM accidents WHERE date = :date GROUP BY registration) ORDER BY date DESC".$limit_query; |
||
| 66 | } else { |
||
| 67 | $date = $date.'%'; |
||
| 68 | $query = "SELECT * FROM accidents WHERE accidents_id IN (SELECT max(accidents_id) FROM accidents WHERE to_char(date,'YYYY-MM-DD') LIKE :date GROUP BY registration) ORDER BY date DESC".$limit_query; |
||
| 69 | } |
||
| 70 | $query_values = array(':date' => $date); |
||
| 71 | } else { |
||
| 72 | $query = "SELECT * FROM accidents WHERE accidents_id IN (SELECT max(accidents_id) FROM accidents GROUP BY registration) ORDER BY date DESC".$limit_query; |
||
| 73 | $query_values = array(); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | try { |
||
| 78 | $sth = $this->db->prepare($query); |
||
| 79 | $sth->execute($query_values); |
||
| 80 | } catch(PDOException $e) { |
||
| 81 | return "error : ".$e->getMessage(); |
||
| 82 | } |
||
| 83 | $i = 0; |
||
| 84 | while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { |
||
| 85 | $data = array(); |
||
| 86 | if ($row['registration'] != '') { |
||
| 87 | $image_array = $Image->getSpotterImage($row['registration']); |
||
| 88 | if (count($image_array) > 0) $data = array_merge($data,array('image' => $image_array[0]['image'],'image_thumbnail' => $image_array[0]['image_thumbnail'],'image_copyright' => $image_array[0]['image_copyright'],'image_source' => $image_array[0]['image_source'],'image_source_website' => $image_array[0]['image_source_website'])); |
||
| 89 | else $data = array_merge($data,array('image' => '','image_thumbnail' => '','image_copyright' => '','image_source' => '','image_source_website' => '')); |
||
| 90 | $aircraft_type = $Spotter->getAllAircraftTypeByRegistration($row['registration']); |
||
| 91 | $aircraft_info = $Spotter->getAllAircraftInfo($aircraft_type); |
||
| 92 | if (!empty($aircraft_info)) { |
||
| 93 | $data['aircraft_type'] = $aircraft_info[0]['icao']; |
||
| 94 | $data['aircraft_name'] = $aircraft_info[0]['type']; |
||
| 95 | $data['aircraft_manufacturer'] = $aircraft_info[0]['manufacturer']; |
||
| 96 | } else { |
||
| 97 | $data = array_merge($data,array('aircraft_type' => 'NA')); |
||
| 98 | } |
||
| 99 | $owner_data = $Spotter->getAircraftOwnerByRegistration($row['registration']); |
||
| 100 | if (!empty($owner_data)) { |
||
| 101 | $data['aircraft_owner'] = $owner_data['owner']; |
||
| 102 | $data['aircraft_base'] = $owner_data['base']; |
||
| 103 | $data['aircraft_date_first_reg'] = $owner_data['date_first_reg']; |
||
| 104 | } |
||
| 105 | } else $data = array_merge($data,array('image' => '','image_thumbnail' => '','image_copyright' => '','image_source' => '','image_source_website' => '')); |
||
| 106 | if ($row['registration'] == '') $row['registration'] = 'NA'; |
||
| 107 | if ($row['ident'] == '') $row['ident'] = 'NA'; |
||
| 108 | $identicao = $Spotter->getAllAirlineInfo(substr($row['ident'],0,2)); |
||
| 109 | if (isset($identicao[0])) { |
||
| 110 | if (substr($row['ident'],0,2) == 'AF') { |
||
| 111 | if (filter_var(substr($row['ident'],2),FILTER_VALIDATE_INT,array("flags"=>FILTER_FLAG_ALLOW_OCTAL))) $icao = $row['ident']; |
||
| 112 | else $icao = 'AFR'.ltrim(substr($row['ident'],2),'0'); |
||
| 113 | } else $icao = $identicao[0]['icao'].ltrim(substr($row['ident'],2),'0'); |
||
| 114 | $data = array_merge($data,array('airline_icao' => $identicao[0]['icao'],'airline_name' => $identicao[0]['name'])); |
||
| 115 | } else $icao = $row['ident']; |
||
| 116 | $icao = $Translation->checkTranslation($icao,false); |
||
| 117 | //$data = array_merge($data,array('registration' => $row['registration'], 'date' => $row['date'], 'ident' => $icao,'url' => $row['url'])); |
||
| 118 | $data = array_merge($row,$data); |
||
| 119 | if ($data['ident'] == null) $data['ident'] = $icao; |
||
| 120 | if ($data['title'] == null) { |
||
| 121 | $data['message'] = $row['type'].' of '.$row['registration'].' at '.$row['place'].','.$row['country']; |
||
| 122 | } else $data['message'] = strtolower($data['title']); |
||
| 123 | $result[] = $data; |
||
| 124 | $i++; |
||
| 125 | } |
||
| 126 | if (isset($result)) { |
||
| 127 | $result[0]['query_number_rows'] = $i; |
||
| 128 | return $result; |
||
| 129 | } |
||
| 130 | else return array(); |
||
| 131 | } |
||
| 132 | |||
| 133 | |||
| 134 | /* |
||
| 135 | * Import csv accidents file into the DB |
||
| 136 | * @param String $file filename of the file to import |
||
| 137 | */ |
||
| 138 | public function import($file) { |
||
| 139 | global $globalTransaction, $globalDebug; |
||
| 140 | if ($globalDebug) echo 'Import '.$file."\n"; |
||
| 141 | $result = array(); |
||
| 142 | if (file_exists($file)) { |
||
| 143 | if (($handle = fopen($file,'r')) !== FALSE) { |
||
| 144 | while (($data = fgetcsv($handle,2000,",")) !== FALSE) { |
||
| 145 | if (isset($data[1]) && $data[1] != '0000-00-00 00:00:00') { |
||
| 146 | $result[] = array('registration' => $data[0],'date' => strtotime($data[1]),'url' => $data[2],'country' => $data[3],'place' => $data[4],'title' => $data[5],'fatalities' => $data[6],'latitude' => $data[7],'longitude' => $data[8],'type' => $data[9],'ident' => $data[10],'aircraft_manufacturer' => $data[11],'aircraft_name' => $data[12],'source' => 'website_fam'); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | fclose($handle); |
||
| 150 | } |
||
| 151 | if (!empty($result)) $this->add($result,true); |
||
| 152 | elseif ($globalDebug) echo 'Nothing to import'; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | public function download_update() { |
||
| 157 | global $globalDebug, |
||
| 158 | require_once('class.Common.php'); |
||
|
|
|||
| 159 | $Common = new Common(); |
||
| 160 | $all_md5 = array(); |
||
| 161 | $all_md5_new = array(); |
||
| 162 | if (file_exists(dirname(__FILE__).'/../install/tmp/cr-all.md5')) { |
||
| 163 | if ($this->check_accidents_nb() > 0) { |
||
| 164 | if (($handle = fopen(dirname(__FILE__).'/../install/tmp/cr-all.md5','r')) !== FALSE) { |
||
| 165 | while (($data = fgetcsv($handle,2000,"\t")) !== FALSE) { |
||
| 166 | if (isset($data[1])) { |
||
| 167 | $year = $data[0]; |
||
| 168 | $all_md5[$year] = $data[1]; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | fclose($handle); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 | $Common->download('https://data.flightairmap.fr/data/cr/cr-all.md5',dirname(__FILE__).'/../install/tmp/cr-all.md5'); |
||
| 176 | if (file_exists(dirname(__FILE__).'/../install/tmp/cr-all.md5')) { |
||
| 177 | if (($handle = fopen(dirname(__FILE__).'/../install/tmp/cr-all.md5','r')) !== FALSE) { |
||
| 178 | while (($data = fgetcsv($handle,2000,"\t")) !== FALSE) { |
||
| 179 | if (isset($data[1])) { |
||
| 180 | $year = $data[0]; |
||
| 181 | $all_md5_new[$year] = $data[1]; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | fclose($handle); |
||
| 185 | } |
||
| 186 | } elseif ($globalDebug) echo 'Download cr-all.md5 failed. '.dirname(__FILE__).'/../install/tmp/cr-all.md5 not here.'; |
||
| 187 | //print_r($all_md5_new); |
||
| 188 | //print_r($all_md5); |
||
| 189 | $result = $Common->arr_diff($all_md5_new,$all_md5); |
||
| 190 | //print_r($result); |
||
| 191 | foreach ($result as $file => $md5) { |
||
| 192 | $Common->download('https://data.flightairmap.fr/data/cr/'.$file,dirname(__FILE__).'/../install/tmp/'.$file); |
||
| 193 | if (file_exists(dirname(__FILE__).'/../install/tmp/'.$file)) $this->import(dirname(__FILE__).'/../install/tmp/'.$file); |
||
| 194 | elseif ($globalDebug) echo 'Download '.$file.' failed'; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | public function add($crash,$new = false) { |
||
| 199 | global $globalTransaction, $globalDebug; |
||
| 200 | require_once('class.Connection.php'); |
||
| 201 | require_once('class.Image.php'); |
||
| 202 | $Connection = new Connection(); |
||
| 203 | $Image = new Image(); |
||
| 204 | |||
| 205 | if (empty($crash)) return false; |
||
| 206 | if (!$new) { |
||
| 207 | $query_delete = 'DELETE FROM accidents WHERE source = :source'; |
||
| 208 | $sthd = $Connection->db->prepare($query_delete); |
||
| 209 | $sthd->execute(array(':source' => $crash[0]['source'])); |
||
| 210 | } |
||
| 211 | if ($globalTransaction) $Connection->db->beginTransaction(); |
||
| 212 | $initial_array = array('ident' => null,'type' => 'accident','url' => null,'registration' => null, 'date' => null, 'place' => null,'country' => null, 'latitude' => null, 'longitude' => null, 'fatalities' => null, 'title' => '','source' => '','aircraft_manufacturer' => null,'aircraft_name' => null); |
||
| 213 | $query_check = 'SELECT COUNT(*) as nb FROM accidents WHERE registration = :registration AND date = :date AND type = :type AND source = :source'; |
||
| 214 | $sth_check = $Connection->db->prepare($query_check); |
||
| 215 | $query = 'INSERT INTO accidents (aircraft_manufacturer,aircraft_name,ident,registration,date,url,country,place,title,fatalities,latitude,longitude,type,source) VALUES (:aircraft_manufacturer,:aircraft_name,:ident,:registration,:date,:url,:country,:place,:title,:fatalities,:latitude,:longitude,:type,:source)'; |
||
| 216 | $sth = $Connection->db->prepare($query); |
||
| 217 | $j = 0; |
||
| 218 | try { |
||
| 219 | foreach ($crash as $cr) { |
||
| 220 | //print_r($cr); |
||
| 221 | $cr = $cr + $initial_array; |
||
| 222 | $cr = array_map(function($value) { |
||
| 223 | return $value === "" ? NULL : $value; |
||
| 224 | }, $cr); |
||
| 225 | if ($cr['date'] != '' && $cr['registration'] != null && $cr['registration'] != '' && $cr['registration'] != '?' && $cr['registration'] != '-' && $cr['date'] < time() && !preg_match('/\s/',$cr['registration'])) { |
||
| 226 | $query_check_values = array(':registration' => $cr['registration'],':date' => date('Y-m-d',$cr['date']),':type' => $cr['type'],':source' => $cr['source']); |
||
| 227 | $sth_check->execute($query_check_values); |
||
| 228 | $result_check = $sth_check->fetch(PDO::FETCH_ASSOC); |
||
| 229 | if ($result_check['nb'] == 0) { |
||
| 230 | $query_values = array(':registration' => trim($cr['registration']),':date' => date('Y-m-d',$cr['date']),':url' => $cr['url'],':country' => $cr['country'],':place' => $cr['place'],':title' => $cr['title'],':fatalities' => $cr['fatalities'],':latitude' => $cr['latitude'],':longitude' => $cr['longitude'],':type' => $cr['type'],':source' => $cr['source'],':ident' => $cr['ident'],':aircraft_manufacturer' => $cr['aircraft_manufacturer'],':aircraft_name' => $cr['aircraft_name']); |
||
| 231 | $sth->execute($query_values); |
||
| 232 | if ($cr['date'] > time()-(30*86400)) { |
||
| 233 | if (empty($Image->getSpotterImage($cr['registration']))) { |
||
| 234 | //if ($globalDebug) echo 'Get image...'."\n"; |
||
| 235 | $Image->addSpotterImage($cr['registration']); |
||
| 236 | } |
||
| 237 | // elseif ($globalDebug) echo 'Image already in DB'."\n"; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } |
||
| 241 | if ($globalTransaction && $j % 100 == 0) { |
||
| 242 | $Connection->db->commit(); |
||
| 243 | $Connection->db->beginTransaction(); |
||
| 244 | } |
||
| 245 | } |
||
| 246 | if ($globalTransaction) $Connection->db->commit(); |
||
| 247 | } catch(PDOException $e) { |
||
| 248 | if ($globalTransaction) $Connection->db->rollBack(); |
||
| 249 | echo $e->getMessage(); |
||
| 250 | } |
||
| 251 | $sth_check->closeCursor(); |
||
| 252 | } |
||
| 253 | |||
| 254 | public static function check_accidents_nb() { |
||
| 255 | global $globalDBdriver; |
||
| 256 | $query = "SELECT COUNT(*) as nb FROM accidents"; |
||
| 257 | try { |
||
| 258 | $Connection = new Connection(); |
||
| 259 | $sth = $Connection->db->prepare($query); |
||
| 260 | $sth->execute(); |
||
| 261 | } catch(PDOException $e) { |
||
| 262 | return "error : ".$e->getMessage(); |
||
| 263 | } |
||
| 264 | $row = $sth->fetch(PDO::FETCH_ASSOC); |
||
| 265 | return $row['nb']; |
||
| 266 | } |
||
| 267 | |||
| 268 | public static function check_last_accidents_update() { |
||
| 269 | global $globalDBdriver; |
||
| 270 | if ($globalDBdriver == 'mysql') { |
||
| 271 | $query = "SELECT COUNT(*) as nb FROM config WHERE name = 'last_update_accident_db' AND value > DATE_SUB(NOW(), INTERVAL 1 DAY)"; |
||
| 272 | } else { |
||
| 273 | $query = "SELECT COUNT(*) as nb FROM config WHERE name = 'last_update_accident_db' AND value::timestamp > CURRENT_TIMESTAMP - INTERVAL '1 DAYS'"; |
||
| 274 | } |
||
| 275 | try { |
||
| 276 | $Connection = new Connection(); |
||
| 277 | $sth = $Connection->db->prepare($query); |
||
| 278 | $sth->execute(); |
||
| 279 | } catch(PDOException $e) { |
||
| 280 | return "error : ".$e->getMessage(); |
||
| 281 | } |
||
| 282 | $row = $sth->fetch(PDO::FETCH_ASSOC); |
||
| 283 | if ($row['nb'] > 0) return false; |
||
| 284 | else return true; |
||
| 285 | } |
||
| 286 | |||
| 287 | public static function insert_last_accidents_update() { |
||
| 288 | $query = "DELETE FROM config WHERE name = 'last_update_accident_db'; |
||
| 289 | INSERT INTO config (name,value) VALUES ('last_update_accident_db',NOW());"; |
||
| 290 | try { |
||
| 291 | $Connection = new Connection(); |
||
| 292 | $sth = $Connection->db->prepare($query); |
||
| 293 | $sth->execute(); |
||
| 294 | } catch(PDOException $e) { |
||
| 295 | return "error : ".$e->getMessage(); |
||
| 296 | } |
||
| 297 | } |
||
| 300 | ?> |