AdminiCmsActionsExtension::updateStyle()   B
last analyzed

Complexity

Conditions 11
Paths 57

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 27
c 1
b 0
f 0
nc 57
nop 1
dl 0
loc 38
rs 7.3166

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 LeKoala\Admini\LeftAndMain;
6
use SilverStripe\Core\Extension;
7
use LeKoala\Admini\MaterialIcons;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\FormAction;
10
use SilverStripe\Control\Controller;
11
12
/**
13
 * Improve how actions are rendered if you are using the cms-actions module
14
 */
15
class AdminiCmsActionsExtension extends Extension
16
{
17
    /**
18
     * @param FieldList||FormAction[] $actions
0 ignored issues
show
Documentation Bug introduced by
The doc comment FieldList||FormAction[] at position 2 could not be parsed: Unknown type name '|' at position 2 in FieldList||FormAction[].
Loading history...
19
     * @return void
20
     */
21
    public function onAfterUpdateCMSActions(FieldList $actions)
22
    {
23
        $controller = Controller::curr();
24
25
        // Avoid side effects on regular admin
26
        if (!$controller instanceof LeftAndMain) {
27
            return;
28
        }
29
30
        /** @var \SilverStripe\Forms\Tab $dropUpContainer */
31
        $dropUpContainer =  $actions->fieldByName('ActionMenus.MoreOptions');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $dropUpContainer is correct as $actions->fieldByName('ActionMenus.MoreOptions') targeting SilverStripe\Forms\FieldList::fieldByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
32
33
        // Replace with our own logic
34
        if ($dropUpContainer) {
0 ignored issues
show
introduced by
$dropUpContainer is of type SilverStripe\Forms\Tab, thus it always evaluated to true.
Loading history...
35
            $group = new AdminiDropUpField($dropUpContainer->getChildren());
36
            $actions->insertBefore('RightGroup', $group);
37
            $actions->removeByName('ActionMenus');
38
        }
39
40
        $this->updateStyle($actions);
41
    }
42
43
    protected function updateStyle($actions)
44
    {
45
        /** @var FormAction $action */
46
        foreach ($actions as $action) {
47
            if ($action->hasMethod("getChildren")) {
48
                foreach ($action->getChildren() as $child) {
49
                    $this->updateStyle($child);
50
                }
51
                continue;
52
            }
53
            if ($action->hasExtraClass('btn-outline-danger')) {
54
                $action->removeExtraClass('btn-outline-danger');
55
                $action->addExtraClass('btn-danger');
56
                if ($action->hasExtraClass('font-icon-trash-bin')) {
57
                    $action->removeExtraClass('font-icon-trash-bin');
58
                    $action->setIcon(MaterialIcons::DELETE);
59
                }
60
            }
61
            if ($action->hasExtraClass('btn-outline-primary')) {
62
                $action->removeExtraClass('btn-outline-primary');
63
                $action->addExtraClass('btn-outline-success');
64
                if ($action->hasExtraClass('font-icon-level-up')) {
65
                    $action->removeExtraClass('font-icon-level-up');
66
                    $action->setIcon(MaterialIcons::KEYBOARD_RETURN);
67
                }
68
                if ($action->hasExtraClass('font-icon-angle-double-left')) {
69
                    $action->removeExtraClass('font-icon-angle-double-left');
70
                    $action->setIcon(MaterialIcons::NAVIGATE_BEFORE);
71
                }
72
                if ($action->hasExtraClass('font-icon-angle-double-right')) {
73
                    $action->removeExtraClass('font-icon-angle-double-right');
74
                    $action->addExtraClass('btn-flex btn-icon-end');
75
                    $action->setIcon(MaterialIcons::NAVIGATE_NEXT);
76
                }
77
            }
78
            if ($action->hasExtraClass('btn-primary')) {
79
                $action->removeExtraClass('btn-primary');
80
                $action->addExtraClass('btn-success');
81
            }
82
        }
83
    }
84
}
85