PartForm::__construct()   B
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 64
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 45
nc 12
nop 1
dl 0
loc 64
rs 8.5777
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\Cardealer\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
/**
14
 * Module: cardealer
15
 *
16
 * @category        Module
17
 * @package         cardealer
18
 * @author          XOOPS Development Team <[email protected]> - <https://xoops.org>
19
 * @copyright       {@link https://xoops.org/ XOOPS Project}
20
 * @license         GPL 2.0 or later
21
 * @link            https://xoops.org/
22
 * @since           1.0.0
23
 */
24
25
use Xmf\Request;
26
use XoopsModules\Cardealer;
27
28
require dirname(dirname(__DIR__)) . '/include/common.php';
29
30
$moduleDirName = basename(dirname(dirname(__DIR__)));
31
$helper        = Cardealer\Helper::getInstance();
32
$permHelper    = new \Xmf\Module\Helper\Permission();
33
34
xoops_load('XoopsFormLoader');
35
36
/**
37
 * Class PartForm
38
 */
39
class PartForm extends \XoopsThemeForm
40
{
41
    public $targetObject;
42
43
    /**
44
     * Constructor
45
     *
46
     * @param $target
47
     */
48
    public function __construct($target)
49
    {
50
        global $helper;
51
        $this->targetObject = $target;
52
53
        $title = $this->targetObject->isNew() ? sprintf(AM_CARDEALER_PART_ADD) : sprintf(AM_CARDEALER_PART_EDIT);
54
        parent::__construct($title, 'form', xoops_getenv('PHP_SELF'), 'post', true);
55
        $this->setExtra('enctype="multipart/form-data"');
56
57
        //include ID field, it's needed so the module knows if it is a new form or an edited form
58
59
        $hidden = new \XoopsFormHidden('partnum', $this->targetObject->getVar('partnum'));
60
        $this->addElement($hidden);
61
        unset($hidden);
62
63
        // Partnum
64
        $this->addElement(new \XoopsFormLabel(AM_CARDEALER_PART_PARTNUM, $this->targetObject->getVar('partnum'), 'partnum'));
65
        // Price
66
        $this->addElement(new \XoopsFormText(AM_CARDEALER_PART_PRICE, 'price', 50, 255, $this->targetObject->getVar('price')), false);
67
        // Stock
68
        $this->addElement(new \XoopsFormText(AM_CARDEALER_PART_STOCK, 'stock', 50, 255, $this->targetObject->getVar('stock')), false);
69
        // Title
70
        $this->addElement(new \XoopsFormText(AM_CARDEALER_PART_TITLE, 'title', 50, 255, $this->targetObject->getVar('title')), false);
71
        // Description
72
        if (class_exists('XoopsFormEditor')) {
73
            $editorOptions           = [];
74
            $editorOptions['name']   = 'description';
75
            $editorOptions['value']  = $this->targetObject->getVar('description', 'e');
76
            $editorOptions['rows']   = 5;
77
            $editorOptions['cols']   = 40;
78
            $editorOptions['width']  = '100%';
79
            $editorOptions['height'] = '400px';
80
81
            if ($helper->isUserAdmin()) {
82
                $descEditor = new \XoopsFormEditor(AM_CARDEALER_PART_DESCRIPTION, $helper->getConfig('cardealerEditorAdmin'), $editorOptions, $nohtml = false, $onfailure = 'textarea');
83
            } else {
84
                $descEditor = new \XoopsFormEditor(AM_CARDEALER_PART_DESCRIPTION, $helper->getConfig('cardealerEditorUser'), $editorOptions, $nohtml = false, $onfailure = 'textarea');
85
            }
86
        } else {
87
            $descEditor = new \XoopsFormDhtmlTextArea(AM_CARDEALER_PART_DESCRIPTION, 'description', $this->targetObject->getVar('description', 'e'), '100%', '100%');
0 ignored issues
show
Bug introduced by
'100%' of type string is incompatible with the type integer expected by parameter $cols of XoopsFormDhtmlTextArea::__construct(). ( Ignorable by Annotation )

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

87
            $descEditor = new \XoopsFormDhtmlTextArea(AM_CARDEALER_PART_DESCRIPTION, 'description', $this->targetObject->getVar('description', 'e'), '100%', /** @scrutinizer ignore-type */ '100%');
Loading history...
Bug introduced by
'100%' of type string is incompatible with the type integer expected by parameter $rows of XoopsFormDhtmlTextArea::__construct(). ( Ignorable by Annotation )

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

87
            $descEditor = new \XoopsFormDhtmlTextArea(AM_CARDEALER_PART_DESCRIPTION, 'description', $this->targetObject->getVar('description', 'e'), /** @scrutinizer ignore-type */ '100%', '100%');
Loading history...
88
        }
89
        $this->addElement($descEditor);
90
        // Picture
91
        $picture = $this->targetObject->getVar('picture') ?: 'blank.png';
92
93
        $uploadDir   = '/uploads/cardealer/images/';
94
        $imgtray     = new \XoopsFormElementTray(AM_CARDEALER_PART_PICTURE, '<br>');
95
        $imgpath     = sprintf(AM_CARDEALER_FORMIMAGE_PATH, $uploadDir);
96
        $imageselect = new \XoopsFormSelect($imgpath, 'picture', $picture);
97
        $imageArray  = \XoopsLists::getImgListAsArray(XOOPS_ROOT_PATH . $uploadDir);
98
        foreach ($imageArray as $image) {
99
            $imageselect->addOption((string)$image, $image);
100
        }
101
        $imageselect->setExtra("onchange='showImgSelected(\"image_picture\", \"picture\", \"" . $uploadDir . '", "", "' . XOOPS_URL . "\")'");
102
        $imgtray->addElement($imageselect);
103
        $imgtray->addElement(new \XoopsFormLabel('', "<br><img src='" . XOOPS_URL . '/' . $uploadDir . '/' . $picture . "' name='image_picture' id='image_picture' alt='' />"));
104
        $fileseltray = new \XoopsFormElementTray('', '<br>');
105
        $fileseltray->addElement(new \XoopsFormFile(AM_CARDEALER_FORMUPLOAD, 'picture', xoops_getModuleOption('maxsize')));
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getModuleOption() has been deprecated. ( Ignorable by Annotation )

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

105
        $fileseltray->addElement(new \XoopsFormFile(AM_CARDEALER_FORMUPLOAD, 'picture', /** @scrutinizer ignore-deprecated */ xoops_getModuleOption('maxsize')));
Loading history...
Bug introduced by
xoops_getModuleOption('maxsize') of type boolean is incompatible with the type integer expected by parameter $maxfilesize of XoopsFormFile::__construct(). ( Ignorable by Annotation )

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

105
        $fileseltray->addElement(new \XoopsFormFile(AM_CARDEALER_FORMUPLOAD, 'picture', /** @scrutinizer ignore-type */ xoops_getModuleOption('maxsize')));
Loading history...
106
        $fileseltray->addElement(new \XoopsFormLabel(''));
107
        $imgtray->addElement($fileseltray);
108
        $this->addElement($imgtray);
109
110
        $this->addElement(new \XoopsFormHidden('op', 'save'));
111
        $this->addElement(new \XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
112
    }
113
}
114