Completed
Push — master ( 12981b...2f1b03 )
by Robbie
14s
created

EditableRadioField   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 41.18%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 44
ccs 7
cts 17
cp 0.4118
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 8 1
A getSelectorField() 0 6 2
A isRadioField() 0 3 1
A getFormField() 0 13 2
1
<?php
2
/**
3
 * EditableRadioField
4
 *
5
 * Represents a set of selectable radio buttons
6
 *
7
 * @package userforms
8
 */
9
10
class EditableRadioField extends EditableMultipleOptionField
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 = 'Radio Group';
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 = 'Radio Groups';
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
    /**
18
     * @return FieldList
19
     */
20
    public function getCMSFields()
21
    {
22
        $fields = parent::getCMSFields();
23
24
        $fields->removeByName('Default');
25
26
        return $fields;
27
    }
28
29 1
    public function getFormField()
30
    {
31 1
        $field = OptionsetField::create($this->Name, $this->EscapedTitle, $this->getOptionsMap());
0 ignored issues
show
Documentation introduced by
The property EscapedTitle does not exist on object<EditableRadioField>. 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...
32 1
        $field->setFieldHolderTemplate('UserFormsMultipleOptionField_holder');
33
34
        // Set default item
35 1
        $defaultOption = $this->getDefaultOptions()->first();
36 1
        if ($defaultOption) {
37
            $field->setValue($defaultOption->Value);
38
        }
39 1
        $this->doUpdateFormField($field);
40 1
        return $field;
41
    }
42
43
    public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false)
44
    {
45
        // We only want to trigger on load once for the radio group - hence we focus on the first option only.
46
        $first = $forOnLoad ? ':first' : '';
47
        return "$(\"input[name='{$this->Name}']{$first}\")";
48
    }
49
50
	public function isRadioField() {
51
		return true;
52
	}
53
}
54