Form::getTemplateExample()   F
last analyzed

Complexity

Conditions 18
Paths 39

Size

Total Lines 145
Code Lines 123

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 342

Importance

Changes 0
Metric Value
cc 18
eloc 123
c 0
b 0
f 0
nc 39
nop 0
dl 0
loc 145
ccs 0
cts 131
cp 0
crap 342
rs 3.8933

How to fix   Long Method    Complexity   

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 Frontend\Core\Engine;
4
5
use SpoonFilter;
6
use SpoonFormButton;
7
use SpoonFormDropdown;
8
use SpoonFormFile;
9
use SpoonFormPassword;
10
use SpoonFormRadiobutton;
11
use SpoonFormText;
12
use SpoonFormTextarea;
13
use SpoonFormTime;
14
15
/**
16
 * This is our extended version of SpoonForm.
17
 */
18
class Form extends \Common\Core\Form
19
{
20
    /**
21
     * Adds a single file field.
22
     *
23
     * @param string $name Name of the element.
24
     * @param string $class Class(es) that will be applied on the element.
25
     * @param string $classError Class(es) that will be applied on the element when an error occurs.
26
     *
27
     * @return SpoonFormFile
28
     */
29
    public function addFile($name, $class = null, $classError = null): SpoonFormFile
30
    {
31
        $name = (string) $name;
32
        $class = (string) ($class ?? 'inputFile');
33
        $classError = (string) ($classError ?? 'inputFileError error form-control-danger is-invalid');
34
35
        // create and return a file field
36
        return parent::addFile($name, $class, $classError);
37
    }
38
39
    /**
40
     * Adds a single image field.
41
     *
42
     * @param string $name The name of the element.
43
     * @param string $class Class(es) that will be applied on the element.
44
     * @param string $classError Class(es) that will be applied on the element when an error occurs.
45
     *
46
     * @return FormImage
47
     */
48
    public function addImage($name, $class = null, $classError = null): FormImage
49
    {
50
        $name = (string) $name;
51
        $class = (string) ($class ?? 'inputFile inputImage');
52
        $classError = (string) ($classError ?? 'inputFileError error form-control-danger is-invalid inputImageError');
53
54
        // add element
55
        $this->add(new FormImage($name, $class, $classError));
56
57
        return $this->getField($name);
58
    }
59
60
    /**
61
     * Generates an example template, based on the elements already added.
62
     *
63
     * @return string
64
     */
65
    public function getTemplateExample(): string
66
    {
67
        // start form
68
        $value = "\n";
69
        $value .= '{form:' . $this->getName() . "}\n";
70
71
        /**
72
         * At first all the hidden fields need to be added to this form, since
73
         * they're not shown and are best to be put right beneath the start of the form tag.
74
         */
75
        foreach ($this->getFields() as $object) {
76
            // is a hidden field
77
            if (($object instanceof \SpoonFormHidden) && $object->getName() != 'form') {
78
                $value .= "\t" . '{$hid' . str_replace('[]', '', SpoonFilter::toCamelCase($object->getName())) . "}\n";
79
            }
80
        }
81
82
        /**
83
         * Add all the objects that are NOT hidden fields. Based on the existance of some methods
84
         * errors will or will not be shown.
85
         */
86
        foreach ($this->getFields() as $object) {
87
            // NOT a hidden field
88
            if (!($object instanceof \SpoonFormHidden)) {
89
                if ($object instanceof SpoonFormButton) {
90
                    $value .= "\t" . '<p>' . "\n";
91
                    $value .= "\t\t" . '{$btn' . SpoonFilter::toCamelCase($object->getName()) . '}' . "\n";
92
                    $value .= "\t" . '</p>' . "\n\n";
93
                } elseif ($object instanceof \SpoonFormCheckbox) {
94
                    $value .= "\t" . '<p{option:chk' . SpoonFilter::toCamelCase($object->getName()) .
95
                              'Error} class="errorArea"{/option:chk' .
96
                              SpoonFilter::toCamelCase($object->getName()) . 'Error}>' . "\n";
97
                    $value .= "\t\t" . '<label for="' . $object->getAttribute('id') . '">' .
98
                              SpoonFilter::toCamelCase($object->getName()) . '</label>' . "\n";
99
                    $value .= "\t\t" . '{$chk' . SpoonFilter::toCamelCase($object->getName()) .
100
                              '} {$chk' . SpoonFilter::toCamelCase($object->getName()) . 'Error}' . "\n";
101
                    $value .= "\t" . '</p>' . "\n\n";
102
                } elseif ($object instanceof \SpoonFormMultiCheckbox) {
103
                    $value .= "\t" . '<div{option:chk' . SpoonFilter::toCamelCase($object->getName()) .
104
                              'Error} class="errorArea"{/option:chk' .
105
                              SpoonFilter::toCamelCase($object->getName()) . 'Error}>' . "\n";
106
                    $value .= "\t\t" . '<p class="label">' . SpoonFilter::toCamelCase($object->getName()) .
107
                              '</p>' . "\n";
108
                    $value .= "\t\t" . '{$chk' . SpoonFilter::toCamelCase($object->getName()) . 'Error}' . "\n";
109
                    $value .= "\t\t" . '<ul class="inputList">' . "\n";
110
                    $value .= "\t\t\t" . '{iteration:' . $object->getName() . '}' . "\n";
111
                    $value .= "\t\t\t\t" . '<li><label for="{$' . $object->getName() . '.id}">{$' .
112
                              $object->getName() . '.chk' . SpoonFilter::toCamelCase($object->getName()) .
113
                              '} {$' . $object->getName() . '.label}</label></li>' . "\n";
114
                    $value .= "\t\t\t" . '{/iteration:' . $object->getName() . '}' . "\n";
115
                    $value .= "\t\t" . '</ul>' . "\n";
116
                    $value .= "\t" . '</div>' . "\n\n";
117
                } elseif ($object instanceof SpoonFormDropdown) {
118
                    $value .= "\t" . '<p{option:ddm' .str_replace(
119
                        '[]',
120
                        '',
121
                        SpoonFilter::toCamelCase($object->getName())
122
                    ) . 'Error} class="errorArea"{/option:ddm' . str_replace(
123
                        '[]',
124
                        '',
125
                        SpoonFilter::toCamelCase($object->getName())
126
                    ) . 'Error}>' . "\n";
127
                    $value .= "\t\t" . '<label for="' . $object->getAttribute('id') . '">' . str_replace(
128
                        '[]',
129
                        '',
130
                        SpoonFilter::toCamelCase($object->getName())
131
                    ) . '</label>' . "\n";
132
                    $value .= "\t\t" . '{$ddm' . str_replace('[]', '', SpoonFilter::toCamelCase($object->getName())) .
133
                        '} {$ddm' . str_replace(
134
                            '[]',
135
                            '',
136
                            SpoonFilter::toCamelCase($object->getName())
137
                        ) . 'Error}' . "\n";
138
                    $value .= "\t" . '</p>' . "\n\n";
139
                } elseif ($object instanceof \SpoonFormImage) {
140
                    $value .= "\t" . '<p{option:file' . SpoonFilter::toCamelCase($object->getName()) .
141
                        'Error} class="errorArea"{/option:file' .
142
                        SpoonFilter::toCamelCase($object->getName()) . 'Error}>' . "\n";
143
                    $value .= "\t\t" . '<label for="' . $object->getAttribute('id') . '">' .
144
                        SpoonFilter::toCamelCase($object->getName()) . '</label>' . "\n";
145
                    $value .= "\t\t" . '{$file' . SpoonFilter::toCamelCase($object->getName()) .
146
                        '} <span class="helpTxt">{$msgHelpImageField}</span> {$file' .
147
                        SpoonFilter::toCamelCase($object->getName()) . 'Error}' . "\n";
148
                    $value .= "\t" . '</p>' . "\n\n";
149
                } elseif ($object instanceof SpoonFormFile) {
150
                    $value .= "\t" . '<p{option:file' . SpoonFilter::toCamelCase($object->getName()) .
151
                        'Error} class="errorArea"{/option:file' .
152
                        SpoonFilter::toCamelCase($object->getName()) . 'Error}>' . "\n";
153
                    $value .= "\t\t" . '<label for="' . $object->getAttribute('id') . '">' .
154
                        SpoonFilter::toCamelCase($object->getName()) . '</label>' . "\n";
155
                    $value .= "\t\t" . '{$file' . SpoonFilter::toCamelCase($object->getName()) .
156
                        '} {$file' . SpoonFilter::toCamelCase($object->getName()) . 'Error}' . "\n";
157
                    $value .= "\t" . '</p>' . "\n\n";
158
                } elseif ($object instanceof SpoonFormRadiobutton) {
159
                    $value .= "\t" . '<div{option:rbt' . SpoonFilter::toCamelCase($object->getName()) .
160
                        'Error} class="errorArea"{/option:rbt' .
161
                        SpoonFilter::toCamelCase($object->getName()) . 'Error}>' . "\n";
162
                    $value .= "\t\t" . '<p class="label">' . SpoonFilter::toCamelCase($object->getName()) .
163
                        '</p>' . "\n";
164
                    $value .= "\t\t" . '{$rbt' . SpoonFilter::toCamelCase($object->getName()) . 'Error}' . "\n";
165
                    $value .= "\t\t" . '<ul class="inputList">' . "\n";
166
                    $value .= "\t\t\t" . '{iteration:' . $object->getName() . '}' . "\n";
167
                    $value .= "\t\t\t\t" . '<li><label for="{$' . $object->getName() . '.id}">{$' .
168
                        $object->getName() . '.rbt' . SpoonFilter::toCamelCase($object->getName()) .
169
                        '} {$' . $object->getName() . '.label}</label></li>' . "\n";
170
                    $value .= "\t\t\t" . '{/iteration:' . $object->getName() . '}' . "\n";
171
                    $value .= "\t\t" . '</ul>' . "\n";
172
                    $value .= "\t" . '</div>' . "\n\n";
173
                } elseif ($object instanceof \SpoonFormDate) {
174
                    $value .= "\t" . '<p{option:txt' . SpoonFilter::toCamelCase($object->getName()) .
175
                        'Error} class="errorArea"{/option:txt' . SpoonFilter::toCamelCase($object->getName()) .
176
                        'Error}>' . "\n";
177
                    $value .= "\t\t" . '<label for="' . $object->getAttribute('id') . '">' .
178
                        SpoonFilter::toCamelCase($object->getName()) . '</label>' . "\n";
179
                    $value .= "\t\t" . '{$txt' . SpoonFilter::toCamelCase($object->getName()) .
180
                        '} <span class="helpTxt">{$msgHelpDateField}</span> {$txt' .
181
                        SpoonFilter::toCamelCase($object->getName()) . 'Error}' . "\n";
182
                    $value .= "\t" . '</p>' . "\n\n";
183
                } elseif ($object instanceof SpoonFormTime) {
184
                    $value .= "\t" . '<p{option:txt' . SpoonFilter::toCamelCase($object->getName()) .
185
                        'Error} class="errorArea"{/option:txt' . SpoonFilter::toCamelCase($object->getName()) .
186
                        'Error}>' . "\n";
187
                    $value .= "\t\t" . '<label for="' . $object->getAttribute('id') . '">' .
188
                        SpoonFilter::toCamelCase($object->getName()) . '</label>' . "\n";
189
                    $value .= "\t\t" . '{$txt' . SpoonFilter::toCamelCase($object->getName()) .
190
                        '} <span class="helpTxt">{$msgHelpTimeField}</span> {$txt' .
191
                        SpoonFilter::toCamelCase($object->getName()) . 'Error}' . "\n";
192
                    $value .= "\t" . '</p>' . "\n\n";
193
                } elseif (($object instanceof SpoonFormPassword) ||
194
                          ($object instanceof SpoonFormTextarea) ||
195
                          ($object instanceof SpoonFormText)
196
                ) {
197
                    $value .= "\t" . '<p{option:txt' . SpoonFilter::toCamelCase($object->getName()) .
198
                              'Error} class="errorArea"{/option:txt' . SpoonFilter::toCamelCase($object->getName()) .
199
                              'Error}>' . "\n";
200
                    $value .= "\t\t" . '<label for="' . $object->getAttribute('id') . '">' .
201
                              SpoonFilter::toCamelCase($object->getName()) . '</label>' . "\n";
202
                    $value .= "\t\t" . '{$txt' . SpoonFilter::toCamelCase($object->getName()) .
203
                              '} {$txt' . SpoonFilter::toCamelCase($object->getName()) . 'Error}' . "\n";
204
                    $value .= "\t" . '</p>' . "\n\n";
205
                }
206
            }
207
        }
208
209
        return $value . '{/form:' . $this->getName() . '}';
210
    }
211
212
    /**
213
     * Fetches all the values for this form as key/value pairs
214
     *
215
     * @param mixed $excluded Which elements should be excluded?
216
     *
217
     * @return array
218
     */
219
    public function getValues($excluded = ['form', 'save', '_utf8']): array
220
    {
221
        return parent::getValues($excluded);
222
    }
223
224
    /**
225
     * Parse the form
226
     *
227
     * @param TwigTemplate $tpl The template instance wherein the form will be parsed.
228
     */
229 23
    public function parse($tpl): void
230
    {
231 23
        parent::parse($tpl);
0 ignored issues
show
Bug introduced by
$tpl of type Frontend\Core\Engine\TwigTemplate is incompatible with the type SpoonTemplate expected by parameter $template of SpoonForm::parse(). ( Ignorable by Annotation )

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

231
        parent::parse(/** @scrutinizer ignore-type */ $tpl);
Loading history...
232 23
        $this->validate();
233
234
        // if the form is submitted but there was an error, assign a general error
235 23
        if ($this->isSubmitted() && !$this->isCorrect()) {
236 1
            $tpl->assign('formError', true);
237
        }
238 23
    }
239
}
240