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 | public function __construct(array $params) |
||
69 | |||
70 | protected function setForm() |
||
74 | |||
75 | protected function setModel() |
||
86 | |||
87 | protected function setCommonAttributes() |
||
100 | |||
101 | protected function setSpecificAttributes() |
||
105 | |||
106 | protected function setName() |
||
112 | |||
113 | protected function setLabel() |
||
134 | |||
135 | protected function setRequired() |
||
141 | |||
142 | protected function setDisabled() |
||
148 | |||
149 | protected function setReadonly() |
||
155 | |||
156 | protected function setValue() |
||
170 | |||
171 | View Code Duplication | protected function setAutocomplete() |
|
180 | |||
181 | protected function setDesc() |
||
187 | |||
188 | protected function setHelp() |
||
194 | |||
195 | protected function setClass() |
||
210 | |||
211 | protected function setDefaultClass() |
||
216 | } |
||
217 |
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.