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 |
||
18 | class GeoFixer |
||
19 | { |
||
20 | protected $strict = false; |
||
21 | protected $first_letters = false; |
||
22 | protected $full_settlements = false; |
||
23 | |||
24 | protected $geo_with_ids = []; |
||
25 | protected $geo_titles = []; |
||
26 | protected $title_name; |
||
27 | protected $code_name; |
||
28 | |||
29 | protected $string_helper; |
||
30 | protected $fuzzy_helper; |
||
31 | |||
32 | /** |
||
33 | * GeoFixer construct |
||
34 | */ |
||
35 | 3 | public function __construct() |
|
43 | |||
44 | /** |
||
45 | * @param $region |
||
46 | * |
||
47 | * @return bool|mixed |
||
48 | */ |
||
49 | 8 | View Code Duplication | public function findFiasRegion($region) |
68 | |||
69 | /** |
||
70 | * @param $word |
||
71 | * @param $search_array |
||
72 | * |
||
73 | * @return string|false|null |
||
74 | */ |
||
75 | 23 | public function findSimilarWord($word, $search_array) |
|
91 | |||
92 | /** |
||
93 | * @param $city |
||
94 | * @param $region_code |
||
95 | * |
||
96 | * @return bool|mixed |
||
97 | */ |
||
98 | 4 | View Code Duplication | public function findFiasSettlements($city, $region_code) |
117 | |||
118 | /** |
||
119 | * @param $city |
||
120 | * @param $region_code |
||
121 | * |
||
122 | * @return bool|mixed |
||
123 | */ |
||
124 | 4 | View Code Duplication | public function findKladrSettlements($city, $region_code) |
143 | |||
144 | /** |
||
145 | * @param $street |
||
146 | * @param $city_id |
||
147 | * |
||
148 | * @return bool|mixed |
||
149 | */ |
||
150 | 3 | View Code Duplication | public function findFiasStreets($street, $city_id) |
169 | |||
170 | /** |
||
171 | * @param $street |
||
172 | * @param $city_code |
||
173 | * |
||
174 | * @return bool|mixed |
||
175 | */ |
||
176 | 3 | public function findKladrStreets($street, $city_code) |
|
203 | |||
204 | /** |
||
205 | * @param $house |
||
206 | * @param $street_id |
||
207 | * |
||
208 | * @return bool |
||
209 | */ |
||
210 | 4 | public function findFiasHouses($house, $street_id, $building = false) |
|
223 | |||
224 | /** |
||
225 | * Включаем строгий режим поиска |
||
226 | * |
||
227 | * @param bool $strict |
||
228 | */ |
||
229 | 23 | public function isStrict($strict = false) |
|
233 | |||
234 | |||
235 | /** |
||
236 | * Сколько первых букв должны совпадать при поиске по базам ФИАС |
||
237 | * |
||
238 | * (теоретически, снизит кол-во слов, которые придется обрабатывать алгоритмом и тем самым увеличит скорость работы, но может не найти слово, если первые буквы не совпадают |
||
239 | * из-за опечатки или префиксов) |
||
240 | * |
||
241 | * @param int|bool $count |
||
242 | */ |
||
243 | 20 | public function isFirstLetters($count = false) |
|
249 | |||
250 | /** |
||
251 | * Только города, или города и поселения |
||
252 | * |
||
253 | * @param bool $is_full |
||
254 | */ |
||
255 | 8 | public function isFullSettlements($is_full = false) |
|
259 | |||
260 | /** |
||
261 | * @param $geo_array |
||
262 | */ |
||
263 | 20 | protected function geoDataHandler($geo_array) |
|
268 | } |
||
269 | |||
270 |
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.