Completed
Pull Request — master (#636)
by Franco
22:13
created

EditableCheckbox   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 28%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 61
ccs 7
cts 25
cp 0.28
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 11 1
A getFormField() 0 10 1
A migrateSettings() 0 10 2
A getValueFromData() 0 6 3
A isCheckBoxField() 0 3 1
1
<?php
2
/**
3
 * EditableCheckbox
4
 *
5
 * A user modifiable checkbox on a UserDefinedForm
6
 *
7
 * @package userforms
8
 */
9
10
class EditableCheckbox extends EditableFormField
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
11
{
12
13
    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...
14
15
    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...
16
17
    protected $jsEventHandler = 'click';
18
19
    private static $db = array(
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...
20
        'CheckedDefault' => 'Boolean' // from CustomSettings
21
    );
22
23
    /**
24
     * @return FieldList
25
     */
26
    public function getCMSFields()
27
    {
28
        $fields = parent::getCMSFields();
29
30
        $fields->replaceField('Default', CheckboxField::create(
31
            "CheckedDefault",
32
            _t('EditableFormField.CHECKEDBYDEFAULT', 'Checked by Default?')
33
        ));
34
35
        return $fields;
36
    }
37
38
    public function getFormField()
39
    {
40
        $field = CheckboxField::create($this->Name, $this->EscapedTitle, $this->CheckedDefault)
0 ignored issues
show
Documentation introduced by
The property EscapedTitle does not exist on object<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...
41
            ->setFieldHolderTemplate('UserFormsCheckboxField_holder')
42
            ->setTemplate('UserFormsCheckboxField');
43
44
        $this->doUpdateFormField($field);
45
46
        return $field;
47
    }
48
49
    public function getValueFromData($data)
50
    {
51
        $value = (isset($data[$this->Name])) ? $data[$this->Name] : false;
52
53
        return ($value) ? _t('EditableFormField.YES', 'Yes') : _t('EditableFormField.NO', 'No');
54
    }
55
56 2
    public function migrateSettings($data)
57
    {
58
        // Migrate 'Default' setting to 'CheckedDefault'
59 2
        if (isset($data['Default'])) {
60 2
            $this->CheckedDefault = (bool)$data['Default'];
0 ignored issues
show
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...
61 2
            unset($data['Default']);
62 2
        }
63
64 2
        parent::migrateSettings($data);
65 2
    }
66
67
	public function isCheckBoxField() {
68
		return true;
69
	}
70
}
71