Completed
Push — master ( fbddd9...3d9787 )
by Derek Stephen
06:15
created

CheckboxRender   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 114
Duplicated Lines 34.21 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 39
loc 114
ccs 41
cts 41
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A renderBlock() 28 28 4
A processOption() 0 4 1
A renderCheckbox() 0 11 2
A getLabel() 11 11 1
A renderCheckboxInline() 0 16 6

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
 * User: delboy1978uk
4
 * Date: 04/12/2016
5
 * Time: 22:33
6
 */
7
8
namespace Del\Form\Renderer\Field;
9
10
use Del\Form\Field\FieldInterface;
11
use Del\Form\Field\CheckBox;
12
use DOMDocumentFragment;
13
use DOMElement;
14
use DOMNode;
15
use DOMText;
16
use InvalidArgumentException;
17
use LogicException;
18
19
class CheckboxRender extends AbstractFieldRender implements FieldRendererInterface
20
{
21
    /** @var DOMDocumentFragment $div */
22
    private $fragment;
23
24
    /** @var bool $isMultiCheckbox */
25
    private $isMultiCheckbox = false;
26
27
    private $counter = 0;
28
29
    /**
30
     * @param FieldInterface $field
31
     * @param DOMElement $element
32 9
     * @return DOMNode
33
     */
34 View Code Duplication
    public function renderBlock(FieldInterface $field, DOMElement $element)
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...
35
    {
36 9
        // We don't really want a containing div, so we'll ignore $element
37 9
        // and instead create a DOMDocumentFragment
38
        unset($element);
39
        $this->fragment = $this->getDom()->createDocumentFragment();
40 9
41 1
        // Make sure the FieldInterface is actually a Radio
42
        if (!$field instanceof CheckBox) {
43
            throw new InvalidArgumentException('Must be a Del\Form\Field\Checkbox');
44 8
        }
45
46 8
        $inline = $field->isRenderInline();
47 8
48 1
        $options = $field->getOptions();
49
        if (empty($options)) {
50
            throw new LogicException('You must set at least one option.');
51
        }
52 7
53 7
        // Loop through each checkbox element (the options)
54 7
        $this->isMultiCheckbox = count($options) > 1;
55 7
        foreach ($options as $value => $label) {
56
            $radio = $this->processOption($field, $value, $label, $inline);
57
            $this->fragment->appendChild($radio);
58 7
        }
59
60
        return $this->fragment;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->fragment; (DOMDocumentFragment) is incompatible with the return type declared by the abstract method Del\Form\Renderer\Field\...ieldRender::renderBlock of type DOMElement.

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...
61
    }
62
63
64
    /**
65
     * @param FieldInterface $field
66
     * @param $value
67
     * @param $labelText
68 7
     * @return DOMElement
69
     */
70 7
    private function processOption(FieldInterface $field, $value, $labelText, $inline)
71 1
    {
72
        return $this->renderCheckbox($field, $value, $labelText, $inline);
0 ignored issues
show
Compatibility introduced by
$field of type object<Del\Form\Field\FieldInterface> is not a sub-type of object<Del\Form\Field\CheckBox>. It seems like you assume a concrete implementation of the interface Del\Form\Field\FieldInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
73 6
    }
74
75
    /**
76
     * @param CheckBox $field
77
     * @param $value
78
     * @param $labelText
79
     * @return DOMElement
80
     */
81
    private function renderCheckbox(CheckBox $field, $value, $labelText, $inline)
82 6
    {
83
        $div = $this->getDom()->createElement('div');
84 6
        $class = $inline ? 'form-check-inline' : 'form-check';
85 6
        $div->setAttribute('class', $class);
86 6
        $checkbox = $this->renderCheckboxInline($field, $value);
87 6
        $div->appendChild($checkbox);
88 6
        $div->appendChild($this->getLabel($field, $labelText));
89 6
90
        return $div;
91
    }
92
93
    /**
94
     * @param FieldInterface $field
95
     * @param string $labelText
96
     * @return DOMElement
97
     */
98 7 View Code Duplication
    private function getLabel(FieldInterface $field, string $labelText): DOMElement
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...
99
    {
100 7
        $this->counter ++;
101 7
        $label = $this->getDom()->createElement('label');
102 7
        $label->setAttribute('for', $field->getId() . $this->counter);
103
        $label->setAttribute('class', 'form-check-label');
104 7
        $text = $this->createText($labelText);
105 7
        $label->appendChild($text);
106 7
107 7
        return $label;
108 7
    }
109 7
110 7
    /**
111
     * @param FieldInterface $field
112 7
     * @param $value
113 3
     * @param $labelText
114
     * @return DOMElement
115
     */
116 7
    private function renderCheckboxInline(FieldInterface $field, $value)
117 7
    {
118
        $checkbox = $this->getDom()->createElement('input');
119 7
        $checkbox->setAttribute('class', 'form-check-input');
120
        $checkbox->setAttribute('type', 'checkbox');
121
        $fieldName = $this->isMultiCheckbox ? $field->getName() . '[]' : $field->getName();
122
        $checkbox->setAttribute('name', $fieldName);
123
        $checkbox->setAttribute('value', $value);
124
        $fieldValue = $field->getValue();
125
126
        if ($fieldValue === true || $fieldValue == $value || (is_array($fieldValue) && in_array($value, $fieldValue, true))) {
127
            $checkbox->setAttribute('checked', 'checked');
128
        }
129
130
        return $checkbox;
131
    }
132
}
133