AbstractField   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 188
Duplicated Lines 2.66 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 5
loc 188
ccs 41
cts 41
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
registerConfigKeys() 0 1 ?
A __construct() 0 6 1
A setAttributes() 0 6 2
A setRequired() 0 7 2
A labelAfterInput() 0 4 1
A getRecord() 0 4 2
A setup() 5 21 4
A getOptions() 0 10 2

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 namespace PascalKleindienst\FormListGenerator\Fields;
2
3
use AdamWathan\Form\FormBuilder;
4
5
/**
6
 * Abstract Field for Form generator
7
 * @package \PascalKleindienst\FormListGenerator\Fields
8
 */
9
abstract class AbstractField
10
{
11
    /**
12
     * @var string List field name.
13
     */
14
    public $fieldName;
15
16
    /**
17
     * @var string Display mode. Text, number
18
     */
19
    public $type = 'text';
20
21
    /**
22
     * @var string The label of the field
23
     */
24
    public $label;
25
26
    /**
27
     * @var string Specifies a default value when value is empty.
28
     */
29
    public $default;
30
31
    /**
32
     * @var string Specify a CSS class to attach to the input element.
33
     */
34
    public $cssClass;
35
36
    /**
37
     * @var string specify the form field size. Options: half, full
38
     */
39
    public $size = 'full';
40
41
    /**
42
     * @var array Raw field configuration.
43
     */
44
    public $config;
45
46
    /**
47
     * @var array Array of attributes for the input element
48
     */
49
    public $attributes = [];
50
51
    /**
52
     * @var array Options used for a select tag or radio/checkboxlist
53
     */
54
    public $options = [];
55
56
    /**
57
     * @var boolean Whether the input is disabled or not
58
     */
59
    public $disabled = false;
60
61
    /**
62
     * @var boolean Whether the input is required or not
63
     */
64
    public $required = false;
65
66
    /**
67
     * @var boolean Whether the input is readonly or not
68
     */
69
    public $readOnly = false;
70
71
    /**
72
     * @var string Description which is displayed under the input element
73
     */
74
    public $description;
75
76
    /**
77
     * @var \AdamWathan\Form\FormBuilder
78
     */
79
    protected $builder;
80
81
    /**
82
     * @var \AdamWathan\Form\Elements\Element
83
     */
84
    protected $input;
85
86
    /**
87
     * Register Config keys
88
     *
89
     * @return array
90
     */
91
    abstract protected function registerConfigKeys();
92
93
    /**
94
     * Constructor.
95
     * @param string $name
96
     * @param array $config
97
     */
98 147
    public function __construct($name, array $config)
99
    {
100 147
        $this->fieldName = $name;
101 147
        $this->config = $config;
102 147
        $this->builder = new FormBuilder();
103 147
    }
104
105
    /**
106
     * Setup the field properties
107
     *
108
     * @return void
109
     */
110 147
    public function setup()
111
    {
112
        // type
113 147
        $this->type = isset($this->config['type']) ? strtolower($this->config['type']) : $this->type;
114
115
        // save value of properties if they exist
116 147
        $configKeys = array_merge(
117 147
            $this->registerConfigKeys(),
118 147
            ['cssClass', 'default', 'description', 'label', 'readOnly', 'disabled', 'required',
119 147
            'attributes', 'options', 'size']
120 147
        );
121
        
122 147 View Code Duplication
        foreach ($configKeys as $key) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
123 147
            if (isset($this->config[$key])) {
124 129
                $this->{$key} = $this->config[$key];
125 129
            }
126 147
        }
127
128
        // css class
129 147
        $this->cssClass .= ' form-control';
130 147
    }
131
132
    /**
133
     * Set attributes for the input
134
     *
135
     * @return void
136
     */
137 81
    protected function setAttributes()
138
    {
139 81
        foreach ($this->attributes as $attr => $val) {
140 6
            $this->input->attribute($attr, $val);
141 81
        }
142 81
    }
143
144
    /**
145
     * Mark input as required if necessary
146
     *
147
     * @param string $labelSuffix add a suffix like '*' to the label
148
     * @return void
149
     */
150 72
    protected function setRequired($labelSuffix = '')
151
    {
152 72
        if ($this->required) {
153 3
            $this->label .= $labelSuffix;
154 3
            $this->input->required();
155 3
        }
156 72
    }
157
158
    /**
159
     * Add a label after the input
160
     *
161
     * @param string $label
162
     * @param string $class
163
     * @return \AdamWathan\Form\Elements\Element
164
     */
165 30
    protected function labelAfterInput($label, $class = 'd-block')
166
    {
167 30
        return $this->builder->label($label)->addClass($class)->after($this->input);
168
    }
169
170
    /**
171
     * Get the record for this field
172
     *
173
     * @param array $records
174
     * @return mixed
175
     */
176 90
    protected function getRecord(array $records)
177
    {
178 90
        return array_key_exists($this->fieldName, $records) ? $records[$this->fieldName] : null;
179
    }
180
181
    /**
182
     * Get option values for dropdown, radio and checkboxes
183
     *
184
     * @return array
185
     */
186 36
    protected function getOptions()
187
    {
188
        // Check for options callable
189 36
        $options = $this->options;
190 36
        if (is_callable($options)) {
191 3
            $options = call_user_func($options);
192 3
        }
193
194 36
        return $options;
195 6
    }
196
}
197