Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like GoogleMapDataResponse 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 GoogleMapDataResponse, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class GoogleMapDataResponse extends Controller |
||
| 38 | { |
||
| 39 | private static $session_var_prefix = "addCustomGoogleMap"; |
||
| 40 | |||
| 41 | private static $increase_factor_if_nothing_found = 0; |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * Default URL handlers - (Action)/(ID)/(OtherID) |
||
| 46 | */ |
||
| 47 | private static $url_handlers = array( |
||
|
|
|||
| 48 | '/$Action//$OwnerID/$Title/$Longitude/$Latitude/$FilterCode/$SecondFilterCode' => 'handleAction', |
||
| 49 | ); |
||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | ################# |
||
| 56 | # SESSION MANAGEMENT |
||
| 57 | ################# |
||
| 58 | |||
| 59 | protected static function session_var_name($filterCode = "") |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param Array $addCustomGoogleMapArrayNEW |
||
| 66 | * @param string $filterCode |
||
| 67 | * |
||
| 68 | */ |
||
| 69 | public static function add_custom_google_map_session_data($addCustomGoogleMapArrayNEW, $filterCode = "") |
||
| 75 | |||
| 76 | /** |
||
| 77 | * |
||
| 78 | * |
||
| 79 | * @param Array $addCustomGoogleMapArray |
||
| 80 | * @param string $filterCode |
||
| 81 | * |
||
| 82 | */ |
||
| 83 | public static function set_custom_google_map_session_data($addCustomGoogleMapArray, $filterCode = "") |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param string $filterCode |
||
| 93 | * |
||
| 94 | * @return Array |
||
| 95 | */ |
||
| 96 | public static function get_custom_google_map_session_data($filterCode = "") |
||
| 110 | |||
| 111 | /** |
||
| 112 | * |
||
| 113 | * @param string $filterCode |
||
| 114 | */ |
||
| 115 | public static function clear_custom_google_map_session_data($filterCode = "") |
||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | |||
| 126 | ################# |
||
| 127 | # BASICS |
||
| 128 | ################# |
||
| 129 | /** |
||
| 130 | * @inherited |
||
| 131 | */ |
||
| 132 | private static $allowed_actions = array( |
||
| 133 | 'showemptymap' => true, |
||
| 134 | 'showpagepointsmapxml' => true, |
||
| 135 | 'showchildpointsmapxml' => true, |
||
| 136 | 'showdirectchildren' => true, |
||
| 137 | 'showsearchpoint' => true, |
||
| 138 | 'showcustompagesmapxml' => true, |
||
| 139 | 'showcustomdosmapxml' => true, |
||
| 140 | 'showdataobjects' => true, |
||
| 141 | 'updatemexml' => 'ADMIN', |
||
| 142 | 'showaroundmexml' => true, |
||
| 143 | 'showpointbyid' => true |
||
| 144 | ); |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var Array |
||
| 148 | */ |
||
| 149 | private static $actions_without_owner = array( |
||
| 150 | 'showemptymap' |
||
| 151 | ); |
||
| 152 | |||
| 153 | /** |
||
| 154 | * The Page that is displaying the |
||
| 155 | * @var SiteTree |
||
| 156 | */ |
||
| 157 | protected $owner = null; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @var Float |
||
| 161 | */ |
||
| 162 | protected $lng = 0; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @var Float |
||
| 166 | */ |
||
| 167 | protected $lat = 0; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @var String |
||
| 171 | */ |
||
| 172 | protected $title = ""; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @var Stringsql define a variable |
||
| 176 | */ |
||
| 177 | protected $filterCode = ""; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @var String |
||
| 181 | */ |
||
| 182 | protected $secondFilterCode = ""; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @var GoogleMap |
||
| 186 | */ |
||
| 187 | protected $map = null; |
||
| 188 | |||
| 189 | |||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | |||
| 194 | |||
| 195 | |||
| 196 | |||
| 197 | |||
| 198 | |||
| 199 | ################# |
||
| 200 | # SET AND GET VARIABLES |
||
| 201 | ################# |
||
| 202 | |||
| 203 | public function init() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param object $owner |
||
| 249 | */ |
||
| 250 | public function setOwner($owner) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param String $title |
||
| 257 | */ |
||
| 258 | public function setTitle($title) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param float $lng |
||
| 265 | */ |
||
| 266 | public function setLng($lng) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param Float $lat |
||
| 273 | */ |
||
| 274 | public function setLat($lat) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $filterCode |
||
| 281 | */ |
||
| 282 | public function setFilterCode($filterCode) |
||
| 286 | |||
| 287 | |||
| 288 | |||
| 289 | |||
| 290 | |||
| 291 | |||
| 292 | |||
| 293 | |||
| 294 | |||
| 295 | |||
| 296 | |||
| 297 | ################# |
||
| 298 | # ACTIONS |
||
| 299 | ################# |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param SS_HTTPRequest |
||
| 303 | * |
||
| 304 | * @return String (XML) |
||
| 305 | */ |
||
| 306 | public function index($request) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param SS_HTTPRequest |
||
| 313 | * |
||
| 314 | * @return String (XML) |
||
| 315 | */ |
||
| 316 | public function showemptymap($request) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param SS_HTTPRequest |
||
| 323 | * |
||
| 324 | * @return String (XML) |
||
| 325 | */ |
||
| 326 | View Code Duplication | public function showpagepointsmapxml($request) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * @param SS_HTTPRequest |
||
| 337 | * |
||
| 338 | * @return String (XML) |
||
| 339 | */ |
||
| 340 | View Code Duplication | public function showchildpointsmapxml($request) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * @param SS_HTTPRequest |
||
| 350 | * |
||
| 351 | * @return String (XML) |
||
| 352 | */ |
||
| 353 | public function showdirectchildren($request) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param SS_HTTPRequest |
||
| 363 | * |
||
| 364 | * @return String (XML) |
||
| 365 | */ |
||
| 366 | public function showsearchpoint($request) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @param SS_HTTPRequest |
||
| 389 | * |
||
| 390 | * @return String (XML) |
||
| 391 | */ |
||
| 392 | public function showpointbyid($request) |
||
| 418 | |||
| 419 | |||
| 420 | /** |
||
| 421 | * load data from session |
||
| 422 | * |
||
| 423 | * @param SS_HTTPRequest |
||
| 424 | * |
||
| 425 | * @return String (XML) |
||
| 426 | */ |
||
| 427 | public function showcustompagesmapxml($request) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * load a custom set of GoogleMapLocationsObjects |
||
| 436 | * |
||
| 437 | * @param SS_HTTPRequest |
||
| 438 | * |
||
| 439 | * @return String (XML) |
||
| 440 | */ |
||
| 441 | public function showcustomdosmapxml($request) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Show what is around my points |
||
| 450 | * |
||
| 451 | * @param SS_HTTPRequest |
||
| 452 | * |
||
| 453 | * @return String (XML) |
||
| 454 | */ |
||
| 455 | public function showaroundmexml($request) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * URL must contain for GET variables |
||
| 546 | * i - ID of owner |
||
| 547 | * a - action |
||
| 548 | * x - lng |
||
| 549 | * y - lat |
||
| 550 | * |
||
| 551 | * actions are: |
||
| 552 | * - add |
||
| 553 | * - move |
||
| 554 | * - remove |
||
| 555 | * |
||
| 556 | * @param SS_HTTPRequest |
||
| 557 | * |
||
| 558 | * @return String (message) |
||
| 559 | */ |
||
| 560 | public function updatemexml($request) |
||
| 616 | |||
| 617 | |||
| 618 | |||
| 619 | |||
| 620 | |||
| 621 | |||
| 622 | |||
| 623 | ################# |
||
| 624 | # TEMPLATE METHODS |
||
| 625 | ################# |
||
| 626 | /** |
||
| 627 | * |
||
| 628 | * @return GoogleMap |
||
| 629 | */ |
||
| 630 | public function GoogleMapController() |
||
| 637 | |||
| 638 | |||
| 639 | |||
| 640 | |||
| 641 | |||
| 642 | |||
| 643 | |||
| 644 | |||
| 645 | ################# |
||
| 646 | # PRIVATE PARTY |
||
| 647 | ################# |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @param ArrayList $pages |
||
| 651 | * @param ArrayList $dataPoints |
||
| 652 | * @param string $title |
||
| 653 | * @param string $selectionStatement |
||
| 654 | * @param string $noDataPointsMessage |
||
| 655 | * |
||
| 656 | * @return String (XML) |
||
| 657 | */ |
||
| 658 | protected function makeXMLData( |
||
| 674 | |||
| 675 | |||
| 676 | |||
| 677 | ################################ |
||
| 678 | # STATIC METHODS |
||
| 679 | ################################ |
||
| 680 | /** |
||
| 681 | * |
||
| 682 | * @param ArrayList $pages you can also use the $dataPoints variable if you prefer |
||
| 683 | * @param ArrayList $dataPoints you can also use the $pages variable if you prefer |
||
| 684 | * @param string $title |
||
| 685 | * @param string $selectionStatement |
||
| 686 | * @param string $noDataPointsMessage message to show when there are no data points ... |
||
| 687 | * @return [type] |
||
| 688 | */ |
||
| 689 | public static function xml_sheet( |
||
| 710 | |||
| 711 | |||
| 712 | |||
| 713 | /** |
||
| 714 | * var arrayOfLatitudeAndLongitude: Array (Latitude" => 123, "Longitude" => 123, "Marker" => "red1"); |
||
| 715 | * Marker is optional |
||
| 716 | * @param Array arrayOfLatitudeAndLongitude |
||
| 717 | * @param String title |
||
| 718 | * |
||
| 719 | * @return String (HTML - img tag) |
||
| 720 | */ |
||
| 721 | |||
| 722 | public static function quick_static_map($arrayOfLatitudeAndLongitude, $title) |
||
| 752 | |||
| 753 | /** |
||
| 754 | * @param String $staticMapURL |
||
| 755 | * @param String $title |
||
| 756 | * |
||
| 757 | * @return String (HTML - img tag) |
||
| 758 | */ |
||
| 759 | protected static function make_static_map_url_into_image($staticMapURL, $title) |
||
| 774 | } |
||
| 775 |