QuotesForm::__construct()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 56

Duplication

Lines 18
Ratio 32.14 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 1
dl 18
loc 56
rs 8.9599
c 0
b 0
f 0

How to fix   Long Method   

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 namespace Xoopsmodules\randomquote\form;
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
*/
12
/**
13
 * Module: randomquote
14
 *
15
 * @category        Module
16
 * @package         randomquote
17
 * @author          XOOPS Development Team <[email protected]> - <https://xoops.org>
18
 * @copyright       {@link https://xoops.org/ XOOPS Project}
19
 * @license         GPL 2.0 or later
20
 * @link            https://xoops.org/
21
 * @since           1.0.0
22
 */
23
24
use Xmf\Request;
25
use Xoopsmodules\randomquote;
26
27
require_once __DIR__ . '/../../include/common.php';
28
29
$moduleDirName = basename(dirname(dirname(__DIR__)));
30
$helper        = randomquote\Helper::getInstance();
31
$permHelper    = new \Xmf\Module\Helper\Permission();
32
33
xoops_load('XoopsFormLoader');
34
35
/**
36
 * Class QuotesForm
37
 */
38
class QuotesForm extends \XoopsThemeForm
39
{
40
    public $targetObject;
41
42
    /**
43
     * Constructor
44
     *
45
     * @param $target
46
     */
47
    public function __construct($target)
48
    {
49
        global $helper;
50
        $this->targetObject = $target;
51
52
        $title = $this->targetObject->isNew() ? sprintf(AM_RANDOMQUOTE_QUOTES_ADD) : sprintf(AM_RANDOMQUOTE_QUOTES_EDIT);
53
        parent::__construct($title, 'form', xoops_getenv('PHP_SELF'), 'post', true);
54
        $this->setExtra('enctype="multipart/form-data"');
55
56
        //include ID field, it's needed so the module knows if it is a new form or an edited form
57
58
        $hidden = new \XoopsFormHidden('id', $this->targetObject->getVar('id'));
59
        $this->addElement($hidden);
60
        unset($hidden);
61
62
        // Id
63
        $this->addElement(new \XoopsFormLabel(AM_RANDOMQUOTE_QUOTES_ID, $this->targetObject->getVar('id'), 'id'));
64
        // Quote
65 View Code Duplication
        if (class_exists('XoopsFormEditor')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
            $editorOptions           = [];
67
            $editorOptions['name']   = 'quote';
68
            $editorOptions['value']  = $this->targetObject->getVar('quote', 'e');
69
            $editorOptions['rows']   = 5;
70
            $editorOptions['cols']   = 40;
71
            $editorOptions['width']  = '100%';
72
            $editorOptions['height'] = '400px';
73
            //$editorOptions['editor'] = xoops_getModuleOption('randomquote_editor', 'randomquote');
74
            //$this->addElement( new \XoopsFormEditor(AM_RANDOMQUOTE_QUOTES_QUOTE, 'quote', $editorOptions), false  );
75
            if ($helper->isUserAdmin()) {
76
                $descEditor = new \XoopsFormEditor(AM_RANDOMQUOTE_QUOTES_QUOTE, $helper->getConfig('randomquoteEditorAdmin'), $editorOptions, $nohtml = false, $onfailure = 'textarea');
77
            } else {
78
                $descEditor = new \XoopsFormEditor(AM_RANDOMQUOTE_QUOTES_QUOTE, $helper->getConfig('randomquoteEditorUser'), $editorOptions, $nohtml = false, $onfailure = 'textarea');
79
            }
80
        } else {
81
            $descEditor = new \XoopsFormDhtmlTextArea(AM_RANDOMQUOTE_QUOTES_QUOTE, 'description', $this->targetObject->getVar('description', 'e'), '100%', '100%');
82
        }
83
        $this->addElement($descEditor);
84
        // Author
85
        $this->addElement(new \XoopsFormText(AM_RANDOMQUOTE_QUOTES_AUTHOR, 'author', 50, 255, $this->targetObject->getVar('author')), false);
86
        // Online
87
        $this->addElement(new \XoopsFormText(AM_RANDOMQUOTE_QUOTES_ONLINE, 'online', 50, 255, $this->targetObject->getVar('online')), false);
88
        // Created
89
        $this->addElement(new \XoopsFormTextDateSelect(AM_RANDOMQUOTE_QUOTES_CREATED, 'created', '', strtotime($this->targetObject->getVar('created'))));
90
        // Cid
91
        //$categoryHandler = xoops_getModuleHandler('category', 'randomquote');        
92
        $db = \XoopsDatabaseFactory::getDatabaseConnection();
93
        /** @var \XoopsPersistableObjectHandler $categoryHandler */
94
        $categoryHandler = new randomquote\CategoryHandler($db);
95
96
        $category_id_select = new \XoopsFormSelect(AM_RANDOMQUOTE_QUOTES_CID, 'cid', $this->targetObject->getVar('cid'));
97
        $category_id_select->addOptionArray($categoryHandler->getList());
98
        $this->addElement($category_id_select, false);
99
100
        $this->addElement(new \XoopsFormHidden('op', 'save'));
101
        $this->addElement(new \XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
102
    }
103
}
104