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

Topic::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 21
nc 1
nop 0
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
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 23 and the first side effect is on line 18.

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
 * NewBB 5.0x,  the forum module for XOOPS project
5
 *
6
 * @copyright      XOOPS Project (https://xoops.org)
7
 * @license        GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
8
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
9
 * @since          4.00
10
 * @package        module::newbb
11
 */
12
13
use XoopsModules\Newbb;
14
use XoopsModules\Xoopspoll;
0 ignored issues
show
Bug introduced by
The type XoopsModules\Xoopspoll 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...
15
16
// 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...
17
18
defined('NEWBB_FUNCTIONS_INI') || include $GLOBALS['xoops']->path('modules/newbb/include/functions.ini.php');
19
20
/**
21
 * Class Topic
22
 */
23
class Topic extends \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...
24
{
25
    /**
26
     *
27
     */
28
    public function __construct()
29
    {
30
        parent::__construct();
31
        $this->initVar('topic_id', XOBJ_DTYPE_INT);
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Newbb\XOBJ_DTYPE_INT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
32
        $this->initVar('topic_title', XOBJ_DTYPE_TXTBOX);
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Newbb\XOBJ_DTYPE_TXTBOX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
33
        $this->initVar('topic_poster', XOBJ_DTYPE_INT);
34
        $this->initVar('topic_time', XOBJ_DTYPE_INT);
35
        $this->initVar('topic_views', XOBJ_DTYPE_INT);
36
        $this->initVar('topic_replies', XOBJ_DTYPE_INT);
37
        $this->initVar('topic_last_post_id', XOBJ_DTYPE_INT);
38
        $this->initVar('forum_id', XOBJ_DTYPE_INT);
39
        $this->initVar('topic_status', XOBJ_DTYPE_INT);
40
        $this->initVar('type_id', XOBJ_DTYPE_INT);
41
        $this->initVar('topic_sticky', XOBJ_DTYPE_INT);
42
        $this->initVar('topic_digest', XOBJ_DTYPE_INT);
43
        $this->initVar('digest_time', XOBJ_DTYPE_INT);
44
        $this->initVar('approved', XOBJ_DTYPE_INT);
45
        $this->initVar('poster_name', XOBJ_DTYPE_TXTBOX);
46
        $this->initVar('rating', XOBJ_DTYPE_OTHER);
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Newbb\XOBJ_DTYPE_OTHER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
47
        $this->initVar('votes', XOBJ_DTYPE_INT);
48
        $this->initVar('topic_haspoll', XOBJ_DTYPE_INT);
49
        $this->initVar('poll_id', XOBJ_DTYPE_INT);
50
        $this->initVar('topic_tags', XOBJ_DTYPE_SOURCE);
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Newbb\XOBJ_DTYPE_SOURCE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
51
    }
52
53
    // irmtfan add LAST_INSERT_ID to enhance the mysql performances
54
    public function incrementCounter()
55
    {
56
        $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('newbb_topics') . ' SET topic_views = LAST_INSERT_ID(topic_views + 1) WHERE topic_id =' . $this->getVar('topic_id');
57
        $GLOBALS['xoopsDB']->queryF($sql);
58
    }
59
60
    /**
61
     * Create full title of the topic
62
     *
63
     * the title is composed of [type_name] if type_id is greater than 0 plus topic_title
64
     *
65
     */
66
    public function getFullTitle()
67
    {
68
        $topic_title = $this->getVar('topic_title');
69
        if (!$this->getVar('type_id')) {
70
            return $topic_title;
71
        }
72
        $typeHandler = Newbb\Helper::getInstance()->getHandler('Type');
73
        if (!$typeObject = $typeHandler->get($this->getVar('type_id'))) {
74
            return $topic_title;
75
        }
76
77
        include_once __DIR__ . '/../include/functions.topic.php';
78
79
        return getTopicTitle($topic_title, $typeObject->getVar('type_name'), $typeObject->getVar('type_color'));
80
    }
81
    // START irmtfan loadOldPoll function
82
83
    /**
84
     * Load functions needed for old xoopspoll (older than version 1.4 by zyspec) and umfrage modules
85
     *
86
     * @access public
87
     * @param  string $pollModule dirname of the poll module
88
     * @return string|false $classPoll = the name of the old poll class eg: "XoopsPoll" | "Umfrage"
89
     */
90
91
    public function loadOldPoll($pollModule = null)
92
    {
93
        static $classPoll = false;
94
        if ($classPoll && empty($pollModule)) {
95
            return $classPoll;
96
        }
97
        $newbbConfig = newbbLoadConfig();
98
        if (!empty($pollModule)) {
99
            $newbbConfig['poll_module'] = $pollModule;
100
        }
101
//        $relPath = $GLOBALS['xoops']->path('modules/' . $newbbConfig['poll_module'] . '/class/' . $newbbConfig['poll_module']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% 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...
102
//        include_once $relPath . '.php';
103
//        include_once $relPath . 'option.php';
104
//        include_once $relPath . 'log.php';
105
//        include_once $relPath . 'renderer.php';
106
        $classes = get_declared_classes();
107
        foreach (array_reverse($classes) as $class) {
108
            if (strtolower($class) == $newbbConfig['poll_module']) {
109
                $classPoll = $class;
110
111
                return $classPoll;
112
            }
113
        }
114
115
        return false;
116
    }
117
    // END irmtfan loadOldPoll function
118
    // START irmtfan add deletePoll function
119
    /**
120
     * delete a poll in database
121
     *
122
     * @access public
123
     * @param  int $poll_id
124
     * @return bool
125
     */
126
    public function deletePoll($poll_id)
127
    {
128
        if (empty($poll_id)) {
129
            return false;
130
        }
131
        /** @var \XoopsModuleHandler $moduleHandler */
132
        $moduleHandler     = xoops_getHandler('module');
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

132
        $moduleHandler     = /** @scrutinizer ignore-call */ xoops_getHandler('module');
Loading history...
133
        $newbbConfig       = newbbLoadConfig();
134
        $pollModuleHandler = $moduleHandler->getByDirname($newbbConfig['poll_module']);
135
        if (!is_object($pollModuleHandler) || !$pollModuleHandler->getVar('isactive')) {
136
            return false;
137
        }
138
        // new xoopspoll module
139
        if ($pollModuleHandler->getVar('version') >= 140) {
140
            /** @var \XoopsPollHandler $pollHandler */
141
            $pollHandler = Xoopspoll\Helper::getInstance()->getHandler('Poll');
0 ignored issues
show
Bug introduced by
The type XoopsModules\Xoopspoll\Helper 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...
142
            if (false !== $pollHandler->deleteAll(new \Criteria('poll_id', $poll_id, '='))) {
0 ignored issues
show
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...
143
                /** @var XoopsPoll\OptionHandler $optionHandler */
144
                $optionHandler = Xoopspoll\Helper::getInstance()->getHandler('Option');
145
                $optionHandler->deleteAll(new \Criteria('poll_id', $poll_id, '='));
146
                /** @var XoopsPoll\LogHandler $logHandler */
147
                $logHandler = Xoopspoll\Helper::getInstance()->getHandler('Log');
148
                $logHandler->deleteAll(new \Criteria('poll_id', $poll_id, '='));
149
                xoops_comment_delete($GLOBALS['xoopsModule']->getVar('mid'), $poll_id);
0 ignored issues
show
Bug introduced by
The function xoops_comment_delete 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

149
                /** @scrutinizer ignore-call */ 
150
                xoops_comment_delete($GLOBALS['xoopsModule']->getVar('mid'), $poll_id);
Loading history...
150
            }
151
            // old Xoopspoll or Umfrage or any clone from them
152
        } else {
153
            $classPoll = $this->loadOldPoll();
154
            /** @var \XoopsPoll $poll */
155
            $poll = new $classPoll($poll_id);
156
            if (false !== $poll->delete()) {
157
                $classOption = $classPoll . 'Option';
0 ignored issues
show
Bug introduced by
Are you sure $classPoll of type false can be used in concatenation? ( Ignorable by Annotation )

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

157
                $classOption = /** @scrutinizer ignore-type */ $classPoll . 'Option';
Loading history...
158
                $classOption::deleteByPollId($poll->getVar('poll_id'));
159
                $classLog = $classPoll . 'Log';
160
                $classLog::deleteByPollId($poll->getVar('poll_id'));
161
                xoops_comment_delete($GLOBALS['xoopsModule']->getVar('mid'), $poll->getVar('poll_id'));
162
            }
163
        } // end poll_module new or old
164
165
        return true;
166
    }
167
    // END irmtfan add deletePoll function
168
169
    // START irmtfan add getPoll function
170
    /**
171
     * get a poll object from a poll module.
172
     * note: can be used to find if a poll exist in a module
173
     * @access public
174
     * @param  int    $poll_id
175
     * @param  string $pollModule dirname of the poll module
176
     * @return bool|\XoopsObject poll
177
     */
178
    public function getPoll($poll_id, $pollModule = null)
179
    {
180
        if (empty($poll_id)) {
181
            return false;
182
        }
183
        $moduleHandler = xoops_getHandler('module');
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
        $moduleHandler = /** @scrutinizer ignore-call */ xoops_getHandler('module');
Loading history...
184
        $newbbConfig   = newbbLoadConfig();
185
        if (!empty($pollModule)) {
186
            $newbbConfig['poll_module'] = $pollModule;
187
        }
188
189
        $pollModuleHandler = $moduleHandler->getByDirname($newbbConfig['poll_module']);
190
        if (!is_object($pollModuleHandler) || !$pollModuleHandler->getVar('isactive')) {
191
            return false;
192
        }
193
        // new xoopspoll module
194
        if ($pollModuleHandler->getVar('version') >= 140) {
195
            $pollHandler = Xoopspoll\Helper::getInstance()->getHandler('Poll');
196
            $pollObject  = $pollHandler->get($poll_id);
197
            // old xoopspoll or umfrage or any clone from them
198
        } else {
199
            $classPoll  = $this->loadOldPoll($newbbConfig['poll_module']);
200
            $pollObject = new $classPoll($poll_id);
201
        } // end poll_module new or old
202
203
        return $pollObject;
204
    }
205
    // END irmtfan add getPoll function
206
}
207