| Conditions | 68 |
| Paths | > 20000 |
| Total Lines | 189 |
| Code Lines | 116 |
| Lines | 18 |
| Ratio | 9.52 % |
| Changes | 7 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 103 | * This method overrides the default getPhoto method |
||
| 104 | * and returns a nice array of PhotoSize objects. |
||
| 105 | * |
||
| 106 | * @return null|PhotoSize[] |
||
| 107 | */ |
||
| 108 | 6 | public function getPhoto() |
|
| 109 | { |
||
| 110 | 6 | $pretty_array = $this->makePrettyObjectArray(PhotoSize::class, 'photo'); |
|
| 111 | |||
| 112 | 6 | return empty($pretty_array) ? null : $pretty_array; |
|
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Optional. A chat photo was changed to this value |
||
| 117 | * |
||
| 118 | * This method overrides the default getNewChatPhoto method |
||
| 119 | * and returns a nice array of PhotoSize objects. |
||
| 120 | * |
||
| 121 | * @return null|PhotoSize[] |
||
| 122 | */ |
||
| 123 | 6 | public function getNewChatPhoto() |
|
| 124 | { |
||
| 125 | 6 | $pretty_array = $this->makePrettyObjectArray(PhotoSize::class, 'new_chat_photo'); |
|
| 126 | |||
| 127 | 6 | return empty($pretty_array) ? null : $pretty_array; |
|
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Optional. For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text |
||
| 132 | * |
||
| 133 | * This method overrides the default getEntities method |
||
| 134 | * and returns a nice array of MessageEntity objects. |
||
| 135 | * |
||
| 136 | * @return null|MessageEntity[] |
||
| 137 | */ |
||
| 138 | 6 | public function getEntities() |
|
| 139 | { |
||
| 140 | 6 | $pretty_array = $this->makePrettyObjectArray(MessageEntity::class, 'entities'); |
|
| 141 | |||
| 142 | 6 | return empty($pretty_array) ? null : $pretty_array; |
|
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * return the entire command like /echo or /echo@bot1 if specified |
||
| 147 | * |
||
| 148 | * @return string|null |
||
| 149 | */ |
||
| 150 | 7 | public function getFullCommand() |
|
| 151 | { |
||
| 152 | 7 | $text = $this->getProperty('text'); |
|
| 153 | 7 | if (strpos($text, '/') === 0) { |
|
| 154 | 7 | $no_EOL = strtok($text, PHP_EOL); |
|
| 155 | 7 | $no_space = strtok($text, ' '); |
|
| 156 | |||
| 157 | //try to understand which separator \n or space divide /command from text |
||
| 158 | 7 | return strlen($no_space) < strlen($no_EOL) ? $no_space : $no_EOL; |
|
| 159 | } |
||
| 160 | |||
| 161 | 2 | return null; |
|
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get command |
||
| 166 | * |
||
| 167 | * @return bool|string |
||
| 168 | */ |
||
| 169 | 2 | public function getCommand() |
|
| 170 | { |
||
| 171 | 2 | $command = $this->getProperty('command'); |
|
| 172 | 2 | if (!empty($command)) { |
|
| 173 | return $command; |
||
| 174 | } |
||
| 175 | |||
| 176 | 2 | $cmd = $this->getFullCommand(); |
|
| 177 | |||
| 178 | 2 | if (strpos($cmd, '/') === 0) { |
|
| 179 | 2 | $cmd = substr($cmd, 1); |
|
| 180 | |||
| 181 | //check if command is follow by botname |
||
| 182 | 2 | $split_cmd = explode('@', $cmd); |
|
| 183 | 2 | if (isset($split_cmd[1])) { |
|
| 184 | //command is followed by name check if is addressed to me |
||
| 185 | 1 | if (strtolower($split_cmd[1]) === strtolower($this->bot_name)) { |
|
|
|
|||
| 186 | 1 | return $split_cmd[0]; |
|
| 187 | } |
||
| 188 | } else { |
||
| 189 | //command is not followed by name |
||
| 190 | 2 | return $cmd; |
|
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | 2 | return false; |
|
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * For text messages, the actual UTF-8 text of the message, 0-4096 characters. |
||
| 199 | * |
||
| 200 | * @param bool $without_cmd |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | 14 | public function getText($without_cmd = false) |
|
| 205 | { |
||
| 206 | 14 | $text = $this->getProperty('text'); |
|
| 207 | |||
| 208 | 14 | if ($without_cmd && $command = $this->getFullCommand()) { |
|
| 209 | 6 | if (strlen($command) + 1 < strlen($text)) { |
|
| 210 | 5 | $text = substr($text, strlen($command) + 1); |
|
| 211 | } else { |
||
| 212 | 3 | $text = ''; |
|
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | 14 | return $text; |
|
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Bot added in chat |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | public function botAddedInChat() |
||
| 225 | { |
||
| 226 | $member = $this->getNewChatMember(); |
||
| 227 | |||
| 228 | return $member !== null && $member->getUsername() === $this->getBotName(); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Detect type based on properties. |
||
| 233 | * |
||
| 234 | * @return string|null |
||
| 235 | */ |
||
| 236 | 1 | public function getType() |
|
| 237 | { |
||
| 238 | $types = [ |
||
| 239 | 1 | 'text', |
|
| 240 | 'audio', |
||
| 241 | 'document', |
||
| 242 | 'photo', |
||
| 243 | 'sticker', |
||
| 244 | 'video', |
||
| 245 | 'voice', |
||
| 246 | 'contact', |
||
| 247 | 'location', |
||
| 248 | 'venue', |
||
| 249 | 'new_chat_member', |
||
| 250 | 'left_chat_member', |
||
| 251 | 'new_chat_title', |
||
| 252 | 'new_chat_photo', |
||
| 253 | 'delete_chat_photo', |
||
| 254 | 'group_chat_created', |
||
| 255 | 'supergroup_chat_created', |
||
| 256 | 'channel_chat_created', |
||
| 257 | 'migrate_to_chat_id', |
||
| 258 | 'migrate_from_chat_id', |
||
| 259 | 'pinned_message', |
||
| 260 | ]; |
||
| 261 | |||
| 262 | 1 | foreach ($types as $type) { |
|
| 263 | 1 | if ($this->getProperty($type)) { |
|
| 264 | 1 | if ($type === 'text' && $this->getCommand()) { |
|
| 265 | 1 | return 'command'; |
|
| 266 | } |
||
| 267 | |||
| 268 | 1 | return $type; |
|
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | 1 | return 'message'; |
|
| 273 | } |
||
| 274 | } |
||
| 275 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: