ReportParameter::displayParam()   D
last analyzed

Complexity

Conditions 19
Paths 51

Size

Total Lines 68
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 56
c 0
b 0
f 0
nc 51
nop 1
dl 0
loc 68
rs 4.5166

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 declare(strict_types=1);
2
3
namespace XoopsModules\Xhelp;
4
5
/*
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * @copyright    {@link https://xoops.org/ XOOPS Project}
17
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
18
 * @author       Eric Juden <[email protected]>
19
 * @author       XOOPS Development Team
20
 */
21
22
if (!\defined('XHELP_CLASS_PATH')) {
23
    exit();
24
}
25
26
/**
27
 * ReportParameter class
28
 *
29
 * Information about an individual report parameter
30
 *
31
 * @author  Eric Juden <[email protected]>
32
 */
33
class ReportParameter
34
{
35
    public $controltype;
36
    public $dbaction;
37
    public $dbfield;
38
    public $fieldlength;
39
    public $fieldname;
40
    public $maxlength;
41
    public $name;
42
    public $value;
43
    public $values;
44
45
    /**
46
     * ReportParameter constructor.
47
     */
48
    public function __construct()
49
    {
50
        // Contructor
51
    }
52
53
    /**
54
     * Create a new ReportParameter
55
     *
56
     * @return ReportParameter {@link ReportParameter}
57
     */
58
    public static function create(): ReportParameter
59
    {
60
        $ret = new ReportParameter();
61
62
        return $ret;
63
    }
64
65
    /**
66
     * Add a new report parameter
67
     *
68
     * @param int    $controltype
69
     * @param string $name
70
     * @param string $fieldname
71
     * @param string $value
72
     * @param array  $values
73
     * @param int    $fieldlength
74
     * @param string $dbfield
75
     * @param string $dbaction
76
     *
77
     * @return object {@link ReportParameter}
78
     */
79
    public static function addParam(int $controltype, string $name, string $fieldname, string $value, array $values, int $fieldlength, string $dbfield, string $dbaction)
80
    {
81
        $param              = self::create();
82
        $param->controltype = $controltype;
83
        $param->name        = $name;
84
        $param->fieldname   = $fieldname;
85
        $param->value       = $value;
86
        $param->values      = $values;
87
        $param->fieldlength = $fieldlength;
88
        $param->maxlength   = (\min($fieldlength, 50));
89
        $param->dbfield     = $dbfield;
90
        $param->dbaction    = $dbaction;
91
92
        return $param;
93
    }
94
95
    /**
96
     * Creates the html to display a parameter on the report
97
     *
98
     * @param array $vals
99
     * @return string
100
     */
101
    public function displayParam(array $vals = []): ?string
102
    {
103
        $controltype = $this->controltype;
104
        $fieldlength = $this->maxlength;
0 ignored issues
show
Unused Code introduced by
The assignment to $fieldlength is dead and can be removed.
Loading history...
105
106
        if (!empty($vals) && isset($vals[$this->fieldname])) {
107
            if (\is_array($vals[$this->fieldname])) {
108
                $this->values = $vals[$this->fieldname][0];
109
                $this->value  = $vals[$this->fieldname][1];
110
            } else {
111
                $this->value = $vals[$this->fieldname];
112
            }
113
        }
114
115
        switch ($controltype) {
116
            case \XHELP_CONTROL_TXTBOX:
117
                return "<label for='" . $this->fieldname . "'>" . $this->name . '</label>' . "<input type='text' name='" . $this->fieldname . "' id='" . $this->fieldname . "' value='" . $this->value . "' maxlength='" . $this->maxlength . "' size='" . $this->fieldlength . "'>";
118
            case \XHELP_CONTROL_TXTAREA:
119
                return "<label for='" . $this->fieldname . "'>" . $this->name . '</label>' . "<textarea name='" . $this->fieldname . "' id='" . $this->fieldname . "' cols='" . $this->fieldlength . "' rows='5'>" . $this->value . '</textarea>';
120
            case \XHELP_CONTROL_SELECT:
121
                $ret = "<label for='" . $this->fieldname . "'>" . $this->name . '</label>' . "<select name='" . $this->fieldname . "' id='" . $this->fieldname . "' size='1'>";
122
                foreach ($this->values as $key => $value) {
123
                    $ret .= "<option value='" . $key . "' " . (($this->value == $key) ? 'selected' : '') . '>' . $value . '</option>';
124
                }
125
                $ret .= '</select>';
126
127
                return $ret;
128
            case \XHELP_CONTROL_MULTISELECT:
129
                $ret = "<label for='" . $this->fieldname . "'>" . $this->name . '</label>' . "<select name='" . $this->fieldname . "' id='" . $this->fieldname . "' size='3' multiple='multiple'>";
130
                foreach ($this->values as $key => $value) {
131
                    $ret .= "<option value='" . $key . "' " . (($this->value == $key) ? 'selected' : '') . '>' . $value . '</option>';
132
                }
133
                $ret .= '</select>';
134
135
                return $ret;
136
            case \XHELP_CONTROL_YESNO:
137
                return "<label for='"
138
                       . $this->fieldname
139
                       . "'>"
140
                       . $this->name
141
                       . '</label>'
142
                       . "<input type='radio' name='"
143
                       . $this->fieldname
144
                       . "' id='"
145
                       . $this->fieldname
146
                       . "1' value='1' "
147
                       . ((1 == $this->value) ? 'checked' : '')
148
                       . '>'
149
                       . \_XHELP_TEXT_YES
150
                       . "<input type='radio' name='"
151
                       . $this->fieldname
152
                       . "' id='"
153
                       . $this->fieldname
154
                       . "0' value='0' "
155
                       . ((1 == $this->value) ? 'checked' : '')
156
                       . '>'
157
                       . \_XHELP_TEXT_NO;
158
            case \XHELP_CONTROL_RADIOBOX:
159
                $ret = "<label for='" . $this->fieldname . "'>" . $this->name . '</label>';
160
                foreach ($this->values as $key => $value) {
161
                    $ret .= "<input type='checkbox' name='" . $this->fieldname . "' id='" . $this->fieldname . "1' value='1' " . (($key == $this->value) ? 'checked' : '') . '>' . $value;
162
                }
163
164
                return $ret;
165
            case \XHELP_CONTROL_DATETIME:
166
                return "<label for='" . $this->fieldname . "'>" . $this->name . '</label>' . "<input type='text' name='" . $this->fieldname . "' id='" . $this->fieldname . "' value='" . $this->value . "' maxlength='" . $this->maxlength . "' size='" . $this->fieldlength . "'>";
167
            default:
168
                return "<label for='" . $this->fieldname . "'>" . $this->name . '</label>' . "<input type='text' name='" . $this->fieldname . "' id='" . $this->fieldname . "' value='" . $this->value . "' maxlength='" . $this->maxlength . "' size='" . $this->fieldlength . "'>";
169
        }
170
    }
171
}
172