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 |
||
24 | class ProviderAggregator implements Geocoder |
||
25 | { |
||
26 | /** |
||
27 | * @var Provider[] |
||
28 | */ |
||
29 | private $providers = []; |
||
30 | |||
31 | /** |
||
32 | * @var Provider |
||
33 | */ |
||
34 | private $provider; |
||
35 | |||
36 | /** |
||
37 | * @var int |
||
38 | */ |
||
39 | private $limit; |
||
40 | |||
41 | /** |
||
42 | * A callable that decided what provider to use. |
||
43 | * |
||
44 | * @var callable |
||
45 | */ |
||
46 | private $decider; |
||
47 | |||
48 | /** |
||
49 | * @param callable|null $decider |
||
50 | * @param int $limit |
||
51 | */ |
||
52 | 9 | public function __construct(callable $decider = null, int $limit = Geocoder::DEFAULT_RESULT_LIMIT) |
|
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | 1 | View Code Duplication | public function geocodeQuery(GeocodeQuery $query): Collection |
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | 1 | View Code Duplication | public function reverseQuery(ReverseQuery $query): Collection |
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | public function getName(): string |
||
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | 1 | public function geocode(string $value): Collection |
|
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | 1 | public function reverse(float $latitude, float $longitude): Collection |
|
107 | |||
108 | /** |
||
109 | * Registers a new provider to the aggregator. |
||
110 | * |
||
111 | * @param Provider $provider |
||
112 | * |
||
113 | * @return ProviderAggregator |
||
114 | */ |
||
115 | 5 | public function registerProvider(Provider $provider): self |
|
121 | |||
122 | /** |
||
123 | * Registers a set of providers. |
||
124 | * |
||
125 | * @param Provider[] $providers |
||
126 | * |
||
127 | * @return ProviderAggregator |
||
128 | */ |
||
129 | 2 | public function registerProviders(array $providers = []): self |
|
137 | |||
138 | /** |
||
139 | * Sets the default provider to use. |
||
140 | * |
||
141 | * @param string $name |
||
142 | * |
||
143 | * @return ProviderAggregator |
||
144 | */ |
||
145 | 3 | public function using(string $name): self |
|
155 | |||
156 | /** |
||
157 | * Returns all registered providers indexed by their name. |
||
158 | * |
||
159 | * @return Provider[] |
||
160 | */ |
||
161 | 1 | public function getProviders(): array |
|
165 | |||
166 | /** |
||
167 | * Get a provider to use for this query. |
||
168 | * |
||
169 | * @param GeocodeQuery|ReverseQuery $query |
||
170 | * @param Provider[] $providers |
||
171 | * @param Provider $currentProvider |
||
172 | * |
||
173 | * @return Provider |
||
174 | * |
||
175 | * @throws ProviderNotRegistered |
||
176 | */ |
||
177 | 4 | private static function getProvider($query, array $providers, Provider $currentProvider = null): Provider |
|
192 | } |
||
193 |
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.