Completed
Push — master ( f85faa...6214aa )
by Jason
10s
created

FormBlock   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 100
Duplicated Lines 44 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 14.71%

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 4
dl 44
loc 100
ccs 5
cts 34
cp 0.1471
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 16 16 2
A BlockForm() 0 12 4
A canCreate() 14 14 3
A canView() 14 14 3

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 FormBlock extends Block
4
{
5
    /**
6
     * @var string
7
     */
8
    private static $singular_name = 'Form Block';
9
10
    /**
11
     * @var string
12
     */
13
    private static $plural_name = 'Form Blocks';
14
15
    /**
16
     * @var array
17
     */
18
    private static $db = array(
19
        'Content' => 'HTMLText',
20
    );
21
22
    /**
23
     * @var array
24
     */
25
    private static $has_one = array(
26
        'Form' => 'UserDefinedForm',
27
    );
28
29
    /**
30
     * @return FieldList
31
     */
32 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...
33
    {
34 1
        $fields = singleton('Block')->getCMSFields();
35
36 1
        if (class_exists('UserDefinedForm')) {
37
            $fields->addFieldToTab('Root.Main', DropdownField::create(
38
                'FormID',
39
                'Form',
40
                UserDefinedForm::get()->map()
41
            )->setEmptyString(''));
42
        }
43
44 1
        $fields->addFieldToTab('Root.Main', HtmlEditorField::create('Content'));
45
46 1
        return $fields;
47
    }
48
49
    /**
50
     * @return Forms|HTMLText
51
     */
52
    public function BlockForm()
53
    {
54
        if ($this->Form()->exists()) {
0 ignored issues
show
Documentation Bug introduced by
The method Form does not exist on object<FormBlock>? 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...
55
            $controller = new UserDefinedForm_Controller($this->Form());
0 ignored issues
show
Documentation Bug introduced by
The method Form does not exist on object<FormBlock>? 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...
56
            $current = Controller::curr();
57
            if ($current && $current->getAction() == 'finished') {
58
                return $controller->renderWith('ReceivedFormSubmission');
59
            }
60
            $form = $controller->Form();
61
            return $form;
62
        }
63
    }
64
65
    /**
66
     * @param null $member
67
     * @return bool
68
     */
69 View Code Duplication
    public function canCreate($member = null)
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...
70
    {
71
        if (!class_exists('UserDefinedForm')) {
72
            return false;
73
        }
74
75
        // Standard mechanism for accepting permission changes from extensions
76
        $extended = $this->extendedCan('canCreate', $member);
77
        if ($extended !== null) {
78
            return $extended;
79
        }
80
81
        return parent::canCreate();
82
    }
83
84
    /**
85
     * @param null $member
86
     * @return bool
87
     */
88 View Code Duplication
    public function canView($member = null)
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...
89
    {
90
        if (!class_exists('UserDefinedForm')) {
91
            return false;
92
        }
93
94
        // Standard mechanism for accepting permission changes from extensions
95
        $extended = $this->extendedCan('canView', $member);
96
        if ($extended !== null) {
97
            return $extended;
98
        }
99
100
        return parent::canView();
101
    }
102
}