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 | /** |
||
| 42 | * set searches in show around me to a specific max radius |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | private static $max_radius_for_show_around_me = 20000; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Default URL handlers - (Action)/(ID)/(OtherID) |
||
| 49 | */ |
||
| 50 | private static $url_handlers = array( |
||
| 51 | '/$Action//$OwnerID/$Title/$Longitude/$Latitude/$FilterCode/$SecondFilterCode' => 'handleAction', |
||
| 52 | ); |
||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | ################# |
||
| 59 | # SESSION MANAGEMENT |
||
| 60 | ################# |
||
| 61 | |||
| 62 | protected static function session_var_name($filterCode = "") |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param Array $addCustomGoogleMapArrayNEW |
||
| 69 | * @param string $filterCode |
||
| 70 | * |
||
| 71 | */ |
||
| 72 | public static function add_custom_google_map_session_data($addCustomGoogleMapArrayNEW, $filterCode = "") |
||
| 78 | |||
| 79 | /** |
||
| 80 | * |
||
| 81 | * |
||
| 82 | * @param Array $addCustomGoogleMapArray |
||
| 83 | * @param string $filterCode |
||
| 84 | * |
||
| 85 | */ |
||
| 86 | public static function set_custom_google_map_session_data($addCustomGoogleMapArray, $filterCode = "") |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param string $filterCode |
||
| 96 | * |
||
| 97 | * @return Array |
||
| 98 | */ |
||
| 99 | public static function get_custom_google_map_session_data($filterCode = "") |
||
| 113 | |||
| 114 | /** |
||
| 115 | * |
||
| 116 | * @param string $filterCode |
||
| 117 | */ |
||
| 118 | public static function clear_custom_google_map_session_data($filterCode = "") |
||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | |||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | ################# |
||
| 130 | # BASICS |
||
| 131 | ################# |
||
| 132 | /** |
||
| 133 | * @inherited |
||
| 134 | */ |
||
| 135 | private static $allowed_actions = array( |
||
| 136 | 'showemptymap' => true, |
||
| 137 | 'showpagepointsmapxml' => true, |
||
| 138 | 'showchildpointsmapxml' => true, |
||
| 139 | 'showdirectchildren' => true, |
||
| 140 | 'showsearchpoint' => true, |
||
| 141 | 'showcustompagesmapxml' => true, |
||
| 142 | 'showcustomdosmapxml' => true, |
||
| 143 | 'showdataobjects' => true, |
||
| 144 | 'updatemexml' => 'ADMIN', |
||
| 145 | 'showaroundmexml' => true, |
||
| 146 | 'showpointbyid' => true |
||
| 147 | ); |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @var Array |
||
| 151 | */ |
||
| 152 | private static $actions_without_owner = array( |
||
| 153 | 'showemptymap' |
||
| 154 | ); |
||
| 155 | |||
| 156 | /** |
||
| 157 | * The Page that is displaying the |
||
| 158 | * @var SiteTree |
||
| 159 | */ |
||
| 160 | protected $owner = null; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @var Float |
||
| 164 | */ |
||
| 165 | protected $lng = 0; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @var Float |
||
| 169 | */ |
||
| 170 | protected $lat = 0; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @var String |
||
| 174 | */ |
||
| 175 | protected $title = ""; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @var String |
||
| 179 | */ |
||
| 180 | protected $filterCode = ""; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @var String |
||
| 184 | */ |
||
| 185 | protected $secondFilterCode = ""; |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @var GoogleMap |
||
| 189 | */ |
||
| 190 | protected $map = null; |
||
| 191 | |||
| 192 | |||
| 193 | |||
| 194 | |||
| 195 | |||
| 196 | |||
| 197 | |||
| 198 | |||
| 199 | |||
| 200 | |||
| 201 | |||
| 202 | ################# |
||
| 203 | # SET AND GET VARIABLES |
||
| 204 | ################# |
||
| 205 | |||
| 206 | public function init() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param object $owner |
||
| 252 | */ |
||
| 253 | public function setOwner($owner) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param String $title |
||
| 260 | */ |
||
| 261 | public function setTitle($title) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param float $lng |
||
| 268 | */ |
||
| 269 | public function setLng($lng) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param Float $lat |
||
| 276 | */ |
||
| 277 | public function setLat($lat) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param string $filterCode |
||
| 284 | */ |
||
| 285 | public function setFilterCode($filterCode) |
||
| 289 | |||
| 290 | |||
| 291 | |||
| 292 | |||
| 293 | |||
| 294 | |||
| 295 | |||
| 296 | |||
| 297 | |||
| 298 | |||
| 299 | |||
| 300 | ################# |
||
| 301 | # ACTIONS |
||
| 302 | ################# |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param SS_HTTPRequest |
||
| 306 | * |
||
| 307 | * @return String (XML) |
||
| 308 | */ |
||
| 309 | public function index($request) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param SS_HTTPRequest |
||
| 316 | * |
||
| 317 | * @return String (XML) |
||
| 318 | */ |
||
| 319 | public function showemptymap($request) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param SS_HTTPRequest |
||
| 326 | * |
||
| 327 | * @return String (XML) |
||
| 328 | */ |
||
| 329 | View Code Duplication | public function showpagepointsmapxml($request) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * @param SS_HTTPRequest |
||
| 340 | * |
||
| 341 | * @return String (XML) |
||
| 342 | */ |
||
| 343 | View Code Duplication | public function showchildpointsmapxml($request) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * @param SS_HTTPRequest |
||
| 353 | * |
||
| 354 | * @return String (XML) |
||
| 355 | */ |
||
| 356 | public function showdirectchildren($request) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param SS_HTTPRequest |
||
| 366 | * |
||
| 367 | * @return String (XML) |
||
| 368 | */ |
||
| 369 | public function showsearchpoint($request) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @param SS_HTTPRequest |
||
| 392 | * |
||
| 393 | * @return String (XML) |
||
| 394 | */ |
||
| 395 | public function showpointbyid($request) |
||
| 419 | |||
| 420 | |||
| 421 | /** |
||
| 422 | * load data from session |
||
| 423 | * |
||
| 424 | * @param SS_HTTPRequest |
||
| 425 | * |
||
| 426 | * @return String (XML) |
||
| 427 | */ |
||
| 428 | public function showcustompagesmapxml($request) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * load a custom set of GoogleMapLocationsObjects |
||
| 437 | * |
||
| 438 | * @param SS_HTTPRequest |
||
| 439 | * |
||
| 440 | * @return String (XML) |
||
| 441 | */ |
||
| 442 | public function showcustomdosmapxml($request) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Show what is around my points |
||
| 451 | * |
||
| 452 | * @param SS_HTTPRequest |
||
| 453 | * |
||
| 454 | * @return String (XML) |
||
| 455 | */ |
||
| 456 | public function showaroundmexml($request) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * URL must contain for GET variables |
||
| 521 | * i - ID of owner |
||
| 522 | * a - action |
||
| 523 | * x - lng |
||
| 524 | * y - lat |
||
| 525 | * |
||
| 526 | * actions are: |
||
| 527 | * - add |
||
| 528 | * - move |
||
| 529 | * - remove |
||
| 530 | * |
||
| 531 | * @param SS_HTTPRequest |
||
| 532 | * |
||
| 533 | * @return String (message) |
||
| 534 | */ |
||
| 535 | public function updatemexml($request) |
||
| 591 | |||
| 592 | |||
| 593 | |||
| 594 | |||
| 595 | |||
| 596 | |||
| 597 | |||
| 598 | ################# |
||
| 599 | # TEMPLATE METHODS |
||
| 600 | ################# |
||
| 601 | /** |
||
| 602 | * |
||
| 603 | * @return GoogleMap |
||
| 604 | */ |
||
| 605 | public function GoogleMapController() |
||
| 612 | |||
| 613 | |||
| 614 | |||
| 615 | |||
| 616 | |||
| 617 | |||
| 618 | |||
| 619 | |||
| 620 | ################# |
||
| 621 | # PRIVATE PARTY |
||
| 622 | ################# |
||
| 623 | |||
| 624 | /** |
||
| 625 | * @param DataList $pages |
||
| 626 | * @param DataList $dataPoints |
||
| 627 | * @param string $title |
||
| 628 | * @param string $selectionStatement |
||
| 629 | * |
||
| 630 | * @return String (XML) |
||
| 631 | */ |
||
| 632 | protected function makeXMLData( |
||
| 646 | |||
| 647 | |||
| 648 | |||
| 649 | ################################ |
||
| 650 | # STATIC METHODS |
||
| 651 | ################################ |
||
| 652 | |||
| 653 | public static function xml_sheet( |
||
| 670 | |||
| 671 | |||
| 672 | |||
| 673 | /** |
||
| 674 | * var arrayOfLatitudeAndLongitude: Array (Latitude" => 123, "Longitude" => 123, "Marker" => "red1"); |
||
| 675 | * Marker is optional |
||
| 676 | * @param Array arrayOfLatitudeAndLongitude |
||
| 677 | * @param String title |
||
| 678 | * |
||
| 679 | * @return String (HTML - img tag) |
||
| 680 | */ |
||
| 681 | |||
| 682 | public static function quick_static_map($arrayOfLatitudeAndLongitude, $title) |
||
| 712 | |||
| 713 | /** |
||
| 714 | * @param String $staticMapURL |
||
| 715 | * @param String $title |
||
| 716 | * |
||
| 717 | * @return String (HTML - img tag) |
||
| 718 | */ |
||
| 719 | protected static function make_static_map_url_into_image($staticMapURL, $title) |
||
| 734 | } |
||
| 735 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.