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; |
||
28 | class ProviderAndDumperAggregator |
||
29 | { |
||
30 | protected $aggregator; |
||
31 | protected $limit; |
||
32 | protected $results; |
||
33 | |||
34 | 23 | public function __construct() |
|
39 | |||
40 | /** |
||
41 | * @deprecated Use `get()` instead. |
||
42 | */ |
||
43 | 1 | public function all() : array |
|
47 | |||
48 | 16 | public function get() : Collection |
|
52 | |||
53 | 2 | public function dump(string $dumper) : Collection |
|
54 | { |
||
55 | 2 | $dumperClasses = collect([ |
|
56 | 2 | 'geojson' => GeoJson::class, |
|
57 | 'gpx' => Gpx::class, |
||
58 | 'kml' => Kml::class, |
||
59 | 'wkb' => Wkb::class, |
||
60 | 'wkt' => Wkt::class, |
||
61 | ]); |
||
62 | |||
63 | 2 | if (!$dumperClasses->has($dumper)) { |
|
64 | 1 | $errorMessage = implode('', [ |
|
65 | 1 | "The dumper specified ('{$dumper}') is invalid. Valid dumpers ", |
|
66 | 1 | "are: geojson, gpx, kml, wkb, wkt.", |
|
67 | ]); |
||
68 | 1 | throw new InvalidDumperException($errorMessage); |
|
69 | } |
||
70 | |||
71 | 1 | $dumperClass = $dumperClasses->get($dumper); |
|
72 | 1 | $dumper = new $dumperClass; |
|
73 | 1 | $results = collect($this->results->all()); |
|
74 | |||
75 | 1 | return $results->map(function ($result) use ($dumper) { |
|
76 | 1 | return $dumper->dump($result); |
|
77 | 1 | }); |
|
78 | } |
||
79 | |||
80 | 1 | public function geocodeQuery(GeocodeQuery $query) : self |
|
81 | { |
||
82 | 1 | $cacheKey = serialize($query); |
|
83 | 1 | $this->results = app('cache')->remember( |
|
84 | 1 | "geocoder-{$cacheKey}", |
|
85 | 1 | config('geocoder.cache-duration', 0), |
|
86 | 1 | function () use ($query) { |
|
87 | 1 | return collect($this->aggregator->geocodeQuery($query)); |
|
88 | 1 | } |
|
89 | ); |
||
90 | |||
91 | 1 | return $this; |
|
92 | } |
||
93 | |||
94 | 1 | public function reverseQuery(ReverseQuery $query) : self |
|
95 | { |
||
96 | 1 | $cacheKey = serialize($query); |
|
97 | 1 | $this->results = app('cache')->remember( |
|
98 | 1 | "geocoder-{$cacheKey}", |
|
99 | 1 | config('geocoder.cache-duration', 0), |
|
100 | 1 | function () use ($query) { |
|
101 | 1 | return collect($this->aggregator->reverseQuery($query)); |
|
102 | 1 | } |
|
103 | ); |
||
104 | |||
105 | 1 | return $this; |
|
106 | } |
||
107 | |||
108 | 1 | public function getName() : string |
|
112 | |||
113 | 15 | View Code Duplication | public function geocode(string $value) : self |
114 | { |
||
115 | 15 | $cacheKey = str_slug(strtolower(urlencode($value))); |
|
116 | 15 | $this->results = app('cache')->remember( |
|
117 | 15 | "geocoder-{$cacheKey}", |
|
118 | 15 | config('geocoder.cache-duration', 0), |
|
119 | 15 | function () use ($value) { |
|
120 | 15 | return collect($this->aggregator->geocode($value)); |
|
121 | 15 | } |
|
122 | ); |
||
123 | |||
124 | 15 | return $this; |
|
125 | } |
||
126 | |||
127 | 1 | View Code Duplication | public function reverse(float $latitude, float $longitude) : self |
128 | { |
||
129 | 1 | $cacheKey = str_slug(strtolower(urlencode("{$latitude}-{$longitude}"))); |
|
130 | 1 | $this->results = app('cache')->remember( |
|
131 | 1 | "geocoder-{$cacheKey}", |
|
132 | 1 | config('geocoder.cache-duration', 0), |
|
133 | 1 | function () use ($latitude, $longitude) { |
|
134 | 1 | return collect($this->aggregator->reverse($latitude, $longitude)); |
|
135 | 1 | } |
|
136 | ); |
||
137 | |||
138 | 1 | return $this; |
|
139 | } |
||
140 | |||
141 | 1 | public function limit(int $limit) : self |
|
149 | |||
150 | 1 | public function getLimit() : int |
|
154 | |||
155 | 1 | public function registerProvider($provider) : self |
|
161 | |||
162 | 23 | public function registerProviders(array $providers = []) : self |
|
168 | |||
169 | 5 | public function using(string $name) : self |
|
175 | |||
176 | 2 | public function getProviders() : Collection |
|
180 | |||
181 | /** |
||
182 | * @deprecated Use `getProviders()` instead. |
||
183 | */ |
||
184 | 1 | public function getProvider() |
|
185 | { |
||
188 | |||
189 | 23 | public function registerProvidersFromConfig(Collection $providers) : self |
|
195 | |||
196 | protected function getProvidersFromConfiguration(Collection $providers) : array |
||
211 | |||
212 | 23 | protected function getArguments(array $arguments, string $provider) : array |
|
228 | |||
229 | 23 | protected function getAdapterClass(string $provider) : string |
|
242 | } |
||
243 |