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