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:
1 | <?php namespace Geocoder\Laravel; |
||
26 | class ProviderAndDumperAggregator |
||
27 | { |
||
28 | protected $results; |
||
29 | protected $aggregator; |
||
30 | |||
31 | public function __construct(int $limit = Geocoder::DEFAULT_RESULT_LIMIT) |
||
32 | { |
||
33 | $this->aggregator = new ProviderAggregator($limit); |
||
34 | } |
||
35 | |||
36 | public function all() : array |
||
37 | { |
||
38 | return $this->results->all(); |
||
39 | } |
||
40 | |||
41 | public function get() : AddressCollection |
||
42 | { |
||
43 | return $this->results; |
||
44 | } |
||
45 | |||
46 | public function dump($dumper) : Collection |
||
47 | { |
||
48 | $dumperClasses = collect([ |
||
49 | 'geojson' => GeoJson::class, |
||
50 | 'gpx' => Gpx::class, |
||
51 | 'kml' => Kml::class, |
||
52 | 'wkb' => Wkb::class, |
||
53 | 'wkt' => Wkt::class, |
||
54 | ]); |
||
55 | |||
56 | if (!$dumperClasses->has($dumper)) { |
||
57 | $errorMessage = implode('', [ |
||
58 | "The dumper specified ('{$dumper}') is invalid. Valid dumpers ", |
||
59 | "are: geojson, gpx, kml, wkb, wkt.", |
||
60 | ]); |
||
61 | throw new InvalidDumperException($errorMessage); |
||
62 | } |
||
63 | |||
64 | $dumperClass = $dumperClasses->get($dumper); |
||
65 | $dumper = new $dumperClass; |
||
66 | $results = collect($this->results->all()); |
||
67 | |||
68 | return $results->map(function ($result) use ($dumper) { |
||
69 | return $dumper->dump($result); |
||
70 | }); |
||
71 | } |
||
72 | |||
73 | public function geocodeQuery($query) |
||
74 | { |
||
75 | return $this->aggregator->geocodeQuery($query); |
||
76 | } |
||
77 | |||
78 | public function reverseQuery($query) |
||
79 | { |
||
80 | return $this->aggregator->reverseQuery($query); |
||
81 | } |
||
82 | |||
83 | public function getName() |
||
84 | { |
||
85 | return $this->aggregator->getName(); |
||
86 | } |
||
87 | |||
88 | View Code Duplication | public function geocode(string $value) : self |
|
1 ignored issue
–
show
|
|||
89 | { |
||
90 | $cacheId = str_slug($value); |
||
91 | $this->results = cache()->remember( |
||
92 | "geocoder-{$cacheId}", |
||
93 | config('geocoder.cache-duraction', 0), |
||
94 | function () use ($value) { |
||
95 | return $this->aggregator->geocode($value); |
||
96 | } |
||
97 | ); |
||
98 | |||
99 | return $this; |
||
100 | } |
||
101 | |||
102 | View Code Duplication | public function reverse(float $latitude, float $longitude) : self |
|
115 | |||
116 | public function limit($limit) |
||
117 | { |
||
118 | $this->aggregator->limit($limit); |
||
119 | |||
120 | return $this; |
||
121 | } |
||
122 | |||
123 | public function getLimit() |
||
124 | { |
||
127 | |||
128 | public function registerProvider($provider) |
||
134 | |||
135 | public function registerProviders($providers = []) |
||
141 | |||
142 | public function using($name) |
||
148 | |||
149 | public function getProviders() |
||
153 | |||
154 | protected function getProvider() |
||
158 | } |
||
159 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.