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:
Complex classes like FormElement often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FormElement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | abstract class FormElement |
||
9 | { |
||
10 | use GluesAttributes; |
||
11 | |||
12 | /** @var array */ |
||
13 | protected $params; |
||
14 | |||
15 | /** @var Form */ |
||
16 | protected $form; |
||
17 | |||
18 | /** @var object */ |
||
19 | public $model = null; |
||
20 | |||
21 | /** @var string */ |
||
22 | public $id; |
||
23 | |||
24 | /** @var string */ |
||
25 | public $name; |
||
26 | |||
27 | /** @var mixed */ |
||
28 | public $value; |
||
29 | |||
30 | /** @var string */ |
||
31 | public $label; |
||
32 | |||
33 | /** @var array */ |
||
34 | public $class = []; |
||
35 | |||
36 | /** @var array */ |
||
37 | public $labelClass = []; |
||
38 | |||
39 | /** @var bool */ |
||
40 | public $required = false; |
||
41 | |||
42 | /** @var bool */ |
||
43 | public $disabled = false; |
||
44 | |||
45 | /** @var bool */ |
||
46 | public $readonly = false; |
||
47 | |||
48 | /** @var bool */ |
||
49 | public $autocomplete; |
||
50 | |||
51 | /** @var array */ |
||
52 | public $attributesList = []; |
||
53 | |||
54 | /** @var string */ |
||
55 | public $desc; |
||
56 | |||
57 | /** @var string */ |
||
58 | public $help; |
||
59 | |||
60 | /** @var array */ |
||
61 | public $customAttributes; |
||
62 | |||
63 | public function __construct(array $params) |
||
72 | |||
73 | protected function setForm() |
||
77 | |||
78 | protected function setModel() |
||
89 | |||
90 | protected function setCommonAttributes() |
||
106 | |||
107 | protected function setSpecificAttributes() |
||
111 | |||
112 | protected function setName() |
||
118 | |||
119 | protected function setLabel() |
||
140 | |||
141 | protected function setRequired() |
||
147 | |||
148 | protected function setDisabled() |
||
154 | |||
155 | protected function setReadonly() |
||
161 | |||
162 | protected function setValue() |
||
176 | |||
177 | View Code Duplication | protected function setAutocomplete() |
|
186 | |||
187 | protected function setDesc() |
||
193 | |||
194 | protected function setHelp() |
||
200 | |||
201 | protected function setClass() |
||
216 | |||
217 | protected function setDefaultClass() |
||
222 | |||
223 | protected function setCustomAttributes() |
||
229 | } |
||
230 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.