FormModelElementsHTML401::__construct()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 97
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 67
nc 1
nop 1
dl 0
loc 97
ccs 0
cts 8
cp 0
crap 2
rs 8.72
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
2
3
namespace Anax\HTMLForm;
4
5
use Psr\Container\ContainerInterface;
6
7
/**
8
 * Example of FormModel implementation.
9
 */
10
class FormModelElementsHTML401 extends FormModel
11
{
12
    /**
13
     * Constructor injects with DI container.
14
     *
15
     * @param Anax\DI\DIInterface $di a service container
0 ignored issues
show
Bug introduced by
The type Anax\HTMLForm\Anax\DI\DIInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
     */
17
    public function __construct(ContainerInterface $di)
18
    {
19
        parent::__construct($di);
20
        $this->form->create(
21
            [
22
                "id" => __CLASS__,
23
                "legend" => "Legend",
24
            ],
25
            [
26
                "text" => [
27
                    "type"        => "text",
28
                    "description" => "Here you can place a description.",
29
                    "placeholder" => "Here is a placeholder",
30
                ],
31
                        
32
                "password" => [
33
                    "type"        => "password",
34
                    "description" => "Here you can place a description.",
35
                    "placeholder" => "Here is a placeholder",
36
                ],
37
38
                "hidden" => [
39
                    "type"        => "hidden",
40
                    "value"       => "secret value",
41
                ],
42
43
                "file" => [
44
                    "type"        => "file",
45
                    "description" => "Here you can place a description.",
46
                ],
47
48
                "textarea" => [
49
                    "type"        => "textarea",
50
                    "description" => "Here you can place a description.",
51
                    "placeholder" => "Here is a placeholder",
52
                ],
53
54
                "radio" => [
55
                    "type"        => "radio",
56
                    "label"       => "What is your preferred choice of fruite?",
57
                    "description" => "Here you can place a description.",
58
                    "values"      => [
59
                        "tomato",
60
                        "potato",
61
                        "apple",
62
                        "pear",
63
                        "banana"
64
                    ],
65
                    "checked"     => "potato",
66
                ],
67
68
                "checkbox" => [
69
                    "type"        => "checkbox",
70
                    "description" => "Here you can place a description.",
71
                ],
72
73
                "select" => [
74
                    "type"        => "select",
75
                    "label"       => "Select your fruite:",
76
                    "description" => "Here you can place a description.",
77
                    "options"     => [
78
                        "tomato" => "tomato",
79
                        "potato" => "potato",
80
                        "apple"  => "apple",
81
                        "pear"   => "pear",
82
                        "banana" => "banana",
83
                    ],
84
                    "value"    => "potato",
85
                ],
86
87
                "selectm" => [
88
                    "type"        => "select-multiple",
89
                    "label"       => "Select one or more fruite:",
90
                    "description" => "Here you can place a description.",
91
                    "size"        => 6,
92
                    "options"     => [
93
                        "tomato" => "tomato",
94
                        "potato" => "potato",
95
                        "apple"  => "apple",
96
                        "pear"   => "pear",
97
                        "banana" => "banana",
98
                    ],
99
                    "checked"   => ["potato", "pear"],
100
                ],
101
102
                "reset" => [
103
                    "type"      => "reset",
104
                ],
105
106
                "button" => [
107
                    "type"      => "button",
108
                ],
109
110
                "submit" => [
111
                    "type" => "submit",
112
                    "value" => "Submit",
113
                    "callback" => [$this, "callbackSubmit"]
114
                ],
115
            ]
116
        );
117
    }
118
119
120
121
    /**
122
     * Callback for submit-button which should return true if it could
123
     * carry out its work and false if something failed.
124
     *
125
     * @return boolean true if okey, false if something went wrong.
126
     */
127
    public function callbackSubmit()
128
    {
129
        // These return a single value
130
        // Type checkbox returns true if checked
131
        $elements = [
132
            "text", "password", "hidden", "file", "textarea", "select",
133
            "radio", "checkbox",
134
        ];
135
        foreach ($elements as $name) {
136
            $this->form->addOutput(
137
                "$name has value: "
138
                . $this->form->value($name)
139
                . "</br>"
140
            );
141
        }
142
143
        // Select multiple returns an array
144
        $elements = [
145
            "selectm",
146
        ];
147
        foreach ($elements as $name) {
148
            $this->form->addOutput(
149
                "$name has value: "
150
                . implode($this->form->value($name), ", ")
0 ignored issues
show
Bug introduced by
', ' of type string is incompatible with the type array expected by parameter $pieces of implode(). ( Ignorable by Annotation )

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

150
                . implode($this->form->value($name), /** @scrutinizer ignore-type */ ", ")
Loading history...
151
                . "</br>"
152
            );
153
        }
154
155
        // Remember values during resubmit, useful when failing (retunr false)
156
        // and asking the user to resubmit the form.
157
        $this->form->rememberValues();
158
159
        return true;
160
    }
161
}
162