Conditions | 25 |
Paths | 18432 |
Total Lines | 116 |
Code Lines | 73 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
30 | function newbb_search( |
||
31 | $queryarray, |
||
32 | $andor, |
||
33 | $limit, |
||
34 | $offset, |
||
35 | $userid, |
||
36 | $forums = 0, |
||
37 | $sortby = 0, |
||
38 | $searchin = 'both', |
||
39 | \CriteriaCompo $criteriaExtra = null |
||
40 | ) { |
||
41 | global $myts, $xoopsDB; |
||
42 | // irmtfan - in XOOPSCORE/search.php $GLOBALS['xoopsModuleConfig'] is not set |
||
43 | if (!isset($GLOBALS['xoopsModuleConfig'])) { |
||
44 | $GLOBALS['xoopsModuleConfig'] = newbbLoadConfig(); |
||
45 | } |
||
46 | // irmtfan - in XOOPSCORE/search.php $xoopsModule is not set |
||
47 | if (!is_object($GLOBALS['xoopsModule']) && is_object($GLOBALS['module']) |
||
48 | && 'newbb' === $GLOBALS['module']->getVar('dirname')) { |
||
49 | $GLOBALS['xoopsModule'] = $GLOBALS['module']; |
||
50 | } |
||
51 | /** @var Newbb\ForumHandler $forumHandler */ |
||
52 | $forumHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Forum'); |
||
53 | $validForums = $forumHandler->getIdsByValues($forums); // can we use view permission? $forumHandler->getIdsByValues($forums, "view") |
||
54 | |||
55 | $criteriaPost = new \CriteriaCompo(); |
||
56 | $criteriaPost->add(new \Criteria('p.approved', 1), 'AND'); // only active posts |
||
57 | |||
58 | $forum_list = []; // get forum lists just for forum names |
||
59 | if (count($validForums) > 0) { |
||
60 | $criteriaPermissions = new \CriteriaCompo(); |
||
61 | $criteriaPermissions->add(new \Criteria('p.forum_id', '(' . implode(',', $validForums) . ')', 'IN'), 'AND'); |
||
62 | $forum_list = $forumHandler->getAll(new \Criteria('forum_id', '(' . implode(', ', $validForums) . ')', 'IN'), 'forum_name', false); |
||
|
|||
63 | } |
||
64 | |||
65 | if (is_numeric($userid) && 0 !== $userid) { |
||
66 | $criteriaUser = new \CriteriaCompo(); |
||
67 | $criteriaUser->add(new \Criteria('p.uid', $userid), 'OR'); |
||
68 | } elseif ($userid && is_array($userid)) { |
||
69 | $userid = array_map('\intval', $userid); |
||
70 | $criteriaUser = new \CriteriaCompo(); |
||
71 | $criteriaUser->add(new \Criteria('p.uid', '(' . implode(',', $userid) . ')', 'IN'), 'OR'); |
||
72 | } |
||
73 | |||
74 | $count = 0; |
||
75 | if (is_array($queryarray)) { |
||
76 | $count = count($queryarray); |
||
77 | } |
||
78 | $highlightKey = ''; |
||
79 | if ($count > 0) { |
||
80 | $criteriaKeywords = new \CriteriaCompo(); |
||
81 | foreach ($queryarray as $queryTerm) { |
||
82 | $termCriteria = new \CriteriaCompo(); |
||
83 | $queryTermLike = '%' . $xoopsDB->escape($queryTerm) . '%'; |
||
84 | if ('title' === $searchin || 'both' === $searchin) { |
||
85 | $termCriteria->add(new \Criteria('p.subject', $queryTermLike, 'LIKE'), 'OR'); |
||
86 | } |
||
87 | if ('text' === $searchin || 'both' === $searchin) { |
||
88 | $termCriteria->add(new \Criteria('t.post_text', $queryTermLike, 'LIKE'), 'OR'); |
||
89 | } |
||
90 | $criteriaKeywords->add($termCriteria, $andor); |
||
91 | } |
||
92 | // add highlight keywords to post links |
||
93 | $highlightKey = '&keywords=' . implode(' ', $queryarray); |
||
94 | $highlightKey = str_replace(' ', '+', $highlightKey); |
||
95 | } |
||
96 | $criteria = new \CriteriaCompo(); |
||
97 | $criteria->add($criteriaPost, 'AND'); |
||
98 | if (null !== $criteriaPermissions) { |
||
99 | $criteria->add($criteriaPermissions, 'AND'); |
||
100 | } |
||
101 | if (isset($criteriaUser)) { |
||
102 | $criteria->add($criteriaUser, 'AND'); |
||
103 | } |
||
104 | if (isset($criteriaKeywords)) { |
||
105 | $criteria->add($criteriaKeywords, 'AND'); |
||
106 | } |
||
107 | if (isset($criteriaExtra)) { |
||
108 | $criteria->add($criteriaExtra, 'AND'); |
||
109 | } |
||
110 | //$criteria->setLimit($limit); // no need for this |
||
111 | //$criteria->setStart($offset); // no need for this |
||
112 | |||
113 | if (empty($sortby)) { |
||
114 | $sortby = 'p.post_time'; |
||
115 | } |
||
116 | $criteria->setSort($sortby); |
||
117 | $order = 'ASC'; |
||
118 | if ('p.post_time' === $sortby) { |
||
119 | $order = 'DESC'; |
||
120 | } |
||
121 | $criteria->setOrder($order); |
||
122 | |||
123 | /** @var Newbb\PostHandler $postHandler */ |
||
124 | $postHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Post'); |
||
125 | $posts = $postHandler->getPostsByLimit($criteria, $limit, $offset); |
||
126 | |||
127 | $ret = []; |
||
128 | $i = 0; |
||
129 | foreach (array_keys($posts) as $id) { |
||
130 | /** @var Newbb\Post $post */ |
||
131 | $post = $posts[$id]; |
||
132 | $post_data = $post->getPostBody(); |
||
133 | $ret[$i]['topic_id'] = $post->getVar('topic_id'); |
||
134 | $ret[$i]['link'] = XOOPS_URL . '/modules/newbb/viewtopic.php?post_id=' . $post->getVar('post_id') . $highlightKey; // add highlight key |
||
135 | $ret[$i]['title'] = $post_data['subject']; |
||
136 | $ret[$i]['time'] = $post_data['date']; |
||
137 | $ret[$i]['forum_name'] = $myts->htmlSpecialChars($forum_list[$post->getVar('forum_id')]['forum_name']); |
||
138 | $ret[$i]['forum_link'] = XOOPS_URL . '/modules/newbb/viewforum.php?forum=' . $post->getVar('forum_id'); |
||
139 | $ret[$i]['post_text'] = $post_data['text']; |
||
140 | $ret[$i]['uid'] = $post->getVar('uid'); |
||
141 | $ret[$i]['poster'] = $post->getVar('uid') ? '<a href="' . XOOPS_URL . '/userinfo.php?uid=' . $ret[$i]['uid'] . '">' . $post_data['author'] . '</a>' : $post_data['author']; |
||
142 | ++$i; |
||
143 | } |
||
144 | |||
145 | return $ret; |
||
146 | } |
||
147 |