Completed
Pull Request — master (#489)
by Richard
10:39
created

CommentsCommentForm::__construct()   F

Complexity

Conditions 24
Paths 12288

Size

Total Lines 135
Code Lines 102

Duplication

Lines 9
Ratio 6.67 %
Metric Value
dl 9
loc 135
rs 2
cc 24
eloc 102
nc 12288
nop 1

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)
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
__construct uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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)) {
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);
60
        $iconsRadio = new Xoops\Form\Radio(XoopsLocale::MESSAGE_ICON, 'com_icon', $obj->getVar('icon', 'e'));
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);
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'))) {
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')));
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'))));
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');
141
            if (is_array($extraParams = $plugin->extraParams())) {
142
                foreach ($extraParams as $extra_param) {
143
                    // This routine is included from forms accessed via both GET and POST
144 View Code Duplication
                    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
        return $this;
161
    }
162
}
163