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 CompositeField 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 CompositeField, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class CompositeField extends FormField { |
||
13 | |||
14 | /** |
||
15 | * @var FieldList |
||
16 | */ |
||
17 | protected $children; |
||
18 | |||
19 | /** |
||
20 | * Set to true when this field is a readonly field |
||
21 | */ |
||
22 | protected $readonly; |
||
23 | |||
24 | /** |
||
25 | * @var $columnCount int Toggle different css-rendering for multiple columns |
||
26 | * ("onecolumn", "twocolumns", "threecolumns"). The content is determined |
||
27 | * by the $children-array, so wrap all items you want to have grouped in a |
||
28 | * column inside a CompositeField. |
||
29 | * Caution: Please make sure that this variable actually matches the |
||
30 | * count of your $children. |
||
31 | */ |
||
32 | protected $columnCount = null; |
||
33 | |||
34 | /** |
||
35 | * @var String custom HTML tag to render with, e.g. to produce a <fieldset>. |
||
36 | */ |
||
37 | protected $tag = 'div'; |
||
38 | |||
39 | /** |
||
40 | * @var String Optional description for this set of fields. |
||
41 | * If the {@link $tag} property is set to use a 'fieldset', this will be |
||
42 | * rendered as a <legend> tag, otherwise its a 'title' attribute. |
||
43 | */ |
||
44 | protected $legend; |
||
45 | |||
46 | protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_STRUCTURAL; |
||
47 | |||
48 | public function __construct($children = null) { |
||
65 | |||
66 | /** |
||
67 | * Returns all the sub-fields, suitable for <% loop FieldList %> |
||
68 | * |
||
69 | * @return FieldList |
||
70 | */ |
||
71 | public function FieldList() { |
||
74 | |||
75 | public function setID($id) { |
||
79 | |||
80 | /** |
||
81 | * Accessor method for $this->children |
||
82 | * |
||
83 | * @return FieldList |
||
84 | */ |
||
85 | public function getChildren() { |
||
88 | |||
89 | /** |
||
90 | * @param FieldList $children |
||
91 | */ |
||
92 | public function setChildren($children) { |
||
96 | |||
97 | /** |
||
98 | * @param string |
||
99 | */ |
||
100 | public function setTag($tag) { |
||
105 | |||
106 | /** |
||
107 | * @return string |
||
108 | */ |
||
109 | public function getTag() { |
||
112 | |||
113 | /** |
||
114 | * @param string |
||
115 | */ |
||
116 | public function setLegend($legend) { |
||
120 | |||
121 | /** |
||
122 | * @return string |
||
123 | */ |
||
124 | public function getLegend() { |
||
127 | |||
128 | /** |
||
129 | * @deprecated |
||
130 | */ |
||
131 | public function extraClasses() { |
||
135 | |||
136 | public function extraClass() { |
||
142 | |||
143 | public function getAttributes() { |
||
155 | |||
156 | /** |
||
157 | * Add all of the non-composite fields contained within this field to the |
||
158 | * list. |
||
159 | * |
||
160 | * Sequentialisation is used when connecting the form to its data source |
||
161 | */ |
||
162 | public function collateDataFields(&$list, $saveableOnly = false) { |
||
186 | |||
187 | public function setForm($form) { |
||
195 | |||
196 | public function setColumnCount($columnCount) { |
||
200 | |||
201 | public function getColumnCount() { |
||
204 | |||
205 | public function isComposite() { |
||
208 | |||
209 | public function hasData() { |
||
212 | |||
213 | public function fieldByName($name) { |
||
216 | |||
217 | /** |
||
218 | * Add a new child field to the end of the set. |
||
219 | * |
||
220 | * @param FormField |
||
221 | */ |
||
222 | public function push(FormField $field) { |
||
225 | |||
226 | /** |
||
227 | * Add a new child field to the beginning of the set. |
||
228 | * |
||
229 | * @param FormField |
||
230 | */ |
||
231 | public function unshift(FormField $field) { |
||
234 | |||
235 | /** |
||
236 | * @uses FieldList->insertBefore() |
||
237 | */ |
||
238 | public function insertBefore($insertBefore, $field) { |
||
243 | |||
244 | /** |
||
245 | * @uses FieldList->insertAfter() |
||
246 | */ |
||
247 | public function insertAfter($insertAfter, $field) { |
||
252 | |||
253 | /** |
||
254 | * Remove a field from this CompositeField by Name. |
||
255 | * The field could also be inside a CompositeField. |
||
256 | * |
||
257 | * @param string $fieldName The name of the field |
||
258 | * @param boolean $dataFieldOnly If this is true, then a field will only |
||
259 | * be removed if it's a data field. Dataless fields, such as tabs, will |
||
260 | * be left as-is. |
||
261 | */ |
||
262 | public function removeByName($fieldName, $dataFieldOnly = false) { |
||
265 | |||
266 | public function replaceField($fieldName, $newField) { |
||
269 | |||
270 | public function rootFieldList() { |
||
274 | |||
275 | /** |
||
276 | * Return a readonly version of this field. Keeps the composition but returns readonly |
||
277 | * versions of all the child {@link FormField} objects. |
||
278 | * |
||
279 | * @return CompositeField |
||
280 | */ |
||
281 | public function performReadonlyTransformation() { |
||
296 | |||
297 | /** |
||
298 | * Return a disabled version of this field. Keeps the composition but returns disabled |
||
299 | * versions of all the child {@link FormField} objects. |
||
300 | * |
||
301 | * @return CompositeField |
||
302 | */ |
||
303 | public function performDisabledTransformation() { |
||
321 | |||
322 | public function IsReadonly() { |
||
325 | |||
326 | /** |
||
327 | * Find the numerical position of a field within |
||
328 | * the children collection. Doesn't work recursively. |
||
329 | * |
||
330 | * @param string|FormField |
||
331 | * @return int Position in children collection (first position starts with 0). Returns FALSE if the field can't |
||
332 | * be found. |
||
333 | */ |
||
334 | View Code Duplication | public function fieldPosition($field) { |
|
346 | |||
347 | /** |
||
348 | * Transform the named field into a readonly feld. |
||
349 | * |
||
350 | * @param string|FormField |
||
351 | */ |
||
352 | public function makeFieldReadonly($field) { |
||
374 | |||
375 | public function debug() { |
||
383 | |||
384 | /** |
||
385 | * Validate this field |
||
386 | * |
||
387 | * @param Validator $validator |
||
388 | * @return bool |
||
389 | */ |
||
390 | public function validate($validator) { |
||
397 | |||
398 | } |
||
399 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.