Complex classes like Image 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 Image, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class Image { |
||
| 7 | public $db; |
||
| 8 | |||
| 9 | public function __construct($dbc = null) { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Gets the images based on the aircraft registration |
||
| 17 | * |
||
| 18 | * @return Array the images list |
||
| 19 | * |
||
| 20 | */ |
||
| 21 | public function getSpotterImage($registration,$aircraft_icao = '', $airline_icao = '') |
||
| 22 | { |
||
| 23 | $registration = filter_var($registration,FILTER_SANITIZE_STRING); |
||
| 24 | $aircraft_icao = filter_var($aircraft_icao,FILTER_SANITIZE_STRING); |
||
| 25 | $airline_icao = filter_var($airline_icao,FILTER_SANITIZE_STRING); |
||
| 26 | $reg = $registration; |
||
| 27 | if (($reg == '' || $reg == 'NA') && $aircraft_icao != '') $reg = $aircraft_icao.$airline_icao; |
||
| 28 | $reg = trim($reg); |
||
| 29 | $query = "SELECT spotter_image.image, spotter_image.image_thumbnail, spotter_image.image_source, spotter_image.image_source_website,spotter_image.image_copyright, spotter_image.registration |
||
| 30 | FROM spotter_image |
||
| 31 | WHERE spotter_image.registration = :registration LIMIT 1"; |
||
| 32 | $sth = $this->db->prepare($query); |
||
| 33 | $sth->execute(array(':registration' => $reg)); |
||
| 34 | $result = $sth->fetchAll(PDO::FETCH_ASSOC); |
||
| 35 | if (!empty($result)) return $result; |
||
| 36 | elseif ($registration != '' && ($aircraft_icao != '' || $airline_icao != '')) return $this->getSpotterImage('',$aircraft_icao,$airline_icao); |
||
| 37 | else return array(); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Gets the images based on the ship name |
||
| 42 | * |
||
| 43 | * @return Array the images list |
||
| 44 | * |
||
| 45 | */ |
||
| 46 | public function getMarineImage($mmsi,$imo = '',$name = '') |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Gets the image copyright based on the Exif data |
||
| 73 | * |
||
| 74 | * @return String image copyright |
||
| 75 | * |
||
| 76 | */ |
||
| 77 | public function getExifCopyright($url) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Adds the images based on the aircraft registration |
||
| 92 | * |
||
| 93 | * @return String either success or error |
||
| 94 | * |
||
| 95 | */ |
||
| 96 | public function addSpotterImage($registration,$aircraft_icao = '', $airline_icao = '') |
||
| 97 | { |
||
| 98 | global $globalDebug,$globalAircraftImageFetch, $globalOffline; |
||
| 99 | if ((isset($globalAircraftImageFetch) && $globalAircraftImageFetch === FALSE) || (isset($globalOffline) && $globalOffline === TRUE)) return ''; |
||
| 100 | $registration = filter_var($registration,FILTER_SANITIZE_STRING); |
||
| 101 | $registration = trim($registration); |
||
| 102 | //getting the aircraft image |
||
| 103 | if ($globalDebug && $registration != '') echo 'Try to find an aircraft image for '.$registration.'...'; |
||
| 104 | elseif ($globalDebug && $aircraft_icao != '') echo 'Try to find an aircraft image for '.$aircraft_icao.'...'; |
||
| 105 | elseif ($globalDebug && $airline_icao != '') echo 'Try to find an aircraft image for '.$airline_icao.'...'; |
||
| 106 | $image_url = $this->findAircraftImage($registration,$aircraft_icao,$airline_icao); |
||
| 107 | if ($registration == '' && $aircraft_icao != '') $registration = $aircraft_icao.$airline_icao; |
||
| 108 | if ($image_url['original'] != '') { |
||
| 109 | if ($globalDebug) echo 'Found !'."\n"; |
||
| 110 | $query = "INSERT INTO spotter_image (registration, image, image_thumbnail, image_copyright, image_source,image_source_website) VALUES (:registration,:image,:image_thumbnail,:copyright,:source,:source_website)"; |
||
| 111 | try { |
||
| 112 | $sth = $this->db->prepare($query); |
||
| 113 | $sth->execute(array(':registration' => $registration,':image' => $image_url['original'],':image_thumbnail' => $image_url['thumbnail'], ':copyright' => $image_url['copyright'],':source' => $image_url['source'],':source_website' => $image_url['source_website'])); |
||
| 114 | } catch(PDOException $e) { |
||
| 115 | echo $e->getMessage()."\n"; |
||
| 116 | return "error"; |
||
| 117 | } |
||
| 118 | } elseif ($globalDebug) echo "Not found :'(\n"; |
||
| 119 | return "success"; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Adds the images based on the marine name |
||
| 124 | * |
||
| 125 | * @return String either success or error |
||
| 126 | * |
||
| 127 | */ |
||
| 128 | public function addMarineImage($mmsi,$imo = '',$name = '') |
||
| 129 | { |
||
| 130 | global $globalDebug,$globalMarineImageFetch, $globalOffline; |
||
| 131 | if ((isset($globalMarineImageFetch) && !$globalMarineImageFetch) || (isset($globalOffline) && $globalOffline === TRUE)) return ''; |
||
| 132 | $mmsi = filter_var($mmsi,FILTER_SANITIZE_STRING); |
||
| 133 | $imo = filter_var($imo,FILTER_SANITIZE_STRING); |
||
| 134 | $name = filter_var($name,FILTER_SANITIZE_STRING); |
||
| 135 | $name = trim($name); |
||
| 136 | $Marine = new Marine($this->db); |
||
| 137 | if ($imo == '' || $name == '') { |
||
| 138 | $identity = $Marine->getIdentity($mmsi); |
||
| 139 | if (isset($identity[0]['mmsi'])) { |
||
| 140 | $imo = $identity[0]['imo']; |
||
| 141 | if ($identity[0]['ship_name'] != '') $name = $identity[0]['ship_name']; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | unset($Marine); |
||
| 145 | |||
| 146 | //getting the aircraft image |
||
| 147 | if ($globalDebug && $name != '') echo 'Try to find an vessel image for '.$name.'...'; |
||
| 148 | $image_url = $this->findMarineImage($mmsi,$imo,$name); |
||
| 149 | if ($image_url['original'] != '') { |
||
| 150 | if ($globalDebug) echo 'Found !'."\n"; |
||
| 151 | $query = "INSERT INTO marine_image (mmsi,imo,name, image, image_thumbnail, image_copyright, image_source,image_source_website) VALUES (:mmsi,:imo,:name,:image,:image_thumbnail,:copyright,:source,:source_website)"; |
||
| 152 | try { |
||
| 153 | $sth = $this->db->prepare($query); |
||
| 154 | $sth->execute(array(':mmsi' => $mmsi,':imo' => $imo,':name' => $name,':image' => $image_url['original'],':image_thumbnail' => $image_url['thumbnail'], ':copyright' => $image_url['copyright'],':source' => $image_url['source'],':source_website' => $image_url['source_website'])); |
||
| 155 | } catch(PDOException $e) { |
||
| 156 | echo $e->getMessage()."\n"; |
||
| 157 | return "error"; |
||
| 158 | } |
||
| 159 | } elseif ($globalDebug) echo "Not found :'(\n"; |
||
| 160 | return "success"; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Gets the aircraft image |
||
| 165 | * |
||
| 166 | * @param String $aircraft_registration the registration of the aircraft |
||
| 167 | * @return Array the aircraft thumbnail, orignal url and copyright |
||
| 168 | * |
||
| 169 | */ |
||
| 170 | public function findAircraftImage($aircraft_registration, $aircraft_icao = '', $airline_icao = '') |
||
| 171 | { |
||
| 172 | global $globalAircraftImageSources, $globalIVAO, $globalAircraftImageCheckICAO, $globalVA; |
||
| 173 | $Spotter = new Spotter($this->db); |
||
| 174 | if (!isset($globalIVAO)) $globalIVAO = FALSE; |
||
| 175 | $aircraft_registration = filter_var($aircraft_registration,FILTER_SANITIZE_STRING); |
||
| 176 | if ($aircraft_registration != '' && $aircraft_registration != 'NA' && (!isset($globalVA) || $globalVA !== TRUE)) { |
||
| 177 | if (strpos($aircraft_registration,'/') !== false) return array('thumbnail' => '','original' => '', 'copyright' => '','source' => '','source_website' => ''); |
||
| 178 | $aircraft_registration = urlencode(trim($aircraft_registration)); |
||
| 179 | $aircraft_info = $Spotter->getAircraftInfoByRegistration($aircraft_registration); |
||
| 180 | if (isset($aircraft_info[0]['aircraft_name'])) $aircraft_name = $aircraft_info[0]['aircraft_name']; |
||
| 181 | else $aircraft_name = ''; |
||
| 182 | if (isset($aircraft_info[0]['aircraft_icao'])) $aircraft_name = $aircraft_info[0]['aircraft_icao']; |
||
| 183 | else $aircraft_icao = ''; |
||
| 184 | if (isset($aircraft_info[0]['airline_icao'])) $airline_icao = $aircraft_info[0]['airline_icao']; |
||
| 185 | else $airline_icao = ''; |
||
| 186 | } elseif ($aircraft_icao != '') { |
||
| 187 | $aircraft_info = $Spotter->getAllAircraftInfo($aircraft_icao); |
||
| 188 | if (isset($aircraft_info[0]['type'])) $aircraft_name = $aircraft_info[0]['type']; |
||
| 189 | else $aircraft_name = ''; |
||
| 190 | $aircraft_registration = $aircraft_icao; |
||
| 191 | } else return array('thumbnail' => '','original' => '', 'copyright' => '', 'source' => '','source_website' => ''); |
||
| 192 | unset($Spotter); |
||
| 193 | if (!isset($globalAircraftImageSources)) $globalAircraftImageSources = array('ivaomtl','wikimedia','airportdata','deviantart','flickr','bing','jetphotos','planepictures','planespotters'); |
||
| 194 | foreach ($globalAircraftImageSources as $source) { |
||
| 195 | $source = strtolower($source); |
||
| 196 | if ($source == 'ivaomtl' && $globalIVAO && $aircraft_icao != '' && $airline_icao != '') $images_array = $this->fromIvaoMtl('aircraft',$aircraft_icao,$airline_icao); |
||
| 197 | if ($source == 'planespotters' && !$globalIVAO && extension_loaded('simplexml')) $images_array = $this->fromPlanespotters('aircraft',$aircraft_registration,$aircraft_name); |
||
| 198 | if ($source == 'flickr' && extension_loaded('simplexml')) $images_array = $this->fromFlickr('aircraft',$aircraft_registration,$aircraft_name); |
||
| 199 | if ($source == 'bing') $images_array = $this->fromBing('aircraft',$aircraft_registration,$aircraft_name); |
||
| 200 | if ($source == 'deviantart' && extension_loaded('simplexml')) $images_array = $this->fromDeviantart('aircraft',$aircraft_registration,$aircraft_name); |
||
| 201 | if ($source == 'wikimedia') $images_array = $this->fromWikimedia('aircraft',$aircraft_registration,$aircraft_name); |
||
| 202 | if ($source == 'jetphotos' && !$globalIVAO && class_exists("DomDocument")) $images_array = $this->fromJetPhotos('aircraft',$aircraft_registration,$aircraft_name); |
||
| 203 | if ($source == 'planepictures' && !$globalIVAO && class_exists("DomDocument")) $images_array = $this->fromPlanePictures('aircraft',$aircraft_registration,$aircraft_name); |
||
| 204 | if ($source == 'airportdata' && !$globalIVAO) $images_array = $this->fromAirportData('aircraft',$aircraft_registration,$aircraft_name); |
||
| 205 | if ($source == 'customsources') $images_array = $this->fromCustomSource('aircraft',$aircraft_registration,$aircraft_name); |
||
| 206 | if (isset($images_array) && $images_array['original'] != '') return $images_array; |
||
| 207 | } |
||
| 208 | if ((!isset($globalAircraftImageCheckICAO) || $globalAircraftImageCheckICAO === TRUE) && isset($aircraft_icao)) return $this->findAircraftImage($aircraft_icao); |
||
| 209 | return array('thumbnail' => '','original' => '', 'copyright' => '','source' => '','source_website' => ''); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Gets the vessel image |
||
| 214 | * |
||
| 215 | * @param String $mmsi the vessel mmsi |
||
| 216 | * @param String $imo the vessel imo |
||
| 217 | * @param String $name the vessel name |
||
| 218 | * @return Array the aircraft thumbnail, orignal url and copyright |
||
| 219 | * |
||
| 220 | */ |
||
| 221 | public function findMarineImage($mmsi,$imo = '',$name = '') |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Gets the aircraft image from Planespotters |
||
| 255 | * |
||
| 256 | * @param String $aircraft_registration the registration of the aircraft |
||
| 257 | * @param String $aircraft_name type of the aircraft |
||
| 258 | * @return Array the aircraft thumbnail, orignal url and copyright |
||
| 259 | * |
||
| 260 | */ |
||
| 261 | public function fromPlanespotters($type,$aircraft_registration, $aircraft_name='') { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Gets the aircraft image from Deviantart |
||
| 289 | * |
||
| 290 | * @param String $registration the registration of the aircraft |
||
| 291 | * @param String $name type of the aircraft |
||
| 292 | * @return Array the aircraft thumbnail, orignal url and copyright |
||
| 293 | * |
||
| 294 | */ |
||
| 295 | public function fromDeviantart($type,$registration, $name='') { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Gets the aircraft image from JetPhotos |
||
| 329 | * |
||
| 330 | * @param String $aircraft_registration the registration of the aircraft |
||
| 331 | * @param String $aircraft_name type of the aircraft |
||
| 332 | * @return Array the aircraft thumbnail, orignal url and copyright |
||
| 333 | * |
||
| 334 | */ |
||
| 335 | public function fromJetPhotos($type,$aircraft_registration, $aircraft_name='') { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Gets the aircraft image from PlanePictures |
||
| 371 | * |
||
| 372 | * @param String $aircraft_registration the registration of the aircraft |
||
| 373 | * @param String $aircraft_name type of the aircraft |
||
| 374 | * @return Array the aircraft thumbnail, orignal url and copyright |
||
| 375 | * |
||
| 376 | */ |
||
| 377 | public function fromPlanePictures($type,$aircraft_registration, $aircraft_name='') { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Gets the aircraft image from Flickr |
||
| 405 | * |
||
| 406 | * @param String $registration the registration of the aircraft |
||
| 407 | * @param String $name type of the aircraft |
||
| 408 | * @return Array the aircraft thumbnail, orignal url and copyright |
||
| 409 | * |
||
| 410 | */ |
||
| 411 | public function fromFlickr($type,$registration,$name='') { |
||
| 436 | |||
| 437 | public function fromIvaoMtl($type,$aircraft_icao,$airline_icao) { |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Gets the aircraft image from Bing |
||
| 455 | * |
||
| 456 | * @param String $aircraft_registration the registration of the aircraft |
||
| 457 | * @param String $aircraft_name type of the aircraft |
||
| 458 | * @return Array the aircraft thumbnail, orignal url and copyright |
||
| 459 | * |
||
| 460 | */ |
||
| 461 | public function fromBing($type,$aircraft_registration,$aircraft_name='') { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Gets the aircraft image from airport-data |
||
| 492 | * |
||
| 493 | * @param String $aircraft_registration the registration of the aircraft |
||
| 494 | * @param String $aircraft_name type of the aircraft |
||
| 495 | * @return Array the aircraft thumbnail, orignal url and copyright |
||
| 496 | * |
||
| 497 | */ |
||
| 498 | public function fromAirportData($type,$aircraft_registration,$aircraft_name='') { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Gets image from WikiMedia |
||
| 517 | * |
||
| 518 | * @param String $registration the registration of the aircraft/mmsi |
||
| 519 | * @param String $name name |
||
| 520 | * @return Array the aircraft thumbnail, orignal url and copyright |
||
| 521 | * |
||
| 522 | */ |
||
| 523 | public function fromWikimedia($type,$registration,$name='') { |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Gets the aircraft image from custom url |
||
| 578 | * |
||
| 579 | * @param String $registration the registration of the aircraft |
||
| 580 | * @param String $name type of the aircraft |
||
| 581 | * @return Array the aircraft thumbnail, orignal url and copyright |
||
| 582 | * |
||
| 583 | */ |
||
| 584 | public function fromCustomSource($type,$registration,$name='') { |
||
| 656 | } |
||
| 657 | |||
| 658 | //$Image = new Image(); |
||
| 659 | //print_r($Image->fromAirportData('F-GZHM')); |
||
| 660 | |||
| 661 | ?> |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.