| Conditions | 14 |
| Paths | 64 |
| Total Lines | 67 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 48 | public function updateDb() { |
||
| 49 | // Обновление файла базы данных Sypex Geo |
||
| 50 | // Настройки |
||
| 51 | $url = 'https://sypexgeo.net/files/SxGeoCity_utf8.zip'; // Путь к скачиваемому файлу |
||
| 52 | $dat_file_dir = App::$primary->path . $this->geographyDbDir; // Каталог в который сохранять dat-файл |
||
| 53 | $last_updated_file = $dat_file_dir . '/SxGeo.upd'; // Файл в котором хранится дата последнего обновления |
||
| 54 | $info = false; // Вывод сообщений о работе, true заменить на false после установки в cron |
||
| 55 | |||
| 56 | // Конец настроек |
||
| 57 | |||
| 58 | set_time_limit(600); |
||
| 59 | //error_reporting(E_ALL); |
||
| 60 | //header('Content-type: text/plain; charset=utf8'); |
||
| 61 | |||
| 62 | $t = microtime(1); |
||
| 63 | Tools::createDir($dat_file_dir); |
||
| 64 | $types = array( |
||
| 65 | 'Country' => 'SxGeo.dat', |
||
| 66 | 'City' => 'SxGeoCity.dat', |
||
| 67 | 'Max' => 'SxGeoMax.dat', |
||
| 68 | ); |
||
| 69 | // Скачиваем архив |
||
| 70 | preg_match("/(Country|City|Max)/", pathinfo($url, PATHINFO_BASENAME), $m); |
||
| 71 | $type = $m[1]; |
||
| 72 | $dat_file = $types[$type]; |
||
| 73 | if ($info) echo "Скачиваем архив с сервера\n"; |
||
| 74 | |||
| 75 | $fp = fopen($dat_file_dir . '/SxGeoTmp.zip', 'wb'); |
||
| 76 | $ch = curl_init($url); |
||
| 77 | curl_setopt_array($ch, array( |
||
| 78 | CURLOPT_FILE => $fp, |
||
| 79 | CURLOPT_HTTPHEADER => file_exists($last_updated_file) ? array("If-Modified-Since: " . file_get_contents($last_updated_file)) : array(), |
||
| 80 | )); |
||
| 81 | if (!curl_exec($ch)) { |
||
| 82 | if ($info) echo 'Ошибка при скачивании архива'; |
||
| 83 | return; |
||
| 84 | } |
||
| 85 | $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
| 86 | curl_close($ch); |
||
| 87 | fclose($fp); |
||
| 88 | if ($code == 304) { |
||
| 89 | @unlink($dat_file_dir . '/SxGeoTmp.zip'); |
||
| 90 | if ($info) echo "Архив не обновился, с момента предыдущего скачивания\n"; |
||
| 91 | return; |
||
| 92 | } |
||
| 93 | |||
| 94 | if ($info) echo "Архив с сервера скачан\n"; |
||
| 95 | // Распаковываем архив |
||
| 96 | $fp = fopen('zip://' . $dat_file_dir . '/SxGeoTmp.zip#' . $dat_file, 'rb'); |
||
| 97 | $fw = fopen($dat_file_dir . '/' . $dat_file, 'wb'); |
||
| 98 | if (!$fp) { |
||
| 99 | if ($info) |
||
| 100 | echo "Не получается открыть\n"; |
||
| 101 | return; |
||
| 102 | } |
||
| 103 | if ($info) echo "Распаковываем архив\n"; |
||
| 104 | stream_copy_to_stream($fp, $fw); |
||
| 105 | fclose($fp); |
||
| 106 | fclose($fw); |
||
| 107 | if (filesize($dat_file) == 0) { |
||
| 108 | if ($info) echo 'Ошибка при распаковке архива'; |
||
| 109 | } |
||
| 110 | @unlink($dat_file_dir . '/SxGeoTmp.zip'); |
||
| 111 | file_put_contents($last_updated_file, gmdate('D, d M Y H:i:s') . ' GMT'); |
||
| 112 | if ($info) echo "Перемещен файл в {$dat_file_dir}/{$dat_file}\n"; |
||
| 113 | |||
| 114 | } |
||
| 115 | |||
| 117 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..