Completed
Pull Request — master (#62)
by Jason
15:39
created

PromoObject::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Dynamic\DynamicBlocks\Model;
4
5
use Dynamic\DynamicBlocks\Block\PromoBlock;
6
use SilverStripe\Assets\Image;
7
use SilverStripe\Forms\GridField\GridField;
8
use SilverStripe\Forms\GridField\GridFieldConfig_RecordViewer;
9
use SilverStripe\ORM\DataObject;
10
11
class PromoObject extends DataObject
12
{
13
    /**
14
     * @return string
15
     */
16
    private static $singular_name = 'Promo';
17
18
    /**
19
     * @return string
20
     */
21
    private static $plural_name = 'Promos';
22
23
    /**
24
     * @var array
25
     */
26
    private static $db = array(
27
        'Name' => 'Varchar(255)',
28
        'Title' => 'Varchar(255)',
29
        'Content' => 'HTMLText',
30
    );
31
32
    /**
33
     * @var array
34
     */
35
    private static $has_one = array(
36
        'Image' => Image::class,
37
        //'BlockLink' => 'Link', // todo readd once Linkable is SS4 compatible
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...
38
    );
39
40
    /**
41
     * @var array
42
     */
43
    private static $belongs_many_many = array(
44
        'PromoBlocks' => PromoBlock::class,
45
    );
46
47
    /**
48
     * @var string
49
     */
50
    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...
51
52
    /**
53
     * @var array
54
     */
55
    private static $summary_fields = array(
56
        'Image.CMSThumbnail' => 'Image',
57
        'Name' => 'Name',
58
        'Title' => 'Title',
59
    );
60
61
    /**
62
     * @var array
63
     */
64
    private static $searchable_fields = array(
65
        'Name' => 'Name',
66
        'Title' => 'Title',
67
    );
68
69
    /**
70
     * @return \SilverStripe\Forms\FieldList
71 1
     */
72
    public function getCMSFields()
73 1
    {
74 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...
75 1
76 1
            /* // todo readd once Linkable is SS4 compatible
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
77
            $fields->addFieldToTab(
78 1
                'Root.Main',
79 1
                LinkField::create('BlockLinkID', 'Link'),
80
                'Content'
81 1
            );
82
            */
83 1
        });
84 1
85 1
        $fields = parent::getCMSFields();
86
87 1
        $fields->removeByName(array(
88
            'PromoBlocks',
89 1
        ));
90 1
91 1
        $fields->dataFieldByName('Name')->setDescription('For internal reference only');
92
93 1
        $image = $fields->dataFieldByName('Image');
94 1
        $image->setFolderName('Uploads/Promos');
95
        $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...
96 1
97
        $config = GridFieldConfig_RecordViewer::create();
98
        $fields->addFieldToTab('Root.Blocks', GridField::create('PromoBlocks', 'Blocks', $this->PromoBlocks(), $config));
0 ignored issues
show
Documentation Bug introduced by
The method PromoBlocks does not exist on object<Dynamic\DynamicBlocks\Model\PromoObject>? 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...
99
100
        return $fields;
101
    }
102 1
103
    /**
104 1
     * @return \SilverStripe\ORM\ValidationResult
105
     */
106 1
    public function validate()
107 1
    {
108 1
        $result = parent::validate();
109
110 1
        if (!$this->Name) {
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<Dynamic\DynamicBlocks\Model\PromoObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
111
            $result->error('Name is requied before you can save');
0 ignored issues
show
Bug introduced by
The method error() does not seem to exist on object<SilverStripe\ORM\ValidationResult>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
        }
113
114
        return $result;
115
    }
116
117
    /**
118
     * Set permissions, allow all users to access by default.
119
     * Override in descendant classes, or use PermissionProvider.
120
     */
121
122
    /**
123 1
     * @param null $member
124
     *
125 1
     * @return bool
126
     */
127
    public function canCreate($member = null, $context = [])
128
    {
129
        return true;
130
    }
131
132
    /**
133 1
     * @param null $member
134
     *
135 1
     * @return bool
136
     */
137
    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...
138
    {
139
        return true;
140
    }
141
142
    /**
143 1
     * @param null $member
144
     *
145 1
     * @return bool
146
     */
147
    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...
148
    {
149
        return true;
150
    }
151
152
    /**
153 1
     * @param null $member
154
     *
155 1
     * @return bool
156
     */
157
    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...
158
    {
159
        return true;
160
    }
161
}