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 Mapper 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 Mapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Cornford\Googlmapper; |
||
| 14 | class Mapper extends MapperBase implements MappingInterface { |
||
| 15 | |||
| 16 | const GOOGLE_RESPONSE_OK = 'OK'; |
||
| 17 | const GOOGLE_RESPONSE_ZERO_RESULTS = 'ZERO_RESULTS'; |
||
| 18 | const GOOGLE_RESPONSE_QUERY_LIMIT = 'OVER_QUERY_LIMIT'; |
||
| 19 | const GOOGLE_RESPONSE_DENIED = 'REQUEST_DENIED'; |
||
| 20 | const GOOGLE_RESPONSE_INVALID = 'INVALID_REQUEST'; |
||
| 21 | const GOOGLE_RESPONSE_UNKNOWN = 'UNKNOWN_ERROR'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Renders and returns Google Map code. |
||
| 25 | * |
||
| 26 | * @param integer $item |
||
| 27 | * |
||
| 28 | * @return string |
||
| 29 | */ |
||
| 30 | public function render($item = -1) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Renders and returns Google Map javascript code. |
||
| 45 | * |
||
| 46 | * @return string |
||
| 47 | */ |
||
| 48 | public function renderJavascript() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Generates the render options for Google Map. |
||
| 62 | * |
||
| 63 | * @param integer $item |
||
| 64 | * |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | protected function generateRenderOptions($item = -1) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Search for a location against Google Maps Api. |
||
| 84 | * |
||
| 85 | * @param string $location |
||
| 86 | * |
||
| 87 | * @return mixed |
||
| 88 | */ |
||
| 89 | protected function searchLocation($location) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Locate a location and return a Location instance. |
||
| 104 | * |
||
| 105 | * @param string $location |
||
| 106 | * |
||
| 107 | * @throws MapperArgumentException |
||
| 108 | * @throws MapperSearchException |
||
| 109 | * @throws MapperSearchResultException |
||
| 110 | * @throws MapperSearchLimitException |
||
| 111 | * @throws MapperException |
||
| 112 | * |
||
| 113 | * @return Location |
||
| 114 | */ |
||
| 115 | public function location($location) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Add a new map. |
||
| 174 | * |
||
| 175 | * @param float $latitude |
||
| 176 | * @param float $longitude |
||
| 177 | * @param array $options |
||
| 178 | * |
||
| 179 | * @return self |
||
| 180 | */ |
||
| 181 | public function map($latitude, $longitude, array $options = []) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Add a new street view map. |
||
| 201 | * |
||
| 202 | * @param float $latitude |
||
| 203 | * @param float $longitude |
||
| 204 | * @param integer $heading |
||
| 205 | * @param integer $pitch |
||
| 206 | * @param array $options |
||
| 207 | * |
||
| 208 | * @return self |
||
| 209 | */ |
||
| 210 | public function streetview($latitude, $longitude, $heading, $pitch, array $options = []) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Add a new map marker. |
||
| 232 | * |
||
| 233 | * @param float $latitude |
||
| 234 | * @param float $longitude |
||
| 235 | * @param array $options |
||
| 236 | * |
||
| 237 | * @throws MapperException |
||
| 238 | * |
||
| 239 | * @return self |
||
| 240 | */ |
||
| 241 | public function marker($latitude, $longitude, array $options = []) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Add a new map information window. |
||
| 259 | * |
||
| 260 | * @param float $latitude |
||
| 261 | * @param float $longitude |
||
| 262 | * @param string $content |
||
| 263 | * @param array $options |
||
| 264 | * |
||
| 265 | * @throws MapperException |
||
| 266 | * |
||
| 267 | * @return self |
||
| 268 | */ |
||
| 269 | public function informationWindow($latitude, $longitude, $content, array $options = []) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Add a new map polyline. |
||
| 287 | * |
||
| 288 | * @param array $coordinates |
||
| 289 | * @param array $options |
||
| 290 | * |
||
| 291 | * @throws MapperException |
||
| 292 | * |
||
| 293 | * @return self |
||
| 294 | */ |
||
| 295 | public function polyline(array $coordinates = [], array $options = []) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Add a new map polygon. |
||
| 322 | * |
||
| 323 | * @param array $coordinates |
||
| 324 | * @param array $options |
||
| 325 | * |
||
| 326 | * @throws MapperException |
||
| 327 | * |
||
| 328 | * @return self |
||
| 329 | */ |
||
| 330 | View Code Duplication | public function polygon(array $coordinates = [], array $options = []) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Add a new map rectangle. |
||
| 358 | * |
||
| 359 | * @param array $coordinates |
||
| 360 | * @param array $options |
||
| 361 | * |
||
| 362 | * @throws MapperException |
||
| 363 | * |
||
| 364 | * @return self |
||
| 365 | */ |
||
| 366 | View Code Duplication | public function rectangle(array $coordinates = [], array $options = []) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Add a new map circle. |
||
| 394 | * |
||
| 395 | * @param array $coordinates |
||
| 396 | * @param array $options |
||
| 397 | * |
||
| 398 | * @throws MapperException |
||
| 399 | * |
||
| 400 | * @return self |
||
| 401 | */ |
||
| 402 | View Code Duplication | public function circle(array $coordinates = [], array $options = []) |
|
| 428 | |||
| 429 | } |
||
| 430 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.