Passed
Push — master ( d3e687...4990f6 )
by Michael
02:43
created

PostHandler   F

Complexity

Total Complexity 89

Size/Duplication

Total Lines 553
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 553
rs 1.5789
c 0
b 0
f 0
wmc 89

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B getPostCount() 0 22 5
B getByLimit() 0 31 2
C delete() 0 32 7
A get() 0 11 2
F myDelete() 0 96 23
A synchronization() 0 4 1
A cleanExpires() 0 17 4
A cleanOrphan() 0 13 2
A getPostForPrint() 0 3 1
C approve() 0 64 13
A getPostForPDF() 0 3 1
F insert() 0 118 20
C getPostsByLimit() 0 26 7

How to fix   Complexity   

Complex Class

Complex classes like PostHandler 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 PostHandler, and based on these observations, apply Extract Interface, too.

1
<?php namespace XoopsModules\Newbb;
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 42 and the first side effect is on line 37.

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.

Loading history...
2
3
//
4
//  ------------------------------------------------------------------------ //
5
//                XOOPS - PHP Content Management System                      //
6
//                  Copyright (c) 2000-2016 XOOPS.org                        //
7
//                       <https://xoops.org/>                             //
8
//  ------------------------------------------------------------------------ //
9
//  This program is free software; you can redistribute it and/or modify     //
10
//  it under the terms of the GNU General Public License as published by     //
11
//  the Free Software Foundation; either version 2 of the License, or        //
12
//  (at your option) any later version.                                      //
13
//                                                                           //
14
//  You may not change or alter any portion of this comment or credits       //
15
//  of supporting developers from this source code or any supporting         //
16
//  source code which is considered copyrighted (c) material of the          //
17
//  original comment or credit authors.                                      //
18
//                                                                           //
19
//  This program is distributed in the hope that it will be useful,          //
20
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
21
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
22
//  GNU General Public License for more details.                             //
23
//                                                                           //
24
//  You should have received a copy of the GNU General Public License        //
25
//  along with this program; if not, write to the Free Software              //
26
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
27
//  ------------------------------------------------------------------------ //
28
//  Author: phppp (D.J., [email protected])                                  //
29
//  URL: https://xoops.org                                                    //
30
//  Project: Article Project                                                 //
31
//  ------------------------------------------------------------------------ //
32
33
use XoopsModules\Newbb;
34
35
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
36
37
defined('NEWBB_FUNCTIONS_INI') || include $GLOBALS['xoops']->path('modules/newbb/include/functions.ini.php');
38
39
/**
40
 * Class PostHandler
41
 */
42
class PostHandler extends \XoopsPersistableObjectHandler
0 ignored issues
show
Bug introduced by
The type XoopsPersistableObjectHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
{
44
    /**
45
     * @param \XoopsDatabase $db
46
     */
47
    public function __construct(\XoopsDatabase $db)
0 ignored issues
show
Bug introduced by
The type XoopsDatabase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
48
    {
49
        parent::__construct($db, 'newbb_posts', Post::class, 'post_id', 'subject');
50
    }
51
52
    /**
53
     * @param  mixed $id
54
     * @param  null  $var
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $var is correct as it would always require null to be passed?
Loading history...
55
     * @return null|\XoopsObject
0 ignored issues
show
Bug introduced by
The type XoopsObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
56
     */
57
    public function get($id = null, $var = null) //get($id)
58
    {
59
        $id   = (int)$id;
60
        $post = null;
61
        $sql  = 'SELECT p.*, t.* FROM ' . $this->db->prefix('newbb_posts') . ' p LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' t ON p.post_id=t.post_id WHERE p.post_id=' . $id;
62
        if ($array = $this->db->fetchArray($this->db->query($sql))) {
63
            $post = $this->create(false);
64
            $post->assignVars($array);
65
        }
66
67
        return $post;
68
    }
69
70
    /**
71
     * @param  int             $limit
72
     * @param  int             $start
73
     * @param  \CriteriaElement $criteria
74
     * @param  null            $fields
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fields is correct as it would always require null to be passed?
Loading history...
75
     * @param  bool            $asObject
76
     * @param  int             $topic_id
77
     * @param  int             $approved
78
     * @return array
79
     */
80
    //    public function getByLimit($topic_id, $limit, $approved = 1)
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
81
    public function &getByLimit(
82
        $limit = 0,
83
        $start = 0,
84
        \CriteriaElement $criteria = null,
0 ignored issues
show
Bug introduced by
The type CriteriaElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
85
        $fields = null,
86
        $asObject = true,
87
        $topic_id = 0,
88
        $approved = 1
89
    ) {
90
        $sql    = 'SELECT p.*, t.*, tp.topic_status FROM '
91
                  . $this->db->prefix('newbb_posts')
92
                  . ' p LEFT JOIN '
93
                  . $this->db->prefix('newbb_posts_text')
94
                  . ' t ON p.post_id=t.post_id LEFT JOIN '
95
                  . $this->db->prefix('newbb_topics')
96
                  . ' tp ON tp.topic_id=p.topic_id WHERE p.topic_id='
97
                  . $topic_id
98
                  . ' AND p.approved ='
99
                  . $approved
100
                  . ' ORDER BY p.post_time DESC';
101
        $result = $this->db->query($sql, $limit, 0);
102
        $ret    = [];
103
       while (false !== ($myrow = $this->db->fetchArray($result))) {
104
            $post = $this->create(false);
105
            $post->assignVars($myrow);
106
107
            $ret[$myrow['post_id']] = $post;
108
            unset($post);
109
        }
110
111
        return $ret;
112
    }
113
114
    /**
115
     * @param Post $post
116
     * @return mixed
117
     */
118
    public function getPostForPDF(&$post)
119
    {
120
        return $post->getPostBody(true);
0 ignored issues
show
Unused Code introduced by
The call to XoopsModules\Newbb\Post::getPostBody() has too many arguments starting with true. ( Ignorable by Annotation )

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

120
        return $post->/** @scrutinizer ignore-call */ getPostBody(true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
121
    }
122
123
    /**
124
     * @param Post $post
125
     * @return mixed
126
     */
127
    public function getPostForPrint(&$post)
128
    {
129
        return $post->getPostBody();
130
    }
131
132
    /**
133
     * @param  Post $post
134
     * @param  bool $force
135
     * @return bool
136
     */
137
    public function approve(&$post, $force = false)
138
    {
139
        if (empty($post)) {
140
            return false;
141
        }
142
        if (is_numeric($post)) {
0 ignored issues
show
introduced by
The condition is_numeric($post) is always false.
Loading history...
143
            $post = $this->get($post);
144
        }
145
        $post_id = $post->getVar('post_id');
0 ignored issues
show
Unused Code introduced by
The assignment to $post_id is dead and can be removed.
Loading history...
146
147
        $wasApproved = $post->getVar('approved');
148
        // irmtfan approve post if the approved = 0 (pending) or -1 (deleted)
149
        if (empty($force) && $wasApproved > 0) {
150
            return true;
151
        }
152
        $post->setVar('approved', 1);
153
        $this->insert($post, true);
154
155
        /** @var Newbb\TopicHandler $topicHandler */
156
        $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic');
157
        $topicObject  = $topicHandler->get($post->getVar('topic_id'));
158
        if ($topicObject->getVar('topic_last_post_id') < $post->getVar('post_id')) {
159
            $topicObject->setVar('topic_last_post_id', $post->getVar('post_id'));
160
        }
161
        if ($post->isTopic()) {
162
            $topicObject->setVar('approved', 1);
163
        } else {
164
            $topicObject->setVar('topic_replies', $topicObject->getVar('topic_replies') + 1);
165
        }
166
        $topicHandler->insert($topicObject, true);
167
168
        /** @var Newbb\ForumHandler $forumHandler */
169
        $forumHandler = Newbb\Helper::getInstance()->getHandler('Forum');
170
        $forumObject  = $forumHandler->get($post->getVar('forum_id'));
171
        if ($forumObject->getVar('forum_last_post_id') < $post->getVar('post_id')) {
172
            $forumObject->setVar('forum_last_post_id', $post->getVar('post_id'));
173
        }
174
        $forumObject->setVar('forum_posts', $forumObject->getVar('forum_posts') + 1);
175
        if ($post->isTopic()) {
176
            $forumObject->setVar('forum_topics', $forumObject->getVar('forum_topics') + 1);
177
        }
178
        $forumHandler->insert($forumObject, true);
179
180
        // Update user stats
181
        if ($post->getVar('uid') > 0) {
182
            /** @var \XoopsMemberHandler $memberHandler */
183
            $memberHandler = xoops_getHandler('member');
0 ignored issues
show
Bug introduced by
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

183
            $memberHandler = /** @scrutinizer ignore-call */ xoops_getHandler('member');
Loading history...
184
            $poster        = $memberHandler->getUser($post->getVar('uid'));
185
            if (is_object($poster) && $post->getVar('uid') == $poster->getVar('uid')) {
186
                $poster->setVar('posts', $poster->getVar('posts') + 1);
187
                $res = $memberHandler->insertUser($poster, true);
0 ignored issues
show
Unused Code introduced by
The assignment to $res is dead and can be removed.
Loading history...
188
                unset($poster);
189
            }
190
        }
191
192
        // Update forum stats
193
        /** @var StatsHandler $statsHandler */
194
        $statsHandler = Newbb\Helper::getInstance()->getHandler('Stats');
195
        $statsHandler->update($post->getVar('forum_id'), 'post');
196
        if ($post->isTopic()) {
197
            $statsHandler->update($post->getVar('forum_id'), 'topic');
198
        }
199
200
        return true;
201
    }
202
203
    /**
204
     * @param \XoopsObject $post
205
     * @param  bool        $force
206
     * @return bool
207
     */
208
    public function insert(\XoopsObject $post, $force = true) //insert(&$post, $force = true)
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
209
    {
210
        $topicObject = null;
211
        // Set the post time
212
        // The time should be "publish" time. To be adjusted later
213
        if (!$post->getVar('post_time')) {
214
            $post->setVar('post_time', time());
215
        }
216
217
        /** @var Newbb\TopicHandler $topicHandler */
218
        $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic');
219
        // Verify the topic ID
220
        if ($topic_id = $post->getVar('topic_id')) {
221
            $topicObject = $topicHandler->get($topic_id);
222
            // Invalid topic OR the topic is no approved and the post is not top post
223
            if (!$topicObject//    || (!$post->isTopic() && $topicObject->getVar("approved") < 1)
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
224
            ) {
225
                return false;
226
            }
227
        }
228
        if (empty($topic_id)) {
229
            $post->setVar('topic_id', 0);
230
            $post->setVar('pid', 0);
231
            $post->setNew();
232
            $topicObject = $topicHandler->create();
233
        }
234
        $textHandler    = Newbb\Helper::getInstance()->getHandler('Text');
235
        $post_text_vars = ['post_text', 'post_edit', 'dohtml', 'doxcode', 'dosmiley', 'doimage', 'dobr'];
236
        if ($post->isNew()) {
237
            if (!$topic_id = $post->getVar('topic_id')) {
238
                $topicObject->setVar('topic_title', $post->getVar('subject', 'n'));
239
                $topicObject->setVar('topic_poster', $post->getVar('uid'));
240
                $topicObject->setVar('forum_id', $post->getVar('forum_id'));
241
                $topicObject->setVar('topic_time', $post->getVar('post_time'));
242
                $topicObject->setVar('poster_name', $post->getVar('poster_name'));
243
                $topicObject->setVar('approved', $post->getVar('approved'));
244
245
                if (!$topic_id = $topicHandler->insert($topicObject, $force)) {
246
                    $post->deleteAttachment();
247
                    $post->setErrors('insert topic error');
248
249
                    //xoops_error($topicObject->getErrors());
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
250
                    return false;
251
                }
252
                $post->setVar('topic_id', $topic_id);
253
254
                $pid = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $pid is dead and can be removed.
Loading history...
255
                $post->setVar('pid', 0);
256
            } elseif (!$post->getVar('pid')) {
257
                $pid = $topicHandler->getTopPostId($topic_id);
258
                $post->setVar('pid', $pid);
259
            }
260
261
            $textObject = $textHandler->create();
262
            foreach ($post_text_vars as $key) {
263
                $textObject->vars[$key] = $post->vars[$key];
264
            }
265
            $post->destroyVars($post_text_vars);
266
267
            //            if (!$post_id = parent::insert($post, $force)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
268
            //                return false;
269
            //            }
270
271
            if (!$post_id = parent::insert($post, $force)) {
272
                return false;
273
            } else {
274
                $post->unsetNew();
275
            }
276
277
            $textObject->setVar('post_id', $post_id);
278
            if (!$textHandler->insert($textObject, $force)) {
279
                $this->delete($post);
280
                $post->setErrors('post text insert error');
281
282
                //xoops_error($textObject->getErrors());
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
283
                return false;
284
            }
285
            if ($post->getVar('approved') > 0) {
286
                $this->approve($post, true);
287
            }
288
            $post->setVar('post_id', $post_id);
289
        } else {
290
            if ($post->isTopic()) {
291
                if ($post->getVar('subject') !== $topicObject->getVar('topic_title')) {
292
                    $topicObject->setVar('topic_title', $post->getVar('subject', 'n'));
293
                }
294
                if ($post->getVar('approved') !== $topicObject->getVar('approved')) {
295
                    $topicObject->setVar('approved', $post->getVar('approved'));
296
                }
297
                $topicObject->setDirty();
298
                if (!$result = $topicHandler->insert($topicObject, $force)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
299
                    $post->setErrors('update topic error');
300
301
                    //xoops_error($topicObject->getErrors());
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
302
                    return false;
303
                }
304
            }
305
            $textObject = $textHandler->get($post->getVar('post_id'));
306
            $textObject->setDirty();
307
            foreach ($post_text_vars as $key) {
308
                $textObject->vars[$key] = $post->vars[$key];
309
            }
310
            $post->destroyVars($post_text_vars);
311
            if (!$post_id = parent::insert($post, $force)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $post_id is dead and can be removed.
Loading history...
312
                //xoops_error($post->getErrors());
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
313
                return false;
314
            } else {
315
                $post->unsetNew();
316
            }
317
            if (!$textHandler->insert($textObject, $force)) {
318
                $post->setErrors('update post text error');
319
320
                //xoops_error($textObject->getErrors());
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
321
                return false;
322
            }
323
        }
324
325
        return $post->getVar('post_id');
326
    }
327
328
    /**
329
     * @param \XoopsObject $post
330
     * @param  bool        $isDeleteOne
331
     * @param  bool        $force
332
     * @return bool
333
     */
334
    public function delete(\XoopsObject $post, $isDeleteOne = true, $force = false)
335
    {
336
        if (!is_object($post) || 0 == $post->getVar('post_id')) {
337
            return false;
338
        }
339
340
        if ($isDeleteOne) {
341
            if ($post->isTopic()) {
342
                $criteria = new \CriteriaCompo(new \Criteria('topic_id', $post->getVar('topic_id')));
0 ignored issues
show
Bug introduced by
The type CriteriaCompo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type Criteria was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
343
                $criteria->add(new \Criteria('approved', 1));
344
                $criteria->add(new \Criteria('pid', 0, '>'));
345
                if ($this->getPostCount($criteria) > 0) {
346
                    return false;
347
                }
348
            }
349
350
            return $this->myDelete($post, $force);
351
        } else {
352
            require_once $GLOBALS['xoops']->path('class/xoopstree.php');
353
            $mytree = new \XoopsTree($this->db->prefix('newbb_posts'), 'post_id', 'pid');
0 ignored issues
show
Bug introduced by
The type XoopsTree was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
354
            $arr    = $mytree->getAllChild($post->getVar('post_id'));
355
            // irmtfan - delete childs in a reverse order
356
            for ($i = count($arr) - 1; $i >= 0; $i--) {
357
                $childpost = $this->create(false);
358
                $childpost->assignVars($arr[$i]);
359
                $this->myDelete($childpost, $force);
360
                unset($childpost);
361
            }
362
            $this->myDelete($post, $force);
363
        }
364
365
        return true;
366
    }
367
368
    /**
369
     * @param  Post $post
370
     * @param  bool $force
371
     * @return bool
372
     */
373
    public function myDelete(Post $post, $force = false)
374
    {
375
        global $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
376
377
        if (!is_object($post) || 0 == $post->getVar('post_id')) {
378
            return false;
379
        }
380
381
        /* Set active post as deleted */
382
        if ($post->getVar('approved') > 0 && empty($force)) {
383
            $sql = 'UPDATE ' . $this->db->prefix('newbb_posts') . ' SET approved = -1 WHERE post_id = ' . $post->getVar('post_id');
384
            if (!$result = $this->db->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
385
            }
386
            /* delete pending post directly */
387
        } else {
388
            $sql = sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('newbb_posts'), $post->getVar('post_id'));
389
            if (!$result = $this->db->queryF($sql)) {
390
                $post->setErrors('delete post error: ' . $sql);
391
392
                return false;
393
            }
394
            $post->deleteAttachment();
395
396
            $sql = sprintf('DELETE FROM %s WHERE post_id = %u', $this->db->prefix('newbb_posts_text'), $post->getVar('post_id'));
397
            if (!$result = $this->db->queryF($sql)) {
398
                $post->setErrors('Could not remove post text: ' . $sql);
399
400
                return false;
401
            }
402
        }
403
404
        if ($post->isTopic()) {
405
            $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic');
406
            /** @var Topic $topicObject */
407
            $topicObject = $topicHandler->get($post->getVar('topic_id'));
408
            if (is_object($topicObject) && $topicObject->getVar('approved') > 0 && empty($force)) {
409
                $topiccount_toupdate = 1;
0 ignored issues
show
Unused Code introduced by
The assignment to $topiccount_toupdate is dead and can be removed.
Loading history...
410
                $topicObject->setVar('approved', -1);
411
                $topicHandler->insert($topicObject);
412
                xoops_notification_deletebyitem($xoopsModule->getVar('mid'), 'thread', $post->getVar('topic_id'));
0 ignored issues
show
Bug introduced by
The function xoops_notification_deletebyitem was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

412
                /** @scrutinizer ignore-call */ 
413
                xoops_notification_deletebyitem($xoopsModule->getVar('mid'), 'thread', $post->getVar('topic_id'));
Loading history...
413
            } else {
414
                if (is_object($topicObject)) {
415
                    if ($topicObject->getVar('approved') > 0) {
416
                        xoops_notification_deletebyitem($xoopsModule->getVar('mid'), 'thread', $post->getVar('topic_id'));
417
                    }
418
419
                    $poll_id = $topicObject->getVar('poll_id');
420
                    // START irmtfan poll_module
421
                    $topicObject->deletePoll($poll_id);
422
                    // END irmtfan poll_module
423
                }
424
425
                $sql = sprintf('DELETE FROM %s WHERE topic_id = %u', $this->db->prefix('newbb_topics'), $post->getVar('topic_id'));
426
                if (!$result = $this->db->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
427
                    //xoops_error($this->db->error());
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
428
                }
429
                $sql = sprintf('DELETE FROM %s WHERE topic_id = %u', $this->db->prefix('newbb_votedata'), $post->getVar('topic_id'));
430
                if (!$result = $this->db->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
431
                    //xoops_error($this->db->error());
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
432
                }
433
            }
434
        } else {
435
            $sql = 'UPDATE ' . $this->db->prefix('newbb_topics') . ' t
436
                            LEFT JOIN ' . $this->db->prefix('newbb_posts') . ' p ON p.topic_id = t.topic_id
437
                            SET t.topic_last_post_id = p.post_id
438
                            WHERE t.topic_last_post_id = ' . $post->getVar('post_id') . '
439
                                    AND p.post_id = (SELECT MAX(post_id) FROM ' . $this->db->prefix('newbb_posts') . ' WHERE topic_id=t.topic_id)';
440
            if (!$result = $this->db->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
441
            }
442
        }
443
444
        $postcount_toupdate = $post->getVar('approved');
445
446
        if ($postcount_toupdate > 0) {
447
448
            // Update user stats
449
            if ($post->getVar('uid') > 0) {
450
                /** @var \XoopsMemberHandler $memberHandler */
451
                $memberHandler = xoops_getHandler('member');
0 ignored issues
show
Bug introduced by
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

451
                $memberHandler = /** @scrutinizer ignore-call */ xoops_getHandler('member');
Loading history...
452
                $poster        = $memberHandler->getUser($post->getVar('uid'));
453
                if (is_object($poster) && $post->getVar('uid') == $poster->getVar('uid')) {
454
                    $poster->setVar('posts', $poster->getVar('posts') - 1);
455
                    $res = $memberHandler->insertUser($poster, true);
0 ignored issues
show
Unused Code introduced by
The assignment to $res is dead and can be removed.
Loading history...
456
                    unset($poster);
457
                }
458
            }
459
            // irmtfan - just update the pid for approved posts when the post is not topic (pid=0)
460
            if (!$post->isTopic()) {
461
                $sql = 'UPDATE ' . $this->db->prefix('newbb_posts') . ' SET pid = ' . $post->getVar('pid') . ' WHERE approved=1 AND pid=' . $post->getVar('post_id');
462
                if (!$result = $this->db->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
463
                    //xoops_error($this->db->error());
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
464
                }
465
            }
466
        }
467
468
        return true;
469
    }
470
471
    // START irmtfan enhance getPostCount when there is join (read_mode = 2)
472
473
    /**
474
     * @param  null $criteria
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $join is correct as it would always require null to be passed?
Loading history...
Documentation Bug introduced by
Are you sure the doc-type for parameter $criteria is correct as it would always require null to be passed?
Loading history...
475
     * @param  null $join
476
     * @return int|null
477
     */
478
    public function getPostCount($criteria = null, $join = null)
479
    {
480
        // if not join get the count from XOOPS/class/model/stats as before
481
        if (empty($join)) {
482
            return parent::getCount($criteria);
483
        }
484
485
        $sql = 'SELECT COUNT(*) as count' . ' FROM ' . $this->db->prefix('newbb_posts') . ' AS p' . ' LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' AS t ON t.post_id = p.post_id';
486
        // LEFT JOIN
487
        $sql .= $join;
488
        // WHERE
489
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
490
            $sql .= ' ' . $criteria->renderWhere();
491
        }
492
        if (!$result = $this->db->query($sql)) {
493
            //xoops_error($this->db->error().'<br>'.$sql);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
494
            return null;
495
        }
496
        $myrow = $this->db->fetchArray($result);
497
        $count = $myrow['count'];
498
499
        return $count;
500
    }
501
    // END irmtfan enhance getPostCount when there is join (read_mode = 2)
502
    /*
503
     * TODO: combining viewtopic.php
504
     */
505
    /**
506
     * @param  null $criteria
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $criteria is correct as it would always require null to be passed?
Loading history...
Documentation Bug introduced by
Are you sure the doc-type for parameter $join is correct as it would always require null to be passed?
Loading history...
507
     * @param  int  $limit
508
     * @param  int  $start
509
     * @param  null $join
510
     * @return array
511
     */
512
    public function getPostsByLimit($criteria = null, $limit = 1, $start = 0, $join = null)
513
    {
514
        $ret = [];
515
        $sql = 'SELECT p.*, t.* ' . ' FROM ' . $this->db->prefix('newbb_posts') . ' AS p' . ' LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' AS t ON t.post_id = p.post_id';
516
        if (!empty($join)) {
517
            $sql .= $join;
518
        }
519
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
520
            $sql .= ' ' . $criteria->renderWhere();
521
            if ('' !== $criteria->getSort()) {
522
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
523
            }
524
        }
525
        $result = $this->db->query($sql, (int)$limit, (int)$start);
526
        if (!$result) {
527
            //xoops_error($this->db->error());
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
528
            return $ret;
529
        }
530
       while (false !== ($myrow = $this->db->fetchArray($result))) {
531
            $post = $this->create(false);
532
            $post->assignVars($myrow);
533
            $ret[$myrow['post_id']] = $post;
534
            unset($post);
535
        }
536
537
        return $ret;
538
    }
539
540
    /**
541
     * @return bool
542
     */
543
    public function synchronization()
544
    {
545
        //$this->cleanOrphan();
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
546
        return true;
547
    }
548
549
    /**
550
     * clean orphan items from database
551
     *
552
     * @param  string $table_link
553
     * @param  string $field_link
554
     * @param  string $field_object
555
     * @return bool   true on success
556
     */
557
    public function cleanOrphan($table_link = '', $field_link = '', $field_object = '') //cleanOrphan()
558
    {
559
        $this->deleteAll(new \Criteria('post_time', 0), true, true);
560
        parent::cleanOrphan($this->db->prefix('newbb_topics'), 'topic_id');
561
        parent::cleanOrphan($this->db->prefix('newbb_posts_text'), 'post_id');
562
563
        $sql = 'DELETE FROM ' . $this->db->prefix('newbb_posts_text') . ' WHERE (post_id NOT IN ( SELECT DISTINCT post_id FROM ' . $this->table . ') )';
564
        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...
565
            //xoops_error($this->db->error());
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
566
            return false;
567
        }
568
569
        return true;
570
    }
571
572
    /**
573
     * clean expired objects from database
574
     *
575
     * @param  int $expire time limit for expiration
576
     * @return bool true on success
577
     */
578
    public function cleanExpires($expire = 0)
579
    {
580
        // irmtfan if 0 no cleanup look include/plugin.php
581
        if (!func_num_args()) {
582
            $newbbConfig = newbbLoadConfig();
583
            $expire      = isset($newbbConfig['pending_expire']) ? (int)$newbbConfig['pending_expire'] : 7;
584
            $expire      = $expire * 24 * 3600; // days to seconds
585
        }
586
        if (empty($expire)) {
587
            return false;
588
        }
589
        $crit_expire = new \CriteriaCompo(new \Criteria('approved', 0, '<='));
590
        //if (!empty($expire)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
591
        $crit_expire->add(new \Criteria('post_time', time() - (int)$expire, '<'));
592
593
        //}
594
        return $this->deleteAll($crit_expire, true/*, true*/);
595
    }
596
}
597