ThemeCustomisation   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 6
dl 0
loc 164
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A i18n_singular_name() 0 4 1
A i18n_plural_name() 0 4 1
A CMSEditLink() 0 6 1
A CMSAddLink() 0 6 1
B getCMSFields() 0 37 5
A exportToThemeSCSS() 0 7 1
A htmlForPageHeader() 0 10 2
A canCreate() 0 8 2
A canDelete() 0 4 1
A fontFields() 0 4 1
A colourFields() 0 4 1
A fieldsPerType() 0 10 3
1
<?php
2
3
4
5
class ThemeCustomisation extends DataObject {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
7
    private static $location_for_scss_file = 'themes/main_mysite/src/variables/_themecustomisation.scss';
0 ignored issues
show
Unused Code introduced by
The property $location_for_scss_file is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
8
9
    private static $singular_name = 'Theme Customisation';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $singular_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
10
11
    function i18n_singular_name()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
12
    {
13
        return _t('ThemeCustomiser.SINGULAR_NAME', 'Theme Customisation');
14
    }
15
16
    private static $plural_name = 'Theme Customisations';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $plural_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
17
18
    function i18n_plural_name()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
19
    {
20
        return _t('ThemeCustomiser.PLURAL_NAME', 'Theme Customisations');
21
    }
22
23
    private static $db = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
24
        'BackgroundColour' => 'Color',
25
        'FontColour' => 'Color',
26
        'Accent1Colour' => 'Color',
27
        'Accent2Colour' => 'Color',
28
        'MenuBarBackgroundColour' => 'Color',
29
        'LinkColour' => 'Color',
30
        'ActiveColour' => 'Color',
31
        'HeaderFont' => 'Varchar(50)',
32
        'TextFont' => 'Varchar(50)',
33
        'MonoFont' => 'Varchar(50)'
34
    ];
35
36
    private static $has_one = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
37
        'LargeLogo' => 'Image',
38
        'SmallLogo' => 'Image'
39
    ];
40
41
    private static $summary_fields = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $summary_fields is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
42
        'LargeLogo.Stripthumbnail' => 'Name'
43
    ];
44
45
    private static $field_labels = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $field_labels is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
46
        'MonoFont' => 'Mono spaced font'
47
    ];
48
49
    private static $field_labels_right = [
0 ignored issues
show
Unused Code introduced by
The property $field_labels_right is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
50
        'MonoFont' => 'Font similar to courier with identical width used for each character - great for numbers.'
51
    ];
52
53
54
    public function CMSEditLink()
55
    {
56
        $controller = singleton("ThemeCustomiser");
57
58
        return $controller->Link().$this->ClassName."/EditForm/field/".$this->ClassName."/item/".$this->ID."/edit";
59
    }
60
61
    public function CMSAddLink()
62
    {
63
        $controller = singleton("ThemeCustomiser");
64
65
        return $controller->Link().$this->ClassName."/EditForm/field/".$this->ClassName."/item/new";
66
    }
67
68
    public function getCMSFields()
69
    {
70
        $fields = parent::getCMSFields();
71
72
        $fontList = Injector::inst()->get('GoogleFontProvider')->fullList();
73
        $fontFields = $this->Config()->get('font_fields');
0 ignored issues
show
Unused Code introduced by
$fontFields is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
74
        $labels = $this->FieldLabels();
75
        foreach($this->fontFields() as $fontField) {
76
            $fields->replaceField(
77
                $fontField,
78
                DropdownField::create(
79
                    $fontField,
80
                    $labels[$fontField],
81
                    ['' => '-- select --'] + $fontList
82
                )
83
            );
84
        }
85
        foreach($this->colourFields() as $colourField) {
86
            $fields->replaceField(
87
                $colourField,
88
                ColorField::create(
89
                    $colourField,
90
                    $labels[$colourField]
91
                )
92
            );
93
        }
94
95
        $rightFieldDescriptions = $this->Config()->get('field_labels_right');
96
        foreach($rightFieldDescriptions as $field => $desc) {
97
            $field = $fields->DataFieldByName($field);
98
            if($field) {
99
                $field->setDescription($desc);
100
            }
101
        }
102
103
        return $fields;
104
    }
105
106
    /**
107
     * exports to variable SCSS file ...
108
     */
109
    function exportToThemeSCSS()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
110
    {
111
112
        $location = Director::baseFolder() .'/'. $this->Config()->get('location_for_scss_file');
113
        $data = $this->renderWith('ThemeCustomisationSCSSOutput');
114
        file_put_contents($location, $data);
115
    }
116
117
    /**
118
     * stuff to be inserted into head of html
119
     * - custom css
120
     * - link to google fonts ...
121
     */
122
    function htmlForPageHeader()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
123
    {
124
        $list = [];
125
        foreach($this->fontFields() as $fontField) {
126
            $list[$this->$fontField] = $this->$fontField;
127
        }
128
        $fontList = Injecton::inst()->get('GoogleFontProvider')->getLink($list);
129
130
        return $fontList;
131
    }
132
133
    function canCreate($member = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
134
    {
135
        if(ThemeCustomisation::get()->count()) {
136
            return false;
137
        }
138
139
        return parent::canCreate($member);
140
    }
141
142
    function canDelete($member = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
143
    {
144
        return false;
145
    }
146
147
    protected function fontFields()
148
    {
149
        return $this->fieldsPerType('Font');
150
    }
151
152
    protected function colourFields()
153
    {
154
        return $this->fieldsPerType('Colour');
155
    }
156
157
    protected function fieldsPerType($name)
158
    {
159
        $list = $this->Config()->get('db');
160
        foreach($list as $key => $type) {
161
            if(substr($key, strlen($name) * -1) === $name) {
162
                $newList[$key] = $key;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$newList was never initialized. Although not strictly required by PHP, it is generally a good practice to add $newList = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
163
            }
164
        }
165
        return $newList;
0 ignored issues
show
Bug introduced by
The variable $newList does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
166
    }
167
168
}
169