Completed
Push — master ( 1d2fd9...fbfc98 )
by Jason
10s
created

PromoBlock   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 23
loc 69
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A singular_name() 0 4 1
A getPromoList() 0 4 1
A getCMSFields() 23 23 2
A plural_name() 0 4 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
class PromoBlock extends Block
4
{
5
    /**
6
     * @return string
7
     */
8 9
    public function singular_name()
9
    {
10 9
        return _t('PromoBlock.SINGULARNAME', 'Promos Block');
11
    }
12
13
    /**
14
     * @return string
15
     */
16 1
    public function plural_name()
17
    {
18 1
        return _t('PromoBlock.PLURALNAME', 'Promos Blocks');
19
    }
20
21
    /**
22
     * @var array
23
     */
24
    private static $many_many = array(
25
        'Promos' => 'PromoObject',
26
    );
27
28
    /**
29
     * @var array
30
     */
31
    private static $many_many_extraFields = array(
32
        'Promos' => array(
33
            'SortOrder' => 'Int',
34
        ),
35
    );
36
37
    /**
38
     * @return FieldList
39
     */
40 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...
41
    {
42 1
        $fields = parent::getCMSFields();
43
44 1
        $fields->removeByName(array(
45 1
            'Promos',
46 1
        ));
47
48 1
        if ($this->ID) {
49 1
            $config = GridFieldConfig_RelationEditor::create();
50 1
            $config->addComponent(new GridFieldOrderableRows('SortOrder'));
51 1
            $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
52 1
            $config->addComponent(new GridFieldAddExistingSearchButton());
53 1
            $promos = $this->Promos()->sort('SortOrder');
0 ignored issues
show
Documentation Bug introduced by
The method Promos does not exist on object<PromoBlock>? 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...
54 1
            $promoField = GridField::create('Promos', 'Promos', $promos, $config);
55
56 1
            $fields->addFieldsToTab('Root.Promos', array(
57 1
                $promoField,
58 1
            ));
59 1
        }
60
61 1
        return $fields;
62
    }
63
64
    /**
65
     * @return mixed
66
     */
67 1
    public function getPromoList()
68
    {
69 1
        return $this->Promos()->sort('SortOrder');
0 ignored issues
show
Documentation Bug introduced by
The method Promos does not exist on object<PromoBlock>? 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...
70
    }
71
}