Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like NewbbReadHandler 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 NewbbReadHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
64 | class NewbbReadHandler extends XoopsPersistableObjectHandler |
||
65 | { |
||
66 | /** |
||
67 | * Object type. |
||
68 | * <ul> |
||
69 | * <li>forum</li> |
||
70 | * <li>topic</li> |
||
71 | * </ul> |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | public $type; |
||
76 | |||
77 | /** |
||
78 | * seconds records will persist. |
||
79 | * assigned from $GLOBALS['xoopsModuleConfig']["read_expire"] |
||
80 | * <ul> |
||
81 | * <li>positive days = delete all read records exist in the tables before expire time // irmtfan add comment</li> |
||
82 | * <li>0 = never expires // irmtfan change comment</li> |
||
83 | * <li>-1 or any negative days = never records // irmtfan change comment</li> |
||
84 | * </ul> |
||
85 | * |
||
86 | * @var integer |
||
87 | */ |
||
88 | public $lifetime; |
||
89 | |||
90 | /** |
||
91 | * storage mode for records. |
||
92 | * assigned from $GLOBALS['xoopsModuleConfig']["read_mode"] |
||
93 | * <ul> |
||
94 | * <li>0 = never records</li> |
||
95 | * <li>1 = uses cookie</li> |
||
96 | * <li>2 = stores in database</li> |
||
97 | * </ul> |
||
98 | * |
||
99 | * @var integer |
||
100 | */ |
||
101 | public $mode; |
||
102 | |||
103 | /** |
||
104 | * @param XoopsDatabase|null $db |
||
105 | * @param $type |
||
106 | */ |
||
107 | public function __construct(XoopsDatabase $db, $type) |
||
117 | |||
118 | /** |
||
119 | * Clear garbage |
||
120 | * |
||
121 | * Delete all expired and duplicated records |
||
122 | */ |
||
123 | // START irmtfan rephrase function to 1- add clearDuplicate and 2- dont clean when read_expire = 0 |
||
124 | public function clearGarbage() |
||
150 | |||
151 | // END irmtfan rephrase function to 1- add clearDuplicate and 2- dont clean when read_expire = 0 |
||
152 | |||
153 | /** |
||
154 | * @param $read_item |
||
155 | * @param null $uid |
||
156 | * @return bool|mixed|null |
||
157 | */ |
||
158 | public function getRead($read_item, $uid = null) |
||
169 | |||
170 | /** |
||
171 | * @param $item_id |
||
172 | * @return mixed |
||
173 | */ |
||
174 | public function getReadCookie($item_id) |
||
175 | { |
||
176 | $cookie_name = ('forum' === $this->type) ? 'LF' : 'LT'; |
||
177 | $cookie_var = $item_id; |
||
178 | // irmtfan set true to return array |
||
179 | $lastview = newbbGetCookie($cookie_name, true); |
||
180 | |||
181 | return @$lastview[$cookie_var]; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @param $read_item |
||
186 | * @param $uid |
||
187 | * @return bool|null |
||
188 | */ |
||
189 | public function getReadDb($read_item, $uid) |
||
190 | { |
||
191 | View Code Duplication | if (empty($uid)) { |
|
192 | if (is_object($GLOBALS['xoopsUser'])) { |
||
193 | $uid = $GLOBALS['xoopsUser']->getVar('uid'); |
||
194 | } else { |
||
195 | return false; |
||
196 | } |
||
197 | } |
||
198 | $sql = 'SELECT post_id ' . ' FROM ' . $this->table . ' WHERE read_item = ' . (int)$read_item . ' AND uid = ' . (int)$uid; |
||
199 | if (!$result = $this->db->queryF($sql, 1)) { |
||
200 | return null; |
||
201 | } |
||
202 | list($post_id) = $this->db->fetchRow($result); |
||
203 | |||
204 | return $post_id; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @param $read_item |
||
209 | * @param $post_id |
||
210 | * @param null $uid |
||
211 | * @return bool|mixed|void |
||
212 | */ |
||
213 | public function setRead($read_item, $post_id, $uid = null) |
||
224 | |||
225 | /** |
||
226 | * @param $read_item |
||
227 | * @param $post_id |
||
228 | */ |
||
229 | public function setReadCookie($read_item, $post_id) |
||
236 | |||
237 | /** |
||
238 | * @param $read_item |
||
239 | * @param $post_id |
||
240 | * @param $uid |
||
241 | * @return bool|mixed |
||
242 | */ |
||
243 | public function setReadDb($read_item, $post_id, $uid) |
||
265 | |||
266 | /** |
||
267 | * @param $items |
||
268 | * @param null $uid |
||
269 | * @return array|null |
||
270 | */ |
||
271 | public function isReadItems(&$items, $uid = null) |
||
286 | |||
287 | /** |
||
288 | * @param $items |
||
289 | * @return array |
||
290 | */ |
||
291 | public function isReadItemsCookie(&$items) |
||
303 | |||
304 | /** |
||
305 | * @param $items |
||
306 | * @param $uid |
||
307 | * @return array |
||
308 | */ |
||
309 | public function isReadItemsDb(&$items, $uid) |
||
340 | |||
341 | // START irmtfan add clear duplicated rows function |
||
342 | |||
343 | /** |
||
344 | * @return bool |
||
345 | */ |
||
346 | public function clearDuplicate() |
||
400 | // END irmtfan add clear duplicated rows function |
||
401 | } |
||
402 |
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.