Completed
Pull Request — master (#24)
by Jason
13:57
created

ProductDoc::getCMSFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1.0013

Importance

Changes 0
Metric Value
dl 0
loc 28
c 0
b 0
f 0
ccs 16
cts 18
cp 0.8889
rs 8.8571
cc 1
eloc 13
nc 1
nop 0
crap 1.0013
1
<?php
2
3
namespace Dynamic\ProductCatalog\Docs;
4
5
use SilverStripe\AssetAdmin\Forms\UploadField;
6
use SilverStripe\Assets\File;
7
use SilverStripe\Assets\Image;
8
use SilverStripe\Forms\TextField;
9
use SilverStripe\ORM\DataObject;
10
11
class ProductDoc extends DataObject
12
{
13
    /**
14
     * @var array
15
     */
16
    private static $db = array(
17
        'Name' => 'Varchar(255)',
18
        'Title' => 'Varchar(255)',
19
        'Content' => 'HTMLText',
20
        'FileLink' => 'Varchar(255)',
21
    );
22
23
    /**
24
     * @var array
25
     */
26
    private static $has_one = array(
27
        'Image' => Image::class,
28
        'Download' => File::class,
29
    );
30
31
    /**
32
     * @var array
33
     */
34
    private static $summary_fields = array(
35
        'Name' => 'Name',
36
        'Title' => 'Title',
37
    );
38
39
    /**
40
     * @var array
41
     */
42
    private static $searchable_fields = array(
43
        'Name',
44
        'Title',
45
    );
46
47 6
    /**
48
     * @var string
49 6
     */
50
    private static $default_sort = 'Title';
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...
51 6
52 6
    /**
53 6
     * @return \SilverStripe\Forms\FieldList
54 6
     */
55 6
    public function getCMSFields()
56
    {
57 6
        $fields = parent::getCMSFields();
58 6
59 6
        $file = UploadField::create('Download')
60 6
            //->setFolderName('Uploads/FileDownloads')
61 6
            //->setConfig('allowedMaxFileNumber', 1)
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
62 6
            //->setAllowedFileCategories('doc')
63
            //->setAllowedMaxFileNumber(1)
64 6
        ;
65 6
66 6
        $fields->addFieldsToTab('Root.Download', array(
67 6
            $file,
68
            TextField::create('FileLink')
69 6
                ->setDescription('URL of external file. will display on page if no Download is specified above.')
70
                ->setAttribute('placeholder', 'http://'),
71 6
        ));
72
73
        $fields->insertBefore(
74
            UploadField::create('Image')
75
                //->setFolderName('Uploads/ProductDocs/Images')
76
                ->setDescription('Preview image of file')
77 45
            ,
78
            'Content'
0 ignored issues
show
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...
79 45
        );
80 45
81
        return $fields;
82
    }
83
84 45
    /**
85
     * if SetClass dropdown is set, create a new instance of the new class.
86
     */
87
    public function onAfterWrite()
0 ignored issues
show
Coding Style introduced by
onAfterWrite uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
88
    {
89
        parent::onAfterWrite();
90
        if (isset($_REQUEST['SetClass'])) {
91
            $object = $this->newClassInstance($_REQUEST['SetClass']);
92
            $object->write();
93
        }
94
    }
95
}