Total Complexity | 86 |
Total Lines | 524 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like XoopsStory 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 XoopsStory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class XoopsStory |
||
30 | { |
||
31 | public $table; |
||
32 | public $storyid; |
||
33 | public $topicid; |
||
34 | public $uid; |
||
35 | public $title; |
||
36 | public $hometext; |
||
37 | public $bodytext = ''; |
||
38 | public $counter; |
||
39 | public $created; |
||
40 | public $published; |
||
41 | public $expired; |
||
42 | public $hostname; |
||
43 | public $nohtml = 0; |
||
44 | public $nosmiley = 0; |
||
45 | public $ihome = 0; |
||
46 | public $notifypub = 0; |
||
47 | public $type; |
||
48 | public $approved; |
||
49 | public $topicdisplay; |
||
50 | public $topicalign; |
||
51 | public $db; |
||
52 | public $topicstable; |
||
53 | public $comments; |
||
54 | |||
55 | /** |
||
56 | * @param int|array $storyid |
||
57 | */ |
||
58 | public function Story($storyid = -1) |
||
59 | { |
||
60 | $this->db = XoopsDatabaseFactory::getDatabaseConnection(); |
||
61 | $this->table = ''; |
||
62 | $this->topicstable = ''; |
||
63 | if (is_array($storyid)) { |
||
64 | $this->makeStory($storyid); |
||
65 | } elseif ($storyid != -1) { |
||
66 | $this->getStory((int) $storyid); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param $value |
||
72 | */ |
||
73 | public function setStoryId($value) |
||
74 | { |
||
75 | $this->storyid = (int) $value; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param $value |
||
80 | */ |
||
81 | public function setTopicId($value) |
||
82 | { |
||
83 | $this->topicid = (int) $value; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param $value |
||
88 | */ |
||
89 | public function setUid($value) |
||
90 | { |
||
91 | $this->uid = (int) $value; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param $value |
||
96 | */ |
||
97 | public function setTitle($value) |
||
98 | { |
||
99 | $this->title = $value; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param $value |
||
104 | */ |
||
105 | public function setHometext($value) |
||
106 | { |
||
107 | $this->hometext = $value; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param $value |
||
112 | */ |
||
113 | public function setBodytext($value) |
||
114 | { |
||
115 | $this->bodytext = $value; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param $value |
||
120 | */ |
||
121 | public function setPublished($value) |
||
122 | { |
||
123 | $this->published = (int) $value; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @param $value |
||
128 | */ |
||
129 | public function setExpired($value) |
||
130 | { |
||
131 | $this->expired = (int) $value; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @param $value |
||
136 | */ |
||
137 | public function setHostname($value) |
||
138 | { |
||
139 | $this->hostname = $value; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param int $value |
||
144 | */ |
||
145 | public function setNohtml($value = 0) |
||
146 | { |
||
147 | $this->nohtml = $value; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param int $value |
||
152 | */ |
||
153 | public function setNosmiley($value = 0) |
||
154 | { |
||
155 | $this->nosmiley = $value; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @param $value |
||
160 | */ |
||
161 | public function setIhome($value) |
||
162 | { |
||
163 | $this->ihome = $value; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @param $value |
||
168 | */ |
||
169 | public function setNotifyPub($value) |
||
170 | { |
||
171 | $this->notifypub = $value; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @param $value |
||
176 | */ |
||
177 | public function setType($value) |
||
178 | { |
||
179 | $this->type = $value; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @param $value |
||
184 | */ |
||
185 | public function setApproved($value) |
||
186 | { |
||
187 | $this->approved = (int) $value; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @param $value |
||
192 | */ |
||
193 | public function setTopicdisplay($value) |
||
194 | { |
||
195 | $this->topicdisplay = $value; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param $value |
||
200 | */ |
||
201 | public function setTopicalign($value) |
||
202 | { |
||
203 | $this->topicalign = $value; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @param $value |
||
208 | */ |
||
209 | public function setComments($value) |
||
210 | { |
||
211 | $this->comments = (int) $value; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @param bool $approved |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | public function store($approved = false) |
||
|
|||
220 | { |
||
221 | //$newpost = 0; |
||
222 | $myts = \MyTextSanitizer::getInstance(); |
||
223 | $title = $myts->censorString($this->title); |
||
224 | $hometext = $myts->censorString($this->hometext); |
||
225 | $bodytext = $myts->censorString($this->bodytext); |
||
226 | $title = $myts->addSlashes($title); |
||
227 | $hometext = $myts->addSlashes($hometext); |
||
228 | $bodytext = $myts->addSlashes($bodytext); |
||
229 | if (!isset($this->nohtml) || $this->nohtml != 1) { |
||
230 | $this->nohtml = 0; |
||
231 | } |
||
232 | if (!isset($this->nosmiley) || $this->nosmiley != 1) { |
||
233 | $this->nosmiley = 0; |
||
234 | } |
||
235 | if (!isset($this->notifypub) || $this->notifypub != 1) { |
||
236 | $this->notifypub = 0; |
||
237 | } |
||
238 | if (!isset($this->topicdisplay) || $this->topicdisplay != 0) { |
||
239 | $this->topicdisplay = 1; |
||
240 | } |
||
241 | $expired = !empty($this->expired) ? $this->expired : 0; |
||
242 | if (!isset($this->storyid)) { |
||
243 | //$newpost = 1; |
||
244 | $newstoryid = $this->db->genId($this->table . '_storyid_seq'); |
||
245 | $created = time(); |
||
246 | $published = $this->approved ? $this->published : 0; |
||
247 | |||
248 | $sql = sprintf("INSERT INTO %s (storyid, uid, title, created, published, expired, hostname, nohtml, nosmiley, hometext, bodytext, counter, topicid, ihome, notifypub, story_type, topicdisplay, topicalign, comments) VALUES (%u, %u, '%s', %u, %u, %u, '%s', %u, %u, '%s', '%s', %u, %u, %u, %u, '%s', %u, '%s', %u)", $this->table, $newstoryid, $this->uid, $title, $created, $published, $expired, $this->hostname, $this->nohtml, $this->nosmiley, $hometext, $bodytext, 0, $this->topicid, $this->ihome, $this->notifypub, $this->type, $this->topicdisplay, $this->topicalign, $this->comments); |
||
249 | } else { |
||
250 | if ($this->approved) { |
||
251 | $sql = sprintf("UPDATE %s SET title = '%s', published = %u, expired = %u, nohtml = %u, nosmiley = %u, hometext = '%s', bodytext = '%s', topicid = %u, ihome = %u, topicdisplay = %u, topicalign = '%s', comments = %u WHERE storyid = %u", $this->table, $title, $this->published, $expired, $this->nohtml, $this->nosmiley, $hometext, $bodytext, $this->topicid, $this->ihome, $this->topicdisplay, $this->topicalign, $this->comments, $this->storyid); |
||
252 | } else { |
||
253 | $sql = sprintf("UPDATE %s SET title = '%s', expired = %u, nohtml = %u, nosmiley = %u, hometext = '%s', bodytext = '%s', topicid = %u, ihome = %u, topicdisplay = %u, topicalign = '%s', comments = %u WHERE storyid = %u", $this->table, $title, $expired, $this->nohtml, $this->nosmiley, $hometext, $bodytext, $this->topicid, $this->ihome, $this->topicdisplay, $this->topicalign, $this->comments, $this->storyid); |
||
254 | } |
||
255 | $newstoryid = $this->storyid; |
||
256 | } |
||
257 | if (!$result = $this->db->query($sql)) { |
||
258 | return false; |
||
259 | } |
||
260 | if (empty($newstoryid)) { |
||
261 | $newstoryid = $this->db->getInsertId(); |
||
262 | $this->storyid = $newstoryid; |
||
263 | } |
||
264 | |||
265 | return $newstoryid; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @param $storyid |
||
270 | */ |
||
271 | public function getStory($storyid) |
||
272 | { |
||
273 | $storyid = (int) $storyid; |
||
274 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE storyid=' . $storyid . ''; |
||
275 | $result = $this->db->query($sql); |
||
276 | if (!$this->db->isResultSet($result)) { |
||
277 | throw new \RuntimeException( |
||
278 | \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), |
||
279 | E_USER_ERROR, |
||
280 | ); |
||
281 | } |
||
282 | $array = $this->db->fetchArray($result); |
||
283 | $this->makeStory($array); |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * @param $array |
||
288 | */ |
||
289 | public function makeStory($array) |
||
290 | { |
||
291 | foreach ($array as $key => $value) { |
||
292 | $this->$key = $value; |
||
293 | } |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @return bool |
||
298 | */ |
||
299 | public function delete() |
||
300 | { |
||
301 | $sql = sprintf('DELETE FROM %s WHERE storyid = %u', $this->table, $this->storyid); |
||
302 | if (!$result = $this->db->query($sql)) { |
||
303 | return false; |
||
304 | } |
||
305 | |||
306 | return true; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * @return bool |
||
311 | */ |
||
312 | public function updateCounter() |
||
313 | { |
||
314 | $sql = sprintf('UPDATE %s SET counter = counter+1 WHERE storyid = %u', $this->table, $this->storyid); |
||
315 | if (!$result = $this->db->queryF($sql)) { |
||
316 | return false; |
||
317 | } |
||
318 | |||
319 | return true; |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * @param $total |
||
324 | * |
||
325 | * @return bool |
||
326 | */ |
||
327 | public function updateComments($total) |
||
335 | } |
||
336 | |||
337 | public function topicid() |
||
338 | { |
||
339 | return $this->topicid; |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * @return XoopsTopic |
||
344 | */ |
||
345 | public function topic() |
||
346 | { |
||
347 | return new XoopsTopic($this->topicstable, $this->topicid); |
||
348 | } |
||
349 | |||
350 | public function uid() |
||
351 | { |
||
352 | return $this->uid; |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * @return string |
||
357 | */ |
||
358 | public function uname() |
||
359 | { |
||
360 | return XoopsUser::getUnameFromId($this->uid); |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * @param string $format |
||
365 | * |
||
366 | * @return mixed |
||
367 | */ |
||
368 | public function title($format = 'Show') |
||
369 | { |
||
370 | $myts = \MyTextSanitizer::getInstance(); |
||
371 | $smiley = 1; |
||
372 | if ($this->nosmiley()) { |
||
373 | $smiley = 0; |
||
374 | } |
||
375 | switch ($format) { |
||
376 | case 'Show': |
||
377 | case 'Edit': |
||
378 | $title = $myts->htmlSpecialChars($this->title); |
||
379 | break; |
||
380 | case 'Preview': |
||
381 | case 'InForm': |
||
382 | $title = $myts->htmlSpecialChars($this->title); |
||
383 | break; |
||
384 | } |
||
385 | |||
386 | return $title; |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * @param string $format |
||
391 | * |
||
392 | * @return string |
||
393 | */ |
||
394 | public function hometext($format = 'Show') |
||
395 | { |
||
396 | $myts = \MyTextSanitizer::getInstance(); |
||
397 | $html = 1; |
||
398 | $smiley = 1; |
||
399 | $xcodes = 1; |
||
400 | if ($this->nohtml()) { |
||
401 | $html = 0; |
||
402 | } |
||
403 | if ($this->nosmiley()) { |
||
404 | $smiley = 0; |
||
405 | } |
||
406 | switch ($format) { |
||
407 | case 'Show': |
||
408 | $hometext = $myts->displayTarea($this->hometext, $html, $smiley, $xcodes); |
||
409 | break; |
||
410 | case 'Edit': |
||
411 | $hometext = htmlspecialchars($this->hometext, ENT_QUOTES | ENT_HTML5); |
||
412 | break; |
||
413 | case 'Preview': |
||
414 | $hometext = $myts->previewTarea($this->hometext, $html, $smiley, $xcodes); |
||
415 | break; |
||
416 | case 'InForm': |
||
417 | $hometext = htmlspecialchars($this->hometext, ENT_QUOTES | ENT_HTML5); |
||
418 | break; |
||
419 | } |
||
420 | |||
421 | return $hometext; |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * @param string $format |
||
426 | * |
||
427 | * @return string |
||
428 | */ |
||
429 | public function bodytext($format = 'Show') |
||
430 | { |
||
431 | $myts = \MyTextSanitizer::getInstance(); |
||
432 | $html = 1; |
||
433 | $smiley = 1; |
||
434 | $xcodes = 1; |
||
435 | if ($this->nohtml()) { |
||
436 | $html = 0; |
||
437 | } |
||
438 | if ($this->nosmiley()) { |
||
439 | $smiley = 0; |
||
440 | } |
||
441 | switch ($format) { |
||
442 | case 'Show': |
||
443 | $bodytext = $myts->displayTarea($this->bodytext, $html, $smiley, $xcodes); |
||
444 | break; |
||
445 | case 'Edit': |
||
446 | $bodytext = htmlspecialchars($this->bodytext, ENT_QUOTES | ENT_HTML5); |
||
447 | break; |
||
448 | case 'Preview': |
||
449 | $bodytext = $myts->previewTarea($this->bodytext, $html, $smiley, $xcodes); |
||
450 | break; |
||
451 | case 'InForm': |
||
452 | $bodytext = htmlspecialchars($this->bodytext, ENT_QUOTES | ENT_HTML5); |
||
453 | break; |
||
454 | } |
||
455 | |||
456 | return $bodytext; |
||
457 | } |
||
458 | |||
459 | public function counter() |
||
460 | { |
||
461 | return $this->counter; |
||
462 | } |
||
463 | |||
464 | public function created() |
||
465 | { |
||
466 | return $this->created; |
||
467 | } |
||
468 | |||
469 | public function published() |
||
470 | { |
||
471 | return $this->published; |
||
472 | } |
||
473 | |||
474 | public function expired() |
||
475 | { |
||
476 | return $this->expired; |
||
477 | } |
||
478 | |||
479 | public function hostname() |
||
480 | { |
||
481 | return $this->hostname; |
||
482 | } |
||
483 | |||
484 | public function storyid() |
||
487 | } |
||
488 | |||
489 | /** |
||
490 | * @return int |
||
491 | */ |
||
492 | public function nohtml() |
||
493 | { |
||
494 | return $this->nohtml; |
||
495 | } |
||
496 | |||
497 | /** |
||
498 | * @return int |
||
499 | */ |
||
500 | public function nosmiley() |
||
501 | { |
||
502 | return $this->nosmiley; |
||
503 | } |
||
504 | |||
505 | /** |
||
506 | * @return int |
||
507 | */ |
||
508 | public function notifypub() |
||
509 | { |
||
510 | return $this->notifypub; |
||
511 | } |
||
512 | |||
513 | public function type() |
||
514 | { |
||
515 | return $this->type; |
||
516 | } |
||
517 | |||
518 | /** |
||
519 | * @return int |
||
520 | */ |
||
521 | public function ihome() |
||
522 | { |
||
523 | return $this->ihome; |
||
524 | } |
||
525 | |||
526 | public function topicdisplay() |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * @param bool $astext |
||
533 | * |
||
534 | * @return string |
||
535 | */ |
||
536 | public function topicalign($astext = true) |
||
537 | { |
||
538 | $ret = 'left'; |
||
539 | if ($astext) { |
||
540 | if ($this->topicalign === 'R') { |
||
541 | $ret = 'right'; |
||
542 | } |
||
543 | |||
544 | return $ret; |
||
545 | } |
||
546 | |||
547 | return $this->topicalign; |
||
548 | } |
||
549 | |||
550 | public function comments() |
||
551 | { |
||
552 | return $this->comments; |
||
553 | } |
||
554 | } |
||
555 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.