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