Completed
Pull Request — master (#421)
by Robbie
02:06
created

BlogCategoriesWidget   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 107
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 4
dl 107
loc 107
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getCategories() 20 20 5
B getCMSFields() 46 46 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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 19 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

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