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; |
||
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) |
|
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) { |
|
|||
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() |
|
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 = '') |
|
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') |
|
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) |
|
180 | |||
181 | /** |
||
182 | * Get option values for dropdown, radio and checkboxes |
||
183 | * |
||
184 | * @return array |
||
185 | */ |
||
186 | 36 | protected function getOptions() |
|
196 | } |
||
197 |
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.