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 | * Default URL handlers - (Action)/(ID)/(OtherID) |
||
| 43 | */ |
||
| 44 | private static $url_handlers = array( |
||
| 45 | '/$Action//$OwnerID/$Title/$Longitude/$Latitude/$FilterCode/$SecondFilterCode' => 'handleAction', |
||
| 46 | ); |
||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | ################# |
||
| 53 | # SESSION MANAGEMENT |
||
| 54 | ################# |
||
| 55 | |||
| 56 | protected static function session_var_name($filterCode = "") |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param Array $addCustomGoogleMapArrayNEW |
||
| 63 | * @param string $filterCode |
||
| 64 | * |
||
| 65 | */ |
||
| 66 | public static function add_custom_google_map_session_data($addCustomGoogleMapArrayNEW, $filterCode = "") |
||
| 72 | |||
| 73 | /** |
||
| 74 | * |
||
| 75 | * |
||
| 76 | * @param Array $addCustomGoogleMapArray |
||
| 77 | * @param string $filterCode |
||
| 78 | * |
||
| 79 | */ |
||
| 80 | public static function set_custom_google_map_session_data($addCustomGoogleMapArray, $filterCode = "") |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $filterCode |
||
| 90 | * |
||
| 91 | * @return Array |
||
| 92 | */ |
||
| 93 | public static function get_custom_google_map_session_data($filterCode = "") |
||
| 107 | |||
| 108 | /** |
||
| 109 | * |
||
| 110 | * @param string $filterCode |
||
| 111 | */ |
||
| 112 | public static function clear_custom_google_map_session_data($filterCode = "") |
||
| 116 | |||
| 117 | |||
| 118 | |||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | |||
| 123 | ################# |
||
| 124 | # BASICS |
||
| 125 | ################# |
||
| 126 | /** |
||
| 127 | * @inherited |
||
| 128 | */ |
||
| 129 | private static $allowed_actions = array( |
||
| 130 | 'showemptymap' => true, |
||
| 131 | 'showpagepointsmapxml' => true, |
||
| 132 | 'showchildpointsmapxml' => true, |
||
| 133 | 'showdirectchildren' => true, |
||
| 134 | 'showsearchpoint' => true, |
||
| 135 | 'showcustompagesmapxml' => true, |
||
| 136 | 'showcustomdosmapxml' => true, |
||
| 137 | 'showdataobjects' => true, |
||
| 138 | 'updatemexml' => 'ADMIN', |
||
| 139 | 'showaroundmexml' => true, |
||
| 140 | 'showpointbyid' => true |
||
| 141 | ); |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var Array |
||
| 145 | */ |
||
| 146 | private static $actions_without_owner = array( |
||
| 147 | 'showemptymap' |
||
| 148 | ); |
||
| 149 | |||
| 150 | /** |
||
| 151 | * The Page that is displaying the |
||
| 152 | * @var SiteTree |
||
| 153 | */ |
||
| 154 | protected $owner = null; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @var Float |
||
| 158 | */ |
||
| 159 | protected $lng = 0; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @var Float |
||
| 163 | */ |
||
| 164 | protected $lat = 0; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @var String |
||
| 168 | */ |
||
| 169 | protected $title = ""; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @var String |
||
| 173 | */ |
||
| 174 | protected $filterCode = ""; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @var String |
||
| 178 | */ |
||
| 179 | protected $secondFilterCode = ""; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @var GoogleMap |
||
| 183 | */ |
||
| 184 | protected $map = null; |
||
| 185 | |||
| 186 | |||
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | |||
| 194 | |||
| 195 | |||
| 196 | ################# |
||
| 197 | # SET AND GET VARIABLES |
||
| 198 | ################# |
||
| 199 | |||
| 200 | public function init() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param object $owner |
||
| 246 | */ |
||
| 247 | public function setOwner($owner) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param String $title |
||
| 254 | */ |
||
| 255 | public function setTitle($title) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param float $lng |
||
| 262 | */ |
||
| 263 | public function setLng($lng) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param Float $lat |
||
| 270 | */ |
||
| 271 | public function setLat($lat) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param string $filterCode |
||
| 278 | */ |
||
| 279 | public function setFilterCode($filterCode) |
||
| 283 | |||
| 284 | |||
| 285 | |||
| 286 | |||
| 287 | |||
| 288 | |||
| 289 | |||
| 290 | |||
| 291 | |||
| 292 | |||
| 293 | |||
| 294 | ################# |
||
| 295 | # ACTIONS |
||
| 296 | ################# |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param SS_HTTPRequest |
||
| 300 | * |
||
| 301 | * @return String (XML) |
||
| 302 | */ |
||
| 303 | public function index($request) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param SS_HTTPRequest |
||
| 310 | * |
||
| 311 | * @return String (XML) |
||
| 312 | */ |
||
| 313 | public function showemptymap($request) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param SS_HTTPRequest |
||
| 320 | * |
||
| 321 | * @return String (XML) |
||
| 322 | */ |
||
| 323 | View Code Duplication | public function showpagepointsmapxml($request) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @param SS_HTTPRequest |
||
| 334 | * |
||
| 335 | * @return String (XML) |
||
| 336 | */ |
||
| 337 | View Code Duplication | public function showchildpointsmapxml($request) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * @param SS_HTTPRequest |
||
| 347 | * |
||
| 348 | * @return String (XML) |
||
| 349 | */ |
||
| 350 | public function showdirectchildren($request) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param SS_HTTPRequest |
||
| 360 | * |
||
| 361 | * @return String (XML) |
||
| 362 | */ |
||
| 363 | public function showsearchpoint($request) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param SS_HTTPRequest |
||
| 386 | * |
||
| 387 | * @return String (XML) |
||
| 388 | */ |
||
| 389 | public function showpointbyid($request) |
||
| 413 | |||
| 414 | |||
| 415 | /** |
||
| 416 | * load data from session |
||
| 417 | * |
||
| 418 | * @param SS_HTTPRequest |
||
| 419 | * |
||
| 420 | * @return String (XML) |
||
| 421 | */ |
||
| 422 | public function showcustompagesmapxml($request) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * load a custom set of GoogleMapLocationsObjects |
||
| 431 | * |
||
| 432 | * @param SS_HTTPRequest |
||
| 433 | * |
||
| 434 | * @return String (XML) |
||
| 435 | */ |
||
| 436 | public function showcustomdosmapxml($request) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Show what is around my points |
||
| 445 | * |
||
| 446 | * @param SS_HTTPRequest |
||
| 447 | * |
||
| 448 | * @return String (XML) |
||
| 449 | */ |
||
| 450 | public function showaroundmexml($request) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * URL must contain for GET variables |
||
| 511 | * i - ID of owner |
||
| 512 | * a - action |
||
| 513 | * x - lng |
||
| 514 | * y - lat |
||
| 515 | * |
||
| 516 | * actions are: |
||
| 517 | * - add |
||
| 518 | * - move |
||
| 519 | * - remove |
||
| 520 | * |
||
| 521 | * @param SS_HTTPRequest |
||
| 522 | * |
||
| 523 | * @return String (message) |
||
| 524 | */ |
||
| 525 | public function updatemexml($request) |
||
| 581 | |||
| 582 | |||
| 583 | |||
| 584 | |||
| 585 | |||
| 586 | |||
| 587 | |||
| 588 | ################# |
||
| 589 | # TEMPLATE METHODS |
||
| 590 | ################# |
||
| 591 | /** |
||
| 592 | * |
||
| 593 | * @return GoogleMap |
||
| 594 | */ |
||
| 595 | public function GoogleMapController() |
||
| 602 | |||
| 603 | |||
| 604 | |||
| 605 | |||
| 606 | |||
| 607 | |||
| 608 | |||
| 609 | |||
| 610 | ################# |
||
| 611 | # PRIVATE PARTY |
||
| 612 | ################# |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @param DataList $pages |
||
| 616 | * @param DataList $dataPoints |
||
| 617 | * @param string $title |
||
| 618 | * @param string $selectionStatement |
||
| 619 | * |
||
| 620 | * @return String (XML) |
||
| 621 | */ |
||
| 622 | protected function makeXMLData( |
||
| 636 | |||
| 637 | |||
| 638 | |||
| 639 | ################################ |
||
| 640 | # STATIC METHODS |
||
| 641 | ################################ |
||
| 642 | |||
| 643 | public static function xml_sheet( |
||
| 660 | |||
| 661 | |||
| 662 | |||
| 663 | /** |
||
| 664 | * var arrayOfLatitudeAndLongitude: Array (Latitude" => 123, "Longitude" => 123, "Marker" => "red1"); |
||
| 665 | * Marker is optional |
||
| 666 | * @param Array arrayOfLatitudeAndLongitude |
||
| 667 | * @param String title |
||
| 668 | * |
||
| 669 | * @return String (HTML - img tag) |
||
| 670 | */ |
||
| 671 | |||
| 672 | public static function quick_static_map($arrayOfLatitudeAndLongitude, $title) |
||
| 702 | |||
| 703 | /** |
||
| 704 | * @param String $staticMapURL |
||
| 705 | * @param String $title |
||
| 706 | * |
||
| 707 | * @return String (HTML - img tag) |
||
| 708 | */ |
||
| 709 | protected static function make_static_map_url_into_image($staticMapURL, $title) |
||
| 724 | } |
||
| 725 |
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.