Completed
Push — master ( a68940...ed2b3e )
by Mikael
02:38
created

FormModelElementsHTML401   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 164
Duplicated Lines 5.49 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 9
loc 164
ccs 0
cts 103
cp 0
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 101 1
B callbackSubmit() 9 46 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Anax\HTMLForm;
4
5
use Anax\DI\DIInterface;
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
16
     */
17
    public function __construct(DIInterface $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
        $elements = [
131
            "text", "password", "hidden", "file", "textarea", "select",
132
            "radio",
133
        ];
134
        foreach ($elements as $name) {
135
            $this->form->addOutput(
136
                "$name has value: "
137
                . $this->form->value($name)
138
                . "</br>"
139
            );
140
        }
141
142
        // Checkbox needs to be checked for its status
143
        $elements = [
144
            "checkbox",
145
        ];
146 View Code Duplication
        foreach ($elements as $name) {
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...
147
            $this->form->addOutput(
148
                "$name is "
149
                . ($this->form->checked($name)
150
                    ? ""
151
                    : "not ")
152
                . "checked</br>"
153
            );
154
        }
155
156
        // Select multiple returns an array
157
        $elements = [
158
            "selectm",
159
        ];
160
        foreach ($elements as $name) {
161
            $this->form->addOutput(
162
                "$name has value: "
163
                . implode($this->form->value($name), ", ")
164
                . "</br>"
165
            );
166
        }
167
168
        // Remember values during resubmit, for sake of the example
169
        $this->form->rememberValues();
170
171
        return true;
172
    }
173
}
174