Total Complexity | 43 |
Total Lines | 274 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like MessageHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MessageHandler, and based on these observations, apply Extract Interface, too.
1 | <?php namespace XoopsModules\Xfguestbook; |
||
29 | class MessageHandler |
||
30 | { |
||
31 | public $db; |
||
32 | |||
33 | /** |
||
34 | * MessageHandler constructor. |
||
35 | * @param \XoopsDatabase|null $db |
||
36 | */ |
||
37 | public function __construct(\XoopsDatabase $db = null) |
||
38 | { |
||
39 | $this->db = $db; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @return Message |
||
44 | */ |
||
45 | public function create() |
||
46 | { |
||
47 | return new Message(); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param $id |
||
52 | * @return bool|Message |
||
53 | */ |
||
54 | public function get($id) |
||
55 | { |
||
56 | $id = (int)$id; |
||
57 | if ($id > 0) { |
||
58 | $sql = 'SELECT * FROM ' . $this->db->prefix('xfguestbook_msg') . ' WHERE msg_id=' . $id; |
||
|
|||
59 | if (!$result = $this->db->query($sql)) { |
||
60 | return false; |
||
61 | } |
||
62 | $numrows = $this->db->getRowsNum($result); |
||
63 | if (1 == $numrows) { |
||
64 | $msg = new Message(); |
||
65 | $msg->assignVars($this->db->fetchArray($result)); |
||
66 | |||
67 | return $msg; |
||
68 | } |
||
69 | } |
||
70 | |||
71 | return false; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param \XoopsObject $msg |
||
76 | * @return bool |
||
77 | */ |
||
78 | public function insert(\XoopsObject $msg) |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * @param \XoopsObject $msg |
||
171 | * @return bool |
||
172 | */ |
||
173 | public function delete(\XoopsObject $msg) |
||
174 | { |
||
175 | global $xoopsModule; |
||
176 | if (Message::class !== get_class($msg)) { |
||
177 | return false; |
||
178 | } |
||
179 | $sql = sprintf('DELETE FROM `%s` WHERE msg_id = %u', $this->db->prefix('xfguestbook_msg'), $msg->getVar('msg_id')); |
||
180 | if (isset($this->commentstable) && '' !== $this->commentstable) { |
||
181 | xoops_comment_delete($xoopsModule->getVar('mid'), $msg_id); |
||
182 | } |
||
183 | |||
184 | if (!$result = $this->db->query($sql)) { |
||
185 | return false; |
||
186 | } |
||
187 | |||
188 | return true; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param null|\CriteriaElement $criteria |
||
193 | * @return array |
||
194 | */ |
||
195 | public function &getObjects(\CriteriaElement $criteria = null) |
||
196 | { |
||
197 | $ret = []; |
||
198 | $limit = $start = 0; |
||
199 | $sql = 'SELECT * FROM ' . $this->db->prefix('xfguestbook_msg'); |
||
200 | if (null !== $criteria && is_subclass_of($criteria, 'CriteriaElement')) { |
||
201 | $sql .= ' ' . $criteria->renderWhere(); |
||
202 | $sort = ('' !== $criteria->getSort()) ? $criteria->getSort() : 'msg_id'; |
||
203 | $sql .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder(); |
||
204 | $limit = $criteria->getLimit(); |
||
205 | $start = $criteria->getStart(); |
||
206 | } |
||
207 | $result = $this->db->query($sql, $limit, $start); |
||
208 | if (!$result) { |
||
209 | return $ret; |
||
210 | } |
||
211 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
212 | $msg = new Message(); |
||
213 | $msg->assignVars($myrow); |
||
214 | $ret[] = $msg; |
||
215 | unset($msg); |
||
216 | } |
||
217 | |||
218 | return $ret; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @param null|\CriteriaElement $criteria |
||
223 | * @return int |
||
224 | */ |
||
225 | public function countMsg(\CriteriaElement $criteria = null) |
||
226 | { |
||
227 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('xfguestbook_msg'); |
||
228 | if (null !== $criteria && is_subclass_of($criteria, 'CriteriaElement')) { |
||
229 | $sql .= ' ' . $criteria->renderWhere(); |
||
230 | } |
||
231 | if (!$result = $this->db->query($sql)) { |
||
232 | return 0; |
||
233 | } |
||
234 | list($count) = $this->db->fetchRow($result); |
||
235 | |||
236 | return $count; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * @param null|\CriteriaElement $criteria |
||
241 | * @return array|bool |
||
242 | */ |
||
243 | public function countMsgByCountry(\CriteriaElement $criteria = null) |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * @param \CriteriaElement|null $criteria |
||
264 | * @return array|bool |
||
265 | */ |
||
266 | public function countMsgByGender(\CriteriaElement $criteria = null) |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @param null|\CriteriaElement $criteria |
||
286 | * @return array|int |
||
287 | */ |
||
288 | public function getMsgImg(\CriteriaElement $criteria = null) |
||
303 | } |
||
304 | } |
||
305 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.