BaseSliderItem   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 204
Duplicated Lines 13.24 %

Coupling/Cohesion

Components 5
Dependencies 5

Importance

Changes 0
Metric Value
wmc 18
lcom 5
cbo 5
dl 27
loc 204
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getStyles() 9 9 2
A getHorizontalType() 0 3 1
A getLowerStyle() 0 3 1
A getVerticalType() 0 3 1
A singular_name() 0 3 1
A plural_name() 0 3 1
A summaryFields() 0 6 1
A getSliderType() 0 3 1
A getCMSFields() 0 19 1
A fieldLabels() 0 20 1
A forTemplate() 0 5 1
A getSliderImage() 0 3 1
A getVerticallyTypes() 9 9 2
A getHorizontallyTypes() 9 9 2
A getHeading() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @author    Donatas Navidonskis <[email protected]>
5
 * @since     2017
6
 * @class     BaseSliderItem
7
 *
8
 * @property int    BlockID
9
 * @property int    SortOrder
10
 * @property string Title
11
 * @property string Content
12
 * @property string AlignX
13
 * @property string AlignY
14
 * @property string Style
15
 *
16
 * @method SliderBlock Block
17
 */
18
class BaseSliderItem 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...
19
20
    /**
21
     * @var array
22
     * @config
23
     */
24
    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...
25
        'Title'     => 'Varchar(320)',
26
        'Content'   => 'HTMLText',
27
        'AlignX'    => 'Enum(array("Left", "Center", "Right"), "Left")',
28
        'AlignY'    => 'Enum(array("Top", "Middle", "Bottom"), "Middle")',
29
        'Style'     => 'Enum(array("Dark", "Light"), "Light")',
30
        'SortOrder' => 'Int',
31
    ];
32
33
    /**
34
     * @var array
35
     * @config
36
     */
37
    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...
38
        'Block' => 'SliderBlock',
39
    ];
40
41
    /**
42
     * Get localized types
43
     *
44
     * @return array
45
     */
46 View Code Duplication
    public function getHorizontallyTypes() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
        $types = [];
48
49
        foreach ($this->dbObject('AlignX')->enumValues() as $alignType) {
50
            $types[$alignType] = $this->fieldLabel($alignType);
51
        }
52
53
        return $types;
54
    }
55
56
    /**
57
     * Get localized types
58
     *
59
     * @return array
60
     */
61 View Code Duplication
    public function getVerticallyTypes() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
        $types = [];
63
64
        foreach ($this->dbObject('AlignY')->enumValues() as $alignType) {
65
            $types[$alignType] = $this->fieldLabel($alignType);
66
        }
67
68
        return $types;
69
    }
70
71
    /**
72
     * Get localized types
73
     *
74
     * @return array
75
     */
76 View Code Duplication
    public function getStyles() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
        $types = [];
78
79
        foreach ($this->dbObject('Style')->enumValues() as $style) {
80
            $types[$style] = $this->fieldLabel($style);
81
        }
82
83
        return $types;
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getHorizontalType() {
90
        return strtolower($this->AlignX);
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getLowerStyle() {
97
        return strtolower($this->Style);
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getVerticalType() {
104
        return strtolower($this->AlignY);
105
    }
106
107
    /**
108
     * The default sort expression. This will be inserted in the ORDER BY
109
     * clause of a SQL query if no other sort expression is provided.
110
     *
111
     * @var string
112
     * @config
113
     */
114
    private static $default_sort = 'SortOrder ASC';
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...
115
116
    /**
117
     * @return string
118
     */
119
    public function singular_name() {
120
        return _t('SliderItem.SINGULARNAME', 'Slider');
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function plural_name() {
127
        return _t('SliderItem.PLURALNAME', 'Sliders');
128
    }
129
130
    /**
131
     * Default summary fields within localized label title's.
132
     *
133
     * @return array
134
     */
135
    public function summaryFields() {
136
        return [
137
            'Title'      => $this->fieldLabel('Title'),
138
            'SliderType' => $this->fieldLabel('SliderType'),
139
        ];
140
    }
141
142
    /**
143
     * @return string
144
     */
145
    public function getSliderType() {
146
        return $this->singular_name();
147
    }
148
149
    /**
150
     * @return \FieldList
151
     */
152
    public function getCMSFields() {
153
        $fields = parent::getCMSFields();
154
        $fields->removeByName(['SortOrder', 'Title', 'Content', 'AlignX', 'AlignY', 'Style']);
155
        $fields->findOrMakeTab('Root.Media', $this->fieldLabel('Media'));
156
157
        $fields->addFieldsToTab('Root.Main', [
158
            OptionsetField::create('AlignX', $this->fieldLabel('ContentHorizontallyAlignment'), $this->getHorizontallyTypes()),
159
            OptionsetField::create('AlignY', $this->fieldLabel('ContentVerticallyAlignment'), $this->getVerticallyTypes()),
160
            OptionsetField::create('Style', $this->fieldLabel('Style'), $this->getStyles()),
161
            \TextField::create('Title', $this->fieldLabel('Title')),
162
            $contentField = \HtmlEditorField::create('Content', $this->fieldLabel('Content')),
163
        ]);
164
165
        $contentField->setRows(15);
166
167
        $this->extend('updateCMSFields', $fields);
168
169
        return $fields;
170
    }
171
172
    /**
173
     * @param bool $includeRelations
174
     *
175
     * @return array
176
     */
177
    public function fieldLabels($includeRelations = true) {
178
        return array_merge(parent::fieldLabels($includeRelations), [
179
            'Title'                        => _t('BaseSliderItem.TITLE', 'Title'),
180
            'Content'                      => _t('BaseSliderItem.CONTENT', 'Content'),
181
            'Picture'                      => _t('BaseSliderItem.PICTURE', 'Picture'),
182
            'Media'                        => _t('BaseSliderItem.MEDIA', 'Media'),
183
            'Video'                        => _t('BaseSliderItem.VIDEO', 'Video'),
184
            'SliderType'                   => _t('BaseSliderItem.SLIDER_TYPE', 'Slider type'),
185
            'ContentHorizontallyAlignment' => _t('BaseSliderItem.CONTENT_HORIZONTALLY_ALIGNMENT', 'Content horizontally alignment'),
186
            'ContentVerticallyAlignment'   => _t('BaseSliderItem.CONTENT_VERTICALLY_ALIGNMENT', 'Content vertically alignment'),
187
            'Left'                         => _t('BaseSliderItem.LEFT', 'Left'),
188
            'Center'                       => _t('BaseSliderItem.CENTER', 'Center'),
189
            'Right'                        => _t('BaseSliderItem.RIGHT', 'Right'),
190
            'Top'                          => _t('BaseSliderItem.TOP', 'Top'),
191
            'Middle'                       => _t('BaseSliderItem.MIDDLE', 'Middle'),
192
            'Bottom'                       => _t('BaseSliderItem.BOTTOM', 'Bottom'),
193
            'Dark'                         => _t('BaseSliderItem.DARK', 'Dark'),
194
            'Light'                        => _t('BaseSliderItem.LIGHT', 'Light'),
195
        ]);
196
    }
197
198
    /**
199
     * @return \HTMLText
200
     */
201
    public function forTemplate() {
202
        $template = sprintf("%s_%s", $this->Block()->ClassName, $this->ClassName);
0 ignored issues
show
Documentation Bug introduced by
The method Block does not exist on object<BaseSliderItem>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
203
204
        return $this->renderWith($template);
205
    }
206
207
    /**
208
     * @return false
209
     */
210
    public function getSliderImage() {
211
        return false;
212
    }
213
214
    public function getHeading() {
215
        $title = $this->Title;
216
217
        $this->extend('updateBeforeHeading', $title);
218
219
        return $title;
220
    }
221
}