| Conditions | 13 |
| Paths | 822 |
| Total Lines | 107 |
| Code Lines | 86 |
| Lines | 0 |
| Ratio | 0 % |
| 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 declare(strict_types=1); |
||
| 163 | public function reset(): void |
||
| 164 | { |
||
| 165 | $this->db->queryF('TRUNCATE TABLE ' . $this->table); |
||
| 166 | $now = \time(); |
||
| 167 | $time_start = [ |
||
| 168 | 'day' => '%Y%j', |
||
| 169 | 'week' => '%Y%u', |
||
| 170 | 'month' => '%Y%m', |
||
| 171 | ]; |
||
| 172 | $counts = []; |
||
| 173 | |||
| 174 | $sql = ' SELECT forum_id' . ' FROM ' . $this->db->prefix('newbb_forums'); |
||
| 175 | $ret = $this->db->query($sql); |
||
| 176 | if (!$this->db->isResultSet($ret)) { |
||
| 177 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 178 | } |
||
| 179 | while ([$forum_id] = $this->db->fetchRow($ret)) { |
||
| 180 | $sql = ' SELECT COUNT(*), SUM(topic_views)' . ' FROM ' . $this->db->prefix('newbb_topics') . " WHERE approved=1 AND forum_id = {$forum_id}"; |
||
| 181 | $result = $this->db->query($sql); |
||
| 182 | if (!$this->db->isResultSet($result)) { |
||
| 183 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 184 | } |
||
| 185 | [$topics, $views] = $this->db->fetchRow($result); |
||
| 186 | $this->update($forum_id, 'topic', $topics); |
||
| 187 | $this->update($forum_id, 'view', $views); |
||
| 188 | |||
| 189 | $sql = ' SELECT COUNT(*)' . ' FROM ' . $this->db->prefix('newbb_topics') . " WHERE approved=1 AND topic_digest >0 AND forum_id = {$forum_id}"; |
||
| 190 | $result = $this->db->query($sql); |
||
| 191 | if (!$this->db->isResultSet($result)) { |
||
| 192 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 193 | } |
||
| 194 | [$digests] = $this->db->fetchRow($result); |
||
| 195 | $this->update($forum_id, 'digest', $digests); |
||
| 196 | |||
| 197 | $sql = ' SELECT COUNT(*)' . ' FROM ' . $this->db->prefix('newbb_posts') . " WHERE approved=1 AND forum_id = {$forum_id}"; |
||
| 198 | $result = $this->db->query($sql); |
||
| 199 | if (!$this->db->isResultSet($result)) { |
||
| 200 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 201 | } |
||
| 202 | [$posts] = $this->db->fetchRow($result); |
||
| 203 | $this->update($forum_id, 'post', $posts); |
||
| 204 | |||
| 205 | foreach ($time_start as $period => $format) { |
||
| 206 | $sql = ' SELECT COUNT(*), SUM(topic_views)' . ' FROM ' . $this->db->prefix('newbb_topics') . " WHERE approved=1 AND forum_id = {$forum_id}" . " AND FROM_UNIXTIME(topic_time, '{$format}') >= FROM_UNIXTIME({$now}, '{$format}')"; |
||
| 207 | $result = $this->db->query($sql); |
||
| 208 | if (!$this->db->isResultSet($result)) { |
||
| 209 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 210 | } |
||
| 211 | [$topics, $views] = $this->db->fetchRow($result); |
||
| 212 | $views = empty($views) ? 0 : $views; // null check |
||
| 213 | |||
| 214 | $sql = " INSERT INTO {$this->table}" . ' (`stats_id`, `stats_value`, `stats_type`, `stats_period`, `time_update`, `time_format`) ' . ' VALUES ' . " ('{$forum_id}', '{$topics}', '" . \array_search('topic', $this->param['type'], true) . "', '" . \array_search( |
||
| 215 | $period, |
||
| 216 | $this->param['period'], |
||
| 217 | true |
||
| 218 | ) . "', NOW(), '{$format}')"; |
||
| 219 | $result = $this->db->queryF($sql); |
||
| 220 | |||
| 221 | $sql = " INSERT INTO {$this->table}" . ' (`stats_id`, `stats_value`, `stats_type`, `stats_period`, `time_update`, `time_format`) ' . ' VALUES ' . " ('{$forum_id}', '{$views}', '" . \array_search('view', $this->param['type'], true) . "', '" . \array_search( |
||
| 222 | $period, |
||
| 223 | $this->param['period'], |
||
| 224 | true |
||
| 225 | ) . "', NOW(), '{$format}')"; |
||
| 226 | $result = $this->db->queryF($sql); |
||
| 227 | @$counts['topic'][$period] += $topics; |
||
| 228 | @$counts['view'][$period] += $views; |
||
| 229 | |||
| 230 | $sql = ' SELECT COUNT(*)' . ' FROM ' . $this->db->prefix('newbb_topics') . " WHERE approved=1 AND topic_digest >0 AND forum_id = {$forum_id}" . " AND FROM_UNIXTIME(digest_time, '{$format}') >= FROM_UNIXTIME({$now}, '{$format}')"; |
||
| 231 | $result = $this->db->query($sql); |
||
| 232 | if (!$this->db->isResultSet($result)) { |
||
| 233 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 234 | } |
||
| 235 | [$digests] = $this->db->fetchRow($result); |
||
| 236 | $sql = " INSERT INTO {$this->table}" . ' (`stats_id`, `stats_value`, `stats_type`, `stats_period`, `time_update`, `time_format`) ' . ' VALUES ' . " ('{$forum_id}', '{$digests}', '" . \array_search('digest', $this->param['type'], true) . "', '" . \array_search( |
||
| 237 | $period, |
||
| 238 | $this->param['period'], |
||
| 239 | true |
||
| 240 | ) . "', NOW(), '{$format}')"; |
||
| 241 | $result = $this->db->queryF($sql); |
||
| 242 | @$counts['digest'][$period] += $digests; |
||
| 243 | |||
| 244 | $sql = ' SELECT COUNT(*)' . ' FROM ' . $this->db->prefix('newbb_posts') . " WHERE approved=1 AND forum_id = {$forum_id}" . " AND FROM_UNIXTIME(post_time, '{$format}') >= FROM_UNIXTIME({$now}, '{$format}')"; |
||
| 245 | $result = $this->db->query($sql); |
||
| 246 | if (!$this->db->isResultSet($result)) { |
||
| 247 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 248 | } |
||
| 249 | [$posts] = $this->db->fetchRow($result); |
||
| 250 | $sql = " INSERT INTO {$this->table}" . ' (`stats_id`, `stats_value`, `stats_type`, `stats_period`, `time_update`, `time_format`) ' . ' VALUES ' . " ('{$forum_id}', '{$posts}', '" . \array_search('post', $this->param['type'], true) . "', '" . \array_search( |
||
| 251 | $period, |
||
| 252 | $this->param['period'], |
||
| 253 | true |
||
| 254 | ) . "', NOW(), '{$format}')"; |
||
| 255 | $result = $this->db->queryF($sql); |
||
| 256 | @$counts['post'][$period] += $posts; |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | $sql = " DELETE FROM {$this->table}" . " WHERE stats_id = '0' AND stats_period <> " . \array_search('total', $this->param['period'], true); |
||
| 261 | $result = $this->db->queryF($sql); |
||
| 262 | foreach ($time_start as $period => $format) { |
||
| 263 | foreach (\array_keys($counts) as $type) { |
||
| 264 | $sql = " INSERT INTO {$this->table}" . ' (`stats_id`, `stats_value`, `stats_type`, `stats_period`, `time_update`, `time_format`) ' . ' VALUES ' . " ('0', '{$counts[$type][$period]}', '" . \array_search($type, $this->param['type'], true) . "', '" . \array_search( |
||
| 265 | $period, |
||
| 266 | $this->param['period'], |
||
| 267 | true |
||
| 268 | ) . "', NOW(), '{$format}')"; |
||
| 269 | $result = $this->db->queryF($sql); |
||
| 270 | } |
||
| 274 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.