| Conditions | 10 |
| Paths | 129 |
| Total Lines | 194 |
| Code Lines | 162 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 110 |
| 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 |
||
| 143 | private function getQueries($settings) |
||
| 144 | { |
||
| 145 | if (empty($settings) || !is_array($settings)) { |
||
| 146 | throw new TelegramException('Settings variable is not an array or is empty!'); |
||
| 147 | } |
||
| 148 | |||
| 149 | // Convert all clean_older_than times to correct format. |
||
| 150 | $clean_older_than = $settings['clean_older_than']; |
||
| 151 | foreach ($clean_older_than as $table => $time) { |
||
| 152 | $clean_older_than[$table] = date('Y-m-d H:i:s', strtotime('-' . $time)); |
||
| 153 | } |
||
| 154 | $tables_to_clean = $settings['tables_to_clean']; |
||
| 155 | |||
| 156 | $queries = []; |
||
| 157 | |||
| 158 | if (in_array('telegram_update', $tables_to_clean, true)) { |
||
| 159 | $queries[] = sprintf( |
||
| 160 | 'DELETE FROM `%3$s` |
||
| 161 | WHERE `id` != \'%1$s\' |
||
| 162 | AND `chat_id` NOT IN ( |
||
| 163 | SELECT `id` |
||
| 164 | FROM `%4$s` |
||
| 165 | WHERE `chat_id` = `%4$s`.`id` |
||
| 166 | AND `updated_at` < \'%1$s\' |
||
| 167 | ) |
||
| 168 | AND ( |
||
| 169 | `message_id` IS NOT NULL |
||
| 170 | AND `message_id` IN ( |
||
| 171 | SELECT f.id |
||
| 172 | FROM `%5$s` f |
||
| 173 | WHERE `date` < \'%2$s\' |
||
| 174 | ) |
||
| 175 | ) |
||
| 176 | OR ( |
||
| 177 | `edited_message_id` IS NOT NULL |
||
| 178 | AND `edited_message_id` IN ( |
||
| 179 | SELECT f.id |
||
| 180 | FROM `%6$s` f |
||
| 181 | WHERE `edit_date` < \'%2$s\' |
||
| 182 | ) |
||
| 183 | ) |
||
| 184 | OR ( |
||
| 185 | `inline_query_id` IS NOT NULL |
||
| 186 | AND `inline_query_id` IN ( |
||
| 187 | SELECT f.id |
||
| 188 | FROM `%7$s` f |
||
| 189 | WHERE `created_at` < \'%2$s\' |
||
| 190 | ) |
||
| 191 | ) |
||
| 192 | OR ( |
||
| 193 | `chosen_inline_result_id` IS NOT NULL |
||
| 194 | AND `chosen_inline_result_id` IN ( |
||
| 195 | SELECT f.id |
||
| 196 | FROM `%8$s` f |
||
| 197 | WHERE `created_at` < \'%2$s\' |
||
| 198 | ) |
||
| 199 | ) |
||
| 200 | OR ( |
||
| 201 | `callback_query_id` IS NOT NULL |
||
| 202 | AND `callback_query_id` IN ( |
||
| 203 | SELECT f.id |
||
| 204 | FROM `%9$s` f |
||
| 205 | WHERE `created_at` < \'%2$s\' |
||
| 206 | ) |
||
| 207 | ) |
||
| 208 | ', |
||
| 209 | $this->getUpdate()->getUpdateId(), |
||
| 210 | $clean_older_than['telegram_update'], |
||
| 211 | TB_TELEGRAM_UPDATE, |
||
|
1 ignored issue
–
show
|
|||
| 212 | TB_CHAT, |
||
|
1 ignored issue
–
show
|
|||
| 213 | TB_MESSAGE, |
||
|
1 ignored issue
–
show
|
|||
| 214 | TB_EDITED_MESSAGE, |
||
|
1 ignored issue
–
show
|
|||
| 215 | TB_INLINE_QUERY, |
||
|
1 ignored issue
–
show
|
|||
| 216 | TB_CHOSEN_INLINE_RESULT, |
||
|
1 ignored issue
–
show
|
|||
| 217 | TB_CALLBACK_QUERY |
||
|
1 ignored issue
–
show
|
|||
| 218 | ); |
||
| 219 | } |
||
| 220 | |||
| 221 | if (in_array('user_chat', $tables_to_clean, true)) { |
||
| 222 | $queries[] = sprintf( |
||
| 223 | 'DELETE FROM `%1$s` |
||
| 224 | WHERE `user_id` IN ( |
||
| 225 | SELECT f.id |
||
| 226 | FROM `%2$s` f |
||
| 227 | WHERE `updated_at` < \'%3$s\' |
||
| 228 | ) |
||
| 229 | ', |
||
| 230 | TB_USER_CHAT, |
||
|
1 ignored issue
–
show
|
|||
| 231 | TB_USER, |
||
|
1 ignored issue
–
show
|
|||
| 232 | $clean_older_than['chat'] |
||
| 233 | ); |
||
| 234 | } |
||
| 235 | |||
| 236 | // Simple. |
||
| 237 | $simple_tables = [ |
||
| 238 | 'user' => ['table' => TB_USER, 'field' => 'updated_at'], |
||
| 239 | 'chat' => ['table' => TB_CHAT, 'field' => 'updated_at'], |
||
| 240 | 'conversation' => ['table' => TB_CONVERSATION, 'field' => 'updated_at'], |
||
| 241 | 'request_limiter' => ['table' => TB_REQUEST_LIMITER, 'field' => 'created_at'], |
||
|
1 ignored issue
–
show
|
|||
| 242 | ]; |
||
| 243 | |||
| 244 | foreach (array_intersect(array_keys($simple_tables), $tables_to_clean) as $table_to_clean) { |
||
| 245 | $queries[] = sprintf( |
||
| 246 | 'DELETE FROM `%1$s` |
||
| 247 | WHERE `%2$s` < \'%3$s\' |
||
| 248 | ', |
||
| 249 | $simple_tables[$table_to_clean]['table'], |
||
| 250 | $simple_tables[$table_to_clean]['field'], |
||
| 251 | $clean_older_than[$table_to_clean] |
||
| 252 | ); |
||
| 253 | } |
||
| 254 | |||
| 255 | // Queries. |
||
| 256 | $query_tables = [ |
||
| 257 | 'inline_query' => ['table' => TB_INLINE_QUERY, 'field' => 'created_at'], |
||
| 258 | 'chosen_inline_result' => ['table' => TB_CHOSEN_INLINE_RESULT, 'field' => 'created_at'], |
||
| 259 | 'callback_query' => ['table' => TB_CALLBACK_QUERY, 'field' => 'created_at'], |
||
| 260 | ]; |
||
| 261 | foreach (array_intersect(array_keys($query_tables), $tables_to_clean) as $table_to_clean) { |
||
| 262 | $queries[] = sprintf( |
||
| 263 | 'DELETE FROM `%1$s` |
||
| 264 | WHERE `%2$s` < \'%3$s\' |
||
| 265 | AND `id` NOT IN ( |
||
| 266 | SELECT `%4$s` |
||
| 267 | FROM `%5$s` |
||
| 268 | WHERE `%4$s` = `%1$s`.`id` |
||
| 269 | ) |
||
| 270 | ', |
||
| 271 | $query_tables[$table_to_clean]['table'], |
||
| 272 | $query_tables[$table_to_clean]['field'], |
||
| 273 | $clean_older_than[$table_to_clean], |
||
| 274 | $table_to_clean . '_id', |
||
| 275 | TB_TELEGRAM_UPDATE |
||
| 276 | ); |
||
| 277 | } |
||
| 278 | |||
| 279 | // Messages |
||
| 280 | if (in_array('edited_message', $tables_to_clean, true)) { |
||
| 281 | $queries[] = sprintf( |
||
| 282 | 'DELETE FROM `%1$s` |
||
| 283 | WHERE `edit_date` < \'%2$s\' |
||
| 284 | AND `id` NOT IN ( |
||
| 285 | SELECT `message_id` |
||
| 286 | FROM `%3$s` |
||
| 287 | WHERE `edited_message_id` = `%1$s`.`id` |
||
| 288 | ) |
||
| 289 | ', |
||
| 290 | TB_EDITED_MESSAGE, |
||
| 291 | $clean_older_than['edited_message'], |
||
| 292 | TB_TELEGRAM_UPDATE |
||
| 293 | ); |
||
| 294 | } |
||
| 295 | |||
| 296 | if (in_array('message', $tables_to_clean, true)) { |
||
| 297 | $queries[] = sprintf( |
||
| 298 | 'DELETE FROM `%1$s` |
||
| 299 | WHERE id IN ( |
||
| 300 | SELECT id |
||
| 301 | FROM ( |
||
| 302 | SELECT id |
||
| 303 | FROM `%1$s` |
||
| 304 | WHERE `date` < \'%2$s\' |
||
| 305 | AND `id` NOT IN ( |
||
| 306 | SELECT `message_id` |
||
| 307 | FROM `%3$s` |
||
| 308 | WHERE `message_id` = `%1$s`.`id` |
||
| 309 | ) |
||
| 310 | AND `id` NOT IN ( |
||
| 311 | SELECT `message_id` |
||
| 312 | FROM `%4$s` |
||
| 313 | WHERE `message_id` = `%1$s`.`id` |
||
| 314 | ) |
||
| 315 | AND `id` NOT IN ( |
||
| 316 | SELECT `message_id` |
||
| 317 | FROM `%5$s` |
||
| 318 | WHERE `message_id` = `%1$s`.`id` |
||
| 319 | ) |
||
| 320 | AND `id` NOT IN ( |
||
| 321 | SELECT a.`reply_to_message` FROM `%1$s` a |
||
| 322 | INNER JOIN `%1$s` b ON b.`id` = a.`reply_to_message` AND b.`chat_id` = a.`reply_to_chat` |
||
| 323 | ) |
||
| 324 | ORDER BY `id` DESC |
||
| 325 | ) a |
||
| 326 | ) |
||
| 327 | ', |
||
| 328 | TB_MESSAGE, |
||
| 329 | $clean_older_than['message'], |
||
| 330 | TB_EDITED_MESSAGE, |
||
| 331 | TB_TELEGRAM_UPDATE, |
||
| 332 | TB_CALLBACK_QUERY |
||
| 333 | ); |
||
| 334 | } |
||
| 335 | |||
| 336 | return $queries; |
||
| 337 | } |
||
| 426 |