Completed
Pull Request — master (#9)
by Jason
13:23
created

FormBlock   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 18.6 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 5
dl 16
loc 86
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() 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
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 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
        $fields = parent::getCMSFields();
35
36
        if (class_exists('UserDefinedForm')) {
37
            $fields->addFieldToTab('Root.Main', DropdownField::create(
38
                'FormID',
39
                'Form',
40
                UserDefinedForm::get()->map()
41
            )->setEmptyString(''));
42
        }
43
44
        $fields->addFieldToTab('Root.Main', HtmlEditorField::create('Content'));
45
46
        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
    public function canCreate($member = null)
70
    {
71
        if (!class_exists('UserDefinedForm')) {
72
            return false;
73
        }
74
        return parent::canCreate();
75
    }
76
77
    /**
78
     * @param null $member
79
     * @return bool
80
     */
81
    public function canView($member = null)
82
    {
83
        if (!class_exists('UserDefinedForm')) {
84
            return false;
85
        }
86
        return parent::canView();
87
    }
88
}