| Conditions | 12 |
| Paths | 417 |
| Total Lines | 75 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 156 |
| Changes | 5 | ||
| 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 |
||
| 48 | public function execute() |
||
|
1 ignored issue
–
show
|
|||
| 49 | { |
||
| 50 | $pdo = DB::getPdo(); |
||
| 51 | $message = $this->getMessage(); |
||
| 52 | $chat = $message->getChat(); |
||
| 53 | $text = strtolower($message->getText(true)); |
||
| 54 | |||
| 55 | $data = ['chat_id' => $chat->getId()]; |
||
| 56 | |||
| 57 | if ($text !== 'glasnost' && !$chat->isPrivateChat()) { |
||
| 58 | $data['text'] = 'Only available in a private chat.'; |
||
| 59 | |||
| 60 | return Request::sendMessage($data); |
||
| 61 | } |
||
| 62 | |||
| 63 | $debug_info = []; |
||
| 64 | |||
| 65 | $debug_info[] = sprintf('*TelegramBot version:* `%s`', $this->telegram->getVersion()); |
||
| 66 | $debug_info[] = sprintf('*Download path:* `%s`', $this->telegram->getDownloadPath()); |
||
| 67 | $debug_info[] = sprintf('*Upload path:* `%s`', $this->telegram->getUploadPath()); |
||
| 68 | |||
| 69 | // Commands paths. |
||
| 70 | $debug_info[] = '*Commands paths:*'; |
||
| 71 | $debug_info[] = sprintf( |
||
| 72 | '```' . PHP_EOL . '%s```', |
||
| 73 | json_encode($this->telegram->getCommandsPaths(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) |
||
| 74 | ); |
||
| 75 | |||
| 76 | $php_bit = ''; |
||
| 77 | PHP_INT_SIZE === 4 && $php_bit = ' (32bit)'; |
||
| 78 | PHP_INT_SIZE === 8 && $php_bit = ' (64bit)'; |
||
| 79 | $debug_info[] = sprintf('*PHP version:* `%1$s%2$s; %3$s; %4$s`', PHP_VERSION, $php_bit, PHP_SAPI, PHP_OS); |
||
| 80 | $debug_info[] = sprintf('*Maximum PHP script execution time:* `%d seconds`', ini_get('max_execution_time')); |
||
| 81 | |||
| 82 | $mysql_version = $pdo ? $pdo->query('SELECT VERSION() AS version')->fetchColumn() : null; |
||
| 83 | $debug_info[] = sprintf('*MySQL version:* `%s`', $mysql_version ?: 'disabled'); |
||
| 84 | |||
| 85 | $debug_info[] = sprintf('*Operating System:* `%s`', php_uname()); |
||
| 86 | |||
| 87 | if (isset($_SERVER['SERVER_SOFTWARE'])) { |
||
| 88 | $debug_info[] = sprintf('*Web Server:* `%s`', $_SERVER['SERVER_SOFTWARE']); |
||
| 89 | } |
||
| 90 | if (function_exists('curl_init')) { |
||
| 91 | $curlversion = curl_version(); |
||
| 92 | $debug_info[] = sprintf('*curl version:* `%1$s; %2$s`', $curlversion['version'], $curlversion['ssl_version']); |
||
| 93 | } |
||
| 94 | |||
| 95 | $webhook_info_title = '*Webhook Info:*'; |
||
| 96 | try { |
||
| 97 | // Check if we're actually using the Webhook method. |
||
| 98 | if (Request::getInput() === '') { |
||
| 99 | $debug_info[] = $webhook_info_title . ' `Using getUpdates method, not Webhook.`'; |
||
| 100 | } else { |
||
| 101 | $webhook_info_result = json_decode(Request::getWebhookInfo(), true)['result']; |
||
| 102 | // Add a human-readable error date string if necessary. |
||
| 103 | if (isset($webhook_info_result['last_error_date'])) { |
||
| 104 | $webhook_info_result['last_error_date_string'] = date('Y-m-d H:i:s', $webhook_info_result['last_error_date']); |
||
| 105 | } |
||
| 106 | |||
| 107 | $webhook_info_result_str = json_encode($webhook_info_result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
||
| 108 | $debug_info[] = $webhook_info_title; |
||
| 109 | $debug_info[] = sprintf( |
||
| 110 | '```' . PHP_EOL . '%s```', |
||
| 111 | $webhook_info_result_str |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | } catch (\Exception $e) { |
||
| 115 | $debug_info[] = $webhook_info_title . sprintf(' `Failed to get webhook info! (%s)`', $e->getMessage()); |
||
| 116 | } |
||
| 117 | |||
| 118 | $data['parse_mode'] = 'Markdown'; |
||
| 119 | $data['text'] = implode(PHP_EOL, $debug_info); |
||
| 120 | |||
| 121 | return Request::sendMessage($data); |
||
| 122 | } |
||
| 123 | } |
||
| 124 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: