Completed
Pull Request — master (#185)
by Janne
02:32
created

CommentAdmin::getEditForm()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 82
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 3 Features 2
Metric Value
c 8
b 3
f 2
dl 0
loc 82
rs 8.3768
cc 5
eloc 51
nc 6
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Comment administration system within the CMS
5
 *
6
 * @package comments
7
 */
8
class CommentAdmin extends LeftAndMain implements PermissionProvider
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
11
    private static $url_segment = 'comments';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $url_segment 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...
12
13
    private static $url_rule = '/$Action';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $url_rule 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...
14
15
    private static $menu_title = 'Comments';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $menu_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...
16
17
    private static $allowed_actions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions 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...
18
        'approvedmarked',
19
        'deleteall',
20
        'deletemarked',
21
        'hammarked',
22
        'showtable',
23
        'spammarked',
24
        'EditForm',
25
        'unmoderated'
26
    );
27
28
    public function providePermissions()
29
    {
30
        return array(
31
            "CMS_ACCESS_CommentAdmin" => array(
32
                'name' => _t('CommentAdmin.ADMIN_PERMISSION', "Access to 'Comments' section"),
33
                'category' => _t('Permission.CMS_ACCESS_CATEGORY', 'CMS Access')
34
            )
35
        );
36
    }
37
38
    /**
39
     * @return Form
40
     */
41
    public function getEditForm($id = null, $fields = null)
42
    {
43
        if (!$id) {
44
            $id = $this->currentPageID();
45
        }
46
47
        $form = parent::getEditForm($id);
0 ignored issues
show
Unused Code introduced by
$form is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
48
        $record = $this->getRecord($id);
49
50
        if ($record && !$record->canView()) {
51
            return Security::permissionFailure($this);
52
        }
53
54
        $newComments = Comment::get()->filter('Moderated', 0);
55
56
        $newGrid = new CommentsGridField(
57
            'NewComments',
58
            _t('CommentsAdmin.NewComments', 'New'),
59
            $newComments,
60
            CommentsGridFieldConfig::create()
61
        );
62
63
        $approvedComments = Comment::get()->filter('Moderated', 1)->filter('IsSpam', 0);
64
65
        $approvedGrid = new CommentsGridField(
66
            'ApprovedComments',
67
            _t('CommentsAdmin.ApprovedComments', 'Approved'),
68
            $approvedComments,
69
            CommentsGridFieldConfig::create()
70
        );
71
72
        $spamComments = Comment::get()->filter('Moderated', 1)->filter('IsSpam', 1);
73
74
        $spamGrid = new CommentsGridField(
75
            'SpamComments',
76
            _t('CommentsAdmin.SpamComments', 'Spam'),
77
            $spamComments,
78
            CommentsGridFieldConfig::create()
79
        );
80
81
        $newCount = '(' . count($newComments) . ')';
82
        $approvedCount = '(' . count($approvedComments) . ')';
83
        $spamCount = '(' . count($spamComments) . ')';
84
85
        $fields = new FieldList(
86
            $root = new TabSet(
87
                'Root',
88
                new Tab('NewComments', _t('CommentAdmin.NewComments', 'New') . ' ' . $newCount,
89
                    $newGrid
90
                ),
91
                new Tab('ApprovedComments', _t('CommentAdmin.ApprovedComments', 'Approved') . ' ' . $approvedCount,
92
                    $approvedGrid
93
                ),
94
                new Tab('SpamComments', _t('CommentAdmin.SpamComments', 'Spam') . ' ' . $spamCount,
95
                    $spamGrid
96
                )
97
            )
98
        );
99
100
        $root->setTemplate('CMSTabSet');
101
102
        $actions = new FieldList();
103
104
        $form = new Form(
105
            $this,
106
            'EditForm',
107
            $fields,
108
            $actions
109
        );
110
111
        $form->addExtraClass('cms-edit-form');
112
        $form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
0 ignored issues
show
Documentation introduced by
$this->getTemplatesWithSuffix('_EditForm') is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
113
114
        if ($form->Fields()->hasTabset()) {
115
            $form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet');
116
            $form->addExtraClass('center ss-tabset cms-tabset ' . $this->BaseCSSClasses());
117
        }
118
119
        $this->extend('updateEditForm', $form);
120
121
        return $form;
122
    }
123
}
124