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 |
||
17 | class GeoFixer |
||
18 | { |
||
19 | protected $strict = false; |
||
20 | protected $first_letters = false; |
||
21 | protected $full_settlements = false; |
||
22 | |||
23 | protected $string_helper; |
||
24 | protected $fuzzy_helper; |
||
25 | |||
26 | /** |
||
27 | * GeoFixer construct |
||
28 | */ |
||
29 | 3 | public function __construct() |
|
34 | |||
35 | /** |
||
36 | * @param $region |
||
37 | * |
||
38 | * @return bool|mixed |
||
39 | */ |
||
40 | 8 | public function findFiasRegion($region) |
|
41 | { |
||
42 | 8 | $regions = new RegionsDatabaseQuery(); |
|
43 | 8 | $regions = $regions->getRegions(); |
|
44 | |||
45 | 8 | if (is_integer($this->first_letters)) { |
|
46 | 2 | $regions = $regions->firstLetters(substr($region, 0, $this->first_letters)); |
|
47 | 2 | } |
|
48 | |||
49 | 8 | $regions = $regions->findAll(); |
|
50 | |||
51 | 8 | $titles = []; |
|
52 | 8 | $regions_with_codes = []; |
|
53 | |||
54 | 8 | foreach ($regions as $v) { |
|
55 | 8 | $regions_with_codes[$v['title']] = $v['code']; |
|
56 | 8 | $titles[] = $v['title']; |
|
57 | 8 | } |
|
58 | |||
59 | 8 | $result = $this->findSimilarWord($region, $titles); |
|
60 | |||
61 | 8 | return $result ? $regions_with_codes[$result] : false; |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param $word |
||
66 | * @param $search_array |
||
67 | * |
||
68 | * @return string |
||
69 | */ |
||
70 | 23 | public function findSimilarWord($word, $search_array) |
|
71 | { |
||
72 | 23 | if (in_array($word, $search_array)) { |
|
73 | 7 | return $word; |
|
74 | } |
||
75 | |||
76 | 19 | $word = $this->string_helper->wordTranslit($word); |
|
77 | |||
78 | 19 | $translited_words = $this->string_helper->arrayTranslit($search_array); |
|
79 | |||
80 | 19 | if ($this->strict == true) { |
|
|
|||
81 | 9 | $result = $this->fuzzy_helper->findBestMatch($word, $translited_words); |
|
82 | 9 | return $result; |
|
83 | } |
||
84 | |||
85 | 10 | $result = key($this->fuzzy_helper->findMostSimilarWords($word, $translited_words)); |
|
86 | |||
87 | 10 | return $result ? $result : false; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param $city |
||
92 | * @param $region_code |
||
93 | * |
||
94 | * @return bool|mixed |
||
95 | */ |
||
96 | 4 | View Code Duplication | public function findFiasSettlements($city, $region_code) |
119 | |||
120 | /** |
||
121 | * @param $city |
||
122 | * @param $region_code |
||
123 | * |
||
124 | * @return bool|mixed |
||
125 | */ |
||
126 | 4 | public function findKladrSettlements($city, $region_code) |
|
156 | |||
157 | /** |
||
158 | * @param $street |
||
159 | * @param $city_id |
||
160 | * |
||
161 | * @return bool|mixed |
||
162 | */ |
||
163 | 3 | View Code Duplication | public function findFiasStreets($street, $city_id) |
186 | |||
187 | /** |
||
188 | * @param $street |
||
189 | * @param $city_code |
||
190 | * |
||
191 | * @return bool|mixed |
||
192 | */ |
||
193 | 3 | public function findKladrStreets($street, $city_code) |
|
230 | |||
231 | /** |
||
232 | * @param $house |
||
233 | * @param $street_id |
||
234 | * |
||
235 | * @return bool |
||
236 | */ |
||
237 | 4 | public function findFiasHouses($house, $street_id, $building = false) |
|
250 | |||
251 | /** |
||
252 | * Включаем строгий режим поиска |
||
253 | * |
||
254 | * @param bool $strict |
||
255 | */ |
||
256 | 23 | public function isStrict($strict = false) |
|
260 | |||
261 | |||
262 | /** |
||
263 | * Сколько первых букв должны совпадать при поиске по базам ФИАС |
||
264 | * |
||
265 | * (теоретически, снизит кол-во слов, которые придется обрабатывать алгоритмом и тем самым увеличит скорость работы, но может не найти слово, если первые буквы не совпадают |
||
266 | * из-за опечатки или префиксов) |
||
267 | * |
||
268 | * @param bool $count |
||
269 | */ |
||
270 | 20 | public function isFirstLetters($count = false) |
|
276 | |||
277 | /** |
||
278 | * Только города, или города и поселения |
||
279 | * |
||
280 | * @param bool $is_full |
||
281 | */ |
||
282 | 8 | public function isFullSettlements($is_full = false) |
|
286 | } |
||
287 | |||
288 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.