| Conditions | 11 |
| Paths | 144 |
| Total Lines | 70 |
| Code Lines | 34 |
| 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 |
||
| 16 | public static function reciver($data, $status) { |
||
| 17 | $config = static::getConfig(); |
||
| 18 | $skey = $config['secret']; |
||
| 19 | |||
| 20 | // Функция, которая возвращает результат в Единую кассу |
||
| 21 | |||
| 22 | function print_answer($result, $description) { |
||
| 23 | $print = "WMI_RESULT=" . strtoupper($result) . "&"; |
||
| 24 | $print .= "WMI_DESCRIPTION=" . urlencode($description); |
||
| 25 | return $print; |
||
| 26 | } |
||
| 27 | |||
| 28 | // Проверка наличия необходимых параметров в POST-запросе |
||
| 29 | |||
| 30 | if (!isset($data["WMI_SIGNATURE"])) { |
||
| 31 | $result['callback'] = print_answer("Retry", "Отсутствует параметр WMI_SIGNATURE"); |
||
|
|
|||
| 32 | } |
||
| 33 | |||
| 34 | if (!isset($data["WMI_PAYMENT_NO"])) { |
||
| 35 | $result['callback'] = print_answer("Retry", "Отсутствует параметр WMI_PAYMENT_NO"); |
||
| 36 | } |
||
| 37 | |||
| 38 | if (!isset($data["WMI_ORDER_STATE"])) { |
||
| 39 | $result['callback'] = print_answer("Retry", "Отсутствует параметр WMI_ORDER_STATE"); |
||
| 40 | } |
||
| 41 | |||
| 42 | // Извлечение всех параметров POST-запроса, кроме WMI_SIGNATURE |
||
| 43 | $params = []; |
||
| 44 | foreach ($data as $name => $value) { |
||
| 45 | if ($name !== "WMI_SIGNATURE") { |
||
| 46 | $params[$name] = $value; |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | // Сортировка массива по именам ключей в порядке возрастания |
||
| 51 | // и формирование сообщения, путем объединения значений формы |
||
| 52 | |||
| 53 | uksort($params, "strcasecmp"); |
||
| 54 | $values = ""; |
||
| 55 | |||
| 56 | foreach ($params as $name => $value) { |
||
| 57 | //Конвертация из текущей кодировки (UTF-8) |
||
| 58 | //необходима только если кодировка магазина отлична от Windows-1251 |
||
| 59 | $value = iconv("utf-8", "windows-1251", $value); |
||
| 60 | $values .= $value; |
||
| 61 | } |
||
| 62 | |||
| 63 | // Формирование подписи для сравнения ее с параметром WMI_SIGNATURE |
||
| 64 | |||
| 65 | $signature = base64_encode(pack("H*", md5($values . $skey))); |
||
| 66 | |||
| 67 | //Сравнение полученной подписи с подписью W1 |
||
| 68 | |||
| 69 | if (!empty($data["WMI_SIGNATURE"]) && $signature == $data["WMI_SIGNATURE"]) { |
||
| 70 | if (strtoupper($data["WMI_ORDER_STATE"]) == "ACCEPTED") { |
||
| 71 | // вызываем функцию обработки в случае успеха |
||
| 72 | $result['callback'] = print_answer("Ok", "Заказ #" . $data["WMI_PAYMENT_NO"] . " оплачен!"); |
||
| 73 | $result['payId'] = $data["WMI_PAYMENT_NO"]; |
||
| 74 | $result['status'] = 'success'; |
||
| 75 | return $result; |
||
| 76 | } else { |
||
| 77 | // Случилось что-то странное, пришло неизвестное состояние заказа |
||
| 78 | $result['callback'] = print_answer("Retry", "Неверное состояние " . $data["WMI_ORDER_STATE"]); |
||
| 79 | } |
||
| 80 | } else { |
||
| 81 | // Подпись не совпадает, возможно вы поменяли настройки интернет-магазина |
||
| 82 | $result['callback'] = print_answer("Retry", "Неверная подпись " . (!empty($data["WMI_SIGNATURE"]) ? $data["WMI_SIGNATURE"] : 'empty')); |
||
| 83 | } |
||
| 84 | return $result; |
||
| 85 | } |
||
| 86 | |||
| 150 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.