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 |
||
15 | class GeoFixerFacade |
||
16 | { |
||
17 | /** |
||
18 | * @var KLogger\Logger |
||
19 | */ |
||
20 | private $logger; |
||
21 | |||
22 | /** |
||
23 | * @var GeoFixer |
||
24 | */ |
||
25 | private $geo; |
||
26 | |||
27 | /** |
||
28 | * Инициализируем (если передан параметр) БД и логирование |
||
29 | * |
||
30 | * GeoFixerFacade constructor. |
||
31 | * @param bool $fias |
||
32 | * @param array $config |
||
33 | */ |
||
34 | 3 | public function __construct($fias = false, $config = null) |
|
52 | |||
53 | /** |
||
54 | * Поиск похожих слов в массиве |
||
55 | * Логирование ошибок |
||
56 | * |
||
57 | * @param $word |
||
58 | * @param $search_array |
||
59 | * @param bool $strict_search |
||
60 | * |
||
61 | * @return string|false |
||
62 | */ |
||
63 | 3 | View Code Duplication | public function findSimilarWord($word, $search_array, $strict_search = false) |
77 | |||
78 | /** |
||
79 | * Поиск кода региона в базе КЛАДР |
||
80 | * Логирование ошибок |
||
81 | * |
||
82 | * @param $region |
||
83 | * @param bool $first_letters |
||
84 | * @param bool $strict_search |
||
85 | * |
||
86 | * @return string|false |
||
87 | */ |
||
88 | 4 | public function findKladrRegion($region, $first_letters = false, $strict_search = false) |
|
89 | { |
||
90 | 4 | $result = $this->findFiasRegion($region, $first_letters, $strict_search); |
|
91 | |||
92 | 4 | if ($result !== false) { |
|
93 | 3 | return str_pad($result, 13, '0'); |
|
94 | } |
||
95 | |||
96 | 1 | return $result; |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * Поиск кода региона в базе ФИАС |
||
101 | * Логирование ошибок |
||
102 | * |
||
103 | * @param $region |
||
104 | * @param bool $first_letters |
||
105 | * @param bool $strict_search |
||
106 | * |
||
107 | * @return string|false |
||
108 | */ |
||
109 | 8 | View Code Duplication | public function findFiasRegion($region, $first_letters = false, $strict_search = false) |
110 | { |
||
111 | 8 | $this->geo->isStrict($strict_search); |
|
112 | 8 | $this->geo->isFirstLetters($first_letters); |
|
113 | |||
114 | 8 | $result = $this->geo->findFiasRegion($region); |
|
115 | |||
116 | 8 | if ($result === false) { |
|
117 | 2 | $this->logger->warning('Не найден регион ' . $region . ' в базе ФИАС'); |
|
118 | 2 | $this->logger->warning('Строгий режим: ' . (int)$strict_search); |
|
119 | 2 | $this->logger->warning('Режим "совпадают первые буквы": ' . (int)$first_letters . PHP_EOL); |
|
120 | } |
||
121 | |||
122 | 8 | return $result; |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Поиск ID городов, или ID городов и поселений по коду региона в базе ФИАС |
||
127 | * Логирование ошибок |
||
128 | * |
||
129 | * @param $city |
||
130 | * @param $region_code |
||
131 | * @param bool $first_letters |
||
132 | * @param bool $strict_search |
||
133 | * |
||
134 | * @return string|false |
||
135 | */ |
||
136 | 4 | View Code Duplication | public function findFiasSettlement($city, $region_code, $first_letters = false, $strict_search = false, $full_settlements = false) |
152 | |||
153 | /** |
||
154 | * Поиск ID городов, или ID городов и поселений по коду региона в базе КЛАДР |
||
155 | * Логирование ошибок |
||
156 | * |
||
157 | * @param $city |
||
158 | * @param $region_code |
||
159 | * @param bool $first_letters |
||
160 | * @param bool $strict_search |
||
161 | * |
||
162 | * @return string|false |
||
163 | */ |
||
164 | 4 | public function findKladrSettlement($city, $region_code, $first_letters = false, $strict_search = false, $full_settlements = false) |
|
182 | |||
183 | /** |
||
184 | * Поиск ID улицы по ID города в базе ФИАС |
||
185 | * Логирование ошибок |
||
186 | * |
||
187 | * @param $street |
||
188 | * @param $city_id |
||
189 | * @param bool $first_letters |
||
190 | * @param bool $strict_search |
||
191 | * |
||
192 | * @return string|false |
||
193 | */ |
||
194 | 3 | View Code Duplication | public function findFiasStreet($street, $city_id, $first_letters = false, $strict_search = false) |
209 | |||
210 | /** |
||
211 | * Поиск кода улицы по коду города в базе КЛАДР |
||
212 | * Логирование ошибок |
||
213 | * |
||
214 | * @param $street |
||
215 | * @param $city_code |
||
216 | * @param bool $first_letters |
||
217 | * @param bool $strict_search |
||
218 | * |
||
219 | * @return string|false |
||
220 | */ |
||
221 | 3 | View Code Duplication | public function findKladrStreet($street, $city_code, $first_letters = false, $strict_search = false) |
236 | |||
237 | |||
238 | /** |
||
239 | * Поиск ID дома по ID улицы в базе ФИАС |
||
240 | * |
||
241 | * @param $house |
||
242 | * @param $street_id |
||
243 | * @param bool $building |
||
244 | * |
||
245 | * @return string|false |
||
246 | */ |
||
247 | 4 | public function findFiasHouse($house, $street_id, $building = false) |
|
261 | } |
||
262 |
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.