Completed
Push — master ( 2b4610...a5af43 )
by Robbie
02:08
created

BlogTagsWidget   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 112
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 5
dl 112
loc 112
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 46 46 1
A getTags() 20 20 5

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
namespace SilverStripe\Blog\Widgets;
4
5
use SilverStripe\Blog\Model\Blog;
6
use SilverStripe\Core\Convert;
7
use SilverStripe\Forms\DropdownField;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\NumericField;
10
use SilverStripe\ORM\DataList;
11
use SilverStripe\Widgets\Model\Widget;
12
13
if (!class_exists(Widget::class)) {
14
    return;
15
}
16
17
/**
18
 * @method Blog Blog()
19
 */
20 View Code Duplication
class BlogTagsWidget extends Widget
0 ignored issues
show
Duplication introduced by
This class 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...
21
{
22
    /**
23
     * @var string
24
     */
25
    private static $title = 'Tags';
26
27
    /**
28
     * @var string
29
     */
30
    private static $cmsTitle = 'Blog Tags';
31
32
    /**
33
     * @var string
34
     */
35
    private static $description = 'Displays a list of blog tags.';
36
37
    /**
38
     * @var array
39
     */
40
    private static $db = [
41
        'Limit' => 'Int',
42
        'Order' => 'Varchar',
43
        'Direction' => 'Varchar',
44
    ];
45
46
    /**
47
     * @var array
48
     */
49
    private static $has_one = [
50
        'Blog' => Blog::class
51
    ];
52
53
    /**
54
     * @var string
55
     */
56
    private static $table_name = 'BlogTagsWidget';
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getCMSFields()
62
    {
63
        $this->beforeUpdateCMSFields(function (Fieldlist $fields) {
64
            $fields[] = DropdownField::create(
65
                'BlogID',
66
                _t(__CLASS__ . '.Blog', 'Blog'),
67
                Blog::get()->map()
68
            );
69
70
            $fields[] = NumericField::create(
71
                'Limit',
72
                _t(__CLASS__ . '.Limit', 'Limit'),
73
                0
74
            )
75
                ->setDescription(
76
                    _t(
77
                        __CLASS__ . '.Limit_Description',
78
                        'Limit the number of tags shown by this widget (set to 0 to show all tags).'
79
                    )
80
                )
81
                ->setMaxLength(3);
82
83
            $fields[] = DropdownField::create(
84
                'Order',
85
                _t(__CLASS__ . '.Sort', 'Sort'),
86
                ['Title' => 'Title', 'Created' => 'Created', 'LastEdited' => 'Updated']
87
            )
88
                ->setDescription(
89
                    _t(__CLASS__ . '.Sort_Description', 'Change the order of tags shown by this widget.')
90
                );
91
92
            $fields[] = DropdownField::create(
93
                'Direction',
94
                _t(__CLASS__ . '.Direction', 'Direction'),
95
                ['ASC' => 'Ascending', 'DESC' => 'Descending']
96
            )
97
                ->setDescription(
98
                    _t(
99
                        __CLASS__ . '.Direction_Description',
100
                        'Change the direction of ordering of tags shown by this widget.'
101
                    )
102
                );
103
        });
104
105
        return parent::getCMSFields();
106
    }
107
108
    /**
109
     * @return DataList
110
     */
111
    public function getTags()
112
    {
113
        $blog = $this->Blog();
114
115
        if (!$blog) {
116
            return [];
117
        }
118
119
        $query = $blog->Tags();
120
121
        if ($this->Limit) {
122
            $query = $query->limit(Convert::raw2sql($this->Limit));
0 ignored issues
show
Documentation introduced by
\SilverStripe\Core\Convert::raw2sql($this->Limit) is of type array|string, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
123
        }
124
125
        if ($this->Order && $this->Direction) {
126
            $query = $query->sort(Convert::raw2sql($this->Order), Convert::raw2sql($this->Direction));
127
        }
128
129
        return $query;
130
    }
131
}
132