Passed
Push — master ( f32c79...edabcd )
by Gabor
04:07
created

IndexAction::getTestForm()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 94
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 94
rs 8.4378
c 0
b 0
f 0
cc 1
eloc 66
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 7.1
6
 *
7
 * @copyright 2012 - 2017 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
declare(strict_types = 1);
13
14
namespace WebHemi\Middleware\Action\Website;
15
16
use WebHemi\Form\Html\HtmlMultipleFormElement;
17
use WebHemi\Middleware\AbstractMiddlewareAction;
18
use WebHemi\Form\Html\HtmlForm;
19
use WebHemi\Form\Html\HtmlFormElement;
20
use WebHemi\Form\Html\Html5FormElement;
21
22
/**
23
 * Class IndexAction
24
 */
25
class IndexAction extends AbstractMiddlewareAction
26
{
27
    /**
28
     * Gets template map name or template file path.
29
     *
30
     * @return string
31
     */
32
    public function getTemplateName() : string
33
    {
34
        return 'website-index';
35
    }
36
37
    /**
38
     * Gets template data.
39
     *
40
     * @return array
41
     */
42
    public function getTemplateData() : array
43
    {
44
        $form = $this->getTestForm();
45
46
        $data = [];
47
48
        if ($this->request->getMethod() == 'POST') {
49
            $data = $this->request->getParsedBody();
50
            $form->loadData($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $this->request->getParsedBody() on line 49 can also be of type null or object; however, WebHemi\Form\Html\HtmlForm::loadData() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
51
        }
52
53
        return [
54
            'form' => $form,
55
            'data' => $data,
56
        ];
57
    }
58
59
    /**
60
     * @return HtmlForm
61
     */
62
    private function getTestForm()
63
    {
64
        // Test refactore Form elements
65
        $singleCheckbox = new HtmlFormElement(
66
            HtmlFormElement::HTML_ELEMENT_INPUT_CHECKBOX,
67
            'accept',
68
            'Accept terms of usage.',
69
            [true]
70
        );
71
72
        $multiCheckbox = new HtmlFormElement(
73
            HtmlFormElement::HTML_ELEMENT_INPUT_CHECKBOX,
74
            'my_locations',
75
            'I have already been...',
76
            ['uk', 'eu'],
77
            [
78
                'The United States' => 'us',
79
                'European Union' => 'eu',
80
                'United Kingdom' => 'uk'
81
            ]
82
        );
83
84
        $radioGroup = new HtmlFormElement(
85
            HtmlFormElement::HTML_ELEMENT_INPUT_RADIO,
86
            'curent_lang',
87
            'Current language',
88
            ['hu-HU'],
89
            [
90
                'English' => 'en-GB',
91
                'German' => 'de-DE',
92
                'Hungarian' => 'hu-HU'
93
            ]
94
        );
95
96
        $select = new HtmlMultipleFormElement(
97
            HtmlMultipleFormElement::HTML_ELEMENT_SELECT,
98
            'something',
99
            null,
100
            ['australia', 'india'],
101
            [
102
                'Default' => [
103
                    'Europe' => 'europe',
104
                    'America' => 'america',
105
                    'Australia' => 'australia'
106
                ],
107
                'Africa' => 'africa',
108
                'Asia' => 'asia',
109
                'Antarctica' => 'antarctica',
110
                'India' => 'india'
111
            ]
112
        );
113
        $select->setMultiple(true);
114
115
        $form = new HtmlForm('test', '', 'POST');
116
        $form
117
            ->addElement(
118
                new HtmlFormElement(
119
                    HtmlFormElement::HTML_ELEMENT_INPUT_HIDDEN,
120
                    'csrf',
121
                    null,
122
                    [md5('something')]
123
                )
124
            )
125
            ->addElement(
126
                new HtmlFormElement(
127
                    HtmlFormElement::HTML_ELEMENT_INPUT_TEXT,
128
                    'name',
129
                    'Login name',
130
                    ['Joker']
131
                )
132
            )
133
            ->addElement(
134
                new HtmlFormElement(
135
                    HtmlFormElement::HTML_ELEMENT_INPUT_PASSWORD,
136
                    'password',
137
                    'Password'
138
                )
139
            )
140
            ->addElement($singleCheckbox)
141
            ->addElement($multiCheckbox)
142
            ->addElement($radioGroup)
143
            ->addElement($select)
144
            ->addElement(
145
                new Html5FormElement(Html5FormElement::HTML_ELEMENT_INPUT_NUMBER, 'num', 'Num', [4], [1, 16])
146
            )
147
            ->addElement(
148
                new Html5FormElement(Html5FormElement::HTML_ELEMENT_INPUT_RANGE, 'range', 'Range', [4], [1, 6, 0.2])
149
            )
150
            ->addElement(
151
                new HtmlFormElement(HtmlFormElement::HTML_ELEMENT_INPUT_SUBMIT, 'submit', 'Submit')
152
            );
153
154
        return $form;
155
    }
156
}
157