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

RadioRender   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 38.38 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 38
loc 99
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A renderBlock() 27 27 4
A renderRadio() 0 12 2
A getLabel() 11 11 1
A renderRadioInline() 0 15 2

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
namespace Del\Form\Renderer\Field;
4
5
use Del\Form\Field\FieldInterface;
6
use Del\Form\Field\Radio;
7
use DOMDocumentFragment;
8
use DOMElement;
9
use DOMNode;
10
use DOMText;
11
use InvalidArgumentException;
12
use LogicException;
13
14
class RadioRender extends AbstractFieldRender implements FieldRendererInterface
15
{
16
    /** @var DOMDocumentFragment $div */
17
    private $fragment;
18
19
    private $counter = 0;
20
21
    /**
22
     * @param FieldInterface $field
23
     * @param DOMElement $element
24 10
     * @return DOMNode
25
     */
26 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...
27
    {
28 10
        // We don't really want a containing div, so we'll ignore $element
29 10
        // and instead create a DOMDocumentFragment
30
        unset($element);
31
        $this->fragment = $this->getDom()->createDocumentFragment();
32 10
33 1
        // Make sure the FieldInterface is actually a Radio
34
        if (!$field instanceof Radio) {
35
            throw new InvalidArgumentException('Must be a Del\Form\Field\Radio');
36 9
        }
37
38 9
        $inline = $field->isRenderInline();
39 9
40 1
        $options = $field->getOptions();
41
        if (empty($options)) {
42
            throw new LogicException('You must set at least one option.');
43
        }
44 8
45 8
        // Loop through each radio element (the options)
46 8
        foreach ($options as $value => $label) {
47
            $radio = $this->renderRadio($field, $value, $label, $inline);
48
            $this->fragment->appendChild($radio);
49 8
        }
50
51
        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...
52
    }
53
54
    /**
55
     * @param FieldInterface $field
56
     * @param $value
57
     * @param $labelText
58
     * @param $inline
59 8
     * @return DOMElement
60
     */
61 8
    private function renderRadio(FieldInterface $field, $value, $labelText, $inline)
62 2
    {
63
        $div = $this->createElement('div');
64 7
        $class = $inline ? 'form-check-inline' : 'form-check';
65
        $div->setAttribute('class', $class);
66
        $radio = $this->renderRadioInline($field, $value, $labelText);
67
        $label = $this->getLabel($field, $labelText);
68
        $div->appendChild($radio);
69
        $div->appendChild($label);
70
71
        return $div;
72
    }
73 7
74
    /**
75 7
     * @param FieldInterface $field
76 7
     * @param string $labelText
77 7
     * @return DOMElement
78 7
     */
79 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...
80 7
    {
81
        $this->counter ++;
82
        $label = $this->getDom()->createElement('label');
83
        $label->setAttribute('for', $field->getId() . $this->counter);
84
        $label->setAttribute('class', 'form-check-label');
85
        $text = $this->createText($labelText);
86
        $label->appendChild($text);
87
88
        return $label;
89 8
    }
90
91 8
    /**
92 8
     * @param FieldInterface $field
93 8
     * @param $value
94
     * @param $labelText
95 8
     * @return DOMElement
96 8
     */
97 8
    private function renderRadioInline(FieldInterface $field, $value, $labelText)
98 8
    {
99 8
        $radio = $this->createElement('input');
100
        $radio->setAttribute('class', 'form-check-input');
101 8
        $radio->setAttribute('type', 'radio');
102 2
        $radio->setAttribute('name', $field->getName());
103
        $radio->setAttribute('value', $value);
104
        $text = $this->createText($labelText);
0 ignored issues
show
Unused Code introduced by
$text is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
105 8
106 8
        if ($field->getValue() == $radio->getAttribute('value')) {
107
            $radio->setAttribute('checked', 'checked');
108 8
        }
109
110
        return $radio;
111
    }
112
}