Total Complexity | 67 |
Total Lines | 632 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like BuildableFieldList 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.
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 BuildableFieldList, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class BuildableFieldList extends FieldList |
||
35 | { |
||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $defaultTab = null; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $currentTab = null; |
||
45 | |||
46 | /** |
||
47 | * @var FieldGroup |
||
48 | */ |
||
49 | protected $currentGroup = null; |
||
50 | |||
51 | /** |
||
52 | * The entity scope that will be used to attempt translation |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $i18nEntity = 'Global'; |
||
56 | |||
57 | /** |
||
58 | * @var boolean |
||
59 | */ |
||
60 | protected $placeholderAsLabel = false; |
||
61 | |||
62 | /** |
||
63 | * Returns an instance of BuildableFieldList from a FieldList |
||
64 | * |
||
65 | * @param FieldList $fields |
||
66 | * @return $this |
||
67 | */ |
||
68 | public static function fromFieldList(FieldList $fields = null) |
||
69 | { |
||
70 | if ($fields === null) { |
||
71 | return new self(); |
||
72 | } |
||
73 | $arr = $fields->toArray(); |
||
74 | return new self($arr); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Slightly improve way to normalize titles in forms |
||
79 | * |
||
80 | * @param string $name |
||
81 | * @param string $title |
||
82 | * @return string |
||
83 | */ |
||
84 | protected function normalizeTitle($name, $title = "") |
||
85 | { |
||
86 | if ($title === null) { |
||
87 | // For items list like [idx, name] |
||
88 | if (is_array($name)) { |
||
89 | $name = $name[1]; |
||
90 | } |
||
91 | $fallback = FormField::name_to_label($name); |
||
92 | $fallback = str_replace(['[', ']', '_'], ' ', $fallback); |
||
93 | // Attempt translation |
||
94 | $validKey = str_replace(['[', ']', '_'], '', $name); |
||
95 | $title = _t($this->i18nEntity . '.' . $validKey, $fallback); |
||
96 | } |
||
97 | return $title; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Apply attributes to a form object |
||
102 | * |
||
103 | * @param FormField $object |
||
104 | * @param array $attributes |
||
105 | * @return FormField |
||
106 | */ |
||
107 | protected function applyAttributes($object, $attributes) |
||
108 | { |
||
109 | foreach ($attributes as $k => $v) { |
||
110 | if ($k == 'class') { |
||
111 | $object->addExtraClass($v); |
||
112 | } elseif ($k == 'description') { |
||
113 | $object->setDescription($v); |
||
114 | } elseif ($k == 'options') { |
||
115 | $object->setSource($v); |
||
116 | } elseif ($k == 'empty') { |
||
117 | $object->setHasEmptyDefault($v); |
||
118 | } elseif ($k == 'value') { |
||
119 | $object->setValue($v); |
||
120 | } else { |
||
121 | $object->setAttribute($k, $v); |
||
122 | } |
||
123 | } |
||
124 | if ($this->placeholderAsLabel) { |
||
125 | $object->setAttribute('placeholder', $object->Title()); |
||
126 | $object->setTitle(''); |
||
127 | } |
||
128 | return $object; |
||
129 | } |
||
130 | |||
131 | protected function pushOrAddToTab($field) |
||
132 | { |
||
133 | // if we have a default tab set, make sure it's set to active |
||
134 | if (!$this->currentTab && $this->defaultTab) { |
||
135 | $this->currentTab = $this->defaultTab; |
||
136 | } |
||
137 | // Groups have priority |
||
138 | if ($this->currentGroup) { |
||
139 | $this->currentGroup->push($field); |
||
140 | } elseif ($this->currentTab) { |
||
141 | $this->addFieldToTab('Root.' . $this->currentTab, $field); |
||
142 | } else { |
||
143 | $this->push($field); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @param string $name |
||
149 | * @return GridField |
||
150 | */ |
||
151 | public function getGridField($name) |
||
152 | { |
||
153 | $gridfield = $this->dataFieldByName($name); |
||
154 | if (!$gridfield || !$gridfield instanceof GridField) { |
||
155 | return null; |
||
156 | } |
||
157 | return $gridfield; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Quickly add an action to a list |
||
162 | * |
||
163 | * @param string $name |
||
164 | * @param string $title |
||
165 | * @param array $attributes |
||
166 | * @return FormAction |
||
167 | */ |
||
168 | public function addAction($name, $title = "", $attributes = []) |
||
169 | { |
||
170 | return $this->addField(FormAction::class, $name, $title, $attributes); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Push a FormField already defined |
||
175 | * |
||
176 | * @param FormField $field |
||
177 | * @return FormField |
||
178 | */ |
||
179 | public function pushField(FormField $field) |
||
180 | { |
||
181 | $this->pushOrAddToTab($field); |
||
182 | return $field; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Add a field to the list |
||
187 | * |
||
188 | * @param string $class |
||
189 | * @param string $name |
||
190 | * @param string $title |
||
191 | * @param array $attributes |
||
192 | * @return FormField |
||
193 | */ |
||
194 | public function addField($class, $name, $title = "", $attributes = []) |
||
195 | { |
||
196 | $title = $this->normalizeTitle($name, $title); |
||
197 | $field = $class::create($name, $title); |
||
198 | $field = $this->applyAttributes($field, $attributes); |
||
199 | $this->pushOrAddToTab($field); |
||
200 | return $field; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @param string $name |
||
205 | * @param string $title |
||
206 | * @return HeaderField |
||
207 | */ |
||
208 | public function addHeader($title, $level = 2) |
||
209 | { |
||
210 | static $i = 0; |
||
211 | $i++; |
||
212 | $field = HeaderField::create("H_$i", $title, $level); |
||
213 | $this->pushOrAddToTab($field); |
||
214 | return $field; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @param string $content |
||
219 | * @param string $name |
||
220 | * @return LiteralField |
||
221 | */ |
||
222 | public function addLiteral($content, $name = null) |
||
223 | { |
||
224 | static $i = 0; |
||
225 | if ($name === null) { |
||
226 | $i++; |
||
227 | $name = "L_$i"; |
||
228 | } |
||
229 | $field = LiteralField::create($name, $content); |
||
230 | $this->pushOrAddToTab($field); |
||
231 | return $field; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @param string $name |
||
236 | * @param string $title |
||
237 | * @param array $attributes |
||
238 | * @return ReadonlyField |
||
239 | */ |
||
240 | public function addReadonly($name, $title = null, $attributes = []) |
||
241 | { |
||
242 | return $this->addField(ReadonlyField::class, $name, $title, $attributes); |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * TODO: extract |
||
247 | * @param string $content |
||
248 | * @param string $name |
||
249 | * @param string $type |
||
250 | * @return \LeKoala\Base\Forms\AlertField |
||
251 | */ |
||
252 | public function addAlert($content, $type = null, $name = null) |
||
253 | { |
||
254 | static $i = 0; |
||
255 | if ($name === null) { |
||
256 | $i++; |
||
257 | $name = "A_$i"; |
||
258 | } |
||
259 | $field = \LeKoala\Base\Forms\AlertField::create($name, $content, $type); |
||
260 | $this->pushOrAddToTab($field); |
||
261 | return $field; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @param string $name Name without id since it's used as $record->{"{$fieldname}ID"} = $id; |
||
266 | * @param string $title |
||
267 | * @param array $attributes |
||
268 | * @return UploadField |
||
269 | */ |
||
270 | public function addUpload($name = "Image", $title = null, $attributes = []) |
||
271 | { |
||
272 | return $this->addField(UploadField::class, $name, $title, $attributes); |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * @param string $name Name without id since it's used as $record->{"{$fieldname}ID"} = $id; |
||
277 | * @param string $title |
||
278 | * @param array $attributes |
||
279 | * @return FileField |
||
280 | */ |
||
281 | public function addFile($name = "Image", $title = null, $attributes = []) |
||
282 | { |
||
283 | return $this->addField(FileField::class, $name, $title, $attributes); |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * @param string $name Name without id since it's used as $record->{"{$fieldname}ID"} = $id; |
||
288 | * @param string $title |
||
289 | * @param array $attributes |
||
290 | * @return \LeKoala\Base\Forms\FilePondField |
||
291 | */ |
||
292 | public function addFilePond($name = "Image", $title = null, $attributes = []) |
||
293 | { |
||
294 | return $this->addField(\LeKoala\Base\Forms\FilePondField::class, $name, $title, $attributes); |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * @param string $name Name without id since it's used as $record->{"{$fieldname}ID"} = $id; |
||
299 | * @param string $title |
||
300 | * @param array $attributes |
||
301 | * @return \LeKoala\Base\Forms\FilePondField |
||
302 | */ |
||
303 | public function addSingleFilePond($name = "Image", $title = null, $attributes = []) |
||
304 | { |
||
305 | $fp = $this->addField(\LeKoala\Base\Forms\FilePondField::class, $name, $title, $attributes); |
||
306 | $fp->setAllowedMaxFileNumber(1); |
||
307 | return $fp; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @param string $name |
||
312 | * @param string $title |
||
313 | * @param array $attributes |
||
314 | * @return InputMaskField |
||
315 | */ |
||
316 | public function addInputMask($name, $title = null, $attributes = []) |
||
317 | { |
||
318 | return $this->addField(InputMaskField::class, $name, $title, $attributes); |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * @param string $name |
||
323 | * @param string $title |
||
324 | * @param array $attributes |
||
325 | * @return CheckboxField |
||
326 | */ |
||
327 | public function addCheckbox($name = "IsEnabled", $title = null, $attributes = []) |
||
328 | { |
||
329 | return $this->addField(CheckboxField::class, $name, $title, $attributes); |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * @param string $name |
||
334 | * @param string $title |
||
335 | * @param array $src |
||
336 | * @param array $attributes |
||
337 | * @return CheckboxSetField |
||
338 | */ |
||
339 | public function addCheckboxset($name = "Options", $title = null, $src = [], $attributes = []) |
||
340 | { |
||
341 | $attributes['options'] = $src; |
||
342 | return $this->addField(CheckboxSetField::class, $name, $title, $attributes); |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @param array $attributes |
||
347 | * @param string $name |
||
348 | * @return FieldGroup |
||
349 | */ |
||
350 | public function addFieldGroup($attributes = [], $name = null) |
||
351 | { |
||
352 | static $i = 0; |
||
353 | if ($name === null) { |
||
354 | $i++; |
||
355 | $name = "Group_$i"; |
||
356 | } |
||
357 | return $this->addField(FieldGroup::class, $name, null, $attributes); |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * @param array $attributes |
||
362 | * @param string $name |
||
363 | * @return CompositeField |
||
364 | */ |
||
365 | public function addCompositeField($attributes = [], $name = null) |
||
366 | { |
||
367 | static $i = 0; |
||
368 | if ($name === null) { |
||
369 | $i++; |
||
370 | $name = "Composite_$i"; |
||
371 | } |
||
372 | return $this->addField(CompositeField::class, $name, null, $attributes); |
||
373 | } |
||
374 | |||
375 | |||
376 | /** |
||
377 | * @param string $name |
||
378 | * @param array $attributes |
||
379 | * @return HiddenField |
||
380 | */ |
||
381 | public function addHidden($name = "ID", $attributes = []) |
||
382 | { |
||
383 | return $this->addField(HiddenField::class, $name, null, $attributes); |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * @param string $name |
||
388 | * @param string $title |
||
389 | * @param array $attributes |
||
390 | * @return PasswordField |
||
391 | */ |
||
392 | public function addPassword($name = "Password", $title = null, $attributes = []) |
||
393 | { |
||
394 | return $this->addField(PasswordField::class, $name, $title, $attributes); |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * @param string $name |
||
399 | * @param string $title |
||
400 | * @param array $attributes |
||
401 | * @return EmailField |
||
402 | */ |
||
403 | public function addEmail($name = "Email", $title = null, $attributes = []) |
||
404 | { |
||
405 | return $this->addField(EmailField::class, $name, $title, $attributes); |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * @param string $name |
||
410 | * @param string $title |
||
411 | * @param array $src |
||
412 | * @param array $attributes Special attrs : empty, source |
||
413 | * @return DropdownField |
||
414 | */ |
||
415 | public function addDropdown($name = "Option", $title = null, $src = [], $attributes = []) |
||
416 | { |
||
417 | $attributes['options'] = $src; |
||
418 | return $this->addField(DropdownField::class, $name, $title, $attributes); |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * @param string $name |
||
423 | * @param string $title |
||
424 | * @param array $src |
||
425 | * @param array $attributes |
||
426 | * @return OptionsetField |
||
427 | */ |
||
428 | public function addOptionset($name = "Option", $title = null, $src = [], $attributes = []) |
||
429 | { |
||
430 | $attributes['options'] = $src; |
||
431 | return $this->addField(OptionsetField::class, $name, $title, $attributes); |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * TODO: extract |
||
436 | * @param string $name |
||
437 | * @param string $title |
||
438 | * @param array $attributes |
||
439 | * @return \LeKoala\Base\Forms\YesNoOptionsetField |
||
440 | */ |
||
441 | public function addYesNo($name = "Option", $title = null, $attributes = []) |
||
442 | { |
||
443 | return $this->addField(\LeKoala\Base\Forms\YesNoOptionsetField::class, $name, $title, $attributes); |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * @param string $name |
||
448 | * @param string $title |
||
449 | * @param array $attributes |
||
450 | * @return DateField |
||
451 | */ |
||
452 | public function addDate($name = "Date", $title = null, $attributes = []) |
||
453 | { |
||
454 | return $this->addField(DateField::class, $name, $title, $attributes); |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * @param string $name |
||
459 | * @param string $title |
||
460 | * @param array $src |
||
461 | * @param array $attributes |
||
462 | * @return \LeKoala\Base\Forms\InputMaskDateField |
||
463 | */ |
||
464 | public function addDateMask($name = "BirthDate", $title = null, $attributes = []) |
||
465 | { |
||
466 | return $this->addField(\LeKoala\Base\Forms\InputMaskDateField::class, $name, $title, $attributes); |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * @param string $name |
||
471 | * @param string $title |
||
472 | * @param array $src |
||
473 | * @param array $attributes |
||
474 | * @return \LeKoala\Base\Forms\InputMaskNumericField |
||
475 | */ |
||
476 | public function addNumericMask($name = "Number", $title = null, $attributes = []) |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * @param string $name |
||
483 | * @param string $title |
||
484 | * @param array $src |
||
485 | * @param array $attributes |
||
486 | * @return \LeKoala\Base\Forms\InputMaskCurrencyField |
||
487 | */ |
||
488 | public function addCurrencyMask($name = "Amount", $title = null, $attributes = []) |
||
489 | { |
||
490 | return $this->addField(\LeKoala\Base\Forms\InputMaskCurrencyField::class, $name, $title, $attributes); |
||
491 | } |
||
492 | /** |
||
493 | * @param string $name |
||
494 | * @param string $title |
||
495 | * @param array $src |
||
496 | * @param array $attributes |
||
497 | * @return \LeKoala\Base\Forms\InputMaskDateField |
||
498 | */ |
||
499 | public function addIntegerMask($name = "Number", $title = null, $attributes = []) |
||
500 | { |
||
501 | return $this->addField(\LeKoala\Base\Forms\InputMaskIntegerField::class, $name, $title, $attributes); |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * @param string $name |
||
506 | * @param string $title |
||
507 | * @param array $attributes |
||
508 | * @return TextField |
||
509 | */ |
||
510 | public function addText($name = "Title", $title = null, $attributes = []) |
||
511 | { |
||
512 | return $this->addField(TextField::class, $name, $title, $attributes); |
||
513 | } |
||
514 | |||
515 | /** |
||
516 | * @param string $name |
||
517 | * @param string $title |
||
518 | * @param array $attributes |
||
519 | * @return \LeKoala\Base\Forms\PhoneField |
||
520 | */ |
||
521 | public function addPhone($name = "Phone", $title = null, $attributes = []) |
||
522 | { |
||
523 | return $this->addField(\LeKoala\Base\Forms\PhoneField::class, $name, $title, $attributes); |
||
524 | } |
||
525 | |||
526 | /** |
||
527 | * @param string $name |
||
528 | * @param string $title |
||
529 | * @param array $attributes |
||
530 | * @return NumericField |
||
531 | */ |
||
532 | public function addNumeric($name = "Number", $title = null, $attributes = []) |
||
533 | { |
||
534 | return $this->addField(NumericField::class, $name, $title, $attributes); |
||
535 | } |
||
536 | |||
537 | /** |
||
538 | * @param string $name |
||
539 | * @param string $title |
||
540 | * @param array $attributes |
||
541 | * @return TextareaField |
||
542 | */ |
||
543 | public function addTextarea($name = "Description", $title = null, $attributes = []) |
||
544 | { |
||
545 | return $this->addField(TextareaField::class, $name, $title, $attributes); |
||
546 | } |
||
547 | |||
548 | /** |
||
549 | * @param string $name |
||
550 | * @param string $title |
||
551 | * @param array $attributes |
||
552 | * @return HTMLEditorField |
||
553 | */ |
||
554 | public function addEditor($name = "Description", $title = null, $attributes = []) |
||
555 | { |
||
556 | return $this->addField(HTMLEditorField::class, $name, $title, $attributes); |
||
557 | } |
||
558 | |||
559 | /** |
||
560 | * Group fields into a column field |
||
561 | * |
||
562 | * Usage is something like this |
||
563 | * |
||
564 | * $fields->group(function (BuildableFieldList $fields) { |
||
565 | * $fields->addText('Item1'); |
||
566 | * $fields->addText('Item2'); |
||
567 | * }); |
||
568 | * |
||
569 | * @param callable $callable |
||
570 | * @param array $columnSizes Eg: [1 => 4, 2 => 8] |
||
571 | * @return $this |
||
572 | */ |
||
573 | public function group($callable, $columnSizes = null) |
||
574 | { |
||
575 | $group = new ColumnsField(); |
||
576 | if ($columnSizes !== null) { |
||
577 | $group->setColumnSizes($columnSizes); |
||
578 | } |
||
579 | // First push the group |
||
580 | $this->pushOrAddToTab($group); |
||
581 | // Then set it as current (don't do the opposite, otherwise pushOrAddToTab doesn't work) |
||
582 | $this->currentGroup = $group; |
||
583 | $callable($this); |
||
584 | $this->currentGroup = null; |
||
585 | return $this; |
||
586 | } |
||
587 | |||
588 | /** |
||
589 | * Get the value of i18nEntity |
||
590 | */ |
||
591 | public function getI18nEntity() |
||
592 | { |
||
593 | return $this->i18nEntity; |
||
594 | } |
||
595 | |||
596 | /** |
||
597 | * Set the value of i18nEntity |
||
598 | * |
||
599 | * @return $this |
||
600 | */ |
||
601 | public function setI18nEntity($i18nEntity) |
||
602 | { |
||
603 | $this->i18nEntity = $i18nEntity; |
||
604 | return $this; |
||
605 | } |
||
606 | |||
607 | /** |
||
608 | * Get the value of currentTab |
||
609 | */ |
||
610 | public function getCurrentTab() |
||
611 | { |
||
612 | return $this->currentTab; |
||
613 | } |
||
614 | |||
615 | /** |
||
616 | * The current tab |
||
617 | * |
||
618 | * @return $this |
||
619 | */ |
||
620 | public function setCurrentTab($currentTab) |
||
621 | { |
||
622 | $this->currentTab = $currentTab; |
||
623 | return $this; |
||
624 | } |
||
625 | |||
626 | /** |
||
627 | * Get the value of defaultTab |
||
628 | */ |
||
629 | public function getDefaultTab() |
||
630 | { |
||
631 | return $this->defaultTab; |
||
632 | } |
||
633 | |||
634 | /** |
||
635 | * The default tab if there is no current tab |
||
636 | * |
||
637 | * This only apply before any field is added. After that it's better to use setCurrentTab |
||
638 | * |
||
639 | * @return $this |
||
640 | */ |
||
641 | public function setDefaultTab($defaultTab) |
||
645 | } |
||
646 | |||
647 | /** |
||
648 | * Get the value of placeholderAsLabel |
||
649 | * @return boolean |
||
650 | */ |
||
651 | public function getPlaceholderAsLabel() |
||
652 | { |
||
653 | return $this->placeholderAsLabel; |
||
654 | } |
||
655 | |||
656 | /** |
||
657 | * Set the value of placeholderAsLabel |
||
658 | * |
||
659 | * @param boolean $placeholderAsLabel |
||
660 | * @return $this |
||
661 | */ |
||
662 | public function setPlaceholderAsLabel($placeholderAsLabel) |
||
663 | { |
||
664 | $this->placeholderAsLabel = $placeholderAsLabel; |
||
665 | return $this; |
||
666 | } |
||
667 | } |
||
668 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths