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

EditableCheckboxGroupField   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 70
Duplicated Lines 21.43 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 22.86%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 15
loc 70
ccs 8
cts 35
cp 0.2286
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormField() 15 15 2
B getValueFromData() 0 19 6
A getSelectorField() 0 10 2
A isCheckBoxField() 0 3 1
A getSelectorFieldOnly() 0 4 1
A isCheckBoxGroupField() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * EditableCheckboxGroup
4
 *
5
 * Represents a set of selectable radio buttons
6
 *
7
 * @package userforms
8
 */
9
10
class EditableCheckboxGroupField 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 = "Checkbox Group";
0 ignored issues
show
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...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
14
15
    private static $plural_name = "Checkbox Groups";
0 ignored issues
show
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...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
16
17
    protected $jsEventHandler = 'click';
18
19 1 View Code Duplication
    public function getFormField()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
    {
21 1
        $field = UserFormsCheckboxSetField::create($this->Name, $this->EscapedTitle, $this->getOptionsMap())
0 ignored issues
show
Documentation introduced by
The property EscapedTitle does not exist on object<EditableCheckboxGroupField>. 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...
22 1
            ->setFieldHolderTemplate('UserFormsMultipleOptionField_holder')
23 1
            ->setTemplate('UserFormsCheckboxSetField');
24
25
        // Set the default checked items
26 1
        $defaultCheckedItems = $this->getDefaultOptions();
27 1
        if ($defaultCheckedItems->count()) {
28
            $field->setDefaultItems($defaultCheckedItems->map('Value')->keys());
0 ignored issues
show
Bug introduced by
The method keys cannot be called on $defaultCheckedItems->map('Value') (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
29
        }
30
31 1
        $this->doUpdateFormField($field);
32 1
        return $field;
33
    }
34
35
    public function getValueFromData($data)
36
    {
37
        $result = '';
38
        $entries = (isset($data[$this->Name])) ? $data[$this->Name] : false;
39
40
        if ($entries) {
41
            if (!is_array($data[$this->Name])) {
42
                $entries = array($data[$this->Name]);
43
            }
44
            foreach ($entries as $selected => $value) {
45
                if (!$result) {
46
                    $result = $value;
47
                } else {
48
                    $result .= ", " . $value;
49
                }
50
            }
51
        }
52
        return $result;
53
    }
54
55
    public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false)
56
    {
57
        // watch out for checkboxs as the inputs don't have values but are 'checked
58
        // @todo - Test this
59
        if ($rule->FieldValue) {
60
            return "$(\"input[name='{$this->Name}[]'][value='{$rule->FieldValue}']\")";
61
        } else {
62
            return "$(\"input[name='{$this->Name}[]']:first\")";
63
        }
64
    }
65
66
	public function isCheckBoxField() {
67
		return true;
68
	}
69
70
	public function getSelectorFieldOnly()
71
    {
72
        return "[name='{$this->Name}[]']";
73
    }
74
75
    public function isCheckBoxGroupField()
76
    {
77
        return true;
78
    }
79
}
80