| Total Complexity | 48 |
| Total Lines | 336 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ReadHandler 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 ReadHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace XoopsModules\Newbb; |
||
| 49 | class ReadHandler extends \XoopsPersistableObjectHandler |
||
| 50 | { |
||
| 51 | /** |
||
| 52 | * Object type. |
||
| 53 | * <ul> |
||
| 54 | * <li>forum</li> |
||
| 55 | * <li>topic</li> |
||
| 56 | * </ul> |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | public $type; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * seconds records will persist. |
||
| 64 | * assigned from $GLOBALS['xoopsModuleConfig']["read_expire"] |
||
| 65 | * <ul> |
||
| 66 | * <li>positive days = delete all read records exist in the tables before expire time // irmtfan add comment</li> |
||
| 67 | * <li>0 = never expires // irmtfan change comment</li> |
||
| 68 | * <li>-1 or any negative days = never records // irmtfan change comment</li> |
||
| 69 | * </ul> |
||
| 70 | * |
||
| 71 | * @var integer |
||
| 72 | */ |
||
| 73 | public $lifetime; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * storage mode for records. |
||
| 77 | * assigned from $GLOBALS['xoopsModuleConfig']["read_mode"] |
||
| 78 | * <ul> |
||
| 79 | * <li>0 = never records</li> |
||
| 80 | * <li>1 = uses cookie</li> |
||
| 81 | * <li>2 = stores in database</li> |
||
| 82 | * </ul> |
||
| 83 | * |
||
| 84 | * @var integer |
||
| 85 | */ |
||
| 86 | public $mode; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param \XoopsDatabase $db |
||
| 90 | * @param $type |
||
| 91 | */ |
||
| 92 | public function __construct(\XoopsDatabase $db, $type) |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Clear garbage |
||
| 105 | * |
||
| 106 | * Delete all expired and duplicated records |
||
| 107 | */ |
||
| 108 | // START irmtfan rephrase function to 1- add clearDuplicate and 2- dont clean when read_expire = 0 |
||
| 109 | public function clearGarbage() |
||
| 110 | { |
||
| 111 | // irmtfan clear duplicaed rows |
||
| 112 | if (!$result = $this->clearDuplicate()) { |
||
| 113 | return false; |
||
| 114 | } |
||
| 115 | |||
| 116 | $sql = 'DELETE bb FROM ' . $this->table . ' AS bb' . ' LEFT JOIN ' . $this->table . ' AS aa ON bb.read_item = aa.read_item ' . ' WHERE aa.post_id > bb.post_id'; |
||
| 117 | if (!$result = $this->db->queryF($sql)) { |
||
| 118 | //xoops_error($this->db->error()); |
||
| 119 | return false; |
||
| 120 | } |
||
| 121 | // irmtfan if read_expire = 0 dont clean |
||
| 122 | if (empty($this->lifetime)) { |
||
| 123 | return true; |
||
| 124 | } |
||
| 125 | // irmtfan move here and rephrase |
||
| 126 | $expire = time() - (int)$this->lifetime; |
||
| 127 | $sql = 'DELETE FROM ' . $this->table . ' WHERE read_time < ' . $expire; |
||
| 128 | if (!$result = $this->db->queryF($sql)) { |
||
| 129 | //xoops_error($this->db->error()); |
||
| 130 | return false; |
||
| 131 | } |
||
| 132 | |||
| 133 | return true; |
||
| 134 | } |
||
| 135 | |||
| 136 | // END irmtfan rephrase function to 1- add clearDuplicate and 2- dont clean when read_expire = 0 |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param $read_item |
||
| 140 | * @param null $uid |
||
| 141 | * @return bool|mixed|null |
||
| 142 | */ |
||
| 143 | public function getRead($read_item, $uid = null) |
||
| 144 | { |
||
| 145 | if (empty($this->mode)) { |
||
| 146 | return null; |
||
| 147 | } |
||
| 148 | if (1 == $this->mode) { |
||
| 149 | return $this->getReadCookie($read_item); |
||
| 150 | } |
||
| 151 | |||
| 152 | return $this->getReadDb($read_item, $uid); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param $item_id |
||
| 157 | * @return mixed |
||
| 158 | */ |
||
| 159 | public function getReadCookie($item_id) |
||
| 160 | { |
||
| 161 | $cookie_name = ('forum' === $this->type) ? 'LF' : 'LT'; |
||
| 162 | $cookie_var = $item_id; |
||
| 163 | // irmtfan set true to return array |
||
| 164 | $lastview = newbbGetCookie($cookie_name, true); |
||
| 165 | |||
| 166 | return @$lastview[$cookie_var]; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param $read_item |
||
| 171 | * @param $uid |
||
| 172 | * @return bool|null |
||
| 173 | */ |
||
| 174 | public function getReadDb($read_item, $uid) |
||
| 175 | { |
||
| 176 | if (empty($uid)) { |
||
| 177 | if (is_object($GLOBALS['xoopsUser'])) { |
||
| 178 | $uid = $GLOBALS['xoopsUser']->getVar('uid'); |
||
| 179 | } else { |
||
| 180 | return false; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | $sql = 'SELECT post_id ' . ' FROM ' . $this->table . ' WHERE read_item = ' . (int)$read_item . ' AND uid = ' . (int)$uid; |
||
| 184 | if (!$result = $this->db->queryF($sql, 1)) { |
||
| 185 | return null; |
||
| 186 | } |
||
| 187 | list($post_id) = $this->db->fetchRow($result); |
||
| 188 | |||
| 189 | return $post_id; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param $read_item |
||
| 194 | * @param $post_id |
||
| 195 | * @param null $uid |
||
| 196 | * @return bool|mixed|void |
||
| 197 | */ |
||
| 198 | public function setRead($read_item, $post_id, $uid = null) |
||
| 199 | { |
||
| 200 | if (empty($this->mode)) { |
||
| 201 | return true; |
||
| 202 | } |
||
| 203 | |||
| 204 | if (1 == $this->mode) { |
||
| 205 | return $this->setReadCookie($read_item, $post_id); |
||
| 206 | } |
||
| 207 | |||
| 208 | return $this->setReadDb($read_item, $post_id, $uid); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param $read_item |
||
| 213 | * @param $post_id |
||
| 214 | */ |
||
| 215 | public function setReadCookie($read_item, $post_id) |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param $read_item |
||
| 225 | * @param $post_id |
||
| 226 | * @param $uid |
||
| 227 | * @return bool|mixed |
||
| 228 | */ |
||
| 229 | public function setReadDb($read_item, $post_id, $uid) |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param $items |
||
| 254 | * @param null $uid |
||
| 255 | * @return array|null |
||
| 256 | */ |
||
| 257 | public function isReadItems(&$items, $uid = null) |
||
| 258 | { |
||
| 259 | $ret = null; |
||
| 260 | if (empty($this->mode)) { |
||
| 261 | return $ret; |
||
| 262 | } |
||
| 263 | |||
| 264 | if (1 == $this->mode) { |
||
| 265 | $ret = $this->isReadItemsCookie($items); |
||
| 266 | } else { |
||
| 267 | $ret = $this->isReadItemsDb($items, $uid); |
||
| 268 | } |
||
| 269 | |||
| 270 | return $ret; |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param $items |
||
| 275 | * @return array |
||
| 276 | */ |
||
| 277 | public function isReadItemsCookie(&$items) |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param $items |
||
| 292 | * @param $uid |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | public function isReadItemsDb(&$items, $uid) |
||
| 325 | } |
||
| 326 | |||
| 327 | // START irmtfan add clear duplicated rows function |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @return bool |
||
| 331 | */ |
||
| 332 | public function clearDuplicate() |
||
| 385 | } |
||
| 386 | // END irmtfan add clear duplicated rows function |
||
| 388 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.