Completed
Pull Request — master (#661)
by Robbie
01:49
created

EditableCheckbox::migrateSettings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace SilverStripe\UserForms\Model\EditableFormField;
4
5
use SilverStripe\Forms\CheckboxField;
6
use SilverStripe\UserForms\Model\EditableFormField;
7
8
/**
9
 * EditableCheckbox
10
 *
11
 * A user modifiable checkbox on a UserDefinedForm
12
 *
13
 * @package userforms
14
 */
15
16
class EditableCheckbox extends EditableFormField
17
{
18
    private static $singular_name = 'Checkbox Field';
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 $singular_name 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...
19
20
    private static $plural_name = 'Checkboxes';
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 $plural_name 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...
21
22
    protected $jsEventHandler = 'click';
23
24
    private static $db = [
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 $db 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...
25
        'CheckedDefault' => 'Boolean' // from CustomSettings
26
    ];
27
28
    private static $table_name = 'EditableCheckbox';
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 $table_name 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...
29
30
    /**
31
     * @return FieldList
32
     */
33
    public function getCMSFields()
34
    {
35
        $fields = parent::getCMSFields();
36
37
        $fields->replaceField('Default', CheckboxField::create(
38
            "CheckedDefault",
39
            _t('SilverStripe\\UserForms\\Model\\EditableFormField.CHECKEDBYDEFAULT', 'Checked by Default?')
40
        ));
41
42
        return $fields;
43
    }
44
45
    public function getFormField()
46
    {
47
        $field = CheckboxField::create($this->Name, $this->EscapedTitle, $this->CheckedDefault)
0 ignored issues
show
Documentation introduced by
The property EscapedTitle does not exist on object<SilverStripe\User...Field\EditableCheckbox>. 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...
Bug introduced by
The property CheckedDefault does not seem to exist. Did you mean Default?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
48
            ->setFieldHolderTemplate(__CLASS__ . '_holder')
49
            ->setTemplate(__CLASS__);
50
51
        $this->doUpdateFormField($field);
0 ignored issues
show
Documentation introduced by
$field is of type object<SilverStripe\Forms\CheckboxField>, but the function expects a object<SilverStripe\UserForms\Model\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...
52
53
        return $field;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $field; (SilverStripe\Forms\CheckboxField) is incompatible with the return type of the parent method SilverStripe\UserForms\M...FormField::getFormField of type SilverStripe\UserForms\Model\FormField|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
54
    }
55
56
    public function getValueFromData($data)
57
    {
58
        $value = (isset($data[$this->Name])) ? $data[$this->Name] : false;
59
60
        return ($value)
61
            ? _t('SilverStripe\\UserForms\\Model\\EditableFormField.YES', 'Yes')
62
            : _t('SilverStripe\\UserForms\\Model\\EditableFormField.NO', 'No');
63
    }
64
65
    public function isCheckBoxField()
66
    {
67
        return true;
68
    }
69
}
70