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) { |
||
62 | |||
63 | /** |
||
64 | * Merge child field data into this form |
||
65 | */ |
||
66 | public function getSchemaDataDefaults() { |
||
79 | |||
80 | /** |
||
81 | * Returns all the sub-fields, suitable for <% loop FieldList %> |
||
82 | * |
||
83 | * @return FieldList |
||
84 | */ |
||
85 | public function FieldList() { |
||
88 | |||
89 | /** |
||
90 | * Accessor method for $this->children |
||
91 | * |
||
92 | * @return FieldList |
||
93 | */ |
||
94 | public function getChildren() { |
||
97 | |||
98 | /** |
||
99 | * @param FieldList $children |
||
100 | * @return $this |
||
101 | */ |
||
102 | public function setChildren($children) { |
||
106 | |||
107 | /** |
||
108 | * @param string $tag |
||
109 | * @return $this |
||
110 | */ |
||
111 | public function setTag($tag) { |
||
116 | |||
117 | /** |
||
118 | * @return string |
||
119 | */ |
||
120 | public function getTag() { |
||
123 | |||
124 | /** |
||
125 | * @param string $legend |
||
126 | * @return $this |
||
127 | */ |
||
128 | public function setLegend($legend) { |
||
132 | |||
133 | /** |
||
134 | * @return string |
||
135 | */ |
||
136 | public function getLegend() { |
||
139 | |||
140 | /** |
||
141 | * @deprecated |
||
142 | */ |
||
143 | public function extraClasses() { |
||
147 | |||
148 | public function extraClass() { |
||
154 | |||
155 | public function getAttributes() { |
||
166 | |||
167 | /** |
||
168 | * Add all of the non-composite fields contained within this field to the |
||
169 | * list. |
||
170 | * |
||
171 | * Sequentialisation is used when connecting the form to its data source |
||
172 | * |
||
173 | * @param array $list |
||
174 | * @param bool $saveableOnly |
||
175 | */ |
||
176 | public function collateDataFields(&$list, $saveableOnly = false) { |
||
203 | |||
204 | public function setForm($form) { |
||
214 | |||
215 | |||
216 | |||
217 | public function setDisabled($disabled) { |
||
224 | |||
225 | public function setReadonly($readonly) |
||
233 | |||
234 | public function setColumnCount($columnCount) { |
||
238 | |||
239 | public function getColumnCount() { |
||
242 | |||
243 | public function isComposite() { |
||
246 | |||
247 | public function hasData() { |
||
250 | |||
251 | public function fieldByName($name) { |
||
254 | |||
255 | /** |
||
256 | * Add a new child field to the end of the set. |
||
257 | * |
||
258 | * @param FormField |
||
259 | */ |
||
260 | public function push(FormField $field) { |
||
263 | |||
264 | /** |
||
265 | * Add a new child field to the beginning of the set. |
||
266 | * |
||
267 | * @param FormField |
||
268 | */ |
||
269 | public function unshift(FormField $field) { |
||
272 | |||
273 | /** |
||
274 | * @uses FieldList->insertBefore() |
||
275 | * |
||
276 | * @param string $insertBefore |
||
277 | * @param FormField $field |
||
278 | * @return false|FormField |
||
279 | */ |
||
280 | public function insertBefore($insertBefore, $field) { |
||
283 | |||
284 | /** |
||
285 | * @uses FieldList->insertAfter() |
||
286 | * @param string $insertAfter |
||
287 | * @param FormField $field |
||
288 | * @return false|FormField |
||
289 | */ |
||
290 | public function insertAfter($insertAfter, $field) { |
||
293 | |||
294 | /** |
||
295 | * Remove a field from this CompositeField by Name. |
||
296 | * The field could also be inside a CompositeField. |
||
297 | * |
||
298 | * @param string $fieldName The name of the field |
||
299 | * @param boolean $dataFieldOnly If this is true, then a field will only |
||
300 | * be removed if it's a data field. Dataless fields, such as tabs, will |
||
301 | * be left as-is. |
||
302 | */ |
||
303 | public function removeByName($fieldName, $dataFieldOnly = false) { |
||
306 | |||
307 | public function replaceField($fieldName, $newField) { |
||
310 | |||
311 | public function rootFieldList() { |
||
315 | |||
316 | public function __clone() { |
||
320 | |||
321 | /** |
||
322 | * Return a readonly version of this field. Keeps the composition but returns readonly |
||
323 | * versions of all the child {@link FormField} objects. |
||
324 | * |
||
325 | * @return CompositeField |
||
326 | */ |
||
327 | public function performReadonlyTransformation() { |
||
343 | |||
344 | /** |
||
345 | * Return a disabled version of this field. Keeps the composition but returns disabled |
||
346 | * versions of all the child {@link FormField} objects. |
||
347 | * |
||
348 | * @return CompositeField |
||
349 | */ |
||
350 | public function performDisabledTransformation() { |
||
369 | |||
370 | public function IsReadonly() { |
||
373 | |||
374 | /** |
||
375 | * Find the numerical position of a field within |
||
376 | * the children collection. Doesn't work recursively. |
||
377 | * |
||
378 | * @param string|FormField |
||
379 | * @return int Position in children collection (first position starts with 0). Returns FALSE if the field can't |
||
380 | * be found. |
||
381 | */ |
||
382 | View Code Duplication | public function fieldPosition($field) { |
|
401 | |||
402 | /** |
||
403 | * Transform the named field into a readonly feld. |
||
404 | * |
||
405 | * @param string|FormField |
||
406 | * @return bool |
||
407 | */ |
||
408 | public function makeFieldReadonly($field) { |
||
427 | |||
428 | public function debug() { |
||
436 | |||
437 | /** |
||
438 | * Validate this field |
||
439 | * |
||
440 | * @param Validator $validator |
||
441 | * @return bool |
||
442 | */ |
||
443 | public function validate($validator) { |
||
451 | |||
452 | } |
||
453 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: