Completed
Pull Request — master (#62)
by Jason
12:32
created

PageSectionObject::getCMSFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 17
cts 17
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Dynamic\DynamicBlocks\Model;
4
5
use Dynamic\DynamicBlocks\Block\PageSectionBlock;
6
use SilverStripe\Assets\Image;
7
use SilverStripe\ORM\DataObject;
8
9
/**
10
 * Class PageSectionObject
11
 *
12
 * @property string $Name
13
 * @property string $Title
14
 * @property HTMLText $Content
15
 * @property int $SortOrder
16
 * @property int $ImageID
17
 * @property int $PageSectionBlockID
18
 */
19
class PageSectionObject extends DataObject
20
{
21
    /**
22
     * @return string
23
     */
24
    private static $singular_name = 'Page Section';
25
26
    /**
27
     * @return string
28
     */
29
    private static $plural_name = 'Page Sections';
30
31
    /**
32
     * @var array
33
     */
34
    private static $db = array(
35
        'Name' => 'Varchar(255)',
36
        'Title' => 'Varchar(255)',
37
        'Content' => 'HTMLText',
38
        'SortOrder' => 'Int',
39
    );
40
41
    /**
42
     * @var array
43
     */
44
    private static $has_one = array(
45
        'Image' => Image::class,
46
        'PageSectionBlock' => PageSectionBlock::class,
47
        //'BlockLink' => 'Link', // todo readd once Linkable is SS4 compatable
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
48
    );
49
50
    /**
51
     * @var string
52
     */
53
    private static $default_sort = 'Name ASC';
0 ignored issues
show
Unused Code introduced by
The property $default_sort 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...
54
55
    /**
56
     * @var array
57
     */
58
    private static $summary_fields = array(
59
        'Image.CMSThumbnail' => 'Image',
60
        'Name' => 'Name',
61
        'Title' => 'Title',
62
    );
63
64
    /**
65
     * @var array
66
     */
67
    private static $searchable_fields = array(
68
        'Name' => 'Name',
69
        'Title' => 'Title',
70
    );
71
72
    /**
73 1
     * @return FieldList
74
     */
75 1
    public function getCMSFields()
76 1
    {
77 1
        $this->beforeUpdateCMSFields(function ($fields) {
0 ignored issues
show
Unused Code introduced by
The parameter $fields 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...
78 1
79
            /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
80 1
            $fields->addFieldToTab(
81 1
                'Root.Main',
82
                LinkField::create('BlockLinkID', 'Link'),
83 1
                'Content'
84
            );
85 1
            */
86 1
        });
87 1
88 1
        $fields = parent::getCMSFields();
89
90 1
        $fields->removeByName(array(
91
            'PageSectionBlockID',
92 1
            'SortOrder',
93 1
        ));
94 1
95
        $fields->dataFieldByName('Name')->setDescription('For internal reference only');
96 1
97
        $image = $fields->dataFieldByName('Image');
98
        $image->setFolderName('Uploads/PageSections');
99
        $fields->insertBefore($image, 'Content');
0 ignored issues
show
Documentation introduced by
$image is of type object<SilverStripe\Forms\FormField>|null, 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...
Documentation introduced by
'Content' is of type string, but the function expects a object<SilverStripe\Forms\FormField>.

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...
100
101
        return $fields;
102 1
    }
103
104 1
    /**
105
     * @return ValidationResult
106 1
     */
107 1
    public function validate()
108 1
    {
109
        $result = parent::validate();
110 1
111
        if (!$this->Name) {
112
            $result->addError('Name is requied before you can save');
113
        }
114
115
        return $result;
116
    }
117
118
    /**
119
     * Set permissions, allow all users to access by default.
120
     * Override in descendant classes, or use PermissionProvider.
121
     */
122
123 1
    /**
124
     * @param null $member
125 1
     *
126
     * @return bool
127
     */
128
    public function canCreate($member = null, $context = [])
129
    {
130
        return true;
131
    }
132
133 1
    /**
134
     * @param null $member
135 1
     *
136
     * @return bool
137
     */
138
    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...
139
    {
140
        return true;
141
    }
142
143 1
    /**
144
     * @param null $member
145 1
     *
146
     * @return bool
147
     */
148
    public function canEdit($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...
149
    {
150
        return true;
151
    }
152
153 1
    /**
154
     * @param null $member
155 1
     *
156
     * @return bool
157
     */
158
    public function canDelete($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...
159
    {
160
        return true;
161
    }
162
}