| Conditions | 7 |
| Paths | 24 |
| Total Lines | 61 |
| Code Lines | 32 |
| 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 |
||
| 87 | public static function goToMerchant($payId, $amount, $currency, $description = '', $success = '/', $false = '/') { |
||
| 88 | $config = static::getConfig(); |
||
| 89 | $merchantCurrency = static::getMerchantCurrency($currency); |
||
| 90 | |||
| 91 | if (!$description) |
||
| 92 | $description = "Оплата заказа на сайте " . idn_to_utf8(INJI_DOMAIN_NAME); |
||
| 93 | |||
| 94 | //Секретный ключ интернет-магазина |
||
| 95 | $key = $config['secret']; |
||
| 96 | |||
| 97 | $fields = []; |
||
| 98 | |||
| 99 | // Добавление полей формы в ассоциативный массив |
||
| 100 | $fields["WMI_MERCHANT_ID"] = $config['shopId']; |
||
| 101 | $fields["WMI_PAYMENT_AMOUNT"] = number_format($amount, 0, '.', ''); |
||
| 102 | $fields["WMI_CURRENCY_ID"] = $merchantCurrency->code; |
||
| 103 | $fields["WMI_PAYMENT_NO"] = $payId; |
||
| 104 | $fields["WMI_DESCRIPTION"] = "BASE64:" . base64_encode($description); |
||
| 105 | $fields["WMI_EXPIRED_DATE"] = "2019-12-31T23:59:59"; |
||
| 106 | $fields["WMI_SUCCESS_URL"] = $success; |
||
| 107 | $fields["WMI_FAIL_URL"] = $false; |
||
| 108 | //Сортировка значений внутри полей |
||
| 109 | foreach ($fields as $name => $val) { |
||
| 110 | if (is_array($val)) { |
||
| 111 | usort($val, "strcasecmp"); |
||
| 112 | $fields[$name] = $val; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | // Формирование сообщения, путем объединения значений формы, |
||
| 117 | // отсортированных по именам ключей в порядке возрастания. |
||
| 118 | uksort($fields, "strcasecmp"); |
||
| 119 | $fieldValues = ""; |
||
| 120 | |||
| 121 | foreach ($fields as $value) { |
||
| 122 | if (is_array($value)) { |
||
| 123 | foreach ($value as $v) { |
||
| 124 | //Конвертация из текущей кодировки (UTF-8) |
||
| 125 | //необходима только если кодировка магазина отлична от Windows-1251 |
||
| 126 | $v = iconv("utf-8", "windows-1251", $v); |
||
| 127 | } |
||
| 128 | $fieldValues .= $v; |
||
| 129 | } else { |
||
| 130 | //Конвертация из текущей кодировки (UTF-8) |
||
| 131 | //необходима только если кодировка магазина отлична от Windows-1251 |
||
| 132 | $value = iconv("utf-8", "windows-1251", $value); |
||
| 133 | $fieldValues .= $value; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | // Формирование значения параметра WMI_SIGNATURE, путем |
||
| 138 | // вычисления отпечатка, сформированного выше сообщения, |
||
| 139 | // по алгоритму MD5 и представление его в Base64 |
||
| 140 | |||
| 141 | $signature = base64_encode(pack("H*", md5($fieldValues . $key))); |
||
| 142 | |||
| 143 | //Добавление параметра WMI_SIGNATURE в словарь параметров формы |
||
| 144 | |||
| 145 | $fields["WMI_SIGNATURE"] = $signature; |
||
| 146 | \Tools::redirect('https://www.walletone.com/checkout/default.aspx?' . http_build_query($fields)); |
||
| 147 | } |
||
| 148 | |||
| 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.