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 |
||
23 | class Geo extends Api |
||
24 | { |
||
25 | /** |
||
26 | * The response array. |
||
27 | * |
||
28 | * @var array|null |
||
29 | */ |
||
30 | protected $response = null; |
||
31 | |||
32 | /** |
||
33 | * The cache directory. |
||
34 | * |
||
35 | * @var string|null |
||
36 | */ |
||
37 | protected $cacheDir = __DIR__ . '/cache/'; |
||
38 | |||
39 | /** |
||
40 | * The cache file name. |
||
41 | * |
||
42 | * @var string|null |
||
43 | */ |
||
44 | protected $file = __DIR__ . '/cache/' . 'geo_'; |
||
45 | |||
46 | /** |
||
47 | * Address reverse geo. |
||
48 | * |
||
49 | * @param string $latitude |
||
50 | * @param string $longitude |
||
51 | * @param string $lang |
||
52 | * @return Geo |
||
53 | */ |
||
54 | public function coordinates($latitude, $longitude, $lang = 'A') |
||
81 | |||
82 | /** |
||
83 | * Address reverse geo. |
||
84 | * |
||
85 | * @param string $latitude |
||
86 | * @param string $longitude |
||
87 | * @param string $lang |
||
88 | * @return array |
||
89 | */ |
||
90 | public function coords($latitude, $longitude, $lang = 'A') |
||
94 | |||
95 | /** |
||
96 | * Address reverse geo. |
||
97 | * |
||
98 | * @param string $latitude |
||
99 | * @param string $longitude |
||
100 | * @param string $lang |
||
101 | * @return array |
||
102 | */ |
||
103 | public function location($latitude, $longitude, $lang = 'A') |
||
107 | |||
108 | /** |
||
109 | * Returns a the response. |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | public function get() |
||
119 | |||
120 | /** |
||
121 | * Returns a the city name of reversed address. |
||
122 | * |
||
123 | * @return array |
||
124 | */ |
||
125 | public function getCity() |
||
131 | |||
132 | /** |
||
133 | * Returns a the city name of reversed address. |
||
134 | * |
||
135 | * @return array |
||
136 | */ |
||
137 | public function city() |
||
141 | |||
142 | /** |
||
143 | * Returns a the address 1 of reversed address. |
||
144 | * |
||
145 | * @return array |
||
146 | */ |
||
147 | public function getAddressOne() |
||
153 | |||
154 | /** |
||
155 | * Returns a the address 1 of reversed address. |
||
156 | * |
||
157 | * @return array |
||
158 | */ |
||
159 | public function addressOne() |
||
163 | |||
164 | /** |
||
165 | * Returns a the address 2 of reversed address. |
||
166 | * |
||
167 | * @return array |
||
168 | */ |
||
169 | public function getAddressTwo() |
||
175 | |||
176 | /** |
||
177 | * Returns a the address 2 of reversed address. |
||
178 | * |
||
179 | * @return array |
||
180 | */ |
||
181 | public function addressTwo() |
||
185 | |||
186 | /** |
||
187 | * Returns a the street name of reversed address. |
||
188 | * |
||
189 | * @return array |
||
190 | */ |
||
191 | public function getStreet() |
||
197 | |||
198 | /** |
||
199 | * Returns a the street name of reversed address. |
||
200 | * |
||
201 | * @return array |
||
202 | */ |
||
203 | public function street() |
||
207 | |||
208 | /** |
||
209 | * Returns a the region name of reversed address. |
||
210 | * |
||
211 | * @return array |
||
212 | */ |
||
213 | public function getRegion() |
||
219 | |||
220 | /** |
||
221 | * Returns a the region name of reversed address. |
||
222 | * |
||
223 | * @return array |
||
224 | */ |
||
225 | public function region() |
||
229 | |||
230 | /** |
||
231 | * Returns a the district name of reversed address. |
||
232 | * |
||
233 | * @return array |
||
234 | */ |
||
235 | public function getDistrict() |
||
241 | |||
242 | /** |
||
243 | * Returns a the district name of reversed address. |
||
244 | * |
||
245 | * @return array |
||
246 | */ |
||
247 | public function district() |
||
251 | |||
252 | /** |
||
253 | * Returns a the building number name of reversed address. |
||
254 | * |
||
255 | * @return array |
||
256 | */ |
||
257 | public function getBuildingNumber() |
||
263 | |||
264 | /** |
||
265 | * Returns a the building number name of reversed address. |
||
266 | * |
||
267 | * @return array |
||
268 | */ |
||
269 | public function buildingNumber() |
||
273 | |||
274 | /** |
||
275 | * Returns a the post code name of reversed address. |
||
276 | * |
||
277 | * @return array |
||
278 | */ |
||
279 | public function getPostCode() |
||
285 | |||
286 | /** |
||
287 | * Returns a the post code name of reversed address. |
||
288 | * |
||
289 | * @return array |
||
290 | */ |
||
291 | public function postCode() |
||
295 | |||
296 | /** |
||
297 | * Returns a the post code name of reversed address. |
||
298 | * |
||
299 | * @return array |
||
300 | */ |
||
301 | public function getZip() |
||
305 | |||
306 | /** |
||
307 | * Returns a the post code name of reversed address. |
||
308 | * |
||
309 | * @return array |
||
310 | */ |
||
311 | public function zip() |
||
315 | |||
316 | /** |
||
317 | * Returns a the additional number name of reversed address. |
||
318 | * |
||
319 | * @return array |
||
320 | */ |
||
321 | public function getAdditionalNumber() |
||
327 | |||
328 | /** |
||
329 | * Returns a the additional number name of reversed address. |
||
330 | * |
||
331 | * @return array |
||
332 | */ |
||
333 | public function additionalNumber() |
||
337 | |||
338 | /** |
||
339 | * Check if coordinates() method was called first. |
||
340 | * |
||
341 | * @return void |
||
342 | * @throws \BadMethodCallException |
||
343 | */ |
||
344 | protected function check() |
||
350 | } |
||
351 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.