BootstrapFormFieldExtension::updateAttributes()   C
last analyzed

Complexity

Conditions 12
Paths 12

Size

Total Lines 38
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 32
nc 12
nop 1
dl 0
loc 38
rs 6.9666
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace LeKoala\Admini\Forms;
4
5
use SilverStripe\Forms\Tab;
6
use SilverStripe\Forms\TabSet;
7
use SilverStripe\Core\Extension;
8
use SilverStripe\Forms\TextField;
9
use SilverStripe\Forms\FormAction;
10
use SilverStripe\Forms\CheckboxField;
11
use SilverStripe\Forms\TextareaField;
12
use SilverStripe\Forms\OptionsetField;
13
use SilverStripe\Forms\CheckboxSetField;
14
use SilverStripe\Forms\ReadonlyField;
15
use SilverStripe\Forms\SelectField;
16
17
/**
18
 * @extends SilverStripe\Forms\FormField
19
 */
20
class BootstrapFormFieldExtension extends Extension
21
{
22
    use BootstrapFormMessage;
23
24
    /**
25
     * Note: classes are applied on the form element, not on the holder
26
     *
27
     * @param array $attributes
28
     * @return void
29
     */
30
    public function updateAttributes(&$attributes)
31
    {
32
        $class = $attributes['class'] ?? '';
33
        $o = $this->owner;
34
35
        //TODO: check if this is needed at all??
36
        switch (true) {
37
            case $o instanceof ReadonlyField:
38
                $class = 'form-control ' . $class;
39
                break;
40
            case $o instanceof FormAction:
41
                $class = 'btn ' . $class;
42
                if (strpos($class, 'btn-') === false) {
43
                    $class .= " btn-primary";
44
                }
45
                break;
46
            case $o instanceof CheckboxSetField:
47
            case $o instanceof OptionsetField:
48
                $class = 'list-unstyled ' . $class;
49
                break;
50
            case $o instanceof CheckboxField:
51
                $class = 'form-check-input ' . $class;
52
                break;
53
            case $o instanceof TabSet:
54
                $class = 'tab-content ' . $class;
55
                break;
56
            case $o instanceof Tab:
57
                $class = 'tab-pane ' . $class;
58
                break;
59
            case $o instanceof SelectField:
60
                $class = 'form-select ' . $class;
61
                break;
62
            case $o instanceof TextField:
63
            case $o instanceof TextareaField:
64
                $class = 'form-control ' . $class;
65
                break;
66
        }
67
        $attributes['class'] = trim($class);
68
    }
69
70
    public function HasPopover()
71
    {
72
        if (str_contains($this->owner->extraClass(), 'popover-actions-simulate')) {
73
            return true;
74
        }
75
        return false;
76
    }
77
}
78