| Conditions | 30 |
| Paths | 112 |
| Total Lines | 132 |
| Code Lines | 81 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 120 | public function message (message $update) { |
||
| 121 | $text = $update->text ?? ''; |
||
| 122 | |||
| 123 | $user_id = $update->from->id; |
||
| 124 | |||
| 125 | $user = mysql::select('users', '*', ['id' => $user_id]); |
||
| 126 | if ($user->num_rows < 1) { |
||
| 127 | mysql::insert('users', ['id'], [$user_id]); |
||
| 128 | $user = mysql::select('users', '*', ['id' => $user_id]); |
||
| 129 | } |
||
| 130 | $user = $user->fetch_object(); |
||
| 131 | |||
| 132 | if ($text === '/start') { |
||
| 133 | mysql::update('users', ['step' => 'main', 'value' => ''], ['id' => $user_id], 1); |
||
| 134 | return $this->sendMessage(texts::START, reply_markup: keyboards::START, answer: true); |
||
| 135 | } |
||
| 136 | |||
| 137 | if ($text === '/help') { |
||
| 138 | return $this->sendMessage(texts::HELP, answer: true); |
||
| 139 | } |
||
| 140 | |||
| 141 | if ($text === '/back') { |
||
| 142 | $text = buttons::BACK; |
||
| 143 | } |
||
| 144 | |||
| 145 | if ($user->step === 'main') { |
||
| 146 | if ($text === buttons::BALANCE) { |
||
| 147 | return $this->sendMessage(strReplace(['$balance' => $user->balance, '$coin' => texts::COIN], texts::BALANCE), answer: true); |
||
| 148 | } |
||
| 149 | if ($text === buttons::DEPOSIT) { |
||
| 150 | mysql::update('users', ['step' => 'deposit'], ['id' => $user_id], 1); |
||
| 151 | return $this->sendMessage(texts::DEPOSIT, reply_markup: keyboards::BACK, answer: true); |
||
| 152 | } |
||
| 153 | if ($text === buttons::TRANSFER) { |
||
| 154 | if ($user->balance <= self::MIN_TRANSFER) { |
||
| 155 | return $this->sendMessage(strReplace(['$min_transfer' => self::MIN_TRANSFER], texts::MIN_BALANCE), answer: true); |
||
| 156 | } |
||
| 157 | |||
| 158 | mysql::update('users', ['step' => 'transfer'], ['id' => $user_id], 1); |
||
| 159 | return $this->sendMessage(texts::TRANSFER, reply_markup: keyboards::SHARE_USER, answer: true); |
||
| 160 | } |
||
| 161 | if ($text === buttons::HISTORY) { |
||
| 162 | return $this->sendMessage(texts::SOON, answer: true); |
||
| 163 | } |
||
| 164 | if ($text === buttons::SUPPORT) { |
||
| 165 | return $this->sendMessage(texts::SOON, answer: true); |
||
| 166 | } |
||
| 167 | |||
| 168 | return $this->sendMessage(texts::UNKNOWN, answer: true); |
||
| 169 | } |
||
| 170 | if ($user->step === 'transfer') { |
||
| 171 | if ($text === buttons::BACK) { |
||
| 172 | mysql::update('users', ['step' => 'main', 'value' => ''], ['id' => $user_id], 1); |
||
| 173 | return $this->sendMessage(texts::START, reply_markup: keyboards::START, answer: true); |
||
| 174 | } |
||
| 175 | if (isset($update->user_shared)) { |
||
| 176 | $target_id = $update->user_shared->user_id; |
||
| 177 | } |
||
| 178 | elseif (isset($update->forward_date)) { |
||
| 179 | if (!isset($update->forward_from)) { |
||
| 180 | return $this->sendMessage(texts::USER_FORWARD_CLOSED, answer: true); |
||
| 181 | } |
||
| 182 | $target_id = $update->forward_from->id; |
||
| 183 | } |
||
| 184 | else { |
||
| 185 | if (!is_numeric($text) || $text != floor($text)) { |
||
| 186 | return $this->sendMessage(texts::ONLY_INT, answer: true); |
||
| 187 | } |
||
| 188 | $target_id = $text; |
||
| 189 | } |
||
| 190 | |||
| 191 | $target_user = mysql::select('users', '*', ['id' => $target_id], 1); |
||
| 192 | if ($target_user->num_rows < 1) { |
||
| 193 | return $this->sendMessage(texts::USER_NOT_FOUND, answer: true); |
||
| 194 | } |
||
| 195 | |||
| 196 | mysql::update('users', ['step' => 'transfer_money', 'value' => $target_id], ['id' => $user_id], 1); |
||
| 197 | return $this->sendMessage(strReplace(['$balance' => min($user->balance, self::MAX_TRANSFER)], texts::TRANSFER_MONEY), reply_markup: keyboards::BACK, answer: true); |
||
| 198 | } |
||
| 199 | if ($user->step === 'transfer_money') { |
||
| 200 | if ($text === buttons::BACK) { |
||
| 201 | mysql::update('users', ['step' => 'transfer'], ['id' => $user_id], 1); |
||
| 202 | return $this->sendMessage(texts::TRANSFER, reply_markup: keyboards::SHARE_USER, answer: true); |
||
| 203 | } |
||
| 204 | |||
| 205 | if (!is_numeric($text)) { |
||
| 206 | return $this->sendMessage(texts::ONLY_NUMBER, answer: true); |
||
| 207 | } |
||
| 208 | |||
| 209 | $text = floor($text * pow(10, self::MAX_MONEY_DECIMAL)) / pow(10, self::MAX_MONEY_DECIMAL); |
||
| 210 | |||
| 211 | if ($user->balance < $text) { |
||
| 212 | return $this->sendMessage(texts::NOT_ENOUGH_BALANCE, answer: true); |
||
| 213 | } |
||
| 214 | |||
| 215 | if ($text > self::MAX_TRANSFER) { |
||
| 216 | return $this->sendMessage(strReplace(['$max_transfer' => self::MAX_TRANSFER], texts::MAX_TRANSFER), answer: true); |
||
| 217 | } |
||
| 218 | |||
| 219 | mysql::update('users', ['balance' => '+=' . $text], ['id' => $user->value], 1); |
||
| 220 | $this->sendMessage(strReplace(['$amount' => $text, '$id' => $user_id], texts::MONEY_RECEIVED), $user->value); |
||
| 221 | |||
| 222 | mysql::insert('history', ['type', 'amount', 'date', 'user_id', 'target_id'], ['transfer', $text, time(), $user_id, $user->value]); |
||
| 223 | |||
| 224 | mysql::update('users', ['balance' => '-=' . $text, 'step' => 'main', 'value' => ''], ['id' => $user_id], 1); |
||
| 225 | return $this->sendMessage(texts::TRANSFER_DONE, reply_markup: keyboards::START, answer: true); |
||
| 226 | } |
||
| 227 | if ($user->step === 'deposit') { |
||
| 228 | if ($text === buttons::BACK) { |
||
| 229 | mysql::update('users', ['step' => 'main', 'value' => ''], ['id' => $user_id], 1); |
||
| 230 | return $this->sendMessage(texts::START, reply_markup: keyboards::START, answer: true); |
||
| 231 | } |
||
| 232 | |||
| 233 | if (!is_numeric($text)) { |
||
| 234 | return $this->sendMessage(texts::ONLY_NUMBER, answer: true); |
||
| 235 | } |
||
| 236 | |||
| 237 | $text = floor($text * pow(10, self::MAX_MONEY_DECIMAL)) / pow(10, self::MAX_MONEY_DECIMAL); |
||
| 238 | |||
| 239 | $max_deposit = pow(10, self::MAX_MONEY_LENGTH - self::MAX_MONEY_DECIMAL - 1); |
||
| 240 | if ($text > pow(10, self::MAX_MONEY_LENGTH - self::MAX_MONEY_DECIMAL - 1)) { |
||
| 241 | return $this->sendMessage(strReplace(['$max_deposit' => $max_deposit], texts::MAX_DEPOSIT), answer: true); |
||
| 242 | } |
||
| 243 | |||
| 244 | $max_balance = pow(10, self::MAX_MONEY_LENGTH - self::MAX_MONEY_DECIMAL) - 1; |
||
| 245 | if ($user->balance + $text >= $max_balance) { |
||
| 246 | return $this->sendMessage(strReplace(['$max_balance' => $max_balance], texts::MAX_BALANCE), answer: true); |
||
| 247 | } |
||
| 248 | |||
| 249 | $url = crypto::ezPay($text, $user_id, one_time_url: false); |
||
| 250 | mysql::update('users', ['step' => 'main', 'value' => ''], ['id' => $user_id], 1); |
||
| 251 | return $this->sendMessage(strReplace(['$url' => $url], texts::INVOICE_CREATED), reply_markup: keyboards::START, answer: true); |
||
| 252 | } |
||
| 375 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.