Passed
Pull Request — master (#58)
by Michael
03:14
created

UploadForm::__construct()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 55
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 35
c 0
b 0
f 0
nc 8
nop 1
dl 0
loc 55
rs 8.7377

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\Tdmdownloads\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: Tdmdownloads
15
 *
16
 * @category        Module
17
 * @package         tdmdownloads
18
 * @author          XOOPS Development Team <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\Tdmdownloads;
27
28
require_once dirname(dirname(__DIR__)) . '/include/common.php';
29
30
//$moduleDirName = basename(dirname(dirname(__DIR__)));
31
//$helper = Tdmdownloads\Helper::getInstance();
32
$permHelper = new \Xmf\Module\Helper\Permission();
33
34
xoops_load('XoopsFormLoader');
35
36
/**
37
 * Class FieldForm
38
 */
39
class UploadForm extends \XoopsThemeForm
40
{
41
    public $targetObject;
42
    public $helper;
43
44
    /**
45
     * Constructor
46
     *
47
     * @param \XoopsModules\Tdmdownloads\Category $target
48
     */
49
    public function __construct($target)
50
    {
51
        $moduleDirName      = basename(dirname(dirname(__DIR__)));
52
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
53
        /** @var  \XoopsModules\Tdmdownloads\Helper $this->helper */
54
        $this->helper       = $target->helper;
55
        $this->targetObject = $target;
56
57
        $title = $this->targetObject->isNew() ? sprintf(constant('CO_' . $moduleDirNameUpper . '_' . 'FIELD_ADD')) : sprintf(constant('CO_' . $moduleDirNameUpper . '_' . 'FIELD_EDIT'));
0 ignored issues
show
Unused Code introduced by
The assignment to $title is dead and can be removed.
Loading history...
58
        parent::__construct('', 'form', xoops_getenv('PHP_SELF'), 'post', true);
59
        $this->setExtra('enctype="multipart/form-data"');
60
61
        //include ID field, it's needed so the module knows if it is a new form or an edited form
62
63
        $hidden = new \XoopsFormHidden('fid', $this->targetObject->getVar('cat_cid'));
0 ignored issues
show
Bug introduced by
It seems like $this->targetObject->getVar('cat_cid') can also be of type array and array; however, parameter $value of XoopsFormHidden::__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

63
        $hidden = new \XoopsFormHidden('fid', /** @scrutinizer ignore-type */ $this->targetObject->getVar('cat_cid'));
Loading history...
64
        $this->addElement($hidden);
65
        unset($hidden);
66
67
68
        $categoryHandler = new \XoopsModules\Tdmdownloads\CategoryHandler();
69
        $start = Request::getInt('start', 0);
70
        $catPaginationLimit = $this->helper->getConfig('userpager') ?: 10;
71
72
        $criteria = new \CriteriaCompo();
73
        $criteria->setOrder('DESC');
74
        $criteria->setLimit($catPaginationLimit);
75
        $criteria->setStart($start);
76
77
        $catCount = $categoryHandler->getCount($criteria);
0 ignored issues
show
Unused Code introduced by
The assignment to $catCount is dead and can be removed.
Loading history...
78
        $catArray = $categoryHandler->getAll($criteria);
79
80
        // Form Select Category
81
        $categoryIdSelect = new \XoopsFormSelect( constant('CO_' . $moduleDirNameUpper . '_' . 'SELECT'), 'cat_title', $this->targetObject->getVar('cat_cid'));
82
        $categoryIdSelect->setExtra('onchange="submit()"');
83
//        $categoryIdSelect->addOption(0, '&nbsp;');
84
85
        foreach(array_keys($catArray) as $i) {
86
            $catName = $catArray[$i]->getVar('cat_title');
87
            $catPid = $catArray[$i]->getVar('cat_pid');
88
            if ( 0 < $catPid ) {
89
                $categoryObj = $categoryHandler->get($catPid);
90
                if (is_object( $categoryObj)) {
91
                    $catName .= ' (' . $categoryObj->getVar('cat_title') . ')';
92
                } else {
93
                    $catName .= ' (' . constant('CO_' . $moduleDirNameUpper . '_' . 'ERROR_CATPID') . ')';
94
                }
95
            }
96
            $categoryIdSelect->addOption($catArray[$i]->getVar('cat_cid'), $catName);
97
        }
98
99
        $this->addElement($categoryIdSelect);
100
        unset($categoryCriteria);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $categoryCriteria seems to be never defined.
Loading history...
101
102
        $this->addElement(new \XoopsFormHidden('start', 0));
103
        $this->addElement(new \XoopsFormHidden('limit', 0));
104
    }
105
}
106