| Conditions | 14 |
| Paths | 152 |
| Total Lines | 79 |
| Code Lines | 59 |
| 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); |
||
| 203 | public function buildDigest(Digest $digest): bool |
||
| 204 | { |
||
| 205 | global $xoopsModule; |
||
| 206 | |||
| 207 | if (!\defined('SUMMARY_LENGTH')) { |
||
| 208 | \define('SUMMARY_LENGTH', 100); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** @var ForumHandler $forumHandler */ |
||
| 212 | $forumHandler = Helper::getInstance()->getHandler('Forum'); |
||
| 213 | $thisUser = $GLOBALS['xoopsUser']; |
||
| 214 | $GLOBALS['xoopsUser'] = null; // To get posts accessible by anonymous |
||
| 215 | $GLOBALS['xoopsUser'] = $thisUser; |
||
| 216 | |||
| 217 | $accessForums = $forumHandler->getIdsByPermission(); // get all accessible forums |
||
| 218 | $forumCriteria = ' AND t.forum_id IN (' . \implode(',', $accessForums) . ')'; |
||
| 219 | $approveCriteria = ' AND t.approved = 1 AND p.approved = 1'; |
||
| 220 | $time_criteria = ' AND t.digest_time > ' . $this->last_digest; |
||
| 221 | |||
| 222 | $karma_criteria = $GLOBALS['xoopsModuleConfig']['enable_karma'] ? ' AND p.post_karma=0' : ''; |
||
| 223 | $reply_criteria = $GLOBALS['xoopsModuleConfig']['allow_require_reply'] ? ' AND p.require_reply=0' : ''; |
||
| 224 | |||
| 225 | $sql = 'SELECT t.topic_id, t.forum_id, t.topic_title, t.topic_time, t.digest_time, p.uid, p.poster_name, pt.post_text FROM ' |
||
| 226 | . $this->db->prefix('newbb_topics') |
||
| 227 | . ' t, ' |
||
| 228 | . $this->db->prefix('newbb_posts_text') |
||
| 229 | . ' pt, ' |
||
| 230 | . $this->db->prefix('newbb_posts') |
||
| 231 | . ' p WHERE t.topic_digest = 1 AND p.topic_id=t.topic_id AND p.pid=0 ' |
||
| 232 | . $forumCriteria |
||
| 233 | . $approveCriteria |
||
| 234 | . $time_criteria |
||
| 235 | . $karma_criteria |
||
| 236 | . $reply_criteria |
||
| 237 | . ' AND pt.post_id=p.post_id ORDER BY t.digest_time DESC'; |
||
| 238 | $result = $this->db->query($sql); |
||
| 239 | if (!$this->db->isResultSet($result)) { |
||
| 240 | //echo "<br>No result:<br>$sql"; |
||
| 241 | return false; |
||
| 242 | } |
||
| 243 | $rows = []; |
||
| 244 | $users = []; |
||
| 245 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
| 246 | $users[$row['uid']] = 1; |
||
| 247 | $rows[] = $row; |
||
| 248 | } |
||
| 249 | if (\count($rows) < 1) { |
||
| 250 | return false; |
||
| 251 | } |
||
| 252 | $uids = \array_keys($users); |
||
| 253 | if (\count($uids) > 0) { |
||
| 254 | /** @var \XoopsMemberHandler $memberHandler */ |
||
| 255 | $memberHandler = \xoops_getHandler('member'); |
||
| 256 | $user_criteria = new \Criteria('uid', '(' . \implode(',', $uids) . ')', 'IN'); |
||
| 257 | $users = $memberHandler->getUsers($user_criteria, true); |
||
| 258 | } else { |
||
| 259 | $users = []; |
||
| 260 | } |
||
| 261 | |||
| 262 | foreach ($rows as $topic) { |
||
| 263 | if ($topic['uid'] > 0) { |
||
| 264 | if (isset($users[$topic['uid']]) && \is_object($users[$topic['uid']]) |
||
| 265 | && $users[$topic['uid']]->isActive()) { |
||
| 266 | $topic['uname'] = $users[$topic['uid']]->getVar('uname'); |
||
| 267 | } else { |
||
| 268 | $topic['uname'] = $GLOBALS['xoopsConfig']['anonymous']; |
||
| 269 | } |
||
| 270 | } else { |
||
| 271 | $topic['uname'] = $topic['poster_name'] ?: $GLOBALS['xoopsConfig']['anonymous']; |
||
| 272 | } |
||
| 273 | $summary = Metagen::generateDescription($topic['post_text'], \SUMMARY_LENGTH); |
||
| 274 | $author = $topic['uname'] . ' (' . \formatTimestamp($topic['topic_time']) . ')'; |
||
| 275 | $link = XOOPS_URL . '/modules/' . $xoopsModule->dirname() . '/viewtopic.php?topic_id=' . $topic['topic_id'] . '&forum=' . $topic['forum_id']; |
||
| 276 | $title = $topic['topic_title']; |
||
| 277 | $digest->addItem($title, $link, $author, $summary); |
||
| 278 | } |
||
| 279 | $digest->buildContent(); |
||
| 280 | |||
| 281 | return true; |
||
| 282 | } |
||
| 284 |