BlogCategoriesWidget::getCMSFields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 46

Duplication

Lines 46
Ratio 100 %

Importance

Changes 0
Metric Value
dl 46
loc 46
rs 9.1781
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 BlogCategoriesWidget 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 = 'Categories';
26
27
    /**
28
     * @var string
29
     */
30
    private static $cmsTitle = 'Blog Categories';
31
32
    /**
33
     * @var string
34
     */
35
    private static $description = 'Displays a list of blog categories.';
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 = 'BlogCategoriesWidget';
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 categories shown by this widget (set to 0 to show all categories).'
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 categories 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 categories shown by this widget.'
101
                    )
102
                );
103
        });
104
105
        return parent::getCMSFields();
106
    }
107
108
    /**
109
     * @return DataList
110
     */
111
    public function getCategories()
112
    {
113
        $blog = $this->Blog();
114
115
        if (!$blog) {
116
            return [];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (array) is incompatible with the return type documented by SilverStripe\Blog\Widget...esWidget::getCategories of type SilverStripe\ORM\DataList.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
117
        }
118
119
        $query = $blog->Categories();
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