Issues (35)

src/Traits/CMSNicetiesTraitForTabs.php (10 issues)

1
<?php
2
3
namespace Sunnysideup\CMSNiceties\Traits;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\Forms\Tab;
7
use SilverStripe\Versioned\Versioned;
8
9
trait CMSNicetiesTraitForTabs
10
{
11
    public function addSeparator(FieldList $fields, string $name, ?string $after = 'Main')
12
    {
13
        if (false !== $after) {
0 ignored issues
show
The condition false !== $after is always true.
Loading history...
14
            $tab = Tab::create($name, '|');
15
            $fields->insertAfter($after, $tab);
16
        } else {
17
            $fields->addFieldsToTab(
18
                'Root.' . $name,
19
                []
20
            );
21
            $fields->fieldByName('Root.' . $name)->setTitle('|');
22
        }
23
    }
24
25
    public function reorderTabs(FieldList $fields, array $tabOrder): FieldList
26
    {
27
        $tabs = [];
28
        foreach ($tabOrder as $tabName => $title) {
29
            // non-associative array..
30
            if ((int) $tabName === $tabName) {
31
                $tabName = $title;
32
                $items = preg_split('#(?=[A-Z])#', $tabName);
33
                $title = is_array($items) ? trim(implode(' ', $items)) : $tabName;
0 ignored issues
show
The assignment to $title is dead and can be removed.
Loading history...
34
            }
35
36
            $tabNamePlus = $tabName . 'Tab';
37
38
            // fixd existing existing tab
39
            $tab = $fields->fieldByName('Root.' . $tabName);
0 ignored issues
show
Are you sure the assignment to $tab is correct as $fields->fieldByName('Root.' . $tabName) 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...
40
            if (! $tab) {
41
                $tab = $fields->fieldByName('Root.' . $tabNamePlus);
0 ignored issues
show
Are you sure the assignment to $tab is correct as $fields->fieldByName('Root.' . $tabNamePlus) 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...
42
            }
43
44
            if (! $tab) {
45
                $tab = new Tab($tabNamePlus, $tabName);
46
            }
47
48
            $fields->removeByName(['Root.' . $tabName]);
49
            $fields->removeByName(['Root.' . $tabNamePlus]);
50
            $fields->removeFieldFromTab('Root', $tabName);
51
            $fields->removeFieldFromTab('Root', $tabNamePlus);
52
            $fields->removeFieldsFromTab('Root', [$tabName]);
53
            $fields->removeFieldsFromTab('Root', [$tabNamePlus]);
54
            $tab->setTitle($tabName);
55
            $tab->setName($tabNamePlus);
56
            $tabs[] = $tab;
57
            // $fields->addFieldsToTab('Root', $tab);
58
        }
59
60
        // $tabs = array_reverse($tabs);
61
        foreach ($tabs as $tab) {
62
            $fields->addFieldToTab('Root', $tab);
63
        }
64
65
        return $fields;
66
    }
67
68
    public function addTab(FieldList $fields, string $name, ?string $after = 'Main', ?string $title = '')
69
    {
70
        // add spaces between capitals
71
        if (! $title) {
72
            $items = preg_split('#(?=[A-Z])#', $name);
73
            $title = is_array($items) ? trim(implode(' ', $items)) : $name;
0 ignored issues
show
The condition is_array($items) is always true.
Loading history...
74
        }
75
76
        if (null !== $after) {
77
            if ($this->hasExtension(Versioned::class) && $this->isArchived()) {
0 ignored issues
show
It seems like isArchived() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
            if ($this->hasExtension(Versioned::class) && $this->/** @scrutinizer ignore-call */ isArchived()) {
Loading history...
It seems like hasExtension() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
            if ($this->/** @scrutinizer ignore-call */ hasExtension(Versioned::class) && $this->isArchived()) {
Loading history...
78
                // do nothing
79
            } else {
80
                $tab = $fields->fieldByName('Root.' . $name);
0 ignored issues
show
Are you sure the assignment to $tab is correct as $fields->fieldByName('Root.' . $name) 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...
81
                $fields->removeFieldFromTab(
82
                    'Root',
83
                    $name
84
                );
85
                if (! $tab) {
0 ignored issues
show
$tab is of type null, thus it always evaluated to false.
Loading history...
86
                    $tab = Tab::create($name, $title);
87
                }
88
89
                $fields->insertAfter($after, $tab);
90
            }
91
        } else {
92
            $fields->addFieldsToTab(
93
                'Root.' . $name,
94
                []
95
            );
96
            $fields->fieldByName('Root.' . $name)->setTitle($title);
0 ignored issues
show
Are you sure the usage of $fields->fieldByName('Root.' . $name) 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 used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
97
        }
98
    }
99
}
100