Passed
Push — master ( 1e16e5...7bdd06 )
by Goffy
03:14
created

Answer::getForm()   C

Complexity

Conditions 10
Paths 256

Size

Total Lines 54
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 39
nc 256
nop 1
dl 0
loc 54
rs 6.1333
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
4
namespace XoopsModules\Wgevents;
5
6
/*
7
 You may not change or alter any portion of this comment or credits
8
 of supporting developers from this source code or any supporting source code
9
 which is considered copyrighted (c) material of the original comment or credit authors.
10
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
16
/**
17
 * wgEvents module for xoops
18
 *
19
 * @copyright    2021 XOOPS Project (https://xoops.org)
20
 * @license      GPL 2.0 or later
21
 * @package      wgevents
22
 * @since        1.0.0
23
 * @min_xoops    2.5.11 Beta1
24
 * @author       Goffy - Wedega - Email:[email protected] - Website:https://xoops.wedega.com
25
 */
26
27
use XoopsModules\Wgevents;
28
29
\defined('XOOPS_ROOT_PATH') || die('Restricted access');
30
31
/**
32
 * Class Object Answer
33
 */
34
class Answer extends \XoopsObject
35
{
36
    /**
37
     * @var int
38
     */
39
    public $start = 0;
40
41
    /**
42
     * @var int
43
     */
44
    public $limit = 0;
45
46
    /**
47
     * Constructor
48
     *
49
     * @param null
50
     */
51
    public function __construct()
52
    {
53
        $this->initVar('id', \XOBJ_DTYPE_INT);
54
        $this->initVar('regid', \XOBJ_DTYPE_INT);
55
        $this->initVar('queid', \XOBJ_DTYPE_INT);
56
        $this->initVar('evid', \XOBJ_DTYPE_INT);
57
        $this->initVar('text', \XOBJ_DTYPE_TXTBOX);
58
        $this->initVar('datecreated', \XOBJ_DTYPE_INT);
59
        $this->initVar('submitter', \XOBJ_DTYPE_INT);
60
    }
61
62
    /**
63
     * @static function &getInstance
64
     *
65
     * @param null
66
     */
67
    public static function getInstance()
68
    {
69
        static $instance = false;
70
        if (!$instance) {
71
            $instance = new self();
72
        }
73
    }
74
75
    /**
76
     * The new inserted $Id
77
     * @return inserted id
0 ignored issues
show
Bug introduced by
The type XoopsModules\Wgevents\inserted 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...
78
     */
79
    public function getNewInsertedId()
80
    {
81
        return $GLOBALS['xoopsDB']->getInsertId();
82
    }
83
84
    /**
85
     * @public function getForm
86
     * @param bool $action
87
     * @return \XoopsThemeForm
88
     */
89
    public function getForm($action = false)
90
    {
91
        $helper = \XoopsModules\Wgevents\Helper::getInstance();
92
        if (!$action) {
93
            $action = $_SERVER['REQUEST_URI'];
94
        }
95
        $isAdmin = (\is_object($GLOBALS['xoopsUser']) && \is_object($GLOBALS['xoopsModule'])) ? $GLOBALS['xoopsUser']->isAdmin($GLOBALS['xoopsModule']->mid()) : false;
0 ignored issues
show
Unused Code introduced by
The assignment to $isAdmin is dead and can be removed.
Loading history...
96
        // Title
97
        $title = $this->isNew() ? \_MA_WGEVENTS_ANSWER_ADD : \_MA_WGEVENTS_ANSWER_EDIT;
98
        // Get Theme Form
99
        \xoops_load('XoopsFormLoader');
100
        $form = new \XoopsThemeForm($title, 'form', $action, 'post', true);
101
        $form->setExtra('enctype="multipart/form-data"');
102
        // Form Table registrations
103
        $registrationHandler = $helper->getHandler('Registration');
104
        $ansResidSelect = new \XoopsFormSelect(\_MA_WGEVENTS_ANSWER_REGID, 'regid', $this->getVar('regid'));
105
        $ansResidSelect->addOptionArray($registrationHandler->getList());
106
        $form->addElement($ansResidSelect);
107
        // Form Table questions
108
        $questionHandler = $helper->getHandler('Question');
109
        $ansAddidSelect = new \XoopsFormSelect(\_MA_WGEVENTS_ANSWER_QUEID, 'queid', $this->getVar('queid'));
110
        $crQuestion = new \CriteriaCompo();
111
        $crQuestion->add(new \Criteria('evid', $this->getVar('evid')));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('evid') can also be of type array and array; however, parameter $value of Criteria::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

111
        $crQuestion->add(new \Criteria('evid', /** @scrutinizer ignore-type */ $this->getVar('evid')));
Loading history...
112
        $questionsCount = $questionHandler->getCount($crQuestion);
113
        // Table view questions
114
        if ($questionsCount > 0) {
115
            $crQuestion->setSort('weight ASC, id');
116
            $crQuestion->setOrder('DESC');
117
            $questionsAll = $questionHandler->getAll($crQuestion);
118
            foreach (\array_keys($questionsAll) as $i) {
119
                $ansAddidSelect->addOption($questionsAll[$i]->getVar('id'), $questionsAll[$i]->getVar('caption'));
120
            }
121
        }
122
        $form->addElement($ansAddidSelect);
123
        // Form Table events
124
        $eventHandler = $helper->getHandler('Event');
125
        $ansEvidSelect = new \XoopsFormSelect(\_MA_WGEVENTS_ANSWER_EVID, 'evid', $this->getVar('evid'));
126
        $ansEvidSelect->addOptionArray($eventHandler->getList());
127
        $form->addElement($ansEvidSelect);
128
        // Form Text ansText
129
        $form->addElement(new \XoopsFormText(\_MA_WGEVENTS_ANSWER_TEXT, 'text', 50, 255, $this->getVar('text')));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('text') can also be of type array and array; however, parameter $value of XoopsFormText::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

129
        $form->addElement(new \XoopsFormText(\_MA_WGEVENTS_ANSWER_TEXT, 'text', 50, 255, /** @scrutinizer ignore-type */ $this->getVar('text')));
Loading history...
130
        // Form Text Date Select ansDatecreated
131
        $ansDatecreated = $this->isNew() ? \time() : $this->getVar('datecreated');
132
        $form->addElement(new \XoopsFormTextDateSelect(\_MA_WGEVENTS_DATECREATED, 'datecreated', '', $ansDatecreated));
0 ignored issues
show
Bug introduced by
'' of type string is incompatible with the type integer expected by parameter $size of XoopsFormTextDateSelect::__construct(). ( Ignorable by Annotation )

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

132
        $form->addElement(new \XoopsFormTextDateSelect(\_MA_WGEVENTS_DATECREATED, 'datecreated', /** @scrutinizer ignore-type */ '', $ansDatecreated));
Loading history...
133
        // Form Select User ansSubmitter
134
        $uidCurrent = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->uid() : 0;
135
        $ansSubmitter = $this->isNew() ? $uidCurrent : $this->getVar('submitter');
136
        $form->addElement(new \XoopsFormSelectUser(\_MA_WGEVENTS_SUBMITTER, 'submitter', false, $ansSubmitter));
137
        // To Save
138
        $form->addElement(new \XoopsFormHidden('op', 'save'));
139
        $form->addElement(new \XoopsFormHidden('start', $this->start));
140
        $form->addElement(new \XoopsFormHidden('limit', $this->limit));
141
        $form->addElement(new \XoopsFormButtonTray('', \_SUBMIT, 'submit', '', false));
142
        return $form;
143
    }
144
145
    /**
146
     * Get Values
147
     * @param null $keys
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $keys is correct as it would always require null to be passed?
Loading history...
148
     * @param null $format
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $format is correct as it would always require null to be passed?
Loading history...
149
     * @param null $maxDepth
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $maxDepth is correct as it would always require null to be passed?
Loading history...
150
     * @return array
151
     */
152
    public function getValuesAnswers($keys = null, $format = null, $maxDepth = null)
153
    {
154
        $helper  = \XoopsModules\Wgevents\Helper::getInstance();
155
        $ret = $this->getValues($keys, $format, $maxDepth);
156
        $questionHandler = $helper->getHandler('Question');
157
        $questionObj = $questionHandler->get($this->getVar('queid'));
158
        $queCaption = '';
159
        if (\is_object($questionObj)) {
160
            $queCaption = $questionObj->getVar('caption');
161
        }
162
        $ret['quecaption']       = $queCaption;
163
        $eventHandler = $helper->getHandler('Event');
164
        $eventObj = $eventHandler->get($this->getVar('evid'));
165
        $evName = 'invalid event';
166
        if (\is_object($eventObj)) {
167
            $evName = $eventObj->getVar('name');
168
        }
169
        $ret['eventname']        = $evName;
170
        $ret['datecreated_text'] = \formatTimestamp($this->getVar('datecreated'), 's');
171
        $ret['submitter_text']   = \XoopsUser::getUnameFromId($this->getVar('submitter'));
172
        return $ret;
173
    }
174
175
    /**
176
     * Returns an array representation of the object
177
     *
178
     * @return array
179
     */
180
    /*
181
    public function toArray()
182
    {
183
        $ret = [];
184
        $vars = $this->getVars();
185
        foreach (\array_keys($vars) as $var) {
186
            $ret[$var] = $this->getVar($var);
187
        }
188
        return $ret;
189
    }
190
    */
191
}
192