Completed
Push — master ( 488d66...816407 )
by Jason
9s
created

CatalogProduct::getCMSFields()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 97
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 74
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 97
ccs 74
cts 74
cp 1
rs 8.3604
c 0
b 0
f 0
cc 3
eloc 65
nc 4
nop 0
crap 3

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
class CatalogProduct extends DataObject implements PermissionProvider, Dynamic\ViewableDataObject\VDOInterfaces\ViewableDataObjectInterface
4
{
5
    /**
6
     * @var string
7
     */
8
    private static $singular_name = 'Product';
9
10
    /**
11
     * @var string
12
     */
13
    private static $plural_name = 'Products';
14
15
    /**
16
     * @var array
17
     */
18
    private static $db = array(
19
        'Title' => 'Varchar(255)',
20
        'Content' => 'HTMLText',
21
        'QuickFeatures' => 'HTMLText',
22
        'Dimensions' => 'Varchar(255)',
23
        'SKU' => 'Varchar(50)',
24
    );
25
26
    /**
27
     * @var array
28
     */
29
    private static $has_many = array(
30
        'Features' => 'CatalogProductFeature',
31
    );
32
33
    /**
34
     * @var array
35
     */
36
    private static $many_many = array(
37
        'Categories' => 'CatalogCategory',
38
        'CareCleaningDocs' => 'CareCleaningDoc',
39
        'OperationManuals' => 'OperationManual',
40
        'SpecSheets' => 'SpecSheet',
41
        'Warranties' => 'Warranty',
42
    );
43
44
    /**
45
     * @var array
46
     */
47
    private static $many_many_extraFields = array(
48
        'Categories' => array(
49
            'SortOrder' => 'Int',
50
        ),
51
        'CareCleaningDocs' => array(
52
            'Sort' => 'Int',
53
        ),
54
        'OperationManuals' => array(
55
            'Sort' => 'Int',
56
        ),
57
        'SpecSheets' => array(
58
            'Sort' => 'Int',
59
        ),
60
        'Warranties' => array(
61
            'Sort' => 'Int',
62
        ),
63
    );
64
65
    /**
66
     * @var array
67
     */
68
    private static $summary_fields = array(
69
        'Image.CMSThumbnail' => 'Image',
70
        'Title' => 'Title',
71
        'Type' => 'Type',
72
        'CategoryList' => 'Categories',
73
    );
74
75
    /**
76
     * @var array
77
     */
78
    private static $searchable_fields = array(
79
        'Title' => array(
80
            'title' => 'Product Name',
81
        ),
82
        'Categories.ID' => array(
83
            'title' => 'Category',
84
        ),
85
    );
86
87
    /**
88
     * @var array
89
     */
90
    private static $extensions = [
91
        'VersionedDataObject',
92
    ];
93
94
    /**
95
     * @param bool $includerelations
96
     * @return array|string
97
     */
98 2
    public function fieldLabels($includerelations = true)
99
    {
100 2
        $labels = parent::fieldLabels($includerelations);
101
102 2
        $labels['Title'] = 'Product Name';
103 2
        $labels['Categories.ID'] = 'Category';
104
105 2
        return $labels;
106
    }
107
108
    /**
109
     * @return string
110
     */
111 1
    public function CategoryList()
112
    {
113 1
        $list = '';
114
115 1
        if ($this->Categories()) {
0 ignored issues
show
Documentation Bug introduced by
The method Categories does not exist on object<CatalogProduct>? 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...
116 1
            $i = 0;
117 1
            foreach ($this->Categories()->sort('SortOrder') as $category) {
0 ignored issues
show
Documentation Bug introduced by
The method Categories does not exist on object<CatalogProduct>? 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...
118 1
                $list .= $category->Title;
119 1
                if(++$i !== $this->Categories()->Count()) {
0 ignored issues
show
Documentation Bug introduced by
The method Categories does not exist on object<CatalogProduct>? 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...
120
                    $list .= ", ";
121
                }
122 1
            }
123 1
        }
124 1
        return $list;
125
    }
126
127
    /**
128
     * @return mixed
129
     */
130 1
    public function getImage()
131
    {
132 1
        if ($this->Slides()->exists()) {
0 ignored issues
show
Documentation Bug introduced by
The method Slides does not exist on object<CatalogProduct>? 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...
133
            return $this->Slides()->first();
0 ignored issues
show
Documentation Bug introduced by
The method Slides does not exist on object<CatalogProduct>? 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...
134
        }
135 1
    }
136
137
    /**
138
     * @return mixed
139
     */
140
    public function Image()
141
    {
142
        return $this->getImage();
143
    }
144
145
    /**
146
     * @return FieldList
147
     */
148 1
    public function getCMSFields()
149
    {
150 1
        $fields = parent::getCMSFields();
151
152
        $remove = [
153 1
            'Categories',
154 1
            'CareCleaningDocs',
155 1
            'OperationManuals',
156 1
            'SpecSheets',
157 1
            'Warranties',
158 1
            'DisabledBlocks',
159 1
        ];
160
161 1
        if (!$this->ID) {
162 1
            $remove[] = 'Blocks';
163 1
        }
164
165 1
        $fields->removeByName($remove);
166
167 1
        $fields->insertBefore(
168 1
            $fields->dataFieldByName('SKU'),
169
            'Content'
0 ignored issues
show
Documentation introduced by
'Content' is of type string, but the function expects a object<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...
170 1
        );
171
172 1
        $fields->addFieldsToTab('Root.Info', array(
173 1
            TextField::create('Dimensions'),
174 1
            HTMLEditorField::create('QuickFeatures'),
175 1
        ));
176
177 1
        if ($this->ID) {
178
            // Categories
179 1
            $config = GridFieldConfig_RelationEditor::create();
180 1
            $config->addComponent(new GridFieldOrderableRows('SortOrder'));
181 1
            $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
182 1
            $config->addComponent(new GridFieldAddExistingSearchButton());
183 1
            $config->removeComponentsByType('GridFieldAddNewBUtton');
184 1
            $categories = $this->Categories()->sort('SortOrder');
0 ignored issues
show
Documentation Bug introduced by
The method Categories does not exist on object<CatalogProduct>? 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...
185 1
            $categoryField = GridField::create('Categories', 'Categories', $categories, $config);
186
187 1
            $fields->addFieldsToTab('Root.Categories.Categories', array(
188 1
                $categoryField,
189 1
            ));
190
191
            // Features
192 1
            $config = GridFieldConfig_RecordEditor::create();
193 1
            $config->addComponent(new GridFieldOrderableRows('SortOrder'));
194 1
            $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
195 1
            $config->removeComponentsByType('GridFieldDeleteAction');
196 1
            $config->addComponent(new GridFieldDeleteAction(false));
197 1
            $featuresField = GridField::create('Features', 'Features', $this->Features()->sort('SortOrder'), $config);
0 ignored issues
show
Documentation Bug introduced by
The method Features does not exist on object<CatalogProduct>? 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...
198 1
            $fields->addFieldsToTab('Root.Features', array(
199 1
                $featuresField,
200 1
            ));
201
202
            // Care and Cleaning
203 1
            $config = GridFieldConfig_RecordEditor::create();
204 1
            $config->addComponent(new GridFieldOrderableRows('Sort'));
205 1
            $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
206 1
            $config->addComponent(new GridFieldAddExistingSearchButton());
207 1
            $operation = GridField::create('CareCleaningDocs', 'Care and Cleaning', $this->CareCleaningDocs()->sort('Sort'), $config);
0 ignored issues
show
Documentation Bug introduced by
The method CareCleaningDocs does not exist on object<CatalogProduct>? 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...
208 1
            $fields->addFieldsToTab('Root.Files.Care', array(
209 1
                $operation,
210 1
            ));
211
212
            // Operation Manuals
213 1
            $config = GridFieldConfig_RecordEditor::create();
214 1
            $config->addComponent(new GridFieldOrderableRows('Sort'));
215 1
            $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
216 1
            $config->addComponent(new GridFieldAddExistingSearchButton());
217 1
            $operation = GridField::create('OperationManuals', 'Operation Manuals', $this->OperationManuals()->sort('Sort'), $config);
0 ignored issues
show
Documentation Bug introduced by
The method OperationManuals does not exist on object<CatalogProduct>? 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...
218 1
            $fields->addFieldsToTab('Root.Files.Operation', array(
219 1
                $operation,
220 1
            ));
221
222
            // Spec Sheets
223 1
            $config = GridFieldConfig_RecordEditor::create();
224 1
            $config->addComponent(new GridFieldOrderableRows('Sort'));
225 1
            $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
226 1
            $config->addComponent(new GridFieldAddExistingSearchButton());
227 1
            $specsheets = GridField::create('SpecSheets', 'Spec Sheets', $this->SpecSheets()->sort('Sort'), $config);
0 ignored issues
show
Documentation Bug introduced by
The method SpecSheets does not exist on object<CatalogProduct>? 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...
228 1
            $fields->addFieldsToTab('Root.Files.SpecSheets', array(
229 1
                $specsheets,
230 1
            ));
231
232
            // Warranties
233 1
            $config = GridFieldConfig_RecordEditor::create();
234 1
            $config->addComponent(new GridFieldOrderableRows('Sort'));
235 1
            $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
236 1
            $config->addComponent(new GridFieldAddExistingSearchButton());
237 1
            $warranties = GridField::create('Warranties', 'Warranties', $this->Warranties()->sort('Sort'), $config);
0 ignored issues
show
Documentation Bug introduced by
The method Warranties does not exist on object<CatalogProduct>? 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...
238 1
            $fields->addFieldsToTab('Root.Files.Warranty', array(
239 1
                $warranties,
240 1
            ));
241 1
        }
242
243 1
        return $fields;
244
    }
245
246
    /**
247
     * @return CatalogCategory
248
     */
249 2
    public function getPrimaryCategory()
250
    {
251 2
        if ($this->Categories()->exists()) {
0 ignored issues
show
Documentation Bug introduced by
The method Categories does not exist on object<CatalogProduct>? 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...
252 2
            $category = $this->Categories()->sort('SortOrder')->first();
0 ignored issues
show
Documentation Bug introduced by
The method Categories does not exist on object<CatalogProduct>? 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...
253 2
        } else {
254 1
            $category = CatalogCategory::get()->first();
255
        }
256 2
        return $category;
257
    }
258
259
    /**
260
     * @return string
261
     */
262 1
    public function getParentPage()
263
    {
264 1
        return $this->getPrimaryCategory();
265
    }
266
267
    /**
268
     * @return string
269
     */
270 1
    public function getViewAction()
271
    {
272 1
        return 'view';
273
    }
274
275
    /**
276
     * needed for Blocks
277
     *
278
     * @return DataList
279
     */
280
    public function getAncestors()
281
    {
282
        debug::show(CatalogCategory::get());
283
        return CatalogCategory::get();
284
    }
285
286
    /**
287
     * @return array
288
     */
289 1
    public function providePermissions()
290
    {
291
        return array(
292 1
            'Product_EDIT' => 'Edit a Product',
293 1
            'Product_DELETE' => 'Delete a Product',
294 1
            'Product_CREATE' => 'Create a Product',
295 1
        );
296
    }
297
298
    /**
299
     * @param null $member
300
     *
301
     * @return bool|int
302
     */
303 1
    public function canEdit($member = null)
304
    {
305 1
        return Permission::check('Product_EDIT', 'any', $member);
306
    }
307
308
    /**
309
     * @param null $member
310
     *
311
     * @return bool|int
312
     */
313 1
    public function canDelete($member = null)
314
    {
315 1
        return Permission::check('Product_DELETE', 'any', $member);
316
    }
317
318
    /**
319
     * @param null $member
320
     *
321
     * @return bool|int
322
     */
323 1
    public function canCreate($member = null)
324
    {
325 1
        return Permission::check('Product_CREATE', 'any', $member);
326
    }
327
328
    /**
329
     * @param null $member
330
     *
331
     * @return bool
332
     */
333 1
    public function canView($member = null)
334
    {
335 1
        return true;
336
    }
337
}