Passed
Push — master ( 6d0bec...700d12 )
by Michael
05:09 queued 02:42
created

XoopsStory   F

Complexity

Total Complexity 85

Size/Duplication

Total Lines 572
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 85
eloc 231
c 0
b 0
f 0
dl 0
loc 572
rs 2

46 Methods

Rating   Name   Duplication   Size   Complexity  
A uname() 0 3 1
A counter() 0 3 1
A Story() 0 10 3
A topic() 0 3 1
A setComments() 0 3 1
B bodytext() 0 28 7
A ihome() 0 3 1
A setIhome() 0 3 1
A setStoryId() 0 3 1
A hostname() 0 3 1
A updateCounter() 0 8 2
A topicid() 0 3 1
A delete() 0 8 2
A notifypub() 0 3 1
A updateComments() 0 8 2
A topicdisplay() 0 3 1
A makeStory() 0 4 2
B hometext() 0 28 7
A title() 0 19 6
A setTitle() 0 3 1
A setType() 0 3 1
A expired() 0 3 1
A setTopicdisplay() 0 3 1
A published() 0 3 1
A setApproved() 0 3 1
F store() 0 100 15
A setTopicId() 0 3 1
A setBodytext() 0 3 1
A topicalign() 0 13 3
A nohtml() 0 3 1
A setPublished() 0 3 1
A setNotifyPub() 0 3 1
A setNohtml() 0 3 1
A uid() 0 3 1
A setHostname() 0 3 1
A nosmiley() 0 3 1
A setExpired() 0 3 1
A getStory() 0 6 1
A storyid() 0 3 1
A setNosmiley() 0 3 1
A created() 0 3 1
A comments() 0 3 1
A setTopicalign() 0 3 1
A setHometext() 0 3 1
A type() 0 3 1
A setUid() 0 3 1

How to fix   Complexity   

Complex Class

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
2
3
namespace XoopsModules\News;
4
5
/**
6
 * XOOPS news story
7
 *
8
 * You may not change or alter any portion of this comment or credits
9
 * of supporting developers from this source code or any supporting source code
10
 * which is considered copyrighted (c) material of the original comment or credit authors.
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 *
15
 * @copyright       XOOPS Project (https://xoops.org)
16
 * @license         GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 * @package         kernel
18
 * @since           2.0.0
19
 * @author          Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
20
 * @deprecated
21
 */
22
23
//$GLOBALS['xoopsLogger']->addDeprecated("'/class/xoopsstory.php' is deprecated since XOOPS 2.5.4, please create your own class instead.");
24
// require_once XOOPS_ROOT_PATH . '/modules/news/class/xoopstopic.php';
25
require_once XOOPS_ROOT_PATH . '/kernel/user.php';
26
27
/**
28
 * Class XoopsStory
29
 */
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();
0 ignored issues
show
Bug introduced by
The property db does not seem to exist on XoopsMySQLDatabase.
Loading history...
63
        $this->table       = '';
0 ignored issues
show
Bug introduced by
The property table does not seem to exist on XoopsMySQLDatabase.
Loading history...
64
        $this->topicstable = '';
0 ignored issues
show
Bug introduced by
The property topicstable does not seem to exist on XoopsMySQLDatabase.
Loading history...
65
        if (\is_array($storyid)) {
66
            $this->makeStory($storyid);
0 ignored issues
show
Bug introduced by
The method makeStory() does not exist on XoopsMySQLDatabase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
            $this->/** @scrutinizer ignore-call */ 
67
                   makeStory($storyid);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
        } elseif (-1 != $storyid) {
68
            $this->getStory((int)$storyid);
0 ignored issues
show
Bug introduced by
The method getStory() does not exist on XoopsMySQLDatabase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
            $this->/** @scrutinizer ignore-call */ 
69
                   getStory((int)$storyid);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $approved is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

221
    public function store(/** @scrutinizer ignore-unused */ $approved = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
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)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
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)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
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)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
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()
407
    {
408
        return \XoopsUser::getUnameFromId($this->uid);
409
    }
410
411
    /**
412
     * @param string $format
413
     *
414
     * @return mixed
415
     */
416
    public function title($format = 'Show')
417
    {
418
        $myts   = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
419
        $smiley = 1;
0 ignored issues
show
Unused Code introduced by
The assignment to $smiley is dead and can be removed.
Loading history...
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;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $title does not seem to be defined for all execution paths leading up to this point.
Loading history...
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;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $hometext does not seem to be defined for all execution paths leading up to this point.
Loading history...
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;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $bodytext does not seem to be defined for all execution paths leading up to this point.
Loading history...
505
    }
506
507
    public function counter()
508
    {
509
        return $this->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()
570
    {
571
        return $this->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;
594
        }
595
596
        return $this->topicalign;
597
    }
598
599
    public function comments()
600
    {
601
        return $this->comments;
602
    }
603
}
604