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 XoopsTopic 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 XoopsTopic, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class XoopsTopic |
||
31 | { |
||
32 | public $table; |
||
33 | public $topic_id; |
||
34 | public $topic_pid; |
||
35 | public $topic_title; |
||
36 | public $topic_imgurl; |
||
37 | public $prefix; // only used in topic tree |
||
38 | public $use_permission = false; |
||
39 | public $mid; // module id used for setting permission |
||
40 | |||
41 | /** |
||
42 | * @param $table |
||
43 | * @param int $topicid |
||
44 | */ |
||
45 | public function __construct($table, $topicid = 0) |
||
46 | { |
||
47 | $this->db = XoopsDatabaseFactory::getDatabaseConnection(); |
||
48 | $this->table = $table; |
||
49 | if (is_array($topicid)) { |
||
50 | $this->makeTopic($topicid); |
||
51 | } elseif ($topicid != 0) { |
||
52 | $this->getTopic((int)$topicid); |
||
53 | } else { |
||
54 | $this->topic_id = $topicid; |
||
55 | } |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @param $value |
||
60 | */ |
||
61 | public function setTopicTitle($value) |
||
65 | |||
66 | /** |
||
67 | * @param $value |
||
68 | */ |
||
69 | public function setTopicImgurl($value) |
||
73 | |||
74 | /** |
||
75 | * @param $value |
||
76 | */ |
||
77 | public function setTopicPid($value) |
||
81 | |||
82 | /** |
||
83 | * @param $topicid |
||
84 | */ |
||
85 | View Code Duplication | public function getTopic($topicid) |
|
86 | { |
||
87 | $topicid = (int)$topicid; |
||
88 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE topic_id=' . $topicid . ''; |
||
89 | $array = $this->db->fetchArray($this->db->query($sql)); |
||
90 | $this->makeTopic($array); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param $array |
||
95 | */ |
||
96 | public function makeTopic($array) |
||
102 | |||
103 | /** |
||
104 | * @param $mid |
||
105 | */ |
||
106 | public function usePermission($mid) |
||
111 | |||
112 | /** |
||
113 | * @return bool |
||
114 | */ |
||
115 | public function store() |
||
116 | { |
||
117 | $myts = MyTextSanitizer::getInstance(); |
||
118 | $title = ''; |
||
119 | $imgurl = ''; |
||
120 | if (isset($this->topic_title) && $this->topic_title != '') { |
||
121 | $title = $myts->addSlashes($this->topic_title); |
||
122 | } |
||
123 | if (isset($this->topic_imgurl) && $this->topic_imgurl != '') { |
||
124 | $imgurl = $myts->addSlashes($this->topic_imgurl); |
||
125 | } |
||
126 | if (!isset($this->topic_pid) || !is_numeric($this->topic_pid)) { |
||
127 | $this->topic_pid = 0; |
||
128 | } |
||
129 | if (empty($this->topic_id)) { |
||
130 | $this->topic_id = $this->db->genId($this->table . '_topic_id_seq'); |
||
131 | $sql = sprintf("INSERT INTO %s (topic_id, topic_pid, topic_imgurl, topic_title) VALUES (%u, %u, '%s', '%s')", $this->table, $this->topic_id, $this->topic_pid, $imgurl, $title); |
||
132 | } else { |
||
133 | $sql = sprintf("UPDATE %s SET topic_pid = %u, topic_imgurl = '%s', topic_title = '%s' WHERE topic_id = %u", $this->table, $this->topic_pid, $imgurl, $title, $this->topic_id); |
||
134 | } |
||
135 | if (!$result = $this->db->query($sql)) { |
||
136 | ErrorHandler::show('0022'); |
||
137 | } |
||
138 | if ($this->use_permission == true) { |
||
139 | if (empty($this->topic_id)) { |
||
140 | $this->topic_id = $this->db->getInsertId(); |
||
141 | } |
||
142 | $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
||
143 | $parent_topics = $xt->getAllParentId($this->topic_id); |
||
144 | View Code Duplication | if (!empty($this->m_groups) && is_array($this->m_groups)) { |
|
145 | foreach ($this->m_groups as $m_g) { |
||
146 | $moderate_topics = XoopsPerms::getPermitted($this->mid, 'ModInTopic', $m_g); |
||
147 | $add = true; |
||
148 | // only grant this permission when the group has this permission in all parent topics of the created topic |
||
149 | foreach ($parent_topics as $p_topic) { |
||
150 | if (!in_array($p_topic, $moderate_topics)) { |
||
151 | $add = false; |
||
152 | continue; |
||
153 | } |
||
154 | } |
||
155 | if ($add == true) { |
||
156 | $xp = new XoopsPerms(); |
||
157 | $xp->setModuleId($this->mid); |
||
158 | $xp->setName('ModInTopic'); |
||
159 | $xp->setItemId($this->topic_id); |
||
160 | $xp->store(); |
||
161 | $xp->addGroup($m_g); |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | View Code Duplication | if (!empty($this->s_groups) && is_array($this->s_groups)) { |
|
166 | foreach ($s_groups as $s_g) { |
||
167 | $submit_topics = XoopsPerms::getPermitted($this->mid, 'SubmitInTopic', $s_g); |
||
168 | $add = true; |
||
169 | foreach ($parent_topics as $p_topic) { |
||
170 | if (!in_array($p_topic, $submit_topics)) { |
||
171 | $add = false; |
||
172 | continue; |
||
173 | } |
||
174 | } |
||
175 | if ($add == true) { |
||
176 | $xp = new XoopsPerms(); |
||
177 | $xp->setModuleId($this->mid); |
||
178 | $xp->setName('SubmitInTopic'); |
||
179 | $xp->setItemId($this->topic_id); |
||
180 | $xp->store(); |
||
181 | $xp->addGroup($s_g); |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | View Code Duplication | if (!empty($this->r_groups) && is_array($this->r_groups)) { |
|
186 | foreach ($r_groups as $r_g) { |
||
187 | $read_topics = XoopsPerms::getPermitted($this->mid, 'ReadInTopic', $r_g); |
||
188 | $add = true; |
||
189 | foreach ($parent_topics as $p_topic) { |
||
190 | if (!in_array($p_topic, $read_topics)) { |
||
191 | $add = false; |
||
192 | continue; |
||
193 | } |
||
194 | } |
||
195 | if ($add == true) { |
||
196 | $xp = new XoopsPerms(); |
||
197 | $xp->setModuleId($this->mid); |
||
198 | $xp->setName('ReadInTopic'); |
||
199 | $xp->setItemId($this->topic_id); |
||
200 | $xp->store(); |
||
201 | $xp->addGroup($r_g); |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | } |
||
206 | |||
207 | return true; |
||
208 | } |
||
209 | |||
210 | public function delete() |
||
215 | |||
216 | /** |
||
217 | * @return int |
||
218 | */ |
||
219 | public function topic_id() |
||
223 | |||
224 | public function topic_pid() |
||
228 | |||
229 | /** |
||
230 | * @param string $format |
||
231 | * |
||
232 | * @return mixed |
||
233 | */ |
||
234 | View Code Duplication | public function topic_title($format = 'S') |
|
235 | { |
||
236 | $myts = MyTextSanitizer::getInstance(); |
||
237 | switch ($format) { |
||
238 | case 'S': |
||
239 | case 'E': |
||
240 | $title = $myts->htmlSpecialChars($this->topic_title); |
||
241 | break; |
||
242 | case 'P': |
||
243 | case 'F': |
||
244 | $title = $myts->htmlSpecialChars($myts->stripSlashesGPC($this->topic_title)); |
||
245 | break; |
||
246 | } |
||
247 | |||
248 | return $title; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @param string $format |
||
253 | * |
||
254 | * @return mixed |
||
255 | */ |
||
256 | View Code Duplication | public function topic_imgurl($format = 'S') |
|
257 | { |
||
258 | $myts = MyTextSanitizer::getInstance(); |
||
259 | switch ($format) { |
||
260 | case 'S': |
||
261 | case 'E': |
||
262 | $imgurl = $myts->htmlSpecialChars($this->topic_imgurl); |
||
263 | break; |
||
264 | case 'P': |
||
265 | case 'F': |
||
266 | $imgurl = $myts->htmlSpecialChars($myts->stripSlashesGPC($this->topic_imgurl)); |
||
267 | break; |
||
268 | } |
||
269 | |||
270 | return $imgurl; |
||
271 | } |
||
272 | |||
273 | public function prefix() |
||
280 | |||
281 | /** |
||
282 | * @return array |
||
283 | */ |
||
284 | View Code Duplication | public function getFirstChildTopics() |
|
285 | { |
||
286 | $ret = array(); |
||
287 | $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
||
288 | $topic_arr = $xt->getFirstChild($this->topic_id, 'topic_title'); |
||
289 | if (is_array($topic_arr) && count($topic_arr)) { |
||
290 | foreach ($topic_arr as $topic) { |
||
291 | $ret[] = new XoopsTopic($this->table, $topic); |
||
292 | } |
||
293 | } |
||
294 | |||
295 | return $ret; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * @return array |
||
300 | */ |
||
301 | View Code Duplication | public function getAllChildTopics() |
|
302 | { |
||
303 | $ret = array(); |
||
304 | $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
||
305 | $topic_arr = $xt->getAllChild($this->topic_id, 'topic_title'); |
||
306 | if (is_array($topic_arr) && count($topic_arr)) { |
||
307 | foreach ($topic_arr as $topic) { |
||
308 | $ret[] = new XoopsTopic($this->table, $topic); |
||
309 | } |
||
310 | } |
||
311 | |||
312 | return $ret; |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * @return array |
||
317 | */ |
||
318 | View Code Duplication | public function getChildTopicsTreeArray() |
|
319 | { |
||
320 | $ret = array(); |
||
321 | $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
||
322 | $topic_arr = $xt->getChildTreeArray($this->topic_id, 'topic_title'); |
||
323 | if (is_array($topic_arr) && count($topic_arr)) { |
||
324 | foreach ($topic_arr as $topic) { |
||
325 | $ret[] = new XoopsTopic($this->table, $topic); |
||
326 | } |
||
327 | } |
||
328 | |||
329 | return $ret; |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * @param int $none |
||
334 | * @param $seltopic |
||
335 | * @param string $selname |
||
336 | * @param string $onchange |
||
337 | */ |
||
338 | public function makeTopicSelBox($none = 0, $seltopic = -1, $selname = '', $onchange = '') |
||
339 | { |
||
340 | $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
||
341 | if ($seltopic != -1) { |
||
342 | $xt->makeMySelBox('topic_title', 'topic_title', $seltopic, $none, $selname, $onchange); |
||
343 | } elseif (!empty($this->topic_id)) { |
||
344 | $xt->makeMySelBox('topic_title', 'topic_title', $this->topic_id, $none, $selname, $onchange); |
||
345 | } else { |
||
346 | $xt->makeMySelBox('topic_title', 'topic_title', 0, $none, $selname, $onchange); |
||
347 | } |
||
348 | } |
||
349 | |||
350 | //generates nicely formatted linked path from the root id to a given id |
||
351 | /** |
||
352 | * @param $funcURL |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | View Code Duplication | public function getNiceTopicPathFromId($funcURL) |
|
357 | { |
||
358 | $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
||
359 | $ret = $xt->getNicePathFromId($this->topic_id, 'topic_title', $funcURL); |
||
360 | |||
361 | return $ret; |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * @return array |
||
366 | */ |
||
367 | View Code Duplication | public function getAllChildTopicsId() |
|
368 | { |
||
369 | $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
||
370 | $ret = $xt->getAllChildId($this->topic_id, 'topic_title'); |
||
371 | |||
372 | return $ret; |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * @return array |
||
377 | */ |
||
378 | public function getTopicsList() |
||
389 | |||
390 | /** |
||
391 | * @param $pid |
||
392 | * @param $title |
||
393 | * |
||
394 | * @return bool |
||
395 | */ |
||
396 | public function topicExists($pid, $title) |
||
407 | } |
||
408 |
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.