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) |
||
51 | |||
52 | /** |
||
53 | * Search for a location against Google Maps Api. |
||
54 | * |
||
55 | * @param string $location |
||
56 | * |
||
57 | * @return mixed |
||
58 | */ |
||
59 | protected function searchLocation($location) |
||
71 | |||
72 | /** |
||
73 | * Locate a location and return a Location instance. |
||
74 | * |
||
75 | * @param string $location |
||
76 | * |
||
77 | * @throws MapperArgumentException |
||
78 | * @throws MapperSearchException |
||
79 | * @throws MapperSearchResultException |
||
80 | * @throws MapperSearchLimitException |
||
81 | * @throws MapperException |
||
82 | * |
||
83 | * @return Location |
||
84 | */ |
||
85 | public function location($location) |
||
141 | |||
142 | /** |
||
143 | * Add a new map. |
||
144 | * |
||
145 | * @param float $latitude |
||
146 | * @param float $longitude |
||
147 | * @param array $options |
||
148 | * |
||
149 | * @return self |
||
150 | */ |
||
151 | public function map($latitude, $longitude, array $options = []) |
||
168 | |||
169 | /** |
||
170 | * Add a new street view map. |
||
171 | * |
||
172 | * @param float $latitude |
||
173 | * @param float $longitude |
||
174 | * @param integer $heading |
||
175 | * @param integer $pitch |
||
176 | * @param array $options |
||
177 | * |
||
178 | * @return self |
||
179 | */ |
||
180 | public function streetview($latitude, $longitude, $heading, $pitch, array $options = []) |
||
199 | |||
200 | /** |
||
201 | * Add a new map marker. |
||
202 | * |
||
203 | * @param float $latitude |
||
204 | * @param float $longitude |
||
205 | * @param array $options |
||
206 | * |
||
207 | * @throws MapperException |
||
208 | * |
||
209 | * @return self |
||
210 | */ |
||
211 | public function marker($latitude, $longitude, array $options = []) |
||
226 | |||
227 | /** |
||
228 | * Add a new map information window. |
||
229 | * |
||
230 | * @param float $latitude |
||
231 | * @param float $longitude |
||
232 | * @param string $content |
||
233 | * @param array $options |
||
234 | * |
||
235 | * @throws MapperException |
||
236 | * |
||
237 | * @return self |
||
238 | */ |
||
239 | public function informationWindow($latitude, $longitude, $content, array $options = []) |
||
254 | |||
255 | /** |
||
256 | * Add a new map polyline. |
||
257 | * |
||
258 | * @param array $coordinates |
||
259 | * @param array $options |
||
260 | * |
||
261 | * @throws MapperException |
||
262 | * |
||
263 | * @return self |
||
264 | */ |
||
265 | public function polyline(array $coordinates = [], array $options = []) |
||
289 | |||
290 | /** |
||
291 | * Add a new map polygon. |
||
292 | * |
||
293 | * @param array $coordinates |
||
294 | * @param array $options |
||
295 | * |
||
296 | * @throws MapperException |
||
297 | * |
||
298 | * @return self |
||
299 | */ |
||
300 | View Code Duplication | public function polygon(array $coordinates = [], array $options = []) |
|
325 | |||
326 | /** |
||
327 | * Add a new map rectangle. |
||
328 | * |
||
329 | * @param array $coordinates |
||
330 | * @param array $options |
||
331 | * |
||
332 | * @throws MapperException |
||
333 | * |
||
334 | * @return self |
||
335 | */ |
||
336 | View Code Duplication | public function rectangle(array $coordinates = [], array $options = []) |
|
361 | |||
362 | /** |
||
363 | * Add a new map circle. |
||
364 | * |
||
365 | * @param array $coordinates |
||
366 | * @param array $options |
||
367 | * |
||
368 | * @throws MapperException |
||
369 | * |
||
370 | * @return self |
||
371 | */ |
||
372 | View Code Duplication | public function circle(array $coordinates = [], array $options = []) |
|
398 | |||
399 | } |
||
400 |
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.