Passed
Push — master ( 68d467...3da7f9 )
by Mikael
02:33
created

FormModelStyle   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 164
Duplicated Lines 89.63 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 147
loc 164
ccs 0
cts 93
cp 0
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 110 110 1
B callbackSubmit() 37 37 3

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 FormModelStyle extends FormModel
11
{
12
    /**
13
     * Constructor injects with DI container.
14
     *
15
     * @param Anax\DI\DIInterface $di a service container
16
     */
17 View Code Duplication
    public function __construct(DIInterface $di)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
18
    {
19
        parent::__construct($di);
20
        $this->form->create(
21
            [
22
                "id" => __CLASS__,
23
                //"class" => "htmlform class1 class2",
24
                //"use_fieldset" => false,
25
                "legend" => "Legend",
26
                //"wrapper-element" => "div",
27
                //"br-after-label" => true,
28
            ],
29
            [
30
                "text" => [
31
                    "type"        => "text",
32
                    "description" => "Here you can place a description.",
33
                    "placeholder" => "Here is a placeholder",
34
                    //"wrapper-element" => "div",
35
                    //"wrapper-class"   => "wclass",
36
                    //"class"           => "specific",
37
                    //"br-after-label" => false,
38
                ],
39
                        
40
                "password" => [
41
                    "type"        => "password",
42
                    "description" => "Here you can place a description.",
43
                    "placeholder" => "Here is a placeholder",
44
                ],
45
46
                "hidden" => [
47
                    "type"        => "hidden",
48
                    "value"       => "secret value",
49
                ],
50
51
                "file" => [
52
                    "type"        => "file",
53
                    "description" => "Here you can place a description.",
54
                ],
55
56
                "textarea" => [
57
                    "type"        => "textarea",
58
                    "description" => "Here you can place a description.",
59
                    "placeholder" => "Here is a placeholder",
60
                ],
61
62
                "radio" => [
63
                    "type"        => "radio",
64
                    "label"       => "What is your preferred choice of fruite?",
65
                    "description" => "Here you can place a description.",
66
                    "values"      => [
67
                        "tomato",
68
                        "potato",
69
                        "apple",
70
                        "pear",
71
                        "banana"
72
                    ],
73
                    "checked"     => "potato",
74
                ],
75
76
                "checkbox" => [
77
                    "type"        => "checkbox",
78
                    "description" => "Here you can place a description.",
79
                ],
80
81
                "select" => [
82
                    "type"        => "select",
83
                    "label"       => "Select your fruite:",
84
                    "description" => "Here you can place a description.",
85
                    "options"     => [
86
                        "tomato" => "tomato",
87
                        "potato" => "potato",
88
                        "apple"  => "apple",
89
                        "pear"   => "pear",
90
                        "banana" => "banana",
91
                    ],
92
                    "value"    => "potato",
93
                ],
94
95
                "selectm" => [
96
                    "type"        => "select-multiple",
97
                    "label"       => "Select one or more fruite:",
98
                    "description" => "Here you can place a description.",
99
                    "size"        => 6,
100
                    "options"     => [
101
                        "tomato" => "tomato",
102
                        "potato" => "potato",
103
                        "apple"  => "apple",
104
                        "pear"   => "pear",
105
                        "banana" => "banana",
106
                    ],
107
                    "checked"   => ["potato", "pear"],
108
                ],
109
110
                "reset" => [
111
                    "type"      => "reset",
112
                ],
113
114
                "button" => [
115
                    "type"      => "button",
116
                    "onclick"   => "alert('hej');"
117
                ],
118
119
                "submit" => [
120
                    "type" => "submit",
121
                    "value" => "Submit",
122
                    "callback" => [$this, "callbackSubmit"]
123
                ],
124
            ]
125
        );
126
    }
127
128
129
130
    /**
131
     * Callback for submit-button which should return true if it could
132
     * carry out its work and false if something failed.
133
     *
134
     * @return boolean true if okey, false if something went wrong.
135
     */
136 View Code Duplication
    public function callbackSubmit()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
137
    {
138
        // These return a single value
139
        // Type checkbox returns true if checked
140
        $elements = [
141
            "text", "password", "hidden", "file", "textarea", "select",
142
            "radio", "checkbox",
143
        ];
144
        foreach ($elements as $name) {
145
            $this->form->addOutput(
146
                "$name has value: "
147
                . $this->form->value($name)
148
                . "</br>"
149
            );
150
        }
151
152
        // Select multiple returns an array
153
        $elements = [
154
            "selectm",
155
        ];
156
        foreach ($elements as $name) {
157
            $this->form->addOutput(
158
                "$name has value: "
159
                . implode($this->form->value($name), ", ")
160
                . "</br>"
161
            );
162
        }
163
164
        // Set <output> class
165
        $this->form->setOutputClass("info");
166
167
        // Remember values during resubmit, useful when failing (retunr false)
168
        // and asking the user to resubmit the form.
169
        $this->form->rememberValues();
170
171
        return true;
172
    }
173
}
174