Completed
Push — master ( 73de08...39d3cf )
by Gabor
03:53
created

TestForm::getMainFieldSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 55
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 9.7692
c 0
b 0
f 0
cc 1
eloc 27
nc 1
nop 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
 * WebHemi.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
namespace WebHemi\Form;
13
14
use WebHemi\Form\Element\Web;
15
16
/**
17
 * Class TestForm
18
 *
19
 * @codeCoverageIgnore - only for test purposes
20
 */
21
class TestForm extends AbstractForm
22
{
23
    /** @var  Web\FormElement */
24
    protected $form;
25
26
    /**
27
     * Returns the form container element. E.g.: for HTML forms it is the <form> tag.
28
     *
29
     * @return Web\FormElement
30
     */
31
    protected function getFormContainer()
32
    {
33
        return new Web\FormElement();
34
    }
35
36
    /**
37
     * Initialize form.
38
     *
39
     * @return void
40
     */
41
    protected function initForm()
42
    {
43
        // Hidden field.
44
        $hiddenCSRF = new Web\HiddenElement('csrf');
45
        $hiddenCSRF->setValue('Some CSRF test value');
46
47
        $xContent = <<<EOH
48
<h2>This is a static content</h2>
49
<p>
50
    Veritus invenire mei ne. No nam nullam probatus, mea te nostro delicatissimi. Ea deserunt pericula cum, no eos 
51
    tation rationibus scriptorem. Mel ut verterem invenire. Eu vim aperiam nonumes sententiae, ex quas sapientem has. 
52
    Mea autem adolescens ea, ea ridens oportere sit, ex dicta tacimates nec. Scaevola complectitur ad sed, ne ius 
53
    praesent argumentum.
54
</p>
55
<p>
56
    Pro no dicam aliquid bonorum. Inimicus imperdiet mei et. Ut hendrerit adversarium sed. Te cum everti delicata, 
57
    quis utamur argumentum pri id, oratio exerci everti eam te.
58
</p>
59
<p>
60
    No meliore deterruisset nec. An eros electram constituto duo, has reque corrumpit at. Nihil aperiri liberavisse in 
61
    has, eu qui nonumy nusquam. Mazim salutatus te nam, nobis putent mentitum mea ne. Mundi denique at ius, 
62
    illum quaestio vix id.
63
</p>
64
EOH;
65
        $fieldSetX = new Web\StaticContentElement('notice', 'Please Note');
66
        $fieldSetX->setValue($xContent);
67
68
        // Field sets
69
        $fieldSet1 = $this->getMainFieldSet();
70
        $fieldSet2 = $this->getLocationFieldSet();
71
72
        // Submit button.
73
        $submit = new Web\SubmitElement('submit', 'Login');
74
75
        // Assign elements and the field sets to the form
76
        $this->setNodes(
77
            [
0 ignored issues
show
Documentation introduced by
array($hiddenCSRF, $fiel...X, $fieldSet2, $submit) is of type array<integer,object<Web...\Web\\SubmitElement>"}>, but the function expects a array<integer,object<Web...\FormElementInterface>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
                $hiddenCSRF,
79
                $fieldSet1,
80
                $fieldSetX,
81
                $fieldSet2,
82
                $submit
83
            ]
84
        );
85
    }
86
87
    /**
88
     * Gets main field set
89
     *
90
     * @return Web\FieldSetElement
91
     */
92
    private function getMainFieldSet()
93
    {
94
        // Field set
95
        $fieldSet = new Web\FieldSetElement('info', 'Login form');
96
97
        // Text input with custom attribute
98
        $loginname = new Web\TextElement('username', 'Username');
99
        $loginname->setAttributes(['placeholder' => 'Your login name']);
100
101
        // Password input
102
        $password = new Web\PasswordElement('password', 'Password');
103
104
        // HTML5 email input
105
        $email = new Web\InputElement('email', 'Email');
106
        $email->setType('email');
107
108
        // Single-option checkbox.
109
        // For single-option radio boxes the ->setValue(1) is equals to this: ->setAttribute('checked', true);
110
        $checkboxRememberMe = new Web\CheckboxElement('remember_me', 'Remember Me');
111
        $checkboxRememberMe->setValue('1');
112
113
        // Radio group.
114
        $radioLanguage = new Web\RadioElement('language', 'Select language');
115
        $radioLanguage->setOptions(
116
            [
117
                ['label' => 'English', 'value' => 'en', 'checked' => true],
118
                ['label' => 'German', 'value' => 'de', 'checked' => true],
119
                ['label' => 'Spanish', 'value' => 'es', 'checked' => true],
120
            ]
121
        );
122
123
        // Checkbox group.
124
        $checkboxTerms = new Web\CheckboxElement('terms', 'Terms and conditions');
125
        $checkboxTerms->setOptions(
126
            [
127
                ['label' => 'I am human.', 'value' => 'human'],
128
                ['label' => 'I have read the T&C.', 'value' => 'terms_read', 'checked' => false],
129
                ['label' => 'I want newsletters.', 'value' => 'terms_newsletter', 'checked' => true],
130
            ]
131
        );
132
133
        // Assign elements to the field set
134
        $fieldSet->setNodes(
135
            [
136
                $loginname,
137
                $password,
138
                $email,
139
                $checkboxRememberMe,
140
                $radioLanguage,
141
                $checkboxTerms
142
            ]
143
        );
144
145
        return $fieldSet;
146
    }
147
148
    /**
149
     * Gets location field set.
150
     *
151
     * @return Web\FieldSetElement
152
     */
153
    private function getLocationFieldSet()
154
    {
155
        // Field set
156
        $fieldSet = new Web\FieldSetElement('location', 'Location');
157
158
        // Select box with no multi selection and with no option groups
159
        $select1 = new Web\SelectElement('country', 'I live in:');
160
        $select1->setOptions(
161
            [
162
                ['label' => 'Hungary', 'value' => 'hu'],
163
                ['label' => 'Germany', 'value' => 'de', 'checked' => true],
164
                ['label' => 'Austria', 'value' => 'at'],
165
            ]
166
        );
167
168
        // Select box with no multi selection and WITH option groups
169
        $select2 = new Web\SelectElement('dream', 'I\'d like live in:');
170
        $select2->setOptions(
171
            [
172
                ['label' => 'Anywhere', 'value' => '*'],
173
                ['label' => 'Hungary', 'value' => 'hu', 'group' => 'Europe'],
174
                ['label' => 'USA', 'value' => 'us', 'group' => 'America'],
175
                ['label' => 'Germany', 'value' => 'de', 'group' => 'Europe'],
176
                ['label' => 'Austria', 'value' => 'at', 'group' => 'Europe'],
177
                ['label' => 'Canada', 'value' => 'ca', 'group' => 'America'],
178
                ['label' => 'Switzerland', 'value' => 'ch', 'group' => 'Europe', 'checked' => true],
179
                ['label' => 'France', 'value' => 'fr', 'group' => 'Europe'],
180
                ['label' => 'Spain', 'value' => 'es', 'group' => 'Europe'],
181
                ['label' => 'Japan', 'value' => 'jp', 'group' => 'Asia'],
182
            ]
183
        );
184
185
        // Select box WITH multi selection.
186
        $select3 = new Web\SelectElement('past', 'I\'ve already lived in:');
187
        $select3->setOptions(
188
            [
189
                ['label' => 'Hungary', 'value' => 'hu', 'group' => 'Europe', 'checked' => true],
190
                ['label' => 'USA', 'value' => 'us', 'group' => 'America'],
191
                ['label' => 'Germany', 'value' => 'de', 'group' => 'Europe', 'checked' => true],
192
                ['label' => 'Austria', 'value' => 'at', 'group' => 'Europe'],
193
                ['label' => 'Canada', 'value' => 'ca', 'group' => 'America'],
194
                ['label' => 'Switzerland', 'value' => 'ch', 'group' => 'Europe'],
195
                ['label' => 'France', 'value' => 'fr', 'group' => 'Europe'],
196
                ['label' => 'Spain', 'value' => 'es', 'group' => 'Europe'],
197
                ['label' => 'Japan', 'value' => 'jp', 'group' => 'Asia'],
198
            ]
199
        )
200
            ->setAttributes(
201
                [
202
                    'multiple' => true,
203
                    'size' => 10
204
                ]
205
            );
206
207
        // Assign elements to the field set
208
        $fieldSet->setNodes(
209
            [
210
                $select1,
211
                $select2,
212
                $select3
213
            ]
214
        );
215
216
        return $fieldSet;
217
    }
218
}
219