FormElementFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 59
ccs 5
cts 6
cp 0.8333
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 0 48 4
1
<?php
2
3
namespace Anax\HTMLForm;
4
5
/**
6
 * Factory to create form elements.
7
 */
8
class FormElementFactory
9
{
10
    /**
11
     * Create a formelement from an array, factory returns the correct
12
     * instance.
13
     *
14
     * @param string $name       name of the element.
15
     * @param array  $attributes to use when creating the element.
16
     *
17
     * @return the instance of the form element.
0 ignored issues
show
Bug introduced by
The type Anax\HTMLForm\the was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
     */
19 4
    public static function create($name, $attributes)
20
    {
21
        // Not supported is type=image, <button>, list, output, select-optgroup
22
        $types = [
23
24
            // Standard HTML 4.01
25 4
            'text'              => '\Anax\HTMLForm\FormElementText',
26
            'file'              => '\Anax\HTMLForm\FormElementFile',
27
            'password'          => '\Anax\HTMLForm\FormElementPassword',
28
            'hidden'            => '\Anax\HTMLForm\FormElementHidden',
29
            'textarea'          => '\Anax\HTMLForm\FormElementTextarea',
30
            'radio'             => '\Anax\HTMLForm\FormElementRadio',
31
            'checkbox'          => '\Anax\HTMLForm\FormElementCheckbox',
32
            'select'            => '\Anax\HTMLForm\FormElementSelect',
33
            'select-multiple'   => '\Anax\HTMLForm\FormElementSelectMultiple',
34
            'submit'            => '\Anax\HTMLForm\FormElementSubmit',
35
            'reset'             => '\Anax\HTMLForm\FormElementReset',
36
            'button'            => '\Anax\HTMLForm\FormElementButton',
37
38
            // HTML5
39
            'color'             => '\Anax\HTMLForm\FormElementColor',
40
            'date'              => '\Anax\HTMLForm\FormElementDate',
41
            'number'            => '\Anax\HTMLForm\FormElementNumber',
42
            'range'             => '\Anax\HTMLForm\FormElementRange',
43
            'tel'               => '\Anax\HTMLForm\FormElementTel',
44
            'email'             => '\Anax\HTMLForm\FormElementEmail',
45
            'url'               => '\Anax\HTMLForm\FormElementUrl',
46
            'search'            => '\Anax\HTMLForm\FormElementSearch',
47
            'file-multiple'     => '\Anax\HTMLForm\FormElementFileMultiple',
48
            'datetime'          => '\Anax\HTMLForm\FormElementDatetime',
49
            'datetime-local'    => '\Anax\HTMLForm\FormElementDatetimeLocal',
50
            'month'             => '\Anax\HTMLForm\FormElementMonth',
51
            'time'              => '\Anax\HTMLForm\FormElementTime',
52
            'week'              => '\Anax\HTMLForm\FormElementWeek',
53
54
            // Custom
55
            'search-widget'     => '\Anax\HTMLForm\FormElementSearchWidget',
56
            'checkbox-multiple' => '\Anax\HTMLForm\FormElementCheckboxMultiple',
57
            // Address
58
        ];
59
60
        // $attributes['type'] must contain a valid type creating an object
61
        // to succeed.
62 4
        $type = isset($attributes['type']) ? $attributes['type'] : null;
63 4
        if (!($type && isset($types[$type]))) {
64
            throw new Exception("Form element does not exists and can not be created: $name - $type");
65
        }
66 4
        return new $types[$type]($name, $attributes);
67
    }
68
}
69