Completed
Pull Request — master (#62)
by Jason
15:39
created

RecentBlogPostsBlock   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 22.22 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 61.9%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 18
loc 81
ccs 13
cts 21
cp 0.619
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 18 18 2
A canCreate() 0 7 2
A canView() 0 7 2

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 16 and the first side effect is on line 10.

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 1
namespace Dynamic\DynamicBlocks\Block;
4 1
5
use SheaDawson\Blocks\Model\Block;
6
use SilverStripe\Forms\DropdownField;
7
use SilverStripe\Forms\NumericField;
8
9
if (!class_exists('Blog')) {
10
    return;
11
}
12
13
/**
14
 * Class RecentBlogPostsBlock
15
 */
16
class RecentBlogPostsBlock extends Block
17
{
18
    /**
19
     * @var string
20
     */
21
    private static $singular_name = 'Recent Blog Posts Block';
22
23
    /**
24
     * @var string
25
     */
26
    private static $plural_name = 'Recent Blog Posts Blocks';
27
28
    /**
29
     * @var array
30
     */
31
    private static $db = array(
32
        'Limit' => 'Int',
33
    );
34
35
    /**
36
     * @var array
37
     */
38
    private static $has_one = array(
39
        'Blog' => 'Blog',
40
    );
41
42
    /**
43
     * @var array
44
     */
45
    private static $defaults = array(
46 1
        'Limit' => 3,
47
    );
48 1
49
    /**
50 1
     * @return FieldList
51 1
     */
52 1 View Code Duplication
    public function getCMSFields()
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...
53
    {
54 1
        $fields = singleton('Block')->getCMSFields();
55
56
        $fields->addFieldsToTab('Root.Main', array(
57
            NumericField::create('Limit'),
58
        ));
59
60
        if (class_exists('Blog')) {
61
            $fields->addFieldToTab(
62 1
                'Root.Main',
63
                DropdownField::create('BlogID', 'Featured Blog', Blog::get()->map())
64
                    ->setEmptyString('')
65
            );
66
        }
67
68
        return $fields;
69 1
    }
70
71 1
    /**
72 1
     * @param null $member
73
     * @param array $context
74
     * @return bool
75
     */
76
    public function canCreate($member = NULL, $context = [])
77
    {
78
        if (!class_exists('Blog')) {
79
            return false;
80
        }
81 1
        return parent::canCreate();
82
    }
83 1
84 1
    /**
85
     * @param null $member
86
     * @param array $context
87
     * @return bool
88
     */
89
    public function canView($member = NULL, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91
        if (!class_exists('Blog')) {
92
            return false;
93
        }
94
        return parent::canView();
95
    }
96
}