Completed
Pull Request — master (#563)
by Richard
08:33
created

CommentsCommentForm::__construct()   F

Complexity

Conditions 24
Paths 12288

Size

Total Lines 134
Code Lines 101

Duplication

Lines 9
Ratio 6.72 %

Importance

Changes 0
Metric Value
cc 24
eloc 101
nc 12288
nop 1
dl 9
loc 134
rs 2
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
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
use Xmf\Request;
13
14
/**
15
 * @copyright       XOOPS Project (http://xoops.org)
16
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
17
 * @author          trabis <[email protected]>
18
 * @author          Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
19
 * @version         $Id$
20
 */
21
22
class CommentsCommentForm extends Xoops\Form\ThemeForm
23
{
24
    /**
25
     * @param CommentsComment $obj
26
     */
27
    public function __construct(CommentsComment $obj)
28
    {
29
        $xoops = Xoops::getInstance();
30
        $helper = $xoops->getModuleHelper('comments');
31
        $module = $xoops->getModuleById($obj->getVar('modid'));
32
        if (!is_object($module)) {
33
            $xoops->redirect(\XoopsBaseConfig::get('url'), 1, XoopsLocale::E_NO_ACCESS_PERMISSION);
34
        }
35
        $dirname = $module->getVar('dirname');
36
37
        // create form
38
        if ($xoops->isAdminSide) {
39
            $url = $helper->url("admin/comment_post.php");
40
        } else {
41
            $url = $helper->url("comment_post.php");
42
        }
43
        parent::__construct(_MD_COMMENTS_POSTCOMMENT, "commentform", $url, "post", true);
44
45
        switch ($xoops->getModuleConfig('com_rule', $dirname)) {
0 ignored issues
show
Bug introduced by
It seems like $dirname can also be of type array; however, parameter $dirname of Xoops::getModuleConfig() 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

45
        switch ($xoops->getModuleConfig('com_rule', /** @scrutinizer ignore-type */ $dirname)) {
Loading history...
46
            case Comments::APPROVE_ALL:
47
                $rule_text = _MD_COMMENTS_COMAPPROVEALL;
48
                break;
49
            case Comments::APPROVE_USER:
50
                $rule_text = _MD_COMMENTS_COMAPPROVEUSER;
51
                break;
52
            case Comments::APPROVE_ADMIN:
53
            default:
54
                $rule_text = _MD_COMMENTS_COMAPPROVEADMIN;
55
                break;
56
        }
57
        $this->addElement(new Xoops\Form\Label(_MD_COMMENTS_COMRULES, $rule_text));
58
59
        $this->addElement(new Xoops\Form\Text(_MD_COMMENTS_TITLE, 'com_title', 50, 255, $obj->getVar('title', 'e')), true);
0 ignored issues
show
Bug introduced by
It seems like $obj->getVar('title', 'e') can also be of type array; however, parameter $value of Xoops\Form\Text::__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

59
        $this->addElement(new Xoops\Form\Text(_MD_COMMENTS_TITLE, 'com_title', 50, 255, /** @scrutinizer ignore-type */ $obj->getVar('title', 'e')), true);
Loading history...
60
        $iconsRadio = new Xoops\Form\Radio(XoopsLocale::MESSAGE_ICON, 'com_icon', $obj->getVar('icon', 'e'));
0 ignored issues
show
Bug introduced by
It seems like $obj->getVar('icon', 'e') can also be of type array; however, parameter $value of Xoops\Form\Radio::__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

60
        $iconsRadio = new Xoops\Form\Radio(XoopsLocale::MESSAGE_ICON, 'com_icon', /** @scrutinizer ignore-type */ $obj->getVar('icon', 'e'));
Loading history...
61
        \Xoops\Core\Lists\SubjectIcon::setOptionsArray($iconsRadio);
62
        $this->addElement($iconsRadio);
63
        // editor
64
        $editor = $helper->getConfig('com_editor');
65
        if (class_exists('Xoops\Form\Editor')) {
66
            $configs = array(
67
                'name'   => 'com_text',
68
                'value'  => $obj->getVar('text', 'e'),
69
                'rows'   => 25,
70
                'cols'   => 90,
71
                'width'  => '100%',
72
                'height' => '400px',
73
                'editor' => $editor
74
            );
75
            $this->addElement(new Xoops\Form\Editor(_MD_COMMENTS_MESSAGE, 'com_text', $configs, false, $onfailure = 'textarea'));
76
        } else {
77
            $this->addElement(new Xoops\Form\DhtmlTextArea(_MD_COMMENTS_MESSAGE, 'com_text', $obj->getVar('text', 'e'), 10, 50), true);
0 ignored issues
show
Bug introduced by
It seems like $obj->getVar('text', 'e') can also be of type array; however, parameter $value of Xoops\Form\DhtmlTextArea::__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

77
            $this->addElement(new Xoops\Form\DhtmlTextArea(_MD_COMMENTS_MESSAGE, 'com_text', /** @scrutinizer ignore-type */ $obj->getVar('text', 'e'), 10, 50), true);
Loading history...
78
        }
79
        $option_tray = new Xoops\Form\ElementTray(XoopsLocale::OPTIONS, '<br />');
80
        $buttonTray = new Xoops\Form\ElementTray('', '&nbsp;');
81
82
        if ($xoops->isUser()) {
83
            if ($xoops->getModuleConfig('com_anonpost', $dirname)) {
84
                    $noname = $obj->getVar('noname', 'e') ? 1 : 0;
85
                    $noname_checkbox = new Xoops\Form\Checkbox('', 'com_noname', $noname);
86
                    $noname_checkbox->addOption(1, XoopsLocale::POST_ANONYMOUSLY);
87
                    $option_tray->addElement($noname_checkbox);
88
            }
89
            if (false != $xoops->user->isAdmin($obj->getVar('modid'))) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
90
                // show status change box when editing (comment id is not empty)
91
                if ($obj->getVar('id', 'e')) {
92
                    $status_select = new Xoops\Form\Select(_MD_COMMENTS_STATUS, 'com_status', $obj->getVar('status', 'e'));
93
                    $status_select->addOptionArray(array(
94
                        Comments::STATUS_PENDING => _MD_COMMENTS_PENDING,
95
                        Comments::STATUS_ACTIVE  => _MD_COMMENTS_ACTIVE,
96
                        Comments::STATUS_HIDDEN  => _MD_COMMENTS_HIDDEN
97
                    ));
98
                    $this->addElement($status_select);
99
                    $buttonTray->addElement(new Xoops\Form\Button('', 'com_dodelete', XoopsLocale::A_DELETE, 'submit'));
100
                }
101
                if (isset($editor) && in_array($editor, array('textarea', 'dhtmltextarea'))) {
102
                    $html_checkbox = new Xoops\Form\Checkbox('', 'com_dohtml', $obj->getVar('dohtml', 'e'));
103
                    $html_checkbox->addOption(1, _MD_COMMENTS_DOHTML);
104
                    $option_tray->addElement($html_checkbox);
105
                }
106
            }
107
        }
108
        if (isset($editor) && in_array($editor, array('textarea', 'dhtmltextarea'))) {
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...
109
            //Yeah, what?
110
        }
111
        $smiley_checkbox = new Xoops\Form\Checkbox('', 'com_dosmiley', $obj->getVar('domsiley', 'e'));
112
        $smiley_checkbox->addOption(1, _MD_COMMENTS_DOSMILEY);
113
        $option_tray->addElement($smiley_checkbox);
114
        $xcode_checkbox = new Xoops\Form\Checkbox('', 'com_doxcode', $obj->getVar('doxcode', 'e'));
115
        $xcode_checkbox->addOption(1, _MD_COMMENTS_DOXCODE);
116
        $option_tray->addElement($xcode_checkbox);
117
        if (isset($editor) && in_array($editor, array('textarea', 'dhtmltextarea'))) {
118
            $br_checkbox = new Xoops\Form\Checkbox('', 'com_dobr', $obj->getVar('dobr', 'e'));
119
            $br_checkbox->addOption(1, _MD_COMMENTS_DOAUTOWRAP);
120
            $option_tray->addElement($br_checkbox);
121
        } else {
122
            $this->addElement(new Xoops\Form\Hidden('com_dohtml', 1));
123
            $this->addElement(new Xoops\Form\Hidden('com_dobr', 0));
124
        }
125
        $this->addElement($option_tray);
126
        if (!$xoops->isUser()) {
127
            $this->addElement(new Xoops\Form\Captcha());
128
        }
129
        $this->addElement(new Xoops\Form\Hidden('com_modid', $obj->getVar('modid', 'e')));
0 ignored issues
show
Bug introduced by
It seems like $obj->getVar('modid', 'e') can also be of type array; however, parameter $value of Xoops\Form\Hidden::__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
        $this->addElement(new Xoops\Form\Hidden('com_modid', /** @scrutinizer ignore-type */ $obj->getVar('modid', 'e')));
Loading history...
130
        $this->addElement(new Xoops\Form\Hidden('com_pid', $obj->getVar('pid', 'e')));
131
        $this->addElement(new Xoops\Form\Hidden('com_rootid', $obj->getVar('rootid', 'e')));
132
        $this->addElement(new Xoops\Form\Hidden('com_id', $obj->getVar('id', 'e')));
133
        $this->addElement(new Xoops\Form\Hidden('com_itemid', $obj->getVar('itemid', 'e')));
134
        $this->addElement(new Xoops\Form\Hidden('com_order', Request::getInt('com_order', $helper->getUserConfig('com_order'))));
0 ignored issues
show
Bug introduced by
The method getUserConfig() does not exist on Xoops\Module\Helper\HelperAbstract. Did you maybe mean getConfig()? ( Ignorable by Annotation )

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

134
        $this->addElement(new Xoops\Form\Hidden('com_order', Request::getInt('com_order', $helper->/** @scrutinizer ignore-call */ getUserConfig('com_order'))));

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...
135
        $this->addElement(new Xoops\Form\Hidden('com_mode', Request::getString('com_mode', $helper->getUserConfig('com_mode'))));
136
137
        // add module specific extra params
138
        if (!$xoops->isAdminSide) {
139
            /* @var $plugin CommentsPluginInterface */
140
            $plugin = \Xoops\Module\Plugin::getPlugin($dirname, 'comments');
0 ignored issues
show
Bug introduced by
It seems like $dirname can also be of type array; however, parameter $dirname of Xoops\Module\Plugin::getPlugin() 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

140
            $plugin = \Xoops\Module\Plugin::getPlugin(/** @scrutinizer ignore-type */ $dirname, 'comments');
Loading history...
141
            if (is_array($extraParams = $plugin->extraParams())) {
0 ignored issues
show
introduced by
The condition is_array($extraParams = $plugin->extraParams()) can never be false.
Loading history...
142
                foreach ($extraParams as $extra_param) {
143
                    // This routine is included from forms accessed via both GET and POST
144
                    if (isset($_POST[$extra_param])) {
145
                        $hidden_value = $_POST[$extra_param];
146
                    } else {
147
                        if (isset($_GET[$extra_param])) {
148
                            $hidden_value = $_GET[$extra_param];
149
                        } else {
150
                            $hidden_value = '';
151
                        }
152
                    }
153
                    $this->addElement(new Xoops\Form\Hidden($extra_param, $hidden_value));
154
                }
155
            }
156
        }
157
        $buttonTray->addElement(new Xoops\Form\Button('', 'com_dopreview', XoopsLocale::A_PREVIEW, 'submit'));
158
        $buttonTray->addElement(new Xoops\Form\Button('', 'com_dopost', _MD_COMMENTS_POSTCOMMENT, 'submit'));
159
        $this->addElement($buttonTray);
160
    }
161
}
162