| Conditions | 11 |
| Paths | 144 |
| Total Lines | 67 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 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 | { |
||
| 18 | $config = static::getConfig(); |
||
| 19 | $skey = $config['secret']; |
||
| 20 | // Функция, которая возвращает результат в Единую кассу |
||
| 21 | |||
| 22 | function print_answer($result, $description) |
||
| 23 | { |
||
| 24 | $print = "WMI_RESULT=" . strtoupper($result) . "&"; |
||
| 25 | $print .= "WMI_DESCRIPTION=" . urlencode($description); |
||
| 26 | return $print; |
||
| 27 | } |
||
| 28 | |||
| 29 | // Проверка наличия необходимых параметров в POST-запросе |
||
| 30 | |||
| 31 | if (!isset($data["WMI_SIGNATURE"])) |
||
| 32 | $result['callback'] = print_answer("Retry", "Отсутствует параметр WMI_SIGNATURE"); |
||
|
|
|||
| 33 | |||
| 34 | if (!isset($data["WMI_PAYMENT_NO"])) |
||
| 35 | $result['callback'] = print_answer("Retry", "Отсутствует параметр WMI_PAYMENT_NO"); |
||
| 36 | |||
| 37 | if (!isset($data["WMI_ORDER_STATE"])) |
||
| 38 | $result['callback'] = print_answer("Retry", "Отсутствует параметр WMI_ORDER_STATE"); |
||
| 39 | |||
| 40 | // Извлечение всех параметров POST-запроса, кроме WMI_SIGNATURE |
||
| 41 | $params = []; |
||
| 42 | foreach ($data as $name => $value) { |
||
| 43 | if ($name !== "WMI_SIGNATURE") |
||
| 44 | $params[$name] = $value; |
||
| 45 | } |
||
| 46 | |||
| 47 | // Сортировка массива по именам ключей в порядке возрастания |
||
| 48 | // и формирование сообщения, путем объединения значений формы |
||
| 49 | |||
| 50 | uksort($params, "strcasecmp"); |
||
| 51 | $values = ""; |
||
| 52 | |||
| 53 | foreach ($params as $name => $value) { |
||
| 54 | //Конвертация из текущей кодировки (UTF-8) |
||
| 55 | //необходима только если кодировка магазина отлична от Windows-1251 |
||
| 56 | $value = iconv("utf-8", "windows-1251", $value); |
||
| 57 | $values .= $value; |
||
| 58 | } |
||
| 59 | |||
| 60 | // Формирование подписи для сравнения ее с параметром WMI_SIGNATURE |
||
| 61 | |||
| 62 | $signature = base64_encode(pack("H*", md5($values . $skey))); |
||
| 63 | |||
| 64 | //Сравнение полученной подписи с подписью W1 |
||
| 65 | |||
| 66 | if (!empty($data["WMI_SIGNATURE"]) && $signature == $data["WMI_SIGNATURE"]) { |
||
| 67 | if (strtoupper($data["WMI_ORDER_STATE"]) == "ACCEPTED") { |
||
| 68 | // вызываем функцию обработки в случае успеха |
||
| 69 | $result['callback'] = print_answer("Ok", "Заказ #" . $data["WMI_PAYMENT_NO"] . " оплачен!"); |
||
| 70 | $result['payId'] = $data["WMI_PAYMENT_NO"]; |
||
| 71 | $result['status'] = 'success'; |
||
| 72 | return $result; |
||
| 73 | } else { |
||
| 74 | // Случилось что-то странное, пришло неизвестное состояние заказа |
||
| 75 | $result['callback'] = print_answer("Retry", "Неверное состояние " . $data["WMI_ORDER_STATE"]); |
||
| 76 | } |
||
| 77 | } else { |
||
| 78 | // Подпись не совпадает, возможно вы поменяли настройки интернет-магазина |
||
| 79 | $result['callback'] = print_answer("Retry", "Неверная подпись " . (!empty($data["WMI_SIGNATURE"]) ? $data["WMI_SIGNATURE"] : 'empty')); |
||
| 80 | } |
||
| 81 | return $result; |
||
| 82 | } |
||
| 83 | |||
| 162 |
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.