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

EditableRadioField::getCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 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