Completed
Branch dev-master (76dc67)
by Derek Stephen
01:55
created

HorizontalFormRenderer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 11.39 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A renderFieldLabel() 9 9 1
B renderFieldBlock() 0 35 5
A surroundInDiv() 0 7 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
 * User: delboy1978uk
4
 * Date: 29/11/2016
5
 * Time: 19:44
6
 */
7
8
namespace Del\Form\Renderer;
9
10
use Del\Form\Field\CheckBox;
11
use Del\Form\Field\Radio;
12
use Del\Form\Field\Submit;
13
use Del\Form\Renderer\Error\HorizontalFormErrorRender;
14
use DOMElement;
15
use DOMText;
16
17
class HorizontalFormRenderer extends AbstractFormRenderer  implements FormRendererInterface
0 ignored issues
show
Coding Style introduced by
Expected 1 space before implements keyword; 2 found
Loading history...
18
{
19 2
    public function __construct()
20
    {
21 2
        parent::__construct();
22
23
        // Add horizontal form class
24 2
        $this->form->setAttribute('class', 'form-horizontal');
25 2
        $this->errorRenderer = new HorizontalFormErrorRender($this->dom);
26 2
    }
27
28
    /**
29
     * @return DOMElement
30
     */
31 2 View Code Duplication
    public function renderFieldLabel()
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...
32
    {
33 2
        $label = $this->dom->createElement('label');
34 2
        $label->setAttribute('for', $this->field->getId());
35 2
        $label->setAttribute('class', 'col-sm-2 control-label');
36 2
        $text = new DOMText($this->field->getLabel());
37 2
        $label->appendChild($text);
38 2
        return $label;
39
    }
40
41
    /**
42
     * @return DOMElement
43
     */
44 2
    public function renderFieldBlock()
45
    {
46 2
        $formGroup = $this->block;
47 2
        $class = $formGroup->getAttribute('class').'form-group';
48 2
        $formGroup->setAttribute('class', $class);
49
50 2
        $div = $this->dom->createElement('div');
51 2
        $div->setAttribute('class', 'col-sm-offset-2 col-sm-10');
52
53 2
        switch (get_class($this->field)) {
54 2
            case 'Del\Form\Field\Submit':
55 2
                $div->appendChild($this->element);
56 2
                break;
57 2
            case 'Del\Form\Field\Radio':
58 1
                $radioDiv = $this->surroundInDiv($this->element, 'radio');
59 1
                $div->appendChild($radioDiv);
60 1
                break;
61 2
            case 'Del\Form\Field\CheckBox':
62 1
                $checkboxDiv = $this->surroundInDiv($this->element, 'checkbox');
63 1
                $div->appendChild($checkboxDiv);
64 1
                break;
65
            default:
66 2
                $formGroup->appendChild($this->label);
67 2
                $div->setAttribute('class', 'col-sm-10');
68 2
                $div->appendChild($this->element);
69
        }
70
71 2
        $formGroup->appendChild($div);
72
73 2
        if (!is_null($this->errors)) {
74 1
            $formGroup->appendChild($this->errors);
75
        }
76
77 2
        return $formGroup;
78
    }
79
80
    /**
81
     * Surround an element in a div with a given class
82
     *
83
     * @param DOMElement $element
84
     * @param $class
85
     * @return DOMElement
86
     */
87 2
    private function surroundInDiv(DOMElement $element, $class)
88
    {
89 2
        $div = $this->dom->createElement('div');
90 2
        $div->setAttribute('class', $class);
91 2
        $div->appendChild($element);
92 2
        return $div;
93
    }
94
95
}