Test Failed
Branch master (a0b8e2)
by Konstantin
02:59
created

GeoFixerFacade   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 247
Duplicated Lines 30.36 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 21
c 2
b 0
f 2
lcom 1
cbo 3
dl 75
loc 247
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 4
A findSimilarWord() 14 14 2
A findFiasRegion() 15 15 2
A findKladrRegion() 0 10 2
A findFiasSettlement() 16 16 2
A findKladrSettlement() 0 18 2
A findFiasStreet() 15 15 2
A findKladrStreet() 15 15 2
A findFiasHouse() 0 14 3

How to fix   Duplicated Code   

Duplicated Code

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
2
namespace GeoFixer;
3
4
use GeoFixer\models\GeoFixer;
5
use GeoFixer\models\DatabaseConnection;
6
7
use Katzgrau\KLogger;
8
use Exception;
9
10
/**
11
 * Class GeoFixerFacade
12
 *
13
 * @package GeoFixer
14
 */
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
    public function __construct($fias = false, $config = null)
35
    {
36
        $this->logger = new KLogger\Logger(dirname(dirname(__FILE__)) . '/logs');
37
38
        if ($fias == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
39
            if ($config == null) {
40
                $config = include 'config/database.php';
41
            }
42
43
            try {
44
                DatabaseConnection::makeConnection($config);
45
            } catch (Exception $e) {
46
                $this->logger->error('Exception: ' . $e->getMessage());
47
            }
48
        }
49
50
        $this->geo = new GeoFixer();
51
    }
52
53
    /**
54
     * Поиск похожих слов в массиве
55
     * Логирование ошибок
56
     *
57
     * @param $word
58
     * @param $search_array
59
     * @param bool $strict_search
60
     *
61
     * @return string|false
62
     */
63 View Code Duplication
    public function findSimilarWord($word, $search_array, $strict_search = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
64
    {
65
        $this->geo->isStrict($strict_search);
66
67
        $result = $this->geo->findSimilarWord($word, $search_array);
68
69
        if ($result == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $result of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
70
            $this->logger->warning('Не найдено похожее слово: ' . $word);
71
            $this->logger->warning('Строгий режим: ' . (int)$strict_search);
72
            $this->logger->warning('Массива для поиска: ' . implode($search_array, ', ') . PHP_EOL);
73
        }
74
75
        return $result;
76
    }
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 View Code Duplication
    public function findFiasRegion($region, $first_letters = false, $strict_search = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
89
    {
90
        $this->geo->isStrict($strict_search);
91
        $this->geo->isFirstLetters($first_letters);
92
93
        $result =  $this->geo->findFiasRegion($region);
94
95
        if ($result == false) {
96
            $this->logger->warning('Не найден регион ' . $region . ' в базе ФИАС');
97
            $this->logger->warning('Строгий режим: ' . (int)$strict_search);
98
            $this->logger->warning('Режим "совпадают первые буквы": ' . (int)$first_letters . PHP_EOL);
99
        }
100
101
        return $result;
102
    }
103
104
    /**
105
     * Поиск кода региона в базе КЛАДР
106
     * Логирование ошибок
107
     *
108
     * @param $region
109
     * @param bool $first_letters
110
     * @param bool $strict_search
111
     *
112
     * @return string|false
113
     */
114
    public function findKladrRegion($region, $first_letters = false, $strict_search = false)
115
    {
116
        $result = $this->findFiasRegion($region, $first_letters, $strict_search);
117
118
        if ($result != false) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $result of type string|false against false; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
119
            return str_pad($result, 13, '0');
120
        }
121
122
        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 View Code Duplication
    public function findFiasSettlement($city, $region_code, $first_letters = false, $strict_search = false, $full_settlements = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
137
    {
138
        $this->geo->isStrict($strict_search);
139
        $this->geo->isFirstLetters($first_letters);
140
        $this->geo->isFullSettlements($full_settlements);
141
142
        $result =  $this->geo->findFiasSettlements($city, $region_code);
143
144
        if ($result == false) {
145
            $this->logger->warning('Не найден город ' . $city . ' в регионе с кодом ' . $region_code . ' базы ФИАС');
146
            $this->logger->warning('Строгий режим: ' . (int)$strict_search);
147
            $this->logger->warning('Режим "совпадают первые буквы": ' . (int)$first_letters . PHP_EOL);
148
        }
149
150
        return $result;
151
    }
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
    public function findKladrSettlement($city, $region_code, $first_letters = false, $strict_search = false, $full_settlements = false)
165
    {
166
        $this->geo->isStrict($strict_search);
167
        $this->geo->isFirstLetters($first_letters);
168
        $this->geo->isFullSettlements($full_settlements);
169
170
        $region_code = substr($region_code, 0, 2);
171
172
        $result =  $this->geo->findKladrSettlements($city, $region_code);
173
174
        if ($result == false) {
175
            $this->logger->warning('Не найден город ' . $city . ' в регионе с кодом ' . $region_code . ' базы ФИАС');
176
            $this->logger->warning('Строгий режим: ' . (int)$strict_search);
177
            $this->logger->warning('Режим "совпадают первые буквы": ' . (int)$first_letters . PHP_EOL);
178
        }
179
180
        return $result;
181
    }
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 View Code Duplication
    public function findFiasStreet($street, $city_id, $first_letters = false, $strict_search = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
195
    {
196
        $this->geo->isStrict($strict_search);
197
        $this->geo->isFirstLetters($first_letters);
198
199
        $result =  $this->geo->findFiasStreets($street, $city_id);
200
201
        if ($result == false) {
202
            $this->logger->warning('Не найдена улица ' . $street . ' в городе с id ' . $city_id . ' базы ФИАС');
203
            $this->logger->warning('Строгий режим: ' . (int)$strict_search);
204
            $this->logger->warning('Режим "совпадают первые буквы": ' . (int)$first_letters . PHP_EOL);
205
        }
206
207
        return $result;
208
    }
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 View Code Duplication
    public function findKladrStreet($street, $city_code, $first_letters = false, $strict_search = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
222
    {
223
        $this->geo->isStrict($strict_search);
224
        $this->geo->isFirstLetters($first_letters);
225
226
        $result =  $this->geo->findKladrStreets($street, $city_code);
227
228
        if ($result == false) {
229
            $this->logger->warning('Не найдена улица ' . $street . ' в городе с кодом ' . $city_code . ' базы КЛАДР');
230
            $this->logger->warning('Строгий режим: ' . (int)$strict_search);
231
            $this->logger->warning('Режим "совпадают первые буквы": ' . (int)$first_letters . PHP_EOL);
232
        }
233
234
        return $result;
235
    }
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
    public function findFiasHouse($house, $street_id, $building = false)
248
    {
249
        $result =  $this->geo->findFiasHouses($house, $street_id, $building);
250
251
        if ($result == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
252
            if ($building) {
253
                $this->logger->warning('Не найден дом ' .  $house . ', корпус ' . $building . ' на улице с id ' . $street_id . ' базы ФИАС' . PHP_EOL);
254
            } else {
255
                $this->logger->warning('Не найден дом ' .  $house . ' на улице с id ' . $street_id . ' базы ФИАС: ' . $house . PHP_EOL);
256
            }
257
        }
258
259
        return $result;
260
    }
261
}